Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getExit(node: ts.IfStatement): string | undefined {
const parent = node.parent!;
if (isBlock(parent)) {
const container = parent.parent!;
return isCaseOrDefaultClause(container) && container.statements.length === 1
? getCaseClauseExit(container, parent, node)
// Must be the last statement in the block
: isLastStatement(node, parent.statements) ? getEarlyExitKind(container) : undefined;
}
return isCaseOrDefaultClause(parent)
? getCaseClauseExit(parent, parent, node)
// This is the only statement in its container, so of course it's the final statement.
: getEarlyExitKind(parent);
}
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];
}
} while (tsutils_1.isIfStatement(node));
}
}
return ts.forEachChild(node, cb);
});
}
function getLastStatement(clause: ts.Block): ts.Statement {
const block = clause.statements[0];
const statements =
clause.statements.length === 1 && utils.isBlock(block)
? block.statements
: clause.statements;
return last(statements);
}
function size(node: ts.Node, sourceFile: ts.SourceFile): number {
return isBlock(node)
? node.statements.length === 0 ? 0 : diff(node.statements[0].getStart(sourceFile), node.statements.end, sourceFile)
: diff(node.getStart(sourceFile), node.end, sourceFile);
}
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)) {
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 {
return undefined;
}
}
function nodeEquals(a, b, sourceFile) {
function nodeIsContinue(node: ts.Node) {
return (
node.kind === ts.SyntaxKind.ContinueStatement ||
(isBlock(node) &&
node.statements.length === 1 &&
node.statements[0].kind === ts.SyntaxKind.ContinueStatement)
);
}