Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return ts.forEachChild(sourceFile, function cb(node) {
if (utils.isIfStatement(node) && check(node, sourceFile, minCases)) {
var expression = node.expression, thenStatement = node.thenStatement, elseStatement = node.elseStatement;
ctx.addFailureAtNode(expression, Rule.FAILURE_STRING);
// Be careful not to fail again for the "else if"
ts.forEachChild(expression, cb);
ts.forEachChild(thenStatement, cb);
if (elseStatement !== undefined) {
ts.forEachChild(elseStatement, cb);
}
}
else {
return ts.forEachChild(node, cb);
}
});
}
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"
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);
});
}
return ts.forEachChild(sourceFile, function cb(node) {
if (tsutils_1.isIfStatement(node)) {
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"
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];
}
const cb = (node: ts.Node): void => {
if (isIterationStatement(node)) {
this.checkStatement(node.statement, node, 0, node.end);
} else if (isIfStatement(node)) {
this.checkStatement(node.thenStatement, node, 0);
if (
node.elseStatement !== undefined &&
node.elseStatement.kind !== ts.SyntaxKind.IfStatement
) {
this.checkStatement(node.elseStatement, node, 5);
}
}
return ts.forEachChild(node, cb);
};
return ts.forEachChild(sourceFile, cb);
function detectAssignment(
statement: ts.Statement,
sourceFile: ts.SourceFile,
checkElseIf: boolean,
inElse?: boolean,
): ts.Expression | undefined {
if (isIfStatement(statement)) {
if (inElse === false || (!checkElseIf && inElse) || statement.elseStatement === undefined) {
return undefined;
}
const then = detectAssignment(statement.thenStatement, sourceFile, checkElseIf, false);
if (then === undefined) {
return undefined;
}
const elze = detectAssignment(statement.elseStatement, sourceFile, checkElseIf, true);
return elze !== undefined && nodeEquals(then, elze, sourceFile) ? then : undefined;
} else if (isBlock(statement)) {
return statement.statements.length === 1
? detectAssignment(statement.statements[0], sourceFile, checkElseIf, inElse)
: undefined;
} else if (isExpressionStatement(statement) && isBinaryExpression(statement.expression)) {
const {
operatorToken: { kind },
return false;
}
var statement = node.statements[0];
if (tsutils_1.isIterationStatement(parent)) {
return true;
}
/*
Watch out for this case:
if (so) {
if (also)
foo();
} else
bar();
*/
return (tsutils_1.isIfStatement(parent) &&
!(tsutils_1.isIfStatement(statement) &&
statement.elseStatement === undefined &&
parent.thenStatement === node &&
parent.elseStatement !== undefined));
}
var CurlyWalker = /** @class */ (function (_super) {
return false;
}
var statement = node.statements[0];
if (tsutils_1.isIterationStatement(parent)) {
return true;
}
/*
Watch out for this case:
if (so) {
if (also)
foo();
} else
bar();
*/
return (tsutils_1.isIfStatement(parent) &&
!(tsutils_1.isIfStatement(statement) &&
statement.elseStatement === undefined &&
parent.thenStatement === node &&
parent.elseStatement !== undefined));
}
var CurlyWalker = /** @class */ (function (_super) {
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;
}
else if (tsutils_1.isBlock(statement)) {
return statement.statements.length === 1
? detectAssignment(statement.statements[0], sourceFile, checkElseIf, inElse)
: undefined;
}
else if (tsutils_1.isExpressionStatement(statement) && tsutils_1.isBinaryExpression(statement.expression)) {
return false;
}
const statement = node.statements[0];
if (isIterationStatement(parent)) {
return true;
}
/*
Watch out for this case:
if (so) {
if (also)
foo();
} else
bar();
*/
return (
isIfStatement(parent) &&
!(
isIfStatement(statement) &&
statement.elseStatement === undefined &&
parent.thenStatement === node &&
parent.elseStatement !== undefined
)
);
}
function everyCase(_a, test) {
var expression = _a.expression, elseStatement = _a.elseStatement;
if (!everyCondition(expression, test)) {
return false;
}
return (elseStatement === undefined ||
!utils.isIfStatement(elseStatement) ||
everyCase(elseStatement, test));
}
function everyCondition(node, test) {