-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ForStatement.ts
57 lines (52 loc) · 2.05 KB
/
ForStatement.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
import MagicString from 'magic-string';
import { NO_SEMICOLON, RenderOptions } from '../../utils/renderHelpers';
import { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import BlockScope from '../scopes/BlockScope';
import Scope from '../scopes/Scope';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, StatementBase, StatementNode } from './shared/Node';
import VariableDeclaration from './VariableDeclaration';
export default class ForStatement extends StatementBase {
body!: StatementNode;
init!: VariableDeclaration | ExpressionNode | null;
test!: ExpressionNode | null;
type!: NodeType.tForStatement;
update!: ExpressionNode | null;
createScope(parentScope: Scope) {
this.scope = new BlockScope(parentScope);
}
hasEffects(context: HasEffectsContext): boolean {
if (
(this.init && this.init.hasEffects(context)) ||
(this.test && this.test.hasEffects(context)) ||
(this.update && this.update.hasEffects(context))
)
return true;
const {
brokenFlow,
ignore: { breaks, continues }
} = context;
context.ignore.breaks = true;
context.ignore.continues = true;
if (this.body.hasEffects(context)) return true;
context.ignore.breaks = breaks;
context.ignore.continues = continues;
context.brokenFlow = brokenFlow;
return false;
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
this.included = true;
if (this.init) this.init.includeAllDeclaredVariables(context, includeChildrenRecursively);
if (this.test) this.test.include(context, includeChildrenRecursively);
const { brokenFlow } = context;
if (this.update) this.update.include(context, includeChildrenRecursively);
this.body.include(context, includeChildrenRecursively);
context.brokenFlow = brokenFlow;
}
render(code: MagicString, options: RenderOptions) {
if (this.init) this.init.render(code, options, NO_SEMICOLON);
if (this.test) this.test.render(code, options, NO_SEMICOLON);
if (this.update) this.update.render(code, options, NO_SEMICOLON);
this.body.render(code, options);
}
}