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