Class: LintTrappings::LinterSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/lint_trappings/linter_selector.rb

Overview

Chooses the appropriate linters to run against a file.

All linter inclusion/exclusion based on command line flags or configuration is handled here. This is utilized by the runner to generate linter/file tuples representing jobs to execute (i.e. run the linter X against file Y).

Instance Method Summary collapse

Constructor Details

#initialize(application, config, options) ⇒ LinterSelector

Returns a new instance of LinterSelector.

Parameters:

Raises:



13
14
15
16
17
18
19
20
21
# File 'lib/lint_trappings/linter_selector.rb', line 13

def initialize(application, config, options)
  @application = application
  @config = config
  @options = options

  # Pre-compute this as it is expensive to calculate and used many times.
  # This forces any errors in the configuration to be surfaced ahead of time.
  @enabled_linter_classes = enabled_linter_classes
end

Instance Method Details

#all_linter_classesArray<Class>

Return all loaded linter classes for this application.

Returns:

  • (Array<Class>)


25
26
27
# File 'lib/lint_trappings/linter_selector.rb', line 25

def all_linter_classes
  @application.linter_base_class.descendants
end

#enabled_linter_classesArray<LintTrappings::Linter>

Returns a list of linters that are enabled given the specified configuration and additional options.

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/lint_trappings/linter_selector.rb', line 47

def enabled_linter_classes
  # Include the explicit list of linters if a list was specified
  explicitly_included = included_linter_classes =
    linter_classes_from_names(@options.fetch(:included_linters, []))

  if included_linter_classes.empty?
    # Otherwise use the list of enabled linters specified by the config.
    # Note: this means that a linter which is disabled in the configuration
    # can still be run if it is explicitly specified in `included_linters`
    included_linter_classes = all_linter_classes.select do |linter_class|
      linter_enabled?(linter_class)
    end
  end

  excluded_linter_classes =
    linter_classes_from_names(@options.fetch(:excluded_linters, []))

  linter_classes = included_linter_classes - excluded_linter_classes

  # Highlight conditions where all linters were filtered out, as this was
  # likely a mistake on the user's part
  if linter_classes.empty?
    if explicitly_included.any?
      raise NoLintersError,
            'All specified linters were explicitly excluded!'
    elsif included_linter_classes.empty?
      raise NoLintersError,
            'All linters are disabled. Enable some in your configuration!'
    else
      raise NoLintersError,
            'All enabled linters were explicitly excluded!'
    end
  end

  linter_classes
end

#linters_for_file(path) ⇒ Array<LintTrappings::Linter>

Returns initialized linter instances to run against a given file.

Parameters:

  • path (String)

Returns:



34
35
36
37
38
39
40
41
# File 'lib/lint_trappings/linter_selector.rb', line 34

def linters_for_file(path)
  @enabled_linter_classes.map do |linter_class|
    linter_conf = @config.for_linter(linter_class)
    next unless run_linter_on_file?(linter_conf, path)

    linter_class.new(linter_conf)
  end.compact
end