Skip to content

Commit ef9cccc

Browse files
authored
Update custom_helpers.rb
1 parent ae3dd64 commit ef9cccc

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

lib/erblint-github/linters/custom_helpers.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,39 @@ def possible_attribute_values(tag, attr_name)
2727
[value].compact
2828
end
2929

30+
def counter_correct?(processed_source)
31+
comment_node = nil
32+
expected_count = 0
33+
rule_name = self.class.name.gsub("ERBLint::Linters::", "")
34+
offenses_count = @offenses.length
35+
36+
processed_source.parser.ast.descendants(:erb).each do |node|
37+
indicator_node, _, code_node, = *node
38+
indicator = indicator_node&.loc&.source
39+
comment = code_node&.loc&.source&.strip
40+
41+
if indicator == "#" && comment.start_with?("erblint:counter") && comment.match(rule_name)
42+
comment_node = node
43+
expected_count = comment.match(/\s(\d+)\s?$/)[1].to_i
44+
end
45+
end
46+
47+
if offenses_count.zero?
48+
# have to adjust to get `\n` so we delete the whole line
49+
add_offense(processed_source.to_source_range(comment_node.loc.adjust(end_pos: 1)), "Unused erblint:counter comment for #{rule_name}", "") if comment_node
50+
return
51+
end
52+
53+
first_offense = @offenses[0]
54+
55+
if comment_node.nil?
56+
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} %>")
57+
else
58+
clear_offenses
59+
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
60+
end
61+
end
62+
3063
# Map possible values from condition
3164
def basic_conditional_code_check(code)
3265
conditional_match = code.match(/["'](.+)["']\sif|unless\s.+/) || code.match(/.+\s?\s["'](.+)["']\s:\s["'](.+)["']/)

0 commit comments

Comments
 (0)