aboutsummaryrefslogtreecommitdiff
path: root/node_modules/tslint/lib/rules/preferConditionalExpressionRule.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
committerFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
commitbbff7403fbf46f9ad92240ac213df8d30ef31b64 (patch)
treec58400ec5124da1c7d56b01aea83309f80a56c3b /node_modules/tslint/lib/rules/preferConditionalExpressionRule.js
parent003fb34971cf63466184351b4db5f7c67df4f444 (diff)
update packages
Diffstat (limited to 'node_modules/tslint/lib/rules/preferConditionalExpressionRule.js')
-rw-r--r--node_modules/tslint/lib/rules/preferConditionalExpressionRule.js62
1 files changed, 35 insertions, 27 deletions
diff --git a/node_modules/tslint/lib/rules/preferConditionalExpressionRule.js b/node_modules/tslint/lib/rules/preferConditionalExpressionRule.js
index a6f7f173c..cd7a5ea33 100644
--- a/node_modules/tslint/lib/rules/preferConditionalExpressionRule.js
+++ b/node_modules/tslint/lib/rules/preferConditionalExpressionRule.js
@@ -38,8 +38,8 @@ var Rule = /** @class */ (function (_super) {
/* 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)),
+ 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."]))),
+ 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."]))),
optionsDescription: "If `" + OPTION_CHECK_ELSE_IF + "` is specified, the rule also checks nested if-else-if statements.",
options: {
type: "string",
@@ -56,42 +56,50 @@ function walk(ctx) {
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, checkElseIf);
+ var assigned = detectAssignment(node, sourceFile, checkElseIf);
if (assigned !== undefined) {
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);
- if (node.elseStatement !== undefined) {
- ts.forEachChild(node.elseStatement, cb);
- }
- return;
+ do {
+ ts.forEachChild(node.expression, cb);
+ ts.forEachChild(node.thenStatement, cb);
+ if (node.elseStatement === undefined) {
+ return;
+ }
+ node = node.elseStatement;
+ while (tsutils_1.isBlock(node) && node.statements.length === 1) {
+ node = node.statements[0];
+ }
+ } while (tsutils_1.isIfStatement(node));
}
}
return ts.forEachChild(node, cb);
});
}
-function detect(_a, sourceFile, elseIf) {
- var thenStatement = _a.thenStatement, elseStatement = _a.elseStatement;
- if (elseStatement === undefined || !elseIf && elseStatement.kind === ts.SyntaxKind.IfStatement) {
- return undefined;
- }
- var elze = tsutils_1.isIfStatement(elseStatement) ? detect(elseStatement, sourceFile, elseIf) : getAssigned(elseStatement, sourceFile);
- if (elze === undefined) {
- return undefined;
+/**
+ * @param inElse `undefined` when this is the top level if statement, `false` when inside the then branch, `true` when inside else
+ */
+function detectAssignment(statement, sourceFile, checkElseIf, inElse) {
+ if (tsutils_1.isIfStatement(statement)) {
+ if (inElse === false || !checkElseIf && inElse || statement.elseStatement === undefined) {
+ return undefined;
+ }
+ var then = detectAssignment(statement.thenStatement, sourceFile, checkElseIf, false);
+ if (then === undefined) {
+ return undefined;
+ }
+ var elze = detectAssignment(statement.elseStatement, sourceFile, checkElseIf, true);
+ return elze !== undefined && nodeEquals(then, elze, sourceFile) ? then : undefined;
}
- var then = getAssigned(thenStatement, sourceFile);
- return then !== undefined && nodeEquals(elze, then, sourceFile) ? then : undefined;
-}
-/** Returns the left side of an assignment. */
-function getAssigned(node, sourceFile) {
- if (tsutils_1.isBlock(node)) {
- return node.statements.length === 1 ? getAssigned(node.statements[0], sourceFile) : undefined;
+ else if (tsutils_1.isBlock(statement)) {
+ return statement.statements.length === 1
+ ? detectAssignment(statement.statements[0], sourceFile, checkElseIf, inElse)
+ : undefined;
}
- else if (tsutils_1.isExpressionStatement(node) && tsutils_1.isBinaryExpression(node.expression)) {
- var _a = node.expression, kind = _a.operatorToken.kind, left = _a.left, right = _a.right;
+ else if (tsutils_1.isExpressionStatement(statement) && tsutils_1.isBinaryExpression(statement.expression)) {
+ var _a = statement.expression, kind = _a.operatorToken.kind, left = _a.left, right = _a.right;
return kind === ts.SyntaxKind.EqualsToken && tsutils_1.isSameLine(sourceFile, right.getStart(sourceFile), right.end) ? left : undefined;
}
else {
@@ -101,4 +109,4 @@ function getAssigned(node, sourceFile) {
function nodeEquals(a, b, sourceFile) {
return a.getText(sourceFile) === b.getText(sourceFile);
}
-var _a, _b;
+var templateObject_1, templateObject_2;