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