Skip to content

Commit 3dfa13b

Browse files
committed
Update logic
1 parent 65378df commit 3dfa13b

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

lib/erblint-github/linters/custom_helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def generate_offense_from_source_range(klass, source_range, message = nil, repla
5757

5858
def possible_attribute_values(tag, attr_name)
5959
value = tag.attributes[attr_name]&.value || nil
60-
basic_conditional_code_check(value || "") || [value].compact
60+
[value].compact
6161
end
6262

6363
# Map possible values from condition

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

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

13-
MESSAGE = "[aria-label] text should be formatted the same as you would visual text. Use sentence case."
13+
MESSAGE = "[aria-label] text should be formatted the same as you would visual text. Use sentence case, and don't format it like an ID. Additionally, `aria-label` should be concise and should not contain line breaks."
1414

1515
class ConfigSchema < LinterConfig
1616
property :exceptions, accepts: array_of?(String),
@@ -23,9 +23,8 @@ def run(processed_source)
2323
next if tag.closing?
2424

2525
aria_label = possible_attribute_values(tag, "aria-label").join
26-
next if aria_label.empty?
2726

28-
if aria_label.match?(/^[a-z]/) && !@config.exceptions.include?(aria_label)
27+
if (aria_label.start_with?(/^[a-z]/) || aria_label.match?(/[\r\n]+/)) && !@config.exceptions.include?(aria_label)
2928
generate_offense(self.class, processed_source, tag)
3029
end
3130
end

test/linters/accessibility/aria_label_is_well_formatted_test.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,30 @@ def test_warns_when_aria_label_starts_with_downcase
2020
assert_equal 5, @linter.offenses.count
2121
end
2222

23+
def test_warns_when_aria_label_contains_newline_characer
24+
@file = <<~HTML
25+
<button aria-label="Go to my&#10;website." ></button>
26+
<button aria-label="Go to my\nwebsite." ></button>
27+
HTML
28+
@linter.run(processed_source)
29+
assert_equal 2, @linter.offenses.count
30+
end
31+
2332
def test_does_not_warn_when_aria_labelledby_starts_with_downcase
2433
@file = "<button aria-labelledby='some-element-1'</button>"
2534
@linter.run(processed_source)
2635

2736
assert_empty @linter.offenses
2837
end
2938

30-
def test_does_not_warn_when_aria_label_starts_with_anything_other_than_downcase
31-
@file = <<~HTML
32-
<button aria-label="Submit" ></button>
33-
<button aria-label="Close" ></button>
34-
<button aria-label="11 open" ></button>
35-
HTML
39+
def test_does_not_warn_with_string_interpolation
40+
@file = '<button aria-label="Add a cat <%= "or dog " if dog_allowed? %>pet"></button>'
3641
@linter.run(processed_source)
3742

3843
assert_empty @linter.offenses
3944
end
4045

41-
def test_does_not_warn_when_aria_label_is_excepted_in_config
46+
def test_does_not_warn_when_aria_label_starts_with_anything_other_than_downcase
4247
@file = <<~HTML
4348
<button aria-label="Submit" ></button>
4449
<button aria-label="Close" ></button>
@@ -49,15 +54,14 @@ def test_does_not_warn_when_aria_label_is_excepted_in_config
4954
assert_empty @linter.offenses
5055
end
5156

52-
5357
def test_does_not_warn_if_aria_label_is_in_excepted_list
5458
@file = <<~HTML
5559
<button aria-label="hello" ></button>
56-
<button aria-label="hello world" ></button>
60+
<button aria-label="git checkout <%= some_branch %>"></button>
5761
HTML
58-
@linter.config.exceptions = ["hello"]
62+
@linter.config.exceptions = ["hello", "git checkout <%= some_branch %>"]
5963
@linter.run(processed_source)
6064

61-
assert_equal 1, @linter.offenses.count
65+
assert_empty @linter.offenses
6266
end
6367
end

0 commit comments

Comments
 (0)