/
BlockStatement.ts
67 lines (59 loc) · 2.22 KB
/
BlockStatement.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import MagicString from 'magic-string';
import { RenderOptions, renderStatementList } from '../../utils/renderHelpers';
import { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import BlockScope from '../scopes/BlockScope';
import ChildScope from '../scopes/ChildScope';
import Scope from '../scopes/Scope';
import ExpressionStatement from './ExpressionStatement';
import * as NodeType from './NodeType';
import { UNKNOWN_EXPRESSION } from './shared/Expression';
import { IncludeChildren, Node, StatementBase, StatementNode } from './shared/Node';
export default class BlockStatement extends StatementBase {
declare body: StatementNode[];
declare type: NodeType.tBlockStatement;
private declare deoptimizeBody: boolean;
private directlyIncluded = false;
addImplicitReturnExpressionToScope(): void {
const lastStatement = this.body[this.body.length - 1];
if (!lastStatement || lastStatement.type !== NodeType.ReturnStatement) {
this.scope.addReturnExpression(UNKNOWN_EXPRESSION);
}
}
createScope(parentScope: Scope): void {
this.scope = (this.parent as Node).preventChildBlockScope
? (parentScope as ChildScope)
: new BlockScope(parentScope);
}
hasEffects(context: HasEffectsContext): boolean {
if (this.deoptimizeBody) return true;
for (const node of this.body) {
if (node.hasEffects(context)) return true;
if (context.brokenFlow) break;
}
return false;
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
if (!this.deoptimizeBody || !this.directlyIncluded) {
this.included = true;
this.directlyIncluded = true;
if (this.deoptimizeBody) includeChildrenRecursively = true;
for (const node of this.body) {
if (includeChildrenRecursively || node.shouldBeIncluded(context))
node.include(context, includeChildrenRecursively);
}
}
}
initialise(): void {
const firstBodyStatement = this.body[0];
this.deoptimizeBody =
firstBodyStatement instanceof ExpressionStatement &&
firstBodyStatement.directive === 'use asm';
}
render(code: MagicString, options: RenderOptions): void {
if (this.body.length) {
renderStatementList(this.body, code, this.start + 1, this.end - 1, options);
} else {
super.render(code, options);
}
}
}