Module: ERBLint::Linters::CustomHelpers
- Included in:
- GitHub::Accessibility::AvoidBothDisabledAndAriaDisabled, GitHub::Accessibility::AvoidGenericLinkTextCounter, GitHub::Accessibility::DisabledAttributeCounter, GitHub::Accessibility::IframeHasTitle, GitHub::Accessibility::ImageHasAlt, GitHub::Accessibility::LinkHasHrefCounter, GitHub::Accessibility::NoAriaLabelMisuseCounter, GitHub::Accessibility::NoPositiveTabIndex, GitHub::Accessibility::NoRedundantImageAlt, GitHub::Accessibility::NoTitleAttributeCounter, GitHub::Accessibility::SvgHasAccessibleTextCounter
- Defined in:
- lib/erblint-github/linters/custom_helpers.rb
Instance Method Summary collapse
-
#basic_conditional_code_check(code) ⇒ Object
Map possible values from condition.
- #counter_correct?(processed_source) ⇒ Boolean
- #generate_offense(klass, processed_source, tag, message = nil, replacement = nil) ⇒ Object
- #generate_offense_from_source_range(klass, source_range, message = nil, replacement = nil) ⇒ Object
- #possible_attribute_values(tag, attr_name) ⇒ Object
- #rule_disabled?(processed_source) ⇒ Boolean
- #simple_class_name ⇒ Object
- #tags(processed_source) ⇒ Object
Instance Method Details
#basic_conditional_code_check(code) ⇒ Object
Map possible values from condition
80 81 82 83 |
# File 'lib/erblint-github/linters/custom_helpers.rb', line 80 def basic_conditional_code_check(code) conditional_match = code.match(/["'](.+)["']\sif|unless\s.+/) || code.match(/.+\s?\s["'](.+)["']\s:\s["'](.+)["']/) [conditional_match[1], conditional_match[2]].compact if conditional_match end |
#counter_correct?(processed_source) ⇒ Boolean
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/erblint-github/linters/custom_helpers.rb', line 27 def counter_correct?(processed_source) comment_node = nil expected_count = 0 rule_name = simple_class_name 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(processed_source.to_source_range(comment_node.loc.adjust(end_pos: 1)), "Unused erblint:counter comment for #{rule_name}", "") if comment_node return end first_offense = @offenses[0] if comment_node.nil? add_offense(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(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 |
#generate_offense(klass, processed_source, tag, message = nil, replacement = nil) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/erblint-github/linters/custom_helpers.rb', line 60 def generate_offense(klass, processed_source, tag, = nil, replacement = nil) ||= klass::MESSAGE += "\nLearn more at https://github.com/github/erblint-github#rules.\n" offense = ["#{simple_class_name}:#{message}", tag.node.loc.source].join("\n") add_offense(processed_source.to_source_range(tag.loc), offense, replacement) end |
#generate_offense_from_source_range(klass, source_range, message = nil, replacement = nil) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/erblint-github/linters/custom_helpers.rb', line 67 def generate_offense_from_source_range(klass, source_range, = nil, replacement = nil) ||= klass::MESSAGE += "\nLearn more at https://github.com/github/erblint-github#rules.\n" offense = ["#{simple_class_name}:#{message}", source_range.source].join("\n") add_offense(source_range, offense, replacement) end |
#possible_attribute_values(tag, attr_name) ⇒ Object
74 75 76 77 |
# File 'lib/erblint-github/linters/custom_helpers.rb', line 74 def possible_attribute_values(tag, attr_name) value = tag.attributes[attr_name]&.value || nil basic_conditional_code_check(value || "") || [value].compact end |
#rule_disabled?(processed_source) ⇒ Boolean
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/erblint-github/linters/custom_helpers.rb', line 9 def rule_disabled?(processed_source) 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 rule_name = simple_class_name if indicator == "#" && comment.start_with?("erblint:disable") && comment.match(rule_name) if @offenses.any? clear_offenses else add_offense(processed_source.to_source_range(code_node.loc), "Unused erblint:disable comment for #{rule_name}") end end end end |
#simple_class_name ⇒ Object
89 90 91 |
# File 'lib/erblint-github/linters/custom_helpers.rb', line 89 def simple_class_name self.class.name.gsub("ERBLint::Linters::", "") end |
#tags(processed_source) ⇒ Object
85 86 87 |
# File 'lib/erblint-github/linters/custom_helpers.rb', line 85 def (processed_source) processed_source.parser.nodes_with_type(:tag).map { |tag_node| BetterHtml::Tree::Tag.from_node(tag_node) } end |