Class: LintTrappings::Linter Abstract

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

Overview

This class is abstract.

Base implementation for all lint checks.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Linter

Initializes a linter with the specified configuration.

Parameters:

  • config (Hash)

    configuration for this linter


89
90
91
92
93
94
# File 'lib/lint_trappings/linter.rb', line 89

def initialize(config)
  @orig_hash_config = @config = config
  validate_options_specification
  @config = convert_config_hash_to_struct(@config)
  @lints = []
end

Class Attribute Details

.options_struct_classObject

Returns the value of attribute options_struct_class


83
84
85
# File 'lib/lint_trappings/linter.rb', line 83

def options_struct_class
  @options_struct_class
end

Class Method Details

.canonical_nameString

Returns the canonical name for this linter class.

The canonical name is used as the key for configuring the linter in the configuration file, or when referring to it from the command line.

This uses the “Linter” module as an indicator of when to start removing unnecessary module prefixes.

Examples:

LintTrappings::Linter::MyLinter
=> "MyLinter"
MyCustomNamespace::MyLinter
=> "MyCustomNamespace::MyLinter"
MyModule::Linter::MyCustomNamespace::MyLinter
=> "MyCustomNamespace::MyLinter"

Returns:

  • (String)

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lint_trappings/linter.rb', line 38

def canonical_name
  @canonical_name ||=
    begin
      full_name = name.to_s.split('::')

      if linter_class_index = full_name.index('Linter')
        # Otherwise, the name follows the `Linter` module
        linter_class_index += 1
      else
        # If not found, include the full name
        linter_class_index = 0
      end

      full_name[linter_class_index..-1].join('::')
    end
end

.descendantsArray<Class>

Return all subclasses.

Returns:

  • (Array<Class>)

13
14
15
# File 'lib/lint_trappings/linter.rb', line 13

def descendants
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end

.description(*args) ⇒ Object


55
56
57
58
59
60
61
# File 'lib/lint_trappings/linter.rb', line 55

def description(*args)
  if args.any?
    @description = args.first
  else
    @description
  end
end

.option(name, options) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lint_trappings/linter.rb', line 63

def option(name, options)
  options = options.dup

  @options_spec ||= {}
  opt = @options_spec[name] = {}
  %i[type default description].each do |option_sym|
    opt[option_sym] = options.delete(option_sym) if options[option_sym]
  end

  if options.keys.any?
    raise InvalidOptionSpecificationError,
          "Unknown key `#{options.keys.first}` for `#{name}` option " \
          "specification on linter #{self}"
  end
end

.optionsObject


79
80
81
# File 'lib/lint_trappings/linter.rb', line 79

def options
  @options_spec || {}
end

Instance Method Details

#canonical_nameString

Returns the canonical name of this linter's class.

Returns:

  • (String)

See Also:

  • LintTrappings::Linter.{LintTrappings{LintTrappings::Linter{LintTrappings::Linter.canonical_name}

111
112
113
# File 'lib/lint_trappings/linter.rb', line 111

def canonical_name
  self.class.canonical_name
end

#run(document) ⇒ Object

Runs the linter against the given Slim document.

Parameters:


99
100
101
102
103
104
# File 'lib/lint_trappings/linter.rb', line 99

def run(document)
  @document = document
  @lints = []
  scan
  @lints
end