UNPKG

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