-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathForInStatement.ts
71 lines (63 loc) · 2.54 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
66
67
68
69
70
71
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 { EMPTY_PATH, UNKNOWN_PATH } from '../utils/PathTracker';
import type * as NodeType from './NodeType';
import { UNKNOWN_EXPRESSION } from './shared/Expression';
import { hasLoopBodyEffects, includeLoopBody } from './shared/loops';
import {
type ExpressionNode,
type IncludeChildren,
StatementBase,
type StatementNode
} from './shared/Node';
import type { PatternNode } from './shared/Pattern';
import type VariableDeclaration from './VariableDeclaration';
export default class ForInStatement extends StatementBase {
declare body: StatementNode;
declare left: VariableDeclaration | PatternNode;
declare right: ExpressionNode;
declare type: NodeType.tForInStatement;
createScope(parentScope: ChildScope): void {
this.scope = new BlockScope(parentScope);
}
hasEffects(context: HasEffectsContext): boolean {
const { body, deoptimized, left, right } = this;
if (!deoptimized) this.applyDeoptimizations();
if (left.hasEffectsAsAssignmentTarget(context, false) || right.hasEffects(context)) return true;
return hasLoopBodyEffects(context, body);
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
const { body, deoptimized, left, right } = this;
if (!deoptimized) this.applyDeoptimizations();
if (!this.included) this.includeNode(context);
left.includeAsAssignmentTarget(context, includeChildrenRecursively || true, false);
right.include(context, includeChildrenRecursively);
includeLoopBody(context, body, includeChildrenRecursively);
}
includeNode(context: InclusionContext) {
this.included = true;
if (!this.deoptimized) this.applyDeoptimizations();
this.right.includePath(UNKNOWN_PATH, context);
}
initialise() {
super.initialise();
this.left.setAssignedValue(UNKNOWN_EXPRESSION);
}
render(code: MagicString, options: RenderOptions): void {
this.left.render(code, options, NO_SEMICOLON);
this.right.render(code, options, NO_SEMICOLON);
// handle no space between "in" and the right side
if (code.original.charCodeAt(this.right.start - 1) === 110 /* n */) {
code.prependLeft(this.right.start, ' ');
}
this.body.render(code, options);
}
applyDeoptimizations() {
this.deoptimized = true;
this.left.deoptimizePath(EMPTY_PATH);
this.scope.context.requestTreeshakingPass();
}
}