/
ForOfStatement.ts
59 lines (53 loc) · 2.01 KB
/
ForOfStatement.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 { InclusionContext } from '../ExecutionContext';
import BlockScope from '../scopes/BlockScope';
import type Scope from '../scopes/Scope';
import { EMPTY_PATH } from '../utils/PathTracker';
import type * as NodeType from './NodeType';
import type VariableDeclaration from './VariableDeclaration';
import {
type ExpressionNode,
type IncludeChildren,
StatementBase,
type StatementNode
} from './shared/Node';
import type { PatternNode } from './shared/Pattern';
export default class ForOfStatement extends StatementBase {
declare await: boolean;
declare body: StatementNode;
declare left: VariableDeclaration | PatternNode;
declare right: ExpressionNode;
declare type: NodeType.tForOfStatement;
createScope(parentScope: Scope): void {
this.scope = new BlockScope(parentScope);
}
hasEffects(): boolean {
if (!this.deoptimized) this.applyDeoptimizations();
// Placeholder until proper Symbol.Iterator support
return true;
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
if (!this.deoptimized) this.applyDeoptimizations();
this.included = true;
this.left.include(context, includeChildrenRecursively || true);
this.right.include(context, includeChildrenRecursively);
const { brokenFlow } = context;
this.body.include(context, includeChildrenRecursively, { asSingleStatement: true });
context.brokenFlow = brokenFlow;
}
render(code: MagicString, options: RenderOptions): void {
this.left.render(code, options, NO_SEMICOLON);
this.right.render(code, options, NO_SEMICOLON);
// handle no space between "of" and the right side
if (code.original.charCodeAt(this.right.start - 1) === 102 /* f */) {
code.prependLeft(this.right.start, ' ');
}
this.body.render(code, options);
}
protected applyDeoptimizations(): void {
this.deoptimized = true;
this.left.deoptimizePath(EMPTY_PATH);
this.context.requestTreeshakingPass();
}
}