"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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udHJvbC1mbG93LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiY29udHJvbC1mbG93LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsK0JBQWlDO0FBQ2pDLDBDQUE4RjtBQUU5RixTQUFnQixlQUFlLENBQUMsU0FBc0M7SUFDbEUsT0FBTyxpQkFBaUIsQ0FBQyxTQUFTLENBQUMsQ0FBQyxHQUFHLENBQUM7QUFDNUMsQ0FBQztBQUZELDBDQUVDO0FBaUJELElBQU0scUJBQXFCLEdBQW1CLEVBQUMsVUFBVSxFQUFFLEVBQUUsRUFBRSxHQUFHLEVBQUUsS0FBSyxFQUFDLENBQUM7QUFLM0UsU0FBZ0IsaUJBQWlCLENBQUMsU0FBc0M7SUFDcEUsT0FBTyxrQkFBVyxDQUFDLFNBQVMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxXQUFXLENBQUMsU0FBUyxDQUFDLENBQUMsQ0FBQyxDQUFDLHVCQUF1QixDQUFDLFNBQVMsQ0FBQyxDQUFDO0FBQ2hHLENBQUM7QUFGRCw4Q0FFQztBQUVELFNBQVMsdUJBQXVCLENBQUMsU0FBdUI7SUFDcEQsUUFBUSxTQUFTLENBQUMsSUFBSSxFQUFFO1FBQ3BCLEtBQUssRUFBRSxDQUFDLFVBQVUsQ0FBQyxlQUFlLENBQUM7UUFDbkMsS0FBSyxFQUFFLENBQUMsVUFBVSxDQUFDLGNBQWMsQ0FBQztRQUNsQyxLQUFLLEVBQUUsQ0FBQyxVQUFVLENBQUMsaUJBQWlCLENBQUM7UUFDckMsS0FBSyxFQUFFLENBQUMsVUF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