UNPKG

4.64 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 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 ? this.applyWithFunction(sourceFile, walk) : [];
27 };
28 Rule.metadata = {
29 ruleName: 'use-simple-attributes',
30 type: 'functionality',
31 description: 'Enforce usage of only simple attribute types.',
32 rationale: 'Simpler attributes in JSX and TSX files helps keep code clean and readable.\
33 Separate complex expressions into their own line and use clear variable names to make your code more understandable.',
34 options: null,
35 optionsDescription: '',
36 typescriptOnly: false,
37 issueClass: 'Non-SDL',
38 issueType: 'Error',
39 severity: 'Important',
40 level: 'Opportunity for Excellence',
41 group: 'Correctness'
42 };
43 return Rule;
44}(Lint.Rules.AbstractRule));
45exports.Rule = Rule;
46function walk(ctx) {
47 function checkJsxOpeningElement(node) {
48 var attributes = JsxAttribute_1.getJsxAttributesFromJsxElement(node);
49 for (var _i = 0, _a = Object.keys(attributes); _i < _a.length; _i++) {
50 var key = _a[_i];
51 var attribute = attributes[key];
52 var binaryExpression = getNextNodeRecursive(attribute, ts.SyntaxKind.BinaryExpression);
53 if (binaryExpression && !isSimpleBinaryExpression(binaryExpression)) {
54 var binaryExpressionErrorMessage = 'Attribute contains a complex binary expression';
55 ctx.addFailureAt(node.getStart(), node.getWidth(), binaryExpressionErrorMessage);
56 }
57 var ternaryExpression = getNextNodeRecursive(attribute, ts.SyntaxKind.ConditionalExpression);
58 if (ternaryExpression) {
59 var ternaryExpressionErrorMessage = 'Attribute contains a ternary expression';
60 ctx.addFailureAt(node.getStart(), node.getWidth(), ternaryExpressionErrorMessage);
61 }
62 }
63 }
64 function getNextNodeRecursive(node, kind) {
65 if (!node) {
66 return undefined;
67 }
68 var childNodes = node.getChildren();
69 var match = childNodes.find(function (cn) { return cn.kind === kind; });
70 if (!match) {
71 for (var _i = 0, childNodes_1 = childNodes; _i < childNodes_1.length; _i++) {
72 var childNode = childNodes_1[_i];
73 match = getNextNodeRecursive(childNode, kind);
74 }
75 }
76 return match;
77 }
78 function isSimpleBinaryExpression(binaryExpression) {
79 if (binaryExpression.kind !== ts.SyntaxKind.BinaryExpression) {
80 return false;
81 }
82 var allowedBinaryNodes = [
83 ts.SyntaxKind.NumericLiteral,
84 ts.SyntaxKind.StringLiteral,
85 ts.SyntaxKind.TrueKeyword,
86 ts.SyntaxKind.FalseKeyword,
87 ts.SyntaxKind.Identifier
88 ];
89 var leftTerm = allowedBinaryNodes.find(function (kind) { return kind === binaryExpression.left.kind; });
90 var rightTerm = allowedBinaryNodes.find(function (kind) { return kind === binaryExpression.right.kind; });
91 return leftTerm ? (rightTerm ? true : false) : false;
92 }
93 function cb(node) {
94 if (tsutils.isJsxSelfClosingElement(node)) {
95 checkJsxOpeningElement(node);
96 }
97 else if (tsutils.isJsxElement(node)) {
98 checkJsxOpeningElement(node.openingElement);
99 }
100 return ts.forEachChild(node, cb);
101 }
102 return ts.forEachChild(ctx.sourceFile, cb);
103}
104//# sourceMappingURL=useSimpleAttributesRule.js.map
\No newline at end of file