module ERBLint::Linters::CustomHelpers
Public Instance Methods
basic_conditional_code_check(code)
click to toggle source
Map possible values from condition
# File lib/erblint-github/linters/custom_helpers.rb, line 73 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)
click to toggle source
# 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)
click to toggle source
# File lib/erblint-github/linters/custom_helpers.rb, line 60 def generate_offense(klass, processed_source, tag, message = nil, replacement = nil) message ||= klass::MESSAGE 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
possible_attribute_values(tag, attr_name)
click to toggle source
# File lib/erblint-github/linters/custom_helpers.rb, line 67 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)
click to toggle source
# 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()
click to toggle source
# File lib/erblint-github/linters/custom_helpers.rb, line 82 def simple_class_name self.class.name.gsub("ERBLint::Linters::", "") end