Method: ERBLint::Linters::BaseLinter#run

Defined in:
lib/primer/view_components/linters/base_linter.rb

#run(processed_source) ⇒ Object


38
39
40
41
42
43
44
45
46
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
83
# File 'lib/primer/view_components/linters/base_linter.rb', line 38

def run(processed_source)
  @total_offenses = 0
  @offenses_not_corrected = 0
  (tags, tag_tree) = build_tag_tree(processed_source)

  tags.each do |tag|
    next if tag.closing?
    next if self.class::TAGS.present? && self.class::TAGS&.none?(tag.name)

    classes = tag.attributes["class"]&.value&.split(" ") || []
    tag_tree[tag][:offense] = false

    next if (classes & self.class::DISALLOWED_CLASSES).any?
    next unless self.class::CLASSES.blank? || classes.any? do |klass|
      self.class::CLASSES.any? { |matcher| matcher === klass } # rubocop:disable Style/CaseEquality
    end

    args = map_arguments(tag, tag_tree[tag])
    correction = correction(args)

    attributes = tag.attributes.each.map(&:name).join(" ")
    matches_required_attributes = self.class::REQUIRED_ARGUMENTS.blank? || self.class::REQUIRED_ARGUMENTS.all? { |arg| attributes.match?(arg) }

    tag_tree[tag][:offense] = true
    tag_tree[tag][:correctable] = matches_required_attributes && !correction.nil?
    tag_tree[tag][:message] = message(args, processed_source)
    tag_tree[tag][:correction] = correction
  end

  tag_tree.each do |tag, h|
    next unless h[:offense]

    @total_offenses += 1
    # We always fix the offenses using blocks. The closing tag corresponds to `<% end %>`.
    if h[:correctable]
      add_correction(tag, h)
    else
      @offenses_not_corrected += 1
      generate_offense(self.class, processed_source, tag, h[:message])
    end
  end

  counter_correct?(processed_source)

  dump_data(processed_source) if ENV["DUMP_LINT_DATA"] == "1"
end