github-pages-deploy-action/node_modules/tsutils/util/control-flow.js

172 lines
6.2 KiB
JavaScript
Raw Normal View History

2020-01-28 13:08:03 +08:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2020-03-07 11:45:40 +08:00
const ts = require("typescript");
const node_1 = require("../typeguard/node");
2020-01-28 13:08:03 +08:00
function endsControlFlow(statement) {
return getControlFlowEnd(statement).end;
}
exports.endsControlFlow = endsControlFlow;
2020-03-07 11:45:40 +08:00
const defaultControlFlowEnd = { statements: [], end: false };
2020-01-28 13:08:03 +08:00
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) {
2020-03-07 11:45:40 +08:00
const result = { statements: [], end: false };
for (const s of statement.statements) {
const current = getControlFlowEndWorker(s);
result.statements.push(...current.statements);
2020-01-28 13:08:03 +08:00
if (current.end) {
result.end = true;
break;
}
}
return result;
}
function handleForInOrOfStatement(statement) {
2020-03-07 11:45:40 +08:00
const end = matchBreakOrContinue(getControlFlowEndWorker(statement.statement), node_1.isBreakOrContinueStatement);
2020-01-28 13:08:03 +08:00
end.end = false;
return end;
}
function handleForAndWhileStatement(statement) {
2020-03-07 11:45:40 +08:00
const constantCondition = statement.kind === ts.SyntaxKind.WhileStatement
2020-01-28 13:08:03 +08:00
? getConstantCondition(statement.expression)
: statement.condition === undefined || getConstantCondition(statement.condition);
if (constantCondition === false)
return defaultControlFlowEnd;
2020-03-07 11:45:40 +08:00
const end = matchBreakOrContinue(getControlFlowEndWorker(statement.statement), node_1.isBreakOrContinueStatement);
2020-01-28 13:08:03 +08:00
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);
}
2020-03-07 11:45:40 +08:00
const then = getControlFlowEndWorker(node.thenStatement);
2020-01-28 13:08:03 +08:00
if (node.elseStatement === undefined)
return {
statements: then.statements,
end: false,
};
2020-03-07 11:45:40 +08:00
const elze = getControlFlowEndWorker(node.elseStatement);
2020-01-28 13:08:03 +08:00
return {
2020-03-07 11:45:40 +08:00
statements: [...then.statements, ...elze.statements],
2020-01-28 13:08:03 +08:00
end: then.end && elze.end,
};
}
function handleSwitchStatement(node) {
2020-03-07 11:45:40 +08:00
let hasDefault = false;
const result = {
2020-01-28 13:08:03 +08:00
statements: [],
end: false,
};
2020-03-07 11:45:40 +08:00
for (const clause of node.caseBlock.clauses) {
2020-01-28 13:08:03 +08:00
if (clause.kind === ts.SyntaxKind.DefaultClause)
hasDefault = true;
2020-03-07 11:45:40 +08:00
const current = handleBlock(clause);
2020-01-28 13:08:03 +08:00
result.end = current.end;
2020-03-07 11:45:40 +08:00
result.statements.push(...current.statements);
2020-01-28 13:08:03 +08:00
}
if (!hasDefault)
result.end = false;
return result;
}
function handleTryStatement(node) {
2020-03-07 11:45:40 +08:00
let finallyResult;
2020-01-28 13:08:03 +08:00
if (node.finallyBlock !== undefined) {
finallyResult = handleBlock(node.finallyBlock);
if (finallyResult.end)
return finallyResult;
}
2020-03-07 11:45:40 +08:00
const tryResult = handleBlock(node.tryBlock);
2020-01-28 13:08:03 +08:00
if (node.catchClause === undefined)
return { statements: finallyResult.statements.concat(tryResult.statements), end: tryResult.end };
2020-03-07 11:45:40 +08:00
const catchResult = handleBlock(node.catchClause.block);
2020-01-28 13:08:03 +08:00
return {
statements: tryResult.statements
2020-03-07 11:45:40 +08:00
.filter((s) => s.kind !== ts.SyntaxKind.ThrowStatement)
2020-01-28 13:08:03 +08:00
.concat(catchResult.statements, finallyResult === undefined ? [] : finallyResult.statements),
end: tryResult.end && catchResult.end,
};
}
function matchBreakOrContinue(current, pred) {
2020-03-07 11:45:40 +08:00
const result = {
2020-01-28 13:08:03 +08:00
statements: [],
end: current.end,
};
2020-03-07 11:45:40 +08:00
for (const statement of current.statements) {
2020-01-28 13:08:03 +08:00
if (pred(statement) && statement.label === undefined) {
result.end = false;
continue;
}
result.statements.push(statement);
}
return result;
}
function matchLabel(current, label) {
2020-03-07 11:45:40 +08:00
const result = {
2020-01-28 13:08:03 +08:00
statements: [],
end: current.end,
};
2020-03-07 11:45:40 +08:00
const labelText = label.text;
for (const statement of current.statements) {
2020-01-28 13:08:03 +08:00
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;
}