-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
VariableDeclarator.ts
66 lines (59 loc) · 1.97 KB
/
VariableDeclarator.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
import MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import {
findFirstOccurrenceOutsideComment,
findNonWhiteSpace,
RenderOptions
} from '../../utils/renderHelpers';
import { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import { ObjectPath } from '../utils/PathTracker';
import { UNDEFINED_EXPRESSION } from '../values';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';
import { PatternNode } from './shared/Pattern';
export default class VariableDeclarator extends NodeBase {
id!: PatternNode;
init!: ExpressionNode | null;
type!: NodeType.tVariableDeclarator;
declareDeclarator(kind: string) {
this.id.declare(kind, this.init || UNDEFINED_EXPRESSION);
}
deoptimizePath(path: ObjectPath) {
this.id.deoptimizePath(path);
}
hasEffects(context: HasEffectsContext): boolean {
return this.id.hasEffects(context) || (this.init !== null && this.init.hasEffects(context));
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
this.included = true;
if (includeChildrenRecursively || this.id.shouldBeIncluded(context)) {
this.id.include(context, includeChildrenRecursively);
}
if (this.init) {
this.init.include(context, includeChildrenRecursively);
}
}
includeAllDeclaredVariables(
context: InclusionContext,
includeChildrenRecursively: IncludeChildren
): void {
this.included = true;
this.id.include(context, includeChildrenRecursively);
}
render(code: MagicString, options: RenderOptions) {
const renderId = this.id.included;
if (renderId) {
this.id.render(code, options);
} else {
const operatorPos = findFirstOccurrenceOutsideComment(code.original, '=', this.id.end);
code.remove(this.start, findNonWhiteSpace(code.original, operatorPos + 1));
}
if (this.init) {
this.init.render(
code,
options,
renderId ? BLANK : { renderedParentType: NodeType.ExpressionStatement }
);
}
}
}