Skip to content

Commit 2c73c77

Browse files
authored
Linter: Improve html-img-require-alt rule (#1311)
This pull request improves the `html-img-require-alt` linter rule with the insights and doc updates from `erblint-github` via issue #1220.
1 parent 3e5553a commit 2c73c77

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

javascript/packages/linter/docs/rules/html-img-require-alt.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ Omitting the `alt` attribute entirely leads to poor accessibility and can negati
3131
```erb
3232
<img src="/logo.png">
3333
34-
<img src="/avatar.jpg" alt> <!-- TODO -->
34+
<img src="/avatar.jpg" alt>
3535
3636
<%= image_tag image_path("logo.png") %> <!-- TODO -->
3737
```
3838

3939
## References
4040

41-
* [W3C: Alternative Text](https://www.w3.org/WAI/tutorials/images/)
42-
* [WCAG 2.1: Non-text Content](https://www.w3.org/WAI/WCAG22/quickref/?versions=2.1#non-text-content)
41+
- [W3C WAI Images Tutorial](https://www.w3.org/WAI/tutorials/images/)
42+
- [WCAG 2.1: Non-text Content](https://www.w3.org/WAI/WCAG22/quickref/?versions=2.1#non-text-content)
43+
- [Primer: Alternative text for images](https://primer.style/accessibility/design-guidance/alternative-text-for-images/)
44+
- [erblint-github: `GitHub::Accessibility::ImageHasAlt`](https://github.com/github/erblint-github/blob/main/docs/rules/accessibility/image-has-alt.md)

javascript/packages/linter/src/rules/html-img-require-alt.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BaseRuleVisitor } from "./rule-utils.js"
2-
import { hasAttribute, getTagLocalName } from "@herb-tools/core"
2+
import { hasAttribute, getAttribute, hasAttributeValue, getTagLocalName } from "@herb-tools/core"
33

44
import { ParserRule } from "../types.js"
55
import type { UnboundLintOffense, LintContext, FullRuleConfig } from "../types.js"
@@ -23,6 +23,16 @@ class ImgRequireAltVisitor extends BaseRuleVisitor {
2323
'Missing required `alt` attribute on `<img>` tag. Add `alt=""` for decorative images or `alt="description"` for informative images.',
2424
node.tag_name!.location
2525
)
26+
return
27+
}
28+
29+
const altAttribute = getAttribute(node, "alt")
30+
31+
if (altAttribute && !hasAttributeValue(altAttribute)) {
32+
this.addOffense(
33+
'The `alt` attribute has no value. Add `alt=""` for decorative images or `alt="description"` for informative images.',
34+
altAttribute.location
35+
)
2636
}
2737
}
2838
}

javascript/packages/linter/test/rules/html-img-require-alt.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ describe("html-img-require-alt", () => {
4545
test("passes for case-insensitive alt attribute", () => {
4646
expectNoOffenses('<img src="/logo.png" ALT="Logo">')
4747
})
48+
49+
test("fails for img with alt attribute without value", () => {
50+
expectError('The `alt` attribute has no value. Add `alt=""` for decorative images or `alt="description"` for informative images.')
51+
assertOffenses('<img src="/avatar.jpg" alt>')
52+
})
4853
})

0 commit comments

Comments
 (0)