4 * Copyright 2015 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var Rule = /** @class */ (function (_super) {
24 tslib_1.__extends(Rule, _super);
26 return _super !== null && _super.apply(this, arguments) || this;
28 Rule.prototype.apply = function (sourceFile) {
29 return this.applyWithFunction(sourceFile, walk);
31 /* tslint:disable:object-literal-sort-keys */
33 ruleName: "no-conditional-assignment",
34 description: "Disallows any type of assignment in conditionals.",
35 descriptionDetails: "This applies to `do-while`, `for`, `if`, and `while` statements and conditional (ternary) expressions.",
36 rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Assignments in conditionals are often typos:\n for example `if (var1 = var2)` instead of `if (var1 == var2)`.\n They also can be an indicator of overly clever code which decreases maintainability."], ["\n Assignments in conditionals are often typos:\n for example \\`if (var1 = var2)\\` instead of \\`if (var1 == var2)\\`.\n They also can be an indicator of overly clever code which decreases maintainability."]))),
37 optionsDescription: "Not configurable.",
39 optionExamples: [true],
40 type: "functionality",
41 typescriptOnly: false,
43 /* tslint:enable:object-literal-sort-keys */
44 Rule.FAILURE_STRING = "Assignments in conditional expressions are forbidden";
46 }(Lint.Rules.AbstractRule));
50 return ts.forEachChild(ctx.sourceFile, cb);
53 if (!tsutils_1.isNodeKind(kind)) {
54 return; // return early for tokens
57 case ts.SyntaxKind.ConditionalExpression:
58 check(node.condition);
62 case ts.SyntaxKind.IfStatement:
63 check(node.expression);
64 cb(node.thenStatement);
65 maybeCallback(cb, node.elseStatement);
67 case ts.SyntaxKind.DoStatement:
68 case ts.SyntaxKind.WhileStatement:
69 check(node.expression);
72 case ts.SyntaxKind.ForStatement:
73 maybeCallback(cb, node.initializer);
74 maybeCallback(check, node.condition);
75 maybeCallback(cb, node.incrementor);
81 case ts.SyntaxKind.BinaryExpression:
82 if (tsutils_1.isAssignmentKind(node.operatorToken.kind)) {
83 ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
88 case ts.SyntaxKind.ParenthesizedExpression:
89 case ts.SyntaxKind.NonNullExpression:
90 case ts.SyntaxKind.AsExpression:
91 case ts.SyntaxKind.TypeAssertionExpression:
92 return cb(node.expression);
93 case ts.SyntaxKind.PrefixUnaryExpression:
94 return cb(node.operand);
99 return ts.forEachChild(node, cb);
101 function check(node) {
106 function noCheck(node) {
109 ts.forEachChild(node, cb);
113 function maybeCallback(cb, node) {
114 if (node !== undefined) {
118 var templateObject_1;