UNPKG

3.46 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 DOM_SCHEMA = require('./utils/attributes/domSchema.json');
21var ARIA_SCHEMA = require('./utils/attributes/ariaSchema.json');
22function getFailureString(tagName, ariaAttributeNames) {
23 return ("This element " + tagName + " does not support ARIA roles, states and properties. " +
24 ("Try removing attribute(s): " + ariaAttributeNames.join(', ') + "."));
25}
26exports.getFailureString = getFailureString;
27var Rule = (function (_super) {
28 __extends(Rule, _super);
29 function Rule() {
30 return _super !== null && _super.apply(this, arguments) || this;
31 }
32 Rule.prototype.apply = function (sourceFile) {
33 return sourceFile.languageVariant === ts.LanguageVariant.JSX ? this.applyWithFunction(sourceFile, walk) : [];
34 };
35 Rule.metadata = {
36 ruleName: 'react-a11y-aria-unsupported-elements',
37 type: 'maintainability',
38 description: 'Enforce that elements that do not support ARIA roles, states, and properties do not have those attributes.',
39 options: null,
40 optionsDescription: '',
41 typescriptOnly: true,
42 issueClass: 'Non-SDL',
43 issueType: 'Warning',
44 severity: 'Important',
45 level: 'Opportunity for Excellence',
46 group: 'Accessibility'
47 };
48 return Rule;
49}(Lint.Rules.AbstractRule));
50exports.Rule = Rule;
51function walk(ctx) {
52 function validateOpeningElement(node) {
53 var tagName = node.tagName.getText();
54 if (!DOM_SCHEMA[tagName]) {
55 return;
56 }
57 var supportAria = DOM_SCHEMA[tagName].supportAria !== undefined ? DOM_SCHEMA[tagName].supportAria : false;
58 if (supportAria) {
59 return;
60 }
61 var checkAttributeNames = Object.keys(ARIA_SCHEMA).concat('role');
62 var attributes = JsxAttribute_1.getJsxAttributesFromJsxElement(node);
63 var invalidAttributeNames = checkAttributeNames.filter(function (attributeName) { return !!attributes[attributeName]; });
64 if (invalidAttributeNames.length > 0) {
65 var message = getFailureString(tagName, invalidAttributeNames);
66 ctx.addFailureAt(node.getStart(), node.getWidth(), message);
67 }
68 }
69 function cb(node) {
70 if (tsutils.isJsxElement(node)) {
71 validateOpeningElement(node.openingElement);
72 }
73 else if (tsutils.isJsxSelfClosingElement(node)) {
74 validateOpeningElement(node);
75 }
76 return ts.forEachChild(node, cb);
77 }
78 return ts.forEachChild(ctx.sourceFile, cb);
79}
80//# sourceMappingURL=reactA11yAriaUnsupportedElementsRule.js.map
\No newline at end of file