Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Temporarily introduce back counter_correct? helper #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/erblint-github/linters/custom_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,39 @@ def possible_attribute_values(tag, attr_name)
[value].compact
end

def counter_correct?(processed_source)
comment_node = nil
expected_count = 0
rule_name = self.class.name.gsub("ERBLint::Linters::", "")
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

# Map possible values from condition
def basic_conditional_code_check(code)
conditional_match = code.match(/["'](.+)["']\sif|unless\s.+/) || code.match(/.+\s?\s["'](.+)["']\s:\s["'](.+)["']/)
Expand Down