/
ForInStatement.ts
65 lines (59 loc) · 2.15 KB
/
ForInStatement.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
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 { EMPTY_PATH } from '../utils/PathTracker';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, StatementBase, StatementNode } from './shared/Node';
import { PatternNode } from './shared/Pattern';
import VariableDeclaration from './VariableDeclaration';
export default class ForInStatement extends StatementBase {
body!: StatementNode;
left!: VariableDeclaration | PatternNode;
right!: ExpressionNode;
type!: NodeType.tForInStatement;
bind() {
this.left.bind();
this.left.deoptimizePath(EMPTY_PATH);
this.right.bind();
this.body.bind();
}
createScope(parentScope: Scope) {
this.scope = new BlockScope(parentScope);
}
hasEffects(context: HasEffectsContext): boolean {
if (
(this.left &&
(this.left.hasEffects(context) ||
this.left.hasEffectsWhenAssignedAtPath(EMPTY_PATH, context))) ||
(this.right && this.right.hasEffects(context))
)
return true;
const {
breakFlow,
ignore: { breakStatements, continueStatements }
} = context;
context.ignore.breakStatements = true;
context.ignore.continueStatements = true;
if (this.body.hasEffects(context)) return true;
context.ignore.breakStatements = breakStatements;
context.ignore.continueStatements = continueStatements;
context.breakFlow = breakFlow;
return false;
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
this.included = true;
this.left.includeWithAllDeclaredVariables(includeChildrenRecursively, context);
this.left.deoptimizePath(EMPTY_PATH);
this.right.include(context, includeChildrenRecursively);
const { breakFlow } = context;
this.body.include(context, includeChildrenRecursively);
context.breakFlow = breakFlow;
}
render(code: MagicString, options: RenderOptions) {
this.left.render(code, options, NO_SEMICOLON);
this.right.render(code, options, NO_SEMICOLON);
this.body.render(code, options);
}
}