From 9f1e846c8e022bd835419856f52afb543e68346b Mon Sep 17 00:00:00 2001 From: Gavin Elster Date: Mon, 11 May 2015 09:16:11 -0700 Subject: [PATCH 1/3] Add rubocop config option --- lib/slim_lint/options.rb | 5 +++++ spec/slim_lint/options_spec.rb | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/slim_lint/options.rb b/lib/slim_lint/options.rb index 744398f..b687602 100644 --- a/lib/slim_lint/options.rb +++ b/lib/slim_lint/options.rb @@ -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 diff --git a/spec/slim_lint/options_spec.rb b/spec/slim_lint/options_spec.rb index eac9cfa..6313cd0 100644 --- a/spec/slim_lint/options_spec.rb +++ b/spec/slim_lint/options_spec.rb @@ -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] } From 36f811fd76825047a57f0d91daaf86c26f8022af Mon Sep 17 00:00:00 2001 From: Gavin Elster Date: Mon, 11 May 2015 16:42:58 -0700 Subject: [PATCH 2/3] Pass command-line options hash to linters --- lib/slim_lint/linter.rb | 7 +++++-- lib/slim_lint/linter_selector.rb | 2 +- lib/slim_lint/runner.rb | 3 +++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/slim_lint/linter.rb b/lib/slim_lint/linter.rb index 072e974..c6f54c5 100644 --- a/lib/slim_lint/linter.rb +++ b/lib/slim_lint/linter.rb @@ -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 @@ -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. # diff --git a/lib/slim_lint/linter_selector.rb b/lib/slim_lint/linter_selector.rb index 058be9f..1ab9f07 100644 --- a/lib/slim_lint/linter_selector.rb +++ b/lib/slim_lint/linter_selector.rb @@ -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 diff --git a/lib/slim_lint/runner.rb b/lib/slim_lint/runner.rb index d2bcac7..5e945e2 100644 --- a/lib/slim_lint/runner.rb +++ b/lib/slim_lint/runner.rb @@ -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] # @option options :included_linters [Array] @@ -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] @@ -64,6 +66,7 @@ def collect_lints(file, linter_selector, config) # # @param config [SlimLint::Configuration] # @param options [Hash] + # @option (see #run) # @return [Array] def extract_applicable_files(config, options) included_patterns = options[:files] From 1b08f3dce68174e994e4d0776235ac93a4748ea0 Mon Sep 17 00:00:00 2001 From: Gavin Elster Date: Mon, 11 May 2015 16:49:09 -0700 Subject: [PATCH 3/3] Add ability for rubocop linter to manually set rubocop config This enables the use of a separate rubocop config for your slim files, and supports live-linting tools that lint files in temporary paths --- lib/slim_lint/linter/rubocop.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/slim_lint/linter/rubocop.rb b/lib/slim_lint/linter/rubocop.rb index 133e1f5..6fc5dac 100644 --- a/lib/slim_lint/linter/rubocop.rb +++ b/lib/slim_lint/linter/rubocop.rb @@ -51,7 +51,7 @@ def find_lints(ruby, source_map) # @param file [String] # @return [Array] def lint_file(rubocop, file) - rubocop.run(%w[--format SlimLint::OffenseCollector] << file) + rubocop.run(rubocop_settings << file) OffenseCollector.offenses end @@ -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] + 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.