Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ef9cccc

Browse filesBrowse files
authored
Update custom_helpers.rb
1 parent ae3dd64 commit ef9cccc
Copy full SHA for ef9cccc

File tree

Expand file treeCollapse file tree

1 file changed

+33
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+33
-0
lines changed

‎lib/erblint-github/linters/custom_helpers.rb

Copy file name to clipboardExpand all lines: lib/erblint-github/linters/custom_helpers.rb
+33Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.