diff options
Diffstat (limited to 'node_modules/tslint/lib/rules/preferConditionalExpressionRule.js')
-rw-r--r-- | node_modules/tslint/lib/rules/preferConditionalExpressionRule.js | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/node_modules/tslint/lib/rules/preferConditionalExpressionRule.js b/node_modules/tslint/lib/rules/preferConditionalExpressionRule.js index 7be4cbcbe..b8f8693df 100644 --- a/node_modules/tslint/lib/rules/preferConditionalExpressionRule.js +++ b/node_modules/tslint/lib/rules/preferConditionalExpressionRule.js @@ -20,6 +20,7 @@ var tslib_1 = require("tslib"); var tsutils_1 = require("tsutils"); var ts = require("typescript"); var Lint = require("../index"); +var OPTION_CHECK_ELSE_IF = "check-else-if"; var Rule = (function (_super) { tslib_1.__extends(Rule, _super); function Rule() { @@ -30,29 +31,36 @@ var Rule = (function (_super) { return "Use a conditional expression instead of assigning to '" + assigned + "' in multiple places."; }; Rule.prototype.apply = function (sourceFile) { - return this.applyWithFunction(sourceFile, walk); + return this.applyWithFunction(sourceFile, walk, { + checkElseIf: this.ruleArguments.indexOf(OPTION_CHECK_ELSE_IF) !== -1, + }); + }; + /* tslint:disable:object-literal-sort-keys */ + Rule.metadata = { + ruleName: "prefer-conditional-expression", + description: (_a = ["\n Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement."], _a.raw = ["\n Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement."], Lint.Utils.dedent(_a)), + rationale: (_b = ["\n This reduces duplication and can eliminate an unnecessary variable declaration."], _b.raw = ["\n This reduces duplication and can eliminate an unnecessary variable declaration."], Lint.Utils.dedent(_b)), + optionsDescription: "If `" + OPTION_CHECK_ELSE_IF + "` is specified, the rule also checks nested if-else-if statements.", + options: { + type: "string", + enum: [OPTION_CHECK_ELSE_IF], + }, + optionExamples: [true, [true, OPTION_CHECK_ELSE_IF]], + type: "functionality", + typescriptOnly: false, }; return Rule; }(Lint.Rules.AbstractRule)); -/* tslint:disable:object-literal-sort-keys */ -Rule.metadata = { - ruleName: "prefer-conditional-expression", - description: (_a = ["\n Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement."], _a.raw = ["\n Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement."], Lint.Utils.dedent(_a)), - rationale: (_b = ["\n This reduces duplication and can eliminate an unnecessary variable declaration."], _b.raw = ["\n This reduces duplication and can eliminate an unnecessary variable declaration."], Lint.Utils.dedent(_b)), - optionsDescription: "Not configurable.", - options: null, - optionExamples: [true], - type: "functionality", - typescriptOnly: false, -}; exports.Rule = Rule; function walk(ctx) { - var sourceFile = ctx.sourceFile; + var sourceFile = ctx.sourceFile, checkElseIf = ctx.options.checkElseIf; return ts.forEachChild(sourceFile, function cb(node) { if (tsutils_1.isIfStatement(node)) { - var assigned = detect(node, sourceFile); + var assigned = detect(node, sourceFile, checkElseIf); if (assigned !== undefined) { - ctx.addFailureAtNode(Lint.childOfKind(node, ts.SyntaxKind.IfKeyword), Rule.FAILURE_STRING(assigned.getText(sourceFile))); + ctx.addFailureAtNode(node.getChildAt(0, sourceFile), Rule.FAILURE_STRING(assigned.getText(sourceFile))); + } + if (assigned !== undefined || !checkElseIf) { // Be careful not to fail again for the "else if" ts.forEachChild(node.expression, cb); ts.forEachChild(node.thenStatement, cb); @@ -65,12 +73,12 @@ function walk(ctx) { return ts.forEachChild(node, cb); }); } -function detect(_a, sourceFile) { +function detect(_a, sourceFile, elseIf) { var thenStatement = _a.thenStatement, elseStatement = _a.elseStatement; - if (elseStatement === undefined) { + if (elseStatement === undefined || !elseIf && elseStatement.kind === ts.SyntaxKind.IfStatement) { return undefined; } - var elze = tsutils_1.isIfStatement(elseStatement) ? detect(elseStatement, sourceFile) : getAssigned(elseStatement, sourceFile); + var elze = tsutils_1.isIfStatement(elseStatement) ? detect(elseStatement, sourceFile, elseIf) : getAssigned(elseStatement, sourceFile); if (elze === undefined) { return undefined; } |