-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathForStatement.ts
59 lines (53 loc) · 1.98 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
58
59
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 ChildScope from '../scopes/ChildScope';
import type * as NodeType from './NodeType';
import { hasLoopBodyEffects, includeLoopBody } from './shared/loops';
import {
doNotDeoptimize,
type ExpressionNode,
type IncludeChildren,
onlyIncludeSelfNoDeoptimize,
StatementBase,
type StatementNode
} from './shared/Node';
import type VariableDeclaration from './VariableDeclaration';
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: ChildScope): 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);
}
}
ForStatement.prototype.includeNode = onlyIncludeSelfNoDeoptimize;
ForStatement.prototype.applyDeoptimizations = doNotDeoptimize;