-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into custom-fallback-counter
- Loading branch information
Showing
7 changed files
with
100 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@primer/view-components': minor | ||
--- | ||
|
||
Add a linter discouraging use of <details-menu> in favor of Primer::Alpha::ActionMenu | ||
|
||
<!-- Changed components: _none_ --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
lib/primer/view_components/linters/details_menu_migration.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "helpers/rule_helpers" | ||
module ERBLint | ||
module Linters | ||
module Primer | ||
module Accessibility | ||
# Flag when `<details-menu>` is being used and offer alternatives. | ||
class DetailsMenuMigration < Linter | ||
include LinterRegistry | ||
include Helpers::RuleHelpers | ||
|
||
MIGRATE_FROM_DETAILS_MENU = "<details-menu> has been deprecated. Please instead use Primer::Alpha::ActionMenu" \ | ||
"https://primer.style/design/components/action-menu/rails/alpha" | ||
DETAILS_MENU_RUBY_PATTERN = /tag:?\s+:"details-menu"/.freeze | ||
|
||
def run(processed_source) | ||
# HTML tags | ||
tags(processed_source).each do |tag| | ||
next if tag.closing? | ||
|
||
generate_offense(self.class, processed_source, tag, MIGRATE_FROM_DETAILS_MENU) if tag.name == "details-menu" | ||
end | ||
|
||
# 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) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require "lib/erblint_test_case" | ||
|
||
class DetailsMenuMigrationTest < ErblintTestCase | ||
def linter_class | ||
ERBLint::Linters::Primer::Accessibility::DetailsMenuMigration | ||
end | ||
|
||
def test_warns_if_details_menu_tag_is_used | ||
@file = "<details-menu class='SelectMenu' role='menu'></details-menu>" | ||
@linter.run(processed_source) | ||
assert_equal 1, @linter.offenses.count | ||
assert_match(/.<details-menu> has been deprecated./, @linter.offenses.first.message) | ||
end | ||
|
||
def test_warns_if_details_menu_content_tag_is_rendered | ||
@file = <<~HTML | ||
<%= content_tag :"details-menu", | ||
class: "SelectMenu" 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_warns_if_details_menu_view_component_is_rendered | ||
@file = '<%= render SomeComponent.new(tag: :"details-menu") do %>' | ||
@linter.run(processed_source) | ||
assert_equal 1, @linter.offenses.count | ||
assert_match(/.<details-menu> has been deprecated./, @linter.offenses.first.message) | ||
end | ||
|
||
def test_warns_if_details_menu_view_component_slot_is_rendered | ||
@file = '<% component.with_body(tag: :"details-menu") do %>' | ||
@linter.run(processed_source) | ||
assert_equal 1, @linter.offenses.count | ||
assert_match(/.<details-menu> has been deprecated./, @linter.offenses.first.message) | ||
end | ||
|
||
def test_does_not_warn_if_inline_disable_comment | ||
@file = <<~HTML | ||
<%= render SomeComponent.new(tag: :"details-menu") do %><%# erblint:disable Primer::Accessibility::DetailsMenuMigration %> | ||
HTML | ||
@linter.run_and_update_offense_status(processed_source) | ||
offenses = @linter.offenses.reject(&:disabled?) | ||
assert_equal 0, offenses.count | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters