Skip to content

Commit 5295b93

Browse files
committed
Fix lint rule logic
1 parent cc6a406 commit 5295b93

File tree

2 files changed

+37
-45
lines changed

2 files changed

+37
-45
lines changed

lib/erblint-github/linters/github/accessibility/landmark_has_label.rb

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,7 @@ class LandmarkHasLabel < Linter
1010
include ERBLint::Linters::CustomHelpers
1111
include LinterRegistry
1212

13-
LANDMARK_ROLES = %w[complementary navigation region search].freeze
14-
LANDMARK_TAGS = %w[aside nav section].freeze
15-
MESSAGE = "Landmark elements should have an aria-label attribute, or aria-labelledby if a heading elements exists in the landmark."
16-
ROLE_TAG_MAPPING = { "complementary" => "aside", "navigation" => "nav", "region" => "section" }.freeze
17-
18-
def get_additional_message(tag, roles)
19-
role_matched = (roles & ROLE_TAG_MAPPING.keys).first
20-
if role_matched
21-
tag_matched = ROLE_TAG_MAPPING[role_matched]
22-
23-
if tag.name == tag_matched
24-
"The <#{tag_matched}> element will automatically communicate a role of '#{role_matched}'. You can safely drop the role attribute."
25-
else
26-
replace_message = if tag.name == "div"
27-
"If possible replace this tag with a <#{tag_matched}>."
28-
else
29-
"Wrapping this element in a <#{tag_matched}> and setting a label on it is reccomended."
30-
end
31-
32-
"The <#{tag_matched}> element will automatically communicate a role of '#{role_matched}'. #{replace_message}"
33-
end
34-
elsif roles.include?("search") && tag.name != "form"
35-
"The 'search' role works best when applied to a <form> element. If possible replace this tag with a <form>."
36-
end
37-
end
13+
MESSAGE = "The navigation landmark should have a unique accessible name via `aria-label` or `aria-labelledby`."
3814

3915
class ConfigSchema < LinterConfig
4016
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
@@ -44,17 +20,15 @@ class ConfigSchema < LinterConfig
4420
def run(processed_source)
4521
tags(processed_source).each do |tag|
4622
next if tag.closing?
47-
48-
possible_roles = possible_attribute_values(tag, "role")
49-
next unless LANDMARK_TAGS.include?(tag.name) && (possible_roles & LANDMARK_ROLES).empty?
50-
next if tag.attributes["aria-label"]&.value&.present? || tag.attributes["aria-labelledby"]&.value&.present?
51-
52-
message = get_additional_message(tag, possible_roles)
53-
if message
54-
generate_offense(self.class, processed_source, tag, "#{MESSAGE}\n#{message}")
55-
else
56-
generate_offense(self.class, processed_source, tag)
23+
next unless possible_attribute_values(tag, "role").include?("navigation") || tag.name == "nav"
24+
if possible_attribute_values(tag, "aria-label").empty? && possible_attribute_values(tag, "aria-labelledby").empty?
25+
message = MESSAGE
26+
if tag.name != "nav"
27+
message += "Additionally, you can safely drop the `role='navigation'` and replace it with the native HTML `nav` element."
28+
end
29+
generate_offense(self.class, processed_source, tag, message)
5730
end
31+
5832
end
5933

6034
if @config.counter_enabled?

test/linters/accessibility/landmark_has_label_test.rb

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,42 @@ def linter_class
77
ERBLint::Linters::GitHub::Accessibility::LandmarkHasLabel
88
end
99

10-
def test_warns_if_landmark_has_no_label
10+
def test_warns_if_navigation_landmark_has_no_label
1111
@file = <<~ERB
12-
<section>
13-
<h1>This is a text</h1>
14-
</section>
12+
<nav>
13+
</nav>
1514
ERB
1615
@linter.run(processed_source)
1716

1817
assert_equal(1, @linter.offenses.count)
19-
error_messages = @linter.offenses.map(&:message).sort
20-
assert_match(/Landmark elements should have an aria-label attribute, or aria-labelledby if a heading elements exists in the landmark./, error_messages.last)
18+
assert_match(/The navigation landmark should have a unique accessible name via `aria-label` or `aria-labelledby`./, @linter.offenses.first.message)
2119
end
2220

23-
def test_does_not_warn_if_landmark_has_label
21+
def test_warns_if_navigation_role_landmark_has_no_label
2422
@file = <<~ERB
25-
<section aria-labelledby="title_id"t>
26-
<h1 id="title_id">This is a text</h1>
27-
</section>
23+
<div role="navigation">
24+
</div>
25+
ERB
26+
@linter.run(processed_source)
27+
28+
assert_equal(1, @linter.offenses.count)
29+
assert_match(/The navigation landmark should have a unique accessible name via `aria-label` or `aria-labelledby`./, @linter.offenses.first.message)
30+
end
31+
32+
def test_does_not_warn_if_navigation_landmark_has_aria_labelled_by
33+
@file = <<~ERB
34+
<nav aria-labelledby="title_id"t>
35+
</nav>
36+
ERB
37+
@linter.run(processed_source)
38+
39+
assert_empty @linter.offenses
40+
end
41+
42+
def test_does_not_warn_if_navigation_landmark_has_aria_label
43+
@file = <<~ERB
44+
<nav aria-label="Repos"t>
45+
</nav>
2846
ERB
2947
@linter.run(processed_source)
3048

0 commit comments

Comments
 (0)