/
Variable.ts
108 lines (91 loc) · 2.88 KB
/
Variable.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import ExternalModule from '../../ExternalModule';
import Module from '../../Module';
import CallOptions from '../CallOptions';
import { DeoptimizableEntity } from '../DeoptimizableEntity';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import Identifier from '../nodes/Identifier';
import { ExpressionEntity } from '../nodes/shared/Expression';
import { ExpressionNode } from '../nodes/shared/Node';
import SpreadElement from '../nodes/SpreadElement';
import { ImmutableEntityPathTracker } from '../utils/ImmutableEntityPathTracker';
import { LiteralValueOrUnknown, ObjectPath, UNKNOWN_EXPRESSION, UNKNOWN_VALUE } from '../values';
export default class Variable implements ExpressionEntity {
exportName: string | null = null;
included = false;
isId = false;
isNamespace?: boolean;
isReassigned = false;
module?: Module | ExternalModule;
name: string;
renderBaseName: string | null = null;
renderName: string | null = null;
safeExportName: string | null = null;
constructor(name: string) {
this.name = name;
}
/**
* Binds identifiers that reference this variable to this variable.
* Necessary to be able to change variable names.
*/
addReference(_identifier: Identifier) {}
deoptimizePath(_path: ObjectPath) {}
getBaseVariableName(): string {
return this.renderBaseName || this.renderName || this.name;
}
getLiteralValueAtPath(
_path: ObjectPath,
_recursionTracker: ImmutableEntityPathTracker,
_origin: DeoptimizableEntity
): LiteralValueOrUnknown {
return UNKNOWN_VALUE;
}
getName(): string {
const name = this.renderName || this.name;
return this.renderBaseName ? `${this.renderBaseName}.${name}` : name;
}
getReturnExpressionWhenCalledAtPath(
_path: ObjectPath,
_recursionTracker: ImmutableEntityPathTracker,
_origin: DeoptimizableEntity
): ExpressionEntity {
return UNKNOWN_EXPRESSION;
}
hasEffectsWhenAccessedAtPath(path: ObjectPath, _options: ExecutionPathOptions) {
return path.length > 0;
}
hasEffectsWhenAssignedAtPath(_path: ObjectPath, _options: ExecutionPathOptions) {
return true;
}
hasEffectsWhenCalledAtPath(
_path: ObjectPath,
_callOptions: CallOptions,
_options: ExecutionPathOptions
) {
return true;
}
/**
* Marks this variable as being part of the bundle, which is usually the case when one of
* its identifiers becomes part of the bundle. Returns true if it has not been included
* previously.
* Once a variable is included, it should take care all its declarations are included.
*/
include() {
this.included = true;
}
includeCallArguments(args: (ExpressionNode | SpreadElement)[]): void {
for (const arg of args) {
arg.include(false);
}
}
includeInitRecursively() {}
setRenderNames(baseName: string | null, name: string | null) {
this.renderBaseName = baseName;
this.renderName = name;
}
setSafeName(name: string | null) {
this.renderName = name;
}
toString() {
return this.name;
}
}