Skip to content

Commit

Permalink
Prevent an empty list of custom regexes in the DetailsMenuMigration l…
Browse files Browse the repository at this point in the history
…inter from reporting false positives (#2997)
  • Loading branch information
camertron committed Aug 6, 2024
1 parent fac1ec9 commit 0d0b5bc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/metal-shirts-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Prevent an empty list of custom regexes in the DetailsMenuMigration linter from reporting false-positives
24 changes: 23 additions & 1 deletion lib/primer/view_components/linters/details_menu_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,31 @@ def run(processed_source)
# ERB nodes
erb_nodes(processed_source).each do |node|
code = extract_ruby_from_erb_node(node)
generate_node_offense(self.class, processed_source, node, MIGRATE_FROM_DETAILS_MENU) if (code.match?(DETAILS_MENU_RUBY_PATTERN) || code.match?(Regexp.new(@config.custom_erb_pattern.join("|"), true)))

if contains_offense?(code)
generate_node_offense(self.class, processed_source, node, MIGRATE_FROM_DETAILS_MENU)
end
end
end

def contains_offense?(code)
return true if code.match?(DETAILS_MENU_RUBY_PATTERN)
return code.match?(custom_erb_pattern) if custom_erb_pattern
false
end

def custom_erb_pattern
unless defined?(@custom_erb_pattern)
@custom_erb_pattern =
if @config.custom_erb_pattern.empty?
nil
else
Regexp.new(@config.custom_erb_pattern.join("|"), true)
end
end

@custom_erb_pattern
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/lib/erblint/details_menu_migration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def test_warns_if_details_menu_view_component_slot_is_rendered
assert_match(/.<details-menu> has been deprecated./, @linter.offenses.first.message)
end

def test_does_not_warn_if_no_details_menu_used
@file = "<% component.with_body('foo') %>"
@linter.run(processed_source)
assert_equal 0, @linter.offenses.count
end

def test_does_not_warn_if_inline_disable_comment
@file = <<~HTML
<%= render SomeComponent.new(tag: :"details-menu") do %><%# erblint:disable Primer::Accessibility::DetailsMenuMigration %>
Expand Down

0 comments on commit 0d0b5bc

Please sign in to comment.