This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
/
reactA11yImageButtonHasAltRule.ts
77 lines (63 loc) · 2.9 KB
/
reactA11yImageButtonHasAltRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Enforce that inputs element with type="image" must have alt attribute.
*/
import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as tsutils from 'tsutils';
import { ExtendedMetadata } from './utils/ExtendedMetadata';
import { getJsxAttributesFromJsxElement, getStringLiteral, isEmpty } from './utils/JsxAttribute';
import { isStringLiteral } from './utils/TypeGuard';
const NO_ALT_ATTRIBUTE_FAILURE_STRING: string = 'Inputs element with type="image" must have alt attribute.';
const EMPTY_ALT_ATTRIBUTE_FAILURE_STRING: string = 'Inputs element with type="image" must have non-empty alt attribute.';
const TYPE_STRING: string = 'type';
const ALT_STRING: string = 'alt';
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: ExtendedMetadata = {
ruleName: 'react-a11y-image-button-has-alt',
type: 'maintainability',
description: 'Enforce that inputs element with type="image" must have alt attribute.',
options: null, // tslint:disable-line:no-null-keyword
optionsDescription: '',
typescriptOnly: true,
issueClass: 'Non-SDL',
issueType: 'Warning',
severity: 'Important',
level: 'Opportunity for Excellence',
group: 'Accessibility'
};
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return sourceFile.languageVariant === ts.LanguageVariant.JSX ? this.applyWithFunction(sourceFile, walk) : [];
}
}
function walk(ctx: Lint.WalkContext<void>) {
function validateOpeningElement(node: ts.JsxOpeningLikeElement): void {
const tagName: string = node.tagName.getText();
if (tagName !== 'input') {
return;
}
const attributes: { [propName: string]: ts.JsxAttribute } = getJsxAttributesFromJsxElement(node);
const typeAttribute: ts.JsxAttribute = attributes[TYPE_STRING];
if (!typeAttribute || typeAttribute.initializer === undefined || !isStringLiteral(typeAttribute.initializer)) {
return;
}
const stringLiteral = getStringLiteral(typeAttribute);
if (stringLiteral === undefined || stringLiteral.toLowerCase() !== 'image') {
return;
}
const altAttribute: ts.JsxAttribute = attributes[ALT_STRING];
if (!altAttribute) {
ctx.addFailureAt(node.getStart(), node.getWidth(), NO_ALT_ATTRIBUTE_FAILURE_STRING);
} else if (isEmpty(altAttribute) || !getStringLiteral(altAttribute)) {
ctx.addFailureAt(node.getStart(), node.getWidth(), EMPTY_ALT_ATTRIBUTE_FAILURE_STRING);
}
}
function cb(node: ts.Node): void {
if (tsutils.isJsxElement(node)) {
validateOpeningElement(node.openingElement);
} else if (tsutils.isJsxSelfClosingElement(node)) {
validateOpeningElement(node);
}
return ts.forEachChild(node, cb);
}
return ts.forEachChild(ctx.sourceFile, cb);
}