Class: ERBLint::Linters::DeprecatedComponentsCounter

Inherits:
Linter
  • Object
show all
Includes:
CustomHelpers, ERBLint::LinterRegistry, Helpers::DeprecatedComponentsHelpers
Defined in:
lib/yattho/view_components/linters/deprecated_components_counter.rb

Overview

Lints against deprecated components

Instance Method Summary collapse

Methods included from Helpers::DeprecatedComponentsHelpers

#deprecated_components, #message

Instance Method Details

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

this override is necessary because of the github/erblint-github ‘CustomHelpers` module. `counter_correct?` is provided by this module, and calls `add_offense` directly. there is no simple way to modify this without updating the gem and creating what would likely be an API that is non-standard and/or difficult to use

github.com/github/erblint-github/blob/main/lib/erblint-github/linters/custom_helpers.rb



55
56
57
# File 'lib/yattho/view_components/linters/deprecated_components_counter.rb', line 55

def add_offense(source_range, message, context = nil, severity = nil)
  super(source_range, message, context, severity || @config.severity)
end

#autocorrect(processed_source, offense) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/yattho/view_components/linters/deprecated_components_counter.rb', line 35

def autocorrect(processed_source, offense)
  return unless offense.context

  lambda do |corrector|
    if processed_source.file_content.include?("erblint:counter #{self.class.name.gsub('ERBLint::Linters::', '')}")
      # update the counter if exists
      corrector.replace(offense.source_range, offense.context)
    else
      # add comment with counter if none
      corrector.insert_before(processed_source.source_buffer.source_range, "#{offense.context}\n")
    end
  end
end

#run(processed_source) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/yattho/view_components/linters/deprecated_components_counter.rb', line 18

def run(processed_source)
  processed_source.ast.descendants(:erb).each do |erb_node|
    _, _, code_node = *erb_node
    code = code_node.children.first.strip

    next unless code.include?("Yattho::")

    deprecated_components.each do |component|
      next unless code.include?(component)

      add_offense(erb_node.loc, message(component))
    end
  end

  counter_correct?(processed_source)
end