UNPKG

6.78 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 BAD_ORDER_HEADING_FAILURE_STRING = "Heading elements shouldn't increase by more then one level consecutively";
20var EMPTY_HEADING_FAILURE_STRING = 'Heading elements must not be empty';
21var BAD_HEADING_LENGTH_STRING = 'Heading content should be concise';
22var BAD_NUMBER_H1_HEADING_FAILURE_STRING = 'H1 heading cannot exceed 2 elements';
23var VALID_HEADING_TYPES = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']);
24var MAX_NUMBER_OF_H1_HEADINGS = 2;
25var MAX_HEADING_LENGTH_DEFAULT = 60;
26var MAX_HEADING_LENGTH_ATTRIBUTE_NAME = 'maxHeadingLength';
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 if (sourceFile.languageVariant === ts.LanguageVariant.JSX) {
34 return this.applyWithFunction(sourceFile, walk, this.parseOptions(this.getOptions()));
35 }
36 else {
37 return [];
38 }
39 };
40 Rule.prototype.parseOptions = function (options) {
41 var parsed = {};
42 var option = options.ruleArguments.find(function (a) { return a.hasOwnProperty(MAX_HEADING_LENGTH_ATTRIBUTE_NAME); });
43 if (option && typeof option[MAX_HEADING_LENGTH_ATTRIBUTE_NAME] === 'number') {
44 parsed.maxHeadingTextLength = option[MAX_HEADING_LENGTH_ATTRIBUTE_NAME];
45 }
46 return parsed;
47 };
48 Rule.metadata = {
49 ruleName: 'react-a11y-accessible-headings',
50 type: 'functionality',
51 description: "For accessibility of your website, there should be no more than 2 H1 heading elements, HTML heading elements must be concise, shouldn't increase by more then one level consecutively and non-empty.",
52 options: {
53 maxHeadingLength: 'number'
54 },
55 optionsDescription: 'An optional number for a maximum text length of heading elements.',
56 typescriptOnly: true,
57 issueClass: 'Non-SDL',
58 issueType: 'Warning',
59 severity: 'Moderate',
60 level: 'Opportunity for Excellence',
61 group: 'Accessibility'
62 };
63 return Rule;
64}(Lint.Rules.AbstractRule));
65exports.Rule = Rule;
66function walk(ctx) {
67 function validate(node) {
68 var elements = [];
69 var h1HeadingCounter = 0;
70 var previousHeadingNumber;
71 tsutils.forEachToken(node, function (childNode) {
72 var parentNode = childNode.parent;
73 if (tsutils.isJsxOpeningElement(parentNode) && VALID_HEADING_TYPES.has(childNode.getText())) {
74 elements.push(parentNode.parent);
75 }
76 });
77 elements.forEach(function (element) {
78 var openingElement = element.openingElement;
79 var headingNumber = parseInt(openingElement.tagName.getText()[1], 10);
80 if (!previousHeadingNumber) {
81 previousHeadingNumber = headingNumber;
82 }
83 else if (headingNumber > previousHeadingNumber && previousHeadingNumber + 1 !== headingNumber) {
84 ctx.addFailureAt(openingElement.getStart(), openingElement.getWidth(), BAD_ORDER_HEADING_FAILURE_STRING);
85 }
86 else {
87 previousHeadingNumber = headingNumber;
88 }
89 if (openingElement.tagName.getText().toLowerCase() === 'h1') {
90 h1HeadingCounter += 1;
91 if (h1HeadingCounter > MAX_NUMBER_OF_H1_HEADINGS) {
92 ctx.addFailureAt(node.getStart(), node.getWidth(), BAD_NUMBER_H1_HEADING_FAILURE_STRING);
93 }
94 }
95 validateHeadingText(element);
96 });
97 }
98 function validateHeadingText(headingNode) {
99 if (headingNode.children.length === 0) {
100 ctx.addFailureAt(headingNode.getStart(), headingNode.getWidth(), EMPTY_HEADING_FAILURE_STRING);
101 }
102 else {
103 var textResults = [];
104 getTextRecursive(headingNode, textResults);
105 if (textResults.length) {
106 var maxHeadingLength = ctx.options.maxHeadingTextLength ? ctx.options.maxHeadingTextLength : MAX_HEADING_LENGTH_DEFAULT;
107 if (textResults.join('').length > maxHeadingLength) {
108 ctx.addFailureAt(headingNode.getStart(), headingNode.getWidth(), BAD_HEADING_LENGTH_STRING);
109 }
110 }
111 }
112 }
113 function getTextRecursive(node, textResults) {
114 if (textResults === void 0) { textResults = []; }
115 if (!node) {
116 return;
117 }
118 if (tsutils.isJsxElement(node)) {
119 for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
120 var childNode = _a[_i];
121 var textResult = void 0;
122 if (tsutils.isJsxExpression(childNode)) {
123 textResult = extractFromExpression(childNode);
124 }
125 else if (tsutils.isJsxText(childNode)) {
126 textResult = childNode.getText();
127 }
128 if (textResult) {
129 textResults.push(textResult);
130 }
131 getTextRecursive(childNode, textResults);
132 }
133 }
134 }
135 function extractFromExpression(expressionNode) {
136 if (!expressionNode.expression || !tsutils.isStringLiteral(expressionNode.expression)) {
137 return undefined;
138 }
139 return expressionNode.expression.text;
140 }
141 function cb(node) {
142 if (tsutils.isFunctionDeclaration(node)) {
143 validate(node);
144 }
145 else if (tsutils.isMethodDeclaration(node)) {
146 validate(node);
147 }
148 else if (tsutils.isVariableDeclaration(node)) {
149 validate(node);
150 }
151 return ts.forEachChild(node, cb);
152 }
153 return ts.forEachChild(ctx.sourceFile, cb);
154}
155//# sourceMappingURL=reactA11yAccessibleHeadingsRule.js.map
\No newline at end of file