Class: ERBLint::Linters::DeprecatedComponentsCounter
- Inherits:
-
BaseLinter
- Object
- Linter
- BaseLinter
- ERBLint::Linters::DeprecatedComponentsCounter
- Includes:
- ERBLint::LinterRegistry, Helpers::DeprecatedComponentsHelpers
- Defined in:
- lib/primer/view_components/linters/deprecated_components_counter.rb
Overview
Lints against deprecated components
Constant Summary
Constants inherited from BaseLinter
BaseLinter::CLASSES, BaseLinter::DISALLOWED_CLASSES, BaseLinter::DUMP_FILE, BaseLinter::REQUIRED_ARGUMENTS
Constants included from TagTreeHelpers
TagTreeHelpers::SELF_CLOSING_TAGS
Instance Method Summary collapse
- #add_offense_with_severity(source_range, message, context = nil, severity = nil) ⇒ Object
- #autocorrect(processed_source, offense) ⇒ Object
- #counter_correct?(processed_source) ⇒ Boolean
- #run(processed_source) ⇒ Object
Methods included from Helpers::DeprecatedComponentsHelpers
#deprecated_components, #message
Methods inherited from BaseLinter
Methods included from Helpers::RuleHelpers
#erb_nodes, #extract_ruby_from_erb_node, #generate_node_offense, #generate_offense, #tags
Methods included from TagTreeHelpers
#build_tag_tree, #self_closing?
Instance Method Details
#add_offense_with_severity(source_range, message, context = nil, severity = nil) ⇒ Object
79 80 81 |
# File 'lib/primer/view_components/linters/deprecated_components_counter.rb', line 79 def add_offense_with_severity(source_range, , context = nil, severity = nil) add_offense(source_range, , context, severity || @config.severity) end |
#autocorrect(processed_source, offense) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/primer/view_components/linters/deprecated_components_counter.rb', line 32 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 |
#counter_correct?(processed_source) ⇒ Boolean
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 |
# File 'lib/primer/view_components/linters/deprecated_components_counter.rb', line 46 def counter_correct?(processed_source) comment_node = nil expected_count = 0 rule_name = self.class.name.gsub("ERBLint::Linters::", "") offenses_count = @offenses.length processed_source.parser.ast.descendants(:erb).each do |node| indicator_node, _, code_node, = *node indicator = indicator_node&.loc&.source comment = code_node&.loc&.source&.strip if indicator == "#" && comment.start_with?("erblint:counter") && comment.match(rule_name) comment_node = node expected_count = comment.match(/\s(\d+)\s?$/)[1].to_i end end if offenses_count.zero? # have to adjust to get `\n` so we delete the whole line add_offense_with_severity(processed_source.to_source_range(comment_node.loc.adjust(end_pos: 1)), "Unused erblint:counter comment for #{rule_name}", "") if comment_node return false end first_offense = @offenses[0] if comment_node.nil? add_offense_with_severity(processed_source.to_source_range(first_offense.source_range), "#{rule_name}: If you must, add <%# erblint:counter #{rule_name} #{offenses_count} %> to bypass this check.", "<%# erblint:counter #{rule_name} #{offenses_count} %>") else clear_offenses add_offense_with_severity(processed_source.to_source_range(comment_node.loc), "Incorrect erblint:counter number for #{rule_name}. Expected: #{expected_count}, actual: #{offenses_count}.", "<%# erblint:counter #{rule_name} #{offenses_count} %>") if expected_count != offenses_count end end |
#run(processed_source) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/primer/view_components/linters/deprecated_components_counter.rb', line 15 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?("Primer::") deprecated_components.each do |component| next unless code.include?(component) add_offense_with_severity(erb_node.loc, (component)) end end counter_correct?(processed_source) end |