2 Object.defineProperty(exports, "__esModule", { value: true });
\r
3 exports.callExpressionAffectsControlFlow = exports.SignatureEffect = exports.getControlFlowEnd = exports.endsControlFlow = void 0;
\r
4 const ts = require("typescript");
\r
5 const node_1 = require("../typeguard/node");
\r
6 const util_1 = require("./util");
\r
7 function endsControlFlow(statement, checker) {
\r
8 return getControlFlowEnd(statement, checker).end;
\r
10 exports.endsControlFlow = endsControlFlow;
\r
11 const defaultControlFlowEnd = { statements: [], end: false };
\r
12 function getControlFlowEnd(statement, checker) {
\r
13 return node_1.isBlockLike(statement) ? handleBlock(statement, checker) : getControlFlowEndWorker(statement, checker);
\r
15 exports.getControlFlowEnd = getControlFlowEnd;
\r
16 function getControlFlowEndWorker(statement, checker) {
\r
17 switch (statement.kind) {
\r
18 case ts.SyntaxKind.ReturnStatement:
\r
19 case ts.SyntaxKind.ThrowStatement:
\r
20 case ts.SyntaxKind.ContinueStatement:
\r
21 case ts.SyntaxKind.BreakStatement:
\r
22 return { statements: [statement], end: true };
\r
23 case ts.SyntaxKind.Block:
\r
24 return handleBlock(statement, checker);
\r
25 case ts.SyntaxKind.ForStatement:
\r
26 case ts.SyntaxKind.WhileStatement:
\r
27 return handleForAndWhileStatement(statement, checker);
\r
28 case ts.SyntaxKind.ForOfStatement:
\r
29 case ts.SyntaxKind.ForInStatement:
\r
30 return handleForInOrOfStatement(statement, checker);
\r
31 case ts.SyntaxKind.DoStatement:
\r
32 return matchBreakOrContinue(getControlFlowEndWorker(statement.statement, checker), node_1.isBreakOrContinueStatement);
\r
33 case ts.SyntaxKind.IfStatement:
\r
34 return handleIfStatement(statement, checker);
\r
35 case ts.SyntaxKind.SwitchStatement:
\r
36 return matchBreakOrContinue(handleSwitchStatement(statement, checker), node_1.isBreakStatement);
\r
37 case ts.SyntaxKind.TryStatement:
\r
38 return handleTryStatement(statement, checker);
\r
39 case ts.SyntaxKind.LabeledStatement:
\r
40 return matchLabel(getControlFlowEndWorker(statement.statement, checker), statement.label);
\r
41 case ts.SyntaxKind.WithStatement:
\r
42 return getControlFlowEndWorker(statement.statement, checker);
\r
43 case ts.SyntaxKind.ExpressionStatement:
\r
44 if (checker === undefined)
\r
45 return defaultControlFlowEnd;
\r
46 return handleExpressionStatement(statement, checker);
\r
48 return defaultControlFlowEnd;
\r
51 function handleBlock(statement, checker) {
\r
52 const result = { statements: [], end: false };
\r
53 for (const s of statement.statements) {
\r
54 const current = getControlFlowEndWorker(s, checker);
\r
55 result.statements.push(...current.statements);
\r
63 function handleForInOrOfStatement(statement, checker) {
\r
64 const end = matchBreakOrContinue(getControlFlowEndWorker(statement.statement, checker), node_1.isBreakOrContinueStatement);
\r
65 end.end = false; // loop body is guaranteed to be executed
\r
68 function handleForAndWhileStatement(statement, checker) {
\r
69 const constantCondition = statement.kind === ts.SyntaxKind.WhileStatement
\r
70 ? getConstantCondition(statement.expression)
\r
71 : statement.condition === undefined || getConstantCondition(statement.condition);
\r
72 if (constantCondition === false)
\r
73 return defaultControlFlowEnd; // loop body is never executed
\r
74 const end = matchBreakOrContinue(getControlFlowEndWorker(statement.statement, checker), node_1.isBreakOrContinueStatement);
\r
75 if (constantCondition === undefined)
\r
76 end.end = false; // can't be sure that loop body is executed at all
\r
79 /** Simply detects `true` and `false` in conditions. That matches TypeScript's behavior. */
\r
80 function getConstantCondition(node) {
\r
81 switch (node.kind) {
\r
82 case ts.SyntaxKind.TrueKeyword:
\r
84 case ts.SyntaxKind.FalseKeyword:
\r
90 function handleIfStatement(node, checker) {
\r
91 switch (getConstantCondition(node.expression)) {
\r
93 // else branch is never executed
\r
94 return getControlFlowEndWorker(node.thenStatement, checker);
\r
96 // then branch is never executed
\r
97 return node.elseStatement === undefined
\r
98 ? defaultControlFlowEnd
\r
99 : getControlFlowEndWorker(node.elseStatement, checker);
\r
101 const then = getControlFlowEndWorker(node.thenStatement, checker);
\r
102 if (node.elseStatement === undefined)
\r
104 statements: then.statements,
\r
107 const elze = getControlFlowEndWorker(node.elseStatement, checker);
\r
109 statements: [...then.statements, ...elze.statements],
\r
110 end: then.end && elze.end,
\r
113 function handleSwitchStatement(node, checker) {
\r
114 let hasDefault = false;
\r
119 for (const clause of node.caseBlock.clauses) {
\r
120 if (clause.kind === ts.SyntaxKind.DefaultClause)
\r
122 const current = handleBlock(clause, checker);
\r
123 result.end = current.end;
\r
124 result.statements.push(...current.statements);
\r
126 result.end && (result.end = hasDefault || checker !== undefined && util_1.hasExhaustiveCaseClauses(node, checker));
\r
129 function handleTryStatement(node, checker) {
\r
131 if (node.finallyBlock !== undefined) {
\r
132 finallyResult = handleBlock(node.finallyBlock, checker);
\r
133 // if 'finally' always ends control flow, we are not interested in any jump statements from 'try' or 'catch'
\r
134 if (finallyResult.end)
\r
135 return finallyResult;
\r
137 const tryResult = handleBlock(node.tryBlock, checker);
\r
138 if (node.catchClause === undefined)
\r
139 return { statements: finallyResult.statements.concat(tryResult.statements), end: tryResult.end };
\r
140 const catchResult = handleBlock(node.catchClause.block, checker);
\r
142 statements: tryResult.statements
\r
143 // remove all throw statements and throwing function calls from the list of control flow statements inside tryBlock
\r
144 .filter((s) => s.kind !== ts.SyntaxKind.ThrowStatement && s.kind !== ts.SyntaxKind.ExpressionStatement)
\r
145 .concat(catchResult.statements, finallyResult === undefined ? [] : finallyResult.statements),
\r
146 end: tryResult.end && catchResult.end, // only ends control flow if try AND catch definitely end control flow
\r
149 /** Dotted name as TypeScript requires it for assertion signatures to affect control flow. */
\r
150 function isDottedNameWithExplicitTypeAnnotation(node, checker) {
\r
152 switch (node.kind) {
\r
153 case ts.SyntaxKind.Identifier: {
\r
154 const symbol = checker.getExportSymbolOfSymbol(checker.getSymbolAtLocation(node));
\r
155 return isExplicitlyTypedSymbol(util_1.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias) ? checker.getAliasedSymbol(symbol) : symbol, checker);
\r
157 case ts.SyntaxKind.ThisKeyword:
\r
158 return isExplicitlyTypedThis(node);
\r
159 case ts.SyntaxKind.SuperKeyword:
\r
161 case ts.SyntaxKind.PropertyAccessExpression:
\r
162 if (!isExplicitlyTypedSymbol(checker.getSymbolAtLocation(node), checker))
\r
165 case ts.SyntaxKind.ParenthesizedExpression:
\r
166 node = node.expression;
\r
173 function isExplicitlyTypedSymbol(symbol, checker) {
\r
174 if (symbol === undefined)
\r
176 if (util_1.isSymbolFlagSet(symbol, ts.SymbolFlags.Function | ts.SymbolFlags.Method | ts.SymbolFlags.Class | ts.SymbolFlags.ValueModule))
\r
178 if (!util_1.isSymbolFlagSet(symbol, ts.SymbolFlags.Variable | ts.SymbolFlags.Property))
\r
180 if (symbol.valueDeclaration === undefined)
\r
182 if (declarationHasExplicitTypeAnnotation(symbol.valueDeclaration))
\r
184 return node_1.isVariableDeclaration(symbol.valueDeclaration) &&
\r
185 symbol.valueDeclaration.parent.parent.kind === ts.SyntaxKind.ForOfStatement &&
\r
186 isDottedNameWithExplicitTypeAnnotation(symbol.valueDeclaration.parent.parent.expression, checker);
\r
188 function declarationHasExplicitTypeAnnotation(node) {
\r
189 if (ts.isJSDocPropertyLikeTag(node))
\r
190 return node.typeExpression !== undefined;
\r
191 return (node_1.isVariableDeclaration(node) ||
\r
192 node_1.isParameterDeclaration(node) ||
\r
193 node_1.isPropertyDeclaration(node) ||
\r
194 node_1.isPropertySignature(node)) && (util_1.isNodeFlagSet(node, ts.NodeFlags.JavaScriptFile)
\r
195 ? ts.getJSDocType(node)
\r
196 : node.type) !== undefined;
\r
198 function isExplicitlyTypedThis(node) {
\r
201 node = node.parent;
\r
202 if (node_1.isDecorator(node)) {
\r
203 // `this` in decorators always resolves outside of the containing class
\r
204 if (node.parent.kind === ts.SyntaxKind.Parameter && node_1.isClassLikeDeclaration(node.parent.parent.parent)) {
\r
205 node = node.parent.parent.parent.parent;
\r
207 else if (node_1.isClassLikeDeclaration(node.parent.parent)) {
\r
208 node = node.parent.parent.parent;
\r
210 else if (node_1.isClassLikeDeclaration(node.parent)) {
\r
211 node = node.parent.parent;
\r
214 } while (util_1.isFunctionScopeBoundary(node) !== 1 /* Function */ || node.kind === ts.SyntaxKind.ArrowFunction);
\r
215 return util_1.isFunctionWithBody(node) &&
\r
216 (util_1.isNodeFlagSet(node, ts.NodeFlags.JavaScriptFile)
\r
217 ? ((_a = ts.getJSDocThisTag(node)) === null || _a === void 0 ? void 0 : _a.typeExpression) !== undefined
\r
218 : node.parameters.length !== 0 && util_1.isThisParameter(node.parameters[0]) && node.parameters[0].type !== undefined) ||
\r
219 node_1.isClassLikeDeclaration(node.parent);
\r
221 var SignatureEffect;
\r
222 (function (SignatureEffect) {
\r
223 SignatureEffect[SignatureEffect["Never"] = 1] = "Never";
\r
224 SignatureEffect[SignatureEffect["Asserts"] = 2] = "Asserts";
\r
225 })(SignatureEffect = exports.SignatureEffect || (exports.SignatureEffect = {}));
\r
227 * Dermines whether a top level CallExpression has a control flow effect according to TypeScript's rules.
\r
228 * This handles functions returning `never` and `asserts`.
\r
230 function callExpressionAffectsControlFlow(node, checker) {
\r
232 if (!node_1.isExpressionStatement(node.parent) ||
\r
233 ts.isOptionalChain(node) ||
\r
234 !isDottedNameWithExplicitTypeAnnotation(node.expression, checker))
\r
236 const signature = checker.getResolvedSignature(node);
\r
237 if ((signature === null || signature === void 0 ? void 0 : signature.declaration) === undefined)
\r
239 const typeNode = ts.isJSDocSignature(signature.declaration)
\r
240 ? (_b = (_a = signature.declaration.type) === null || _a === void 0 ? void 0 : _a.typeExpression) === null || _b === void 0 ? void 0 : _b.type
\r
241 : (_c = signature.declaration.type) !== null && _c !== void 0 ? _c : (util_1.isNodeFlagSet(signature.declaration, ts.NodeFlags.JavaScriptFile)
\r
242 ? ts.getJSDocReturnType(signature.declaration)
\r
244 if (typeNode === undefined)
\r
246 if (node_1.isTypePredicateNode(typeNode) && typeNode.assertsModifier !== undefined)
\r
247 return 2 /* Asserts */;
\r
248 return util_1.isTypeFlagSet(checker.getTypeFromTypeNode(typeNode), ts.TypeFlags.Never) ? 1 /* Never */ : undefined;
\r
250 exports.callExpressionAffectsControlFlow = callExpressionAffectsControlFlow;
\r
251 function handleExpressionStatement(node, checker) {
\r
252 if (!node_1.isCallExpression(node.expression))
\r
253 return defaultControlFlowEnd;
\r
254 switch (callExpressionAffectsControlFlow(node.expression, checker)) {
\r
255 case 2 /* Asserts */:
\r
256 return { statements: [node], end: false };
\r
257 case 1 /* Never */:
\r
258 return { statements: [node], end: true };
\r
260 return defaultControlFlowEnd;
\r
263 function matchBreakOrContinue(current, pred) {
\r
268 for (const statement of current.statements) {
\r
269 if (pred(statement) && statement.label === undefined) {
\r
270 result.end = false;
\r
273 result.statements.push(statement);
\r
277 function matchLabel(current, label) {
\r
282 const labelText = label.text;
\r
283 for (const statement of current.statements) {
\r
284 switch (statement.kind) {
\r
285 case ts.SyntaxKind.BreakStatement:
\r
286 case ts.SyntaxKind.ContinueStatement:
\r
287 if (statement.label !== undefined && statement.label.text === labelText) {
\r
288 result.end = false;
\r
292 result.statements.push(statement);
\r
296 //# sourceMappingURL=control-flow.js.map