Skip to content

Commit 0d0b5bc

Browse files
authored
Prevent an empty list of custom regexes in the DetailsMenuMigration linter from reporting false positives (#2997)
1 parent fac1ec9 commit 0d0b5bc

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

.changeset/metal-shirts-sparkle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/view-components': patch
3+
---
4+
5+
Prevent an empty list of custom regexes in the DetailsMenuMigration linter from reporting false-positives

lib/primer/view_components/linters/details_menu_migration.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,31 @@ def run(processed_source)
3232
# ERB nodes
3333
erb_nodes(processed_source).each do |node|
3434
code = extract_ruby_from_erb_node(node)
35-
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)))
35+
36+
if contains_offense?(code)
37+
generate_node_offense(self.class, processed_source, node, MIGRATE_FROM_DETAILS_MENU)
38+
end
3639
end
3740
end
41+
42+
def contains_offense?(code)
43+
return true if code.match?(DETAILS_MENU_RUBY_PATTERN)
44+
return code.match?(custom_erb_pattern) if custom_erb_pattern
45+
false
46+
end
47+
48+
def custom_erb_pattern
49+
unless defined?(@custom_erb_pattern)
50+
@custom_erb_pattern =
51+
if @config.custom_erb_pattern.empty?
52+
nil
53+
else
54+
Regexp.new(@config.custom_erb_pattern.join("|"), true)
55+
end
56+
end
57+
58+
@custom_erb_pattern
59+
end
3860
end
3961
end
4062
end

test/lib/erblint/details_menu_migration_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ def test_warns_if_details_menu_view_component_slot_is_rendered
3838
assert_match(/.<details-menu> has been deprecated./, @linter.offenses.first.message)
3939
end
4040

41+
def test_does_not_warn_if_no_details_menu_used
42+
@file = "<% component.with_body('foo') %>"
43+
@linter.run(processed_source)
44+
assert_equal 0, @linter.offenses.count
45+
end
46+
4147
def test_does_not_warn_if_inline_disable_comment
4248
@file = <<~HTML
4349
<%= render SomeComponent.new(tag: :"details-menu") do %><%# erblint:disable Primer::Accessibility::DetailsMenuMigration %>

0 commit comments

Comments
 (0)