Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting custom linters directory in config #124

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Create a `.erb-lint.yml` file in your project, with the following structure:

```yaml
---
custom_linters_directory: '.custom-erb-linters'
linters:
ErbSafety:
enabled: true
Expand Down
3 changes: 2 additions & 1 deletion lib/erb_lint/linter_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def find_by_name(name)
linters.detect { |linter| linter.simple_name == name }
end

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

LinterRegistry.load_custom_linters
LinterRegistry.load_custom_linters(@config.to_hash['custom_linters_directory'])
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
15 changes: 15 additions & 0 deletions spec/erb_lint/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ class FakeLinter2 < FakeLinter1; end
end
end

describe '#new' do
context 'with custom linters in custom directory' do
let(:custom_linters_directory) { File.expand_path('../fixtures/linters', __FILE__) }
let(:config) { ERBLint::RunnerConfig.new({
custom_linters_directory: custom_linters_directory
})}

it 'loads custom linters' do
expect(ERBLint::LinterRegistry).to receive(:require)
.with(File.join(custom_linters_directory, 'custom_linter.rb')).once
described_class.new(file_loader, config)
end
end
end

describe '#run' do
let(:file) { 'DummyFileContent' }
let(:filename) { 'somefolder/otherfolder/dummyfile.html.erb' }
Expand Down