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

Manually set rubocop config #5

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 5 additions & 2 deletions lib/slim_lint/linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ class Linter
# Initializes a linter with the specified configuration.
#
# @param config [Hash] configuration for this linter
def initialize(config)
# @param options [Hash]
# @option (see Runner#run)
def initialize(config, options = {})
@config = config
@options = options
@lints = []
end

Expand All @@ -40,7 +43,7 @@ def name

private

attr_reader :config, :document
attr_reader :config, :options, :document

# Record a lint for reporting back to the user.
#
Expand Down
13 changes: 12 additions & 1 deletion lib/slim_lint/linter/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def find_lints(ruby, source_map)
# @param file [String]
# @return [Array<RuboCop::Cop::Offense>]
def lint_file(rubocop, file)
rubocop.run(%w[--format SlimLint::OffenseCollector] << file)
rubocop.run(rubocop_settings << file)
OffenseCollector.offenses
end

Expand All @@ -69,6 +69,17 @@ def extract_lints_from_offenses(offenses, source_map)
"#{offense.cop_name}: #{offense.message}")
end
end

# Returns settings for the rubocop CLI
#
# @return [Array<String>]
def rubocop_settings
settings = %w[--format SlimLint::OffenseCollector]
if options['rubocop_config_file']
settings << %w[--config #{options['rubocop_config_file']}]
end
settings
end
end

# Collects offenses detected by RuboCop.
Expand Down
2 changes: 1 addition & 1 deletion lib/slim_lint/linter_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def extract_enabled_linters(config, options)
# linters which are enabled in the configuration
linters = (included_linters - excluded_linters).map do |linter_class|
linter_config = config.for_linter(linter_class)
linter_class.new(linter_config) if linter_config['enabled']
linter_class.new(linter_config, options) if linter_config['enabled']
end.compact

# Highlight condition where all linters were filtered out, as this was
Expand Down
5 changes: 5 additions & 0 deletions lib/slim_lint/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ def add_file_options(parser)
@options[:config_file] = conf_file
end

parser.on('--rubocop-config config-file', String,
'Specify which rubocop configuration file you want to use') do |rubocop_conf_file|
@options[:rubocop_config_file] = rubocop_conf_file
end

parser.on('-e', '--exclude file,...', Array,
'List of file names to exclude') do |files|
@options[:excluded_files] = files
Expand Down
3 changes: 3 additions & 0 deletions lib/slim_lint/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Runner
#
# @param [Hash] options
# @option options :config_file [String] path of configuration file to load
# @option options :rubocop_config_file [String] path of rubocop config file
# @option options :config [SlimLint::Configuration] configuration to use
# @option options :excluded_files [Array<String>]
# @option options :included_linters [Array<String>]
Expand All @@ -30,6 +31,7 @@ def run(options = {})
# specified options.
#
# @param options [Hash]
# @option (see #run)
# @return [SlimLint::Configuration]
def load_applicable_config(options)
if options[:config_file]
Expand Down Expand Up @@ -64,6 +66,7 @@ def collect_lints(file, linter_selector, config)
#
# @param config [SlimLint::Configuration]
# @param options [Hash]
# @option (see #run)
# @return [Array<String>]
def extract_applicable_files(config, options)
included_patterns = options[:files]
Expand Down
8 changes: 8 additions & 0 deletions spec/slim_lint/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
end
end

context 'with a rubocop configuration file specified' do
let(:args) { %w[--rubocop-config some-config.yml] }

it 'sets the `rubocop_config_file` option to that file path' do
subject.should include rubocop_config_file: 'some-config.yml'
end
end

context 'with a list of files to exclude' do
let(:args) { %w[--exclude some-glob-pattern/*.slim,some-other-pattern.slim] }

Expand Down