131 lines
11 KiB
JavaScript
131 lines
11 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var ts = require("typescript");
|
|
var node_1 = require("../typeguard/node");
|
|
function endsControlFlow(statement) {
|
|
if (node_1.isBlockLike(statement))
|
|
return handleBlock(statement).end;
|
|
switch (statement.kind) {
|
|
case ts.SyntaxKind.ForStatement:
|
|
case ts.SyntaxKind.ForOfStatement:
|
|
case ts.SyntaxKind.ForInStatement:
|
|
case ts.SyntaxKind.WhileStatement:
|
|
case ts.SyntaxKind.DoStatement:
|
|
case ts.SyntaxKind.SwitchStatement:
|
|
if (statement.parent.kind === ts.SyntaxKind.LabeledStatement)
|
|
statement = statement.parent;
|
|
}
|
|
return getControlFlowEnd(statement).end;
|
|
}
|
|
exports.endsControlFlow = endsControlFlow;
|
|
var defaultControlFlowEnd = { statements: [], end: false };
|
|
function getControlFlowEnd(statement, label) {
|
|
switch (statement.kind) {
|
|
case ts.SyntaxKind.ReturnStatement:
|
|
case ts.SyntaxKind.ThrowStatement:
|
|
case ts.SyntaxKind.ContinueStatement:
|
|
case ts.SyntaxKind.BreakStatement:
|
|
return { statements: [statement], end: true };
|
|
case ts.SyntaxKind.Block:
|
|
return handleBlock(statement);
|
|
case ts.SyntaxKind.ForStatement:
|
|
case ts.SyntaxKind.ForOfStatement:
|
|
case ts.SyntaxKind.ForInStatement:
|
|
case ts.SyntaxKind.DoStatement:
|
|
case ts.SyntaxKind.WhileStatement:
|
|
return matchBreakOrContinue(getControlFlowEnd(statement.statement), node_1.isBreakOrContinueStatement, label);
|
|
case ts.SyntaxKind.IfStatement:
|
|
return handleIfStatement(statement);
|
|
case ts.SyntaxKind.SwitchStatement:
|
|
return matchBreakOrContinue(handleSwitchStatement(statement), node_1.isBreakStatement, label);
|
|
case ts.SyntaxKind.TryStatement:
|
|
return handleTryStatement(statement);
|
|
case ts.SyntaxKind.LabeledStatement:
|
|
return getControlFlowEnd(statement.statement, statement.label);
|
|
case ts.SyntaxKind.WithStatement:
|
|
return getControlFlowEnd(statement.statement);
|
|
default:
|
|
return defaultControlFlowEnd;
|
|
}
|
|
}
|
|
function handleBlock(statement) {
|
|
var result = { statements: [], end: false };
|
|
for (var _i = 0, _a = statement.statements; _i < _a.length; _i++) {
|
|
var s = _a[_i];
|
|
var current = getControlFlowEnd(s);
|
|
(_b = result.statements).push.apply(_b, current.statements);
|
|
if (current.end) {
|
|
result.end = true;
|
|
break;
|
|
}
|
|
}
|
|
return result;
|
|
var _b;
|
|
}
|
|
function handleIfStatement(node) {
|
|
var then = getControlFlowEnd(node.thenStatement);
|
|
if (node.elseStatement === undefined) {
|
|
then.end = false;
|
|
return then;
|
|
}
|
|
var elze = getControlFlowEnd(node.elseStatement);
|
|
return {
|
|
statements: then.statements.concat(elze.statements),
|
|
end: then.end && elze.end,
|
|
};
|
|
}
|
|
function handleSwitchStatement(node) {
|
|
var hasDefault = false;
|
|
var result = {
|
|
statements: [],
|
|
end: false,
|
|
};
|
|
for (var _i = 0, _a = node.caseBlock.clauses; _i < _a.length; _i++) {
|
|
var clause = _a[_i];
|
|
if (clause.kind === ts.SyntaxKind.DefaultClause)
|
|
hasDefault = true;
|
|
var current = handleBlock(clause);
|
|
result.end = current.end;
|
|
(_b = result.statements).push.apply(_b, current.statements);
|
|
}
|
|
if (!hasDefault)
|
|
result.end = false;
|
|
return result;
|
|
var _b;
|
|
}
|
|
function handleTryStatement(node) {
|
|
var result;
|
|
if (node.finallyBlock !== undefined) {
|
|
result = handleBlock(node.finallyBlock);
|
|
if (result.end)
|
|
return result;
|
|
}
|
|
var tryResult = handleBlock(node.tryBlock);
|
|
result = result === undefined
|
|
? tryResult
|
|
: { statements: result.statements.concat(tryResult.statements), end: tryResult.end };
|
|
if (node.catchClause !== undefined) {
|
|
var current = handleBlock(node.catchClause.block);
|
|
result = {
|
|
statements: result.statements.concat(current.statements),
|
|
end: current.end,
|
|
};
|
|
}
|
|
return result;
|
|
}
|
|
function matchBreakOrContinue(current, pred, label) {
|
|
var result = {
|
|
end: current.end,
|
|
statements: [],
|
|
};
|
|
for (var _i = 0, _a = current.statements; _i < _a.length; _i++) {
|
|
var statement = _a[_i];
|
|
if (pred(statement) && (statement.label === undefined || label !== undefined && statement.label.text === label.text)) {
|
|
result.end = false;
|
|
continue;
|
|
}
|
|
result.statements.push(statement);
|
|
}
|
|
return result;
|
|
}
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udHJvbC1mbG93LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiY29udHJvbC1mbG93LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsK0JBQWlDO0FBQ2pDLDBDQUE4RjtBQUU5Rix5QkFBZ0MsU0FBc0M7SUFDbEUsRUFBRSxDQUFDLENBQUMsa0JBQVcsQ0FBQyxTQUFTLENBQUMsQ0FBQztRQUN2QixNQUFNLENBQUMsV0FBVyxDQUFDLFNBQVMsQ0FBQyxDQUFDLEdBQUcsQ0FBQztJQUN0QyxNQUFNLENBQUMsQ0FBQyxTQUFTLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQztRQUNyQixLQUFLLEVBQUUsQ0FBQyxVQUFVLENBQUMsWUFBWSxDQUFDO1FBQ2hDLEtBQUssRUFBRSxDQUFDLFVBQVUsQ0FBQyxjQUFjLENBQUM7UUFDbEMsS0FBSyxFQUFFLENBQUMsVUFBVSxDQUFDLGNBQWMsQ0FBQztRQUNsQyxLQUFLLEVBQUUsQ0FBQyxVQUFVLENBQUMsY0FBYyxDQUFDO1FBQ2xDLEtBQUssRUFBRSxDQUFDLFVBQVUsQ0FBQyxXQUFXLENBQUM7UUFDL0IsS0FBSyxFQUFFLENBQUMsVUFBVSxDQUFDLGVBQWU7WUFDOUIsRUFBRSxDQUFDLENBQUMsU0FBUyxDQUFDLE1BQU8sQ0FBQyxJQUFJLEtBQUssRUFBRSxDQUFDLFVBQVUsQ0FBQyxn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