@@ -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
0 commit comments