4 * Copyright 2017 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 OPTION_CHECK_ELSE_IF = "check-else-if";
24 var Rule = /** @class */ (function (_super) {
25 tslib_1.__extends(Rule, _super);
27 return _super !== null && _super.apply(this, arguments) || this;
29 /* tslint:enable:object-literal-sort-keys */
30 Rule.FAILURE_STRING = function (assigned) {
31 return "Use a conditional expression instead of assigning to '" + assigned + "' in multiple places.";
33 Rule.prototype.apply = function (sourceFile) {
34 return this.applyWithFunction(sourceFile, walk, {
35 checkElseIf: this.ruleArguments.indexOf(OPTION_CHECK_ELSE_IF) !== -1,
38 /* tslint:disable:object-literal-sort-keys */
40 ruleName: "prefer-conditional-expression",
41 description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement."], ["\n Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement."]))),
42 rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n This reduces duplication and can eliminate an unnecessary variable declaration."], ["\n This reduces duplication and can eliminate an unnecessary variable declaration."]))),
43 optionsDescription: "If `" + OPTION_CHECK_ELSE_IF + "` is specified, the rule also checks nested if-else-if statements.",
46 enum: [OPTION_CHECK_ELSE_IF],
48 optionExamples: [true, [true, OPTION_CHECK_ELSE_IF]],
49 type: "functionality",
50 typescriptOnly: false,
53 }(Lint.Rules.AbstractRule));
56 var sourceFile = ctx.sourceFile, checkElseIf = ctx.options.checkElseIf;
57 return ts.forEachChild(sourceFile, function cb(node) {
58 if (tsutils_1.isIfStatement(node)) {
59 var assigned = detectAssignment(node, sourceFile, checkElseIf);
60 if (assigned !== undefined) {
61 ctx.addFailureAtNode(node.getChildAt(0, sourceFile), Rule.FAILURE_STRING(assigned.getText(sourceFile)));
63 if (assigned !== undefined || !checkElseIf) {
64 // Be careful not to fail again for the "else if"
66 ts.forEachChild(node.expression, cb);
67 ts.forEachChild(node.thenStatement, cb);
68 if (node.elseStatement === undefined) {
71 node = node.elseStatement;
72 while (tsutils_1.isBlock(node) && node.statements.length === 1) {
73 node = node.statements[0];
75 } while (tsutils_1.isIfStatement(node));
78 return ts.forEachChild(node, cb);
82 * @param inElse `undefined` when this is the top level if statement, `false` when inside the then branch, `true` when inside else
84 function detectAssignment(statement, sourceFile, checkElseIf, inElse) {
85 if (tsutils_1.isIfStatement(statement)) {
86 if (inElse === false || (!checkElseIf && inElse) || statement.elseStatement === undefined) {
89 var then = detectAssignment(statement.thenStatement, sourceFile, checkElseIf, false);
90 if (then === undefined) {
93 var elze = detectAssignment(statement.elseStatement, sourceFile, checkElseIf, true);
94 return elze !== undefined && nodeEquals(then, elze, sourceFile) ? then : undefined;
96 else if (tsutils_1.isBlock(statement)) {
97 return statement.statements.length === 1
98 ? detectAssignment(statement.statements[0], sourceFile, checkElseIf, inElse)
101 else if (tsutils_1.isExpressionStatement(statement) && tsutils_1.isBinaryExpression(statement.expression)) {
102 var _a = statement.expression, kind = _a.operatorToken.kind, left = _a.left, right = _a.right;
103 return kind === ts.SyntaxKind.EqualsToken &&
104 tsutils_1.isSameLine(sourceFile, right.getStart(sourceFile), right.end)
112 function nodeEquals(a, b, sourceFile) {
113 return a.getText(sourceFile) === b.getText(sourceFile);
115 var templateObject_1, templateObject_2;