/
BlockStatement.ts
50 lines (44 loc) · 1.56 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
import MagicString from 'magic-string';
import { RenderOptions, renderStatementList } from '../../utils/renderHelpers';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import BlockScope from '../scopes/BlockScope';
import ChildScope from '../scopes/ChildScope';
import Scope from '../scopes/Scope';
import { UNKNOWN_EXPRESSION } from '../values';
import * as NodeType from './NodeType';
import { IncludeChildren, Node, StatementBase, StatementNode } from './shared/Node';
export default class BlockStatement extends StatementBase {
body!: StatementNode[];
type!: NodeType.tBlockStatement;
addImplicitReturnExpressionToScope() {
const lastStatement = this.body[this.body.length - 1];
if (!lastStatement || lastStatement.type !== NodeType.ReturnStatement) {
this.scope.addReturnExpression(UNKNOWN_EXPRESSION);
}
}
createScope(parentScope: Scope) {
this.scope = (this.parent as Node).preventChildBlockScope
? (parentScope as ChildScope)
: new BlockScope(parentScope);
}
hasEffects(options: ExecutionPathOptions) {
for (const node of this.body) {
if (node.hasEffects(options)) return true;
}
return false;
}
include(includeChildrenRecursively: IncludeChildren) {
this.included = true;
for (const node of this.body) {
if (includeChildrenRecursively || node.shouldBeIncluded())
node.include(includeChildrenRecursively);
}
}
render(code: MagicString, options: RenderOptions) {
if (this.body.length) {
renderStatementList(this.body, code, this.start + 1, this.end - 1, options);
} else {
super.render(code, options);
}
}
}