Module: ERBLint::Linters::Helpers::RuleHelpers
- Included in:
- BaseLinter, Primer::Accessibility::DetailsMenuMigration, Primer::Accessibility::TooltippedMigration
- Defined in:
- lib/primer/view_components/linters/helpers/rule_helpers.rb
Overview
Helpers for writing rules
Instance Method Summary collapse
- #erb_nodes(processed_source) ⇒ Object
- #extract_ruby_from_erb_node(erb_node) ⇒ Object
-
#generate_node_offense(klass, processed_source, node, message = nil) ⇒ Object
Generate offense for ERB node tag.
- #generate_offense(klass, processed_source, tag, message = nil, replacement = nil) ⇒ Object
- #tags(processed_source) ⇒ Object
Instance Method Details
#erb_nodes(processed_source) ⇒ Object
26 27 28 |
# File 'lib/primer/view_components/linters/helpers/rule_helpers.rb', line 26 def erb_nodes(processed_source) processed_source.parser.ast.descendants(:erb) end |
#extract_ruby_from_erb_node(erb_node) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/primer/view_components/linters/helpers/rule_helpers.rb', line 30 def extract_ruby_from_erb_node(erb_node) return unless erb_node.type == :erb _, _, code_node = *erb_node code_node.loc.source.strip end |
#generate_node_offense(klass, processed_source, node, message = nil) ⇒ Object
Generate offense for ERB node tag
20 21 22 23 24 |
# File 'lib/primer/view_components/linters/helpers/rule_helpers.rb', line 20 def generate_node_offense(klass, processed_source, node, = nil) ||= klass::MESSAGE offense = ["#{klass.name}:#{}", node.loc.source].join("\n") add_offense(processed_source.to_source_range(node.loc), offense) end |
#generate_offense(klass, processed_source, tag, message = nil, replacement = nil) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/primer/view_components/linters/helpers/rule_helpers.rb', line 12 def generate_offense(klass, processed_source, tag, = nil, replacement = nil) ||= klass::MESSAGE klass_name = klass.name.demodulize offense = ["#{klass_name}:#{}", tag.node.loc.source].join("\n") add_offense(processed_source.to_source_range(tag.loc), offense, replacement) end |
#tags(processed_source) ⇒ Object
8 9 10 |
# File 'lib/primer/view_components/linters/helpers/rule_helpers.rb', line 8 def (processed_source) processed_source.parser.nodes_with_type(:tag).map { |tag_node| BetterHtml::Tree::Tag.from_node(tag_node) } end |