UNPKG

3.17 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 }
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16var ts = require("typescript");
17var Lint = require("tslint");
18var tsutils = require("tsutils");
19var JsxAttribute_1 = require("./utils/JsxAttribute");
20var FAILURE_STRING = 'Required input elements must have an aria-required set to true';
21var REQUIRED_STRING = 'required';
22var ARIA_REQUIRED_STRING = 'aria-required';
23var Rule = (function (_super) {
24 __extends(Rule, _super);
25 function Rule() {
26 return _super !== null && _super.apply(this, arguments) || this;
27 }
28 Rule.prototype.apply = function (sourceFile) {
29 return sourceFile.languageVariant === ts.LanguageVariant.JSX ? this.applyWithFunction(sourceFile, walk) : [];
30 };
31 Rule.metadata = {
32 ruleName: 'react-a11y-required',
33 type: 'functionality',
34 description: 'Enforce that required input elements must have aria-required set to true',
35 rationale: "References:\n <ul>\n <li><a href=\"http://www.clarissapeterson.com/2012/11/html5-accessibility/\">Acessibility in HTML5</a></li>\n </ul>",
36 options: null,
37 optionsDescription: '',
38 typescriptOnly: true,
39 issueClass: 'Non-SDL',
40 issueType: 'Warning',
41 severity: 'Low',
42 level: 'Opportunity for Excellence',
43 group: 'Accessibility'
44 };
45 return Rule;
46}(Lint.Rules.AbstractRule));
47exports.Rule = Rule;
48function walk(ctx) {
49 function validateOpeningElement(node) {
50 var tagName = node.tagName.getText();
51 if (tagName !== 'input') {
52 return;
53 }
54 var attributes = JsxAttribute_1.getJsxAttributesFromJsxElement(node);
55 var requiredAttribute = attributes[REQUIRED_STRING];
56 if (!requiredAttribute) {
57 return;
58 }
59 var ariaRequiredAttribute = attributes[ARIA_REQUIRED_STRING];
60 if (!ariaRequiredAttribute || JsxAttribute_1.isEmpty(ariaRequiredAttribute) || !JsxAttribute_1.getBooleanLiteral(ariaRequiredAttribute)) {
61 ctx.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING);
62 }
63 }
64 function cb(node) {
65 if (tsutils.isJsxElement(node)) {
66 validateOpeningElement(node.openingElement);
67 }
68 else if (tsutils.isJsxSelfClosingElement(node)) {
69 validateOpeningElement(node);
70 }
71 return ts.forEachChild(node, cb);
72 }
73 return ts.forEachChild(ctx.sourceFile, cb);
74}
75//# sourceMappingURL=reactA11yRequiredRule.js.map
\No newline at end of file