Class: ERBLint::Linter

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

Overview

Defines common functionality available to all linters.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_loader, config) ⇒ Linter

Must be implemented by the concrete inheriting class.

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
# File 'lib/erb_lint/linter.rb', line 38

def initialize(file_loader, config)
  @file_loader = file_loader
  @config = config
  raise ArgumentError, "expect `config` to be #{self.class.config_schema} instance, " \
    "not #{config.class}" unless config.is_a?(self.class.config_schema)
  @offenses = []
end

Class Attribute Details

.config_schemaObject

Returns the value of attribute config_schema.



10
11
12
# File 'lib/erb_lint/linter.rb', line 10

def config_schema
  @config_schema
end

.simple_nameObject

Returns the value of attribute simple_name.



9
10
11
# File 'lib/erb_lint/linter.rb', line 9

def simple_name
  @simple_name
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



35
36
37
# File 'lib/erb_lint/linter.rb', line 35

def config
  @config
end

#offensesObject (readonly)

Returns the value of attribute offenses.



35
36
37
# File 'lib/erb_lint/linter.rb', line 35

def offenses
  @offenses
end

Class Method Details

.inherited(linter) ⇒ Object

When defining a Linter class, define its simple name as well. This assumes that the module hierarchy of every linter starts with ‘ERBLint::Linters::`, and removes this part of the class name.

‘ERBLint::Linters::Foo.simple_name` #=> “Foo” `ERBLint::Linters::Compass::Bar.simple_name` #=> “Compass::Bar”



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/erb_lint/linter.rb', line 18

def inherited(linter)
  super
  linter.simple_name = if linter.name.start_with?("ERBLint::Linters::")
    name_parts = linter.name.split("::")
    name_parts[2..-1].join("::")
  else
    linter.name
  end

  linter.config_schema = LinterConfig
end

.support_autocorrect?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/erb_lint/linter.rb', line 30

def support_autocorrect?
  method_defined?(:autocorrect)
end

Instance Method Details

#add_offense(source_range, message, context = nil, severity = nil) ⇒ Object



65
66
67
# File 'lib/erb_lint/linter.rb', line 65

def add_offense(source_range, message, context = nil, severity = nil)
  @offenses << Offense.new(self, source_range, message, context, severity)
end

#clear_offensesObject



69
70
71
# File 'lib/erb_lint/linter.rb', line 69

def clear_offenses
  @offenses = []
end

#enabled?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/erb_lint/linter.rb', line 46

def enabled?
  @config.enabled?
end

#excludes_file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/erb_lint/linter.rb', line 50

def excludes_file?(filename)
  @config.excludes_file?(filename, @file_loader.base_path)
end

#run(_processed_source) ⇒ Object

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/erb_lint/linter.rb', line 54

def run(_processed_source)
  raise NotImplementedError, "must implement ##{__method__}"
end

#run_and_update_offense_status(processed_source, enable_inline_configs = true) ⇒ Object



58
59
60
61
62
63
# File 'lib/erb_lint/linter.rb', line 58

def run_and_update_offense_status(processed_source, enable_inline_configs = true)
  run(processed_source)
  if @offenses.any? && enable_inline_configs
    update_offense_status(processed_source)
  end
end