1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return function (d, b) {
|
7 | extendStatics(d, b);
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var ts = require("typescript");
|
14 | var Lint = require("tslint");
|
15 | var JsxAttribute_1 = require("./utils/JsxAttribute");
|
16 | var TypeGuard_1 = require("./utils/TypeGuard");
|
17 | var NO_ALT_ATTRIBUTE_FAILURE_STRING = 'Inputs element with type="image" must have alt attribute.';
|
18 | var EMPTY_ALT_ATTRIBUTE_FAILURE_STRING = 'Inputs element with type="image" must have non-empty alt attribute.';
|
19 | var TYPE_STRING = 'type';
|
20 | var ALT_STRING = 'alt';
|
21 | var Rule = (function (_super) {
|
22 | __extends(Rule, _super);
|
23 | function Rule() {
|
24 | return _super !== null && _super.apply(this, arguments) || this;
|
25 | }
|
26 | Rule.prototype.apply = function (sourceFile) {
|
27 | return sourceFile.languageVariant === ts.LanguageVariant.JSX
|
28 | ? this.applyWithWalker(new ReactA11yImageButtonHasAltWalker(sourceFile, this.getOptions()))
|
29 | : [];
|
30 | };
|
31 | Rule.metadata = {
|
32 | ruleName: 'react-a11y-image-button-has-alt',
|
33 | type: 'maintainability',
|
34 | description: 'Enforce that inputs element with type="image" must have alt attribute.',
|
35 | options: null,
|
36 | optionsDescription: '',
|
37 | typescriptOnly: true,
|
38 | issueClass: 'Non-SDL',
|
39 | issueType: 'Warning',
|
40 | severity: 'Important',
|
41 | level: 'Opportunity for Excellence',
|
42 | group: 'Accessibility'
|
43 | };
|
44 | return Rule;
|
45 | }(Lint.Rules.AbstractRule));
|
46 | exports.Rule = Rule;
|
47 | var ReactA11yImageButtonHasAltWalker = (function (_super) {
|
48 | __extends(ReactA11yImageButtonHasAltWalker, _super);
|
49 | function ReactA11yImageButtonHasAltWalker() {
|
50 | return _super !== null && _super.apply(this, arguments) || this;
|
51 | }
|
52 | ReactA11yImageButtonHasAltWalker.prototype.visitJsxElement = function (node) {
|
53 | this.validateOpeningElement(node.openingElement);
|
54 | _super.prototype.visitJsxElement.call(this, node);
|
55 | };
|
56 | ReactA11yImageButtonHasAltWalker.prototype.visitJsxSelfClosingElement = function (node) {
|
57 | this.validateOpeningElement(node);
|
58 | _super.prototype.visitJsxSelfClosingElement.call(this, node);
|
59 | };
|
60 | ReactA11yImageButtonHasAltWalker.prototype.validateOpeningElement = function (node) {
|
61 | var tagName = node.tagName.getText();
|
62 | if (tagName !== 'input') {
|
63 | return;
|
64 | }
|
65 | var attributes = JsxAttribute_1.getJsxAttributesFromJsxElement(node);
|
66 | var typeAttribute = attributes[TYPE_STRING];
|
67 | if (!typeAttribute || !TypeGuard_1.isStringLiteral(typeAttribute.initializer) || JsxAttribute_1.getStringLiteral(typeAttribute).toLowerCase() !== 'image') {
|
68 | return;
|
69 | }
|
70 | var altAttribute = attributes[ALT_STRING];
|
71 | if (!altAttribute) {
|
72 | this.addFailureAt(node.getStart(), node.getWidth(), NO_ALT_ATTRIBUTE_FAILURE_STRING);
|
73 | }
|
74 | else if (JsxAttribute_1.isEmpty(altAttribute) || !JsxAttribute_1.getStringLiteral(altAttribute)) {
|
75 | this.addFailureAt(node.getStart(), node.getWidth(), EMPTY_ALT_ATTRIBUTE_FAILURE_STRING);
|
76 | }
|
77 | };
|
78 | return ReactA11yImageButtonHasAltWalker;
|
79 | }(Lint.RuleWalker));
|
80 |
|
\ | No newline at end of file |