Skip to content

Commit 1c3a867

Browse files
committed
Clean up code a bit
1 parent f40b8dd commit 1c3a867

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

docs/rules/accessibility/avoid-generic-link-text-counter.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ This linter will raise a flag when it is able to detect that a generic link has
6666
<a href="github.com/about" aria-label="Why dogs are awesome">Read more</a>
6767
```
6868

69+
```erb
70+
<!-- also bad. `aria-describedby` does not count towards accessible name of an element-->
71+
<a href="github.com/about" aria-describedby="element123">Read more</a>
72+
```
73+
6974
### **Correct** code for this rule 👍
7075

7176
```erb

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class AvoidGenericLinkTextCounter < Linter
1818
"Link",
1919
"Here"
2020
].freeze
21+
ARIA_LABEL_ATTRIBUTES = %w[aria-labelledby aria-label].freeze
22+
2123
MESSAGE = "Avoid using generic link text such as #{BANNED_GENERIC_TEXT.join(', ')} which do not make sense in isolation."
2224

2325
def run(processed_source)
@@ -67,22 +69,21 @@ def run(processed_source)
6769
send_node.child_nodes.each do |child_node|
6870
banned_text = child_node.children.join if child_node.methods.include?(:type) && child_node.type == :str && banned_text?(child_node.children.join)
6971
next if banned_text.blank?
70-
7172
next unless child_node.methods.include?(:type) && child_node.type == :hash
7273

7374
child_node.descendants(:pair).each do |pair_node|
7475
next unless pair_node.children.first.type?(:sym)
7576

76-
if pair_node.children.first.children.join == "aria"
77-
pair_node.children[1].descendants(:sym).each do |sym_node|
78-
banned_text = nil if sym_node.children.join == "label" || sym_node.children.join == "labelledby"
79-
end
80-
end
81-
82-
# Skips if `link_to` has `aria-labelledby` or `aria-label` which we cannot be evaluated accurately with ERB lint alone.
77+
# Skips if `link_to` has `aria-labelledby` or `aria-label` which cannot be evaluated accurately with ERB lint alone.
8378
# ERB lint removes Ruby string interpolation so the `aria-label` for "<%= link_to 'Learn more', "aria-label": "Learn #{@some_variable}" %>" will
8479
# only be `Learn` which is unreliable so we can't do checks :(
85-
banned_text = nil if pair_node.children.first.children.join == "aria-labelledby" || pair_node.children.first.children.join == "aria-label"
80+
key_value = pair_node.children.first.children.join
81+
banned_text = nil if ARIA_LABEL_ATTRIBUTES.include?(key_value)
82+
next unless key_value == "aria"
83+
84+
pair_node.children[1].descendants(:sym).each do |sym_node|
85+
banned_text = nil if sym_node.children.join == "label" || sym_node.children.join == "labelledby"
86+
end
8687
end
8788
end
8889
if banned_text.present?
@@ -119,11 +120,6 @@ def valid_accessible_name?(aria_label, text)
119120
aria_label.downcase.include?(text.downcase)
120121
end
121122

122-
# Checks if Ruby node contains `aria-label` or `aria-labelledby`
123-
def ruby_node_contains_aria_label_attributes?(pair_node)
124-
pair_node.children.first.children.join == "aria-labelledby" || pair_node.children.first.children.join == "aria-label"
125-
end
126-
127123
def extract_ruby_node(source)
128124
BetterHtml::TestHelper::RubyNode.parse(source)
129125
rescue ::Parser::SyntaxError

test/linters/accessibility/avoid_generic_link_text_counter_test.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,33 @@ def test_does_not_warns_if_element_has_correct_counter_comment
178178

179179
def test_autocorrects_when_ignores_are_not_correct
180180
@file = <<~ERB
181-
<%# erblint:counter GitHub::Accessibility::AvoidGenericLinkTextCounter 2 %>
182-
<a>Link</a>
181+
<p>
182+
<a href="github.com" aria-label='Click here to learn more'>Click here</a>
183+
<a href="github.com" aria-label='Some totally different text'>Click here</a>
184+
<a href="github.com" aria-labelledby='someElement'>Click here</a>
185+
</p>
186+
<p>
187+
<%= link_to "learn more", billing_path, "aria-label": "something" %>
188+
<%= link_to "learn more", billing_path, aria: { label: "something" } %>
189+
<%= link_to "learn more", billing_path, aria: { describedby: "element123" } %>
190+
<%= link_to "learn more", billing_path, "aria-describedby": "element123" %>
191+
</p>
183192
ERB
184193
refute_equal @file, corrected_content
185194

186195
expected_content = <<~ERB
187-
<%# erblint:counter GitHub::Accessibility::AvoidGenericLinkTextCounter 1 %>
188-
<a>Link</a>
196+
<%# erblint:counter GitHub::Accessibility::AvoidGenericLinkTextCounter 3 %>
197+
<p>
198+
<a href="github.com" aria-label='Click here to learn more'>Click here</a>
199+
<a href="github.com" aria-label='Some totally different text'>Click here</a>
200+
<a href="github.com" aria-labelledby='someElement'>Click here</a>
201+
</p>
202+
<p>
203+
<%= link_to "learn more", billing_path, "aria-label": "something" %>
204+
<%= link_to "learn more", billing_path, aria: { label: "something" } %>
205+
<%= link_to "learn more", billing_path, aria: { describedby: "element123" } %>
206+
<%= link_to "learn more", billing_path, "aria-describedby": "element123" %>
207+
</p>
189208
ERB
190209
assert_equal expected_content, corrected_content
191210
end

0 commit comments

Comments
 (0)