Skip to content

Commit

Permalink
Merge pull request #178 from Shopify/fix_issue-164-loaded-linters
Browse files Browse the repository at this point in the history
Update linters to load custom linters
  • Loading branch information
jen-taylor-shopify authored Aug 13, 2020
2 parents 8f6716e + 926f810 commit 10eddda
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
15 changes: 12 additions & 3 deletions lib/erb_lint/linter_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@ module ERBLint
# Stores all linters available to the application.
module LinterRegistry
CUSTOM_LINTERS_DIR = '.erb-linters'
@linters = []
@loaded_linters = []

class << self
attr_reader :linters
def clear
@linters = nil
end

def included(linter_class)
@linters << linter_class
@loaded_linters << linter_class
end

def find_by_name(name)
linters.detect { |linter| linter.simple_name == name }
end

def linters
@linters ||= begin
load_custom_linters
@loaded_linters
end
end

def load_custom_linters(directory = CUSTOM_LINTERS_DIR)
ruby_files = Dir.glob(File.expand_path(File.join(directory, '**', '*.rb')))
ruby_files.each { |file| require file }
Expand Down
1 change: 0 additions & 1 deletion lib/erb_lint/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def initialize(file_loader, config)
@config = config || RunnerConfig.default
raise ArgumentError, 'expect `config` to be a RunnerConfig instance' unless @config.is_a?(RunnerConfig)

LinterRegistry.load_custom_linters
linter_classes = LinterRegistry.linters.select { |klass| @config.for_linter(klass).enabled? }
@linters = linter_classes.map do |linter_class|
linter_class.new(@file_loader, @config.for_linter(linter_class))
Expand Down
12 changes: 9 additions & 3 deletions spec/erb_lint/linter_registry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
require 'spec_helper'

describe ERBLint::LinterRegistry do
let(:custom_directory) { File.expand_path('../fixtures/linters', __FILE__) }

context 'when including the LinterRegistry module' do
after do
described_class.linters.delete(FakeLinter)
described_class.clear
end

it 'adds the linter to the set of registered linters' do
Expand All @@ -15,11 +17,15 @@ class FakeLinter < ERBLint::Linter
end
end.to(change { described_class.linters.count }.by(1))
end

it 'adds custom linters to the set of registered linters' do
stub_const('ERBLint::LinterRegistry::CUSTOM_LINTERS_DIR', custom_directory)

expect(described_class.linters).to(include(ERBLint::Linters::CustomLinter))
end
end

describe '.load_custom_linters' do
let(:custom_directory) { File.expand_path('../fixtures/linters', __FILE__) }

it 'adds the custom linter to the set of registered linters' do
expect(described_class).to(receive(:require)
.with(File.join(custom_directory, 'custom_linter.rb')).once)
Expand Down

0 comments on commit 10eddda

Please sign in to comment.