1 | "use strict";
|
2 | var __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 | })();
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | var ts = require("typescript");
|
17 | var Lint = require("tslint");
|
18 | var tsutils = require("tsutils");
|
19 | var JsxAttribute_1 = require("./utils/JsxAttribute");
|
20 | exports.MISSING_PLACEHOLDER_INPUT_FAILURE_STRING = 'Input elements must include default, place-holding characters if empty';
|
21 | exports.MISSING_PLACEHOLDER_TEXTAREA_FAILURE_STRING = 'Textarea elements must include default, place-holding characters if empty';
|
22 | var EXCLUDED_INPUT_TYPES = ['checkbox', 'radio', 'file'];
|
23 | var 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 | if (sourceFile.languageVariant === ts.LanguageVariant.JSX) {
|
30 | return this.applyWithFunction(sourceFile, walk);
|
31 | }
|
32 | else {
|
33 | return [];
|
34 | }
|
35 | };
|
36 | Rule.metadata = {
|
37 | ruleName: 'react-a11y-input-elements',
|
38 | type: 'functionality',
|
39 | description: 'For accessibility of your website, HTML input boxes and text areas must include default, place-holding characters.',
|
40 | rationale: "References:\n <ul>\n <li><a href=\"https://www.w3.org/TR/WAI-WEBCONTENT-TECHS/#tech-place-holders\">\n WCAG 10.4\n </a></li>\n <li><a href=\"https://www.w3.org/TR/WCAG10-HTML-TECHS/#forms-specific\">\n WCAG 11.5\n </a></li>\n </ul>",
|
41 | options: undefined,
|
42 | optionsDescription: '',
|
43 | typescriptOnly: true,
|
44 | issueClass: 'Non-SDL',
|
45 | issueType: 'Warning',
|
46 | severity: 'Moderate',
|
47 | level: 'Opportunity for Excellence',
|
48 | group: 'Accessibility'
|
49 | };
|
50 | return Rule;
|
51 | }(Lint.Rules.AbstractRule));
|
52 | exports.Rule = Rule;
|
53 | function isExcludedInputType(node, attributes) {
|
54 | for (var _i = 0, _a = node.attributes.properties; _i < _a.length; _i++) {
|
55 | var attribute = _a[_i];
|
56 | if (tsutils.isJsxAttribute(attribute)) {
|
57 | var isInputAttributeType = attributes.type;
|
58 | if (attribute.initializer !== undefined && tsutils.isStringLiteral(attribute.initializer)) {
|
59 | var attributeText = attribute.initializer.text;
|
60 | if (isInputAttributeType !== undefined && EXCLUDED_INPUT_TYPES.indexOf(attributeText) !== -1) {
|
61 | return true;
|
62 | }
|
63 | }
|
64 | }
|
65 | }
|
66 | return false;
|
67 | }
|
68 | function walk(ctx) {
|
69 | function cb(node) {
|
70 | if (tsutils.isJsxSelfClosingElement(node)) {
|
71 | var tagName = node.tagName.getText();
|
72 | if (tagName === 'input') {
|
73 | var attributes = JsxAttribute_1.getJsxAttributesFromJsxElement(node);
|
74 | var isExcludedInput = isExcludedInputType(node, attributes);
|
75 | var isExcludedInputTypeValueEmpty = JsxAttribute_1.isEmpty(attributes.value) && isExcludedInput;
|
76 | var isPlaceholderEmpty = JsxAttribute_1.isEmpty(attributes.placeholder) && !isExcludedInput;
|
77 | if ((JsxAttribute_1.isEmpty(attributes.value) && isPlaceholderEmpty) || isExcludedInputTypeValueEmpty) {
|
78 | ctx.addFailureAt(node.getStart(), node.getWidth(), exports.MISSING_PLACEHOLDER_INPUT_FAILURE_STRING);
|
79 | }
|
80 | }
|
81 | else if (tagName === 'textarea') {
|
82 | var attributes = JsxAttribute_1.getJsxAttributesFromJsxElement(node);
|
83 | if (JsxAttribute_1.isEmpty(attributes.placeholder)) {
|
84 | ctx.addFailureAt(node.getStart(), node.getWidth(), exports.MISSING_PLACEHOLDER_TEXTAREA_FAILURE_STRING);
|
85 | }
|
86 | }
|
87 | }
|
88 | else if (tsutils.isJsxElement(node)) {
|
89 | var tagName = node.openingElement.tagName.getText();
|
90 | var attributes = JsxAttribute_1.getJsxAttributesFromJsxElement(node);
|
91 | if (tagName === 'textarea') {
|
92 | if (node.children.length === 0 && JsxAttribute_1.isEmpty(attributes.placeholder)) {
|
93 | ctx.addFailureAt(node.getStart(), node.getWidth(), exports.MISSING_PLACEHOLDER_TEXTAREA_FAILURE_STRING);
|
94 | }
|
95 | }
|
96 | }
|
97 | return ts.forEachChild(node, cb);
|
98 | }
|
99 | return ts.forEachChild(ctx.sourceFile, cb);
|
100 | }
|
101 |
|
\ | No newline at end of file |