Skip to content
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

[linter] add custom config for DetailsMenuMigration #2985

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/warm-oranges-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Adds custom config for DetailsMenuMigration linter.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class DetailsMenuMigration < Linter
" https://primer.style/design/components/action-menu/rails/alpha"
DETAILS_MENU_RUBY_PATTERN = /tag:?\s+:"details-menu"/.freeze

# Allow custom pattern matching for ERB nodes
class ConfigSchema < LinterConfig
property :custom_erb_pattern, accepts: array_of?(Regexp),
default: -> { [] }
end
self.config_schema = ConfigSchema

def run(processed_source)
# HTML tags
tags(processed_source).each do |tag|
Expand All @@ -25,7 +32,7 @@ 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)
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)))
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions test/lib/erblint/details_menu_migration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,27 @@ def test_does_not_warn_if_inline_disable_comment
offenses = @linter.offenses.reject(&:disabled?)
assert_equal 0, offenses.count
end

def test_accepts_custom_regex_pattern
@linter.config.custom_erb_pattern = [/render[\s\(]GitHub::MenuComponent/]
@file = <<~HTML
<%= render GitHub::MenuComponent.new do %>
HTML
@linter.run(processed_source)
assert_equal 1, @linter.offenses.count
assert_match(/.<details-menu> has been deprecated./, @linter.offenses.first.message)
end

def test_accepts_multiple_custom_regex_pattern
@linter.config.custom_erb_pattern = [/render[\s\(]GitHub::MenuComponent/, /SomeOtherComponent/]
@file = <<~HTML
<%= render GitHub::MenuComponent.new do %>
<% end %>
<%= render SomeOtherComponent.new do %>
<% end %>
HTML
@linter.run(processed_source)
assert_equal 2, @linter.offenses.count
assert_match(/.<details-menu> has been deprecated./, @linter.offenses.first.message)
end
end
Loading