/
Identifier.ts
187 lines (168 loc) · 5.6 KB
/
Identifier.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import isReference from 'is-reference';
import MagicString from 'magic-string';
import { NormalizedTreeshakingOptions } from '../../rollup/types';
import { BLANK } from '../../utils/blank';
import { NodeRenderOptions, RenderOptions } from '../../utils/renderHelpers';
import { CallOptions } from '../CallOptions';
import { DeoptimizableEntity } from '../DeoptimizableEntity';
import { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import FunctionScope from '../scopes/FunctionScope';
import { EMPTY_PATH, ObjectPath, PathTracker } from '../utils/PathTracker';
import { LiteralValueOrUnknown } from '../values';
import GlobalVariable from '../variables/GlobalVariable';
import LocalVariable from '../variables/LocalVariable';
import Variable from '../variables/Variable';
import * as NodeType from './NodeType';
import { ExpressionEntity } from './shared/Expression';
import { ExpressionNode, NodeBase } from './shared/Node';
import { PatternNode } from './shared/Pattern';
import SpreadElement from './SpreadElement';
export type IdentifierWithVariable = Identifier & { variable: Variable };
export default class Identifier extends NodeBase implements PatternNode {
name!: string;
type!: NodeType.tIdentifier;
variable: Variable | null = null;
private bound = false;
addExportedVariables(
variables: Variable[],
exportNamesByVariable: Map<Variable, string[]>
): void {
if (this.variable !== null && exportNamesByVariable.has(this.variable)) {
variables.push(this.variable);
}
}
bind() {
if (this.bound) return;
this.bound = true;
if (this.variable === null && isReference(this, this.parent as any)) {
this.variable = this.scope.findVariable(this.name);
this.variable.addReference(this);
}
if (
this.variable !== null &&
this.variable instanceof LocalVariable &&
this.variable.additionalInitializers !== null
) {
this.variable.consolidateInitializers();
}
}
declare(kind: string, init: ExpressionEntity) {
let variable: LocalVariable;
switch (kind) {
case 'var':
variable = this.scope.addDeclaration(this, this.context, init, true);
break;
case 'function':
// in strict mode, functions are only hoisted within a scope but not across block scopes
variable = this.scope.addDeclaration(this, this.context, init, false);
break;
case 'let':
case 'const':
case 'class':
variable = this.scope.addDeclaration(this, this.context, init, false);
break;
case 'parameter':
variable = (this.scope as FunctionScope).addParameterDeclaration(this);
break;
/* istanbul ignore next */
default:
/* istanbul ignore next */
throw new Error(`Internal Error: Unexpected identifier kind ${kind}.`);
}
return [(this.variable = variable)];
}
deoptimizePath(path: ObjectPath) {
if (!this.bound) this.bind();
if (path.length === 0 && !this.scope.contains(this.name)) {
this.disallowImportReassignment();
}
this.variable!.deoptimizePath(path);
}
getLiteralValueAtPath(
path: ObjectPath,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): LiteralValueOrUnknown {
if (!this.bound) this.bind();
return this.variable!.getLiteralValueAtPath(path, recursionTracker, origin);
}
getReturnExpressionWhenCalledAtPath(
path: ObjectPath,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
) {
if (!this.bound) this.bind();
return this.variable!.getReturnExpressionWhenCalledAtPath(path, recursionTracker, origin);
}
hasEffects(): boolean {
return (
(this.context.options.treeshake as NormalizedTreeshakingOptions).unknownGlobalSideEffects &&
this.variable instanceof GlobalVariable &&
this.variable.hasEffectsWhenAccessedAtPath(EMPTY_PATH)
);
}
hasEffectsWhenAccessedAtPath(path: ObjectPath, context: HasEffectsContext): boolean {
return this.variable !== null && this.variable.hasEffectsWhenAccessedAtPath(path, context);
}
hasEffectsWhenAssignedAtPath(path: ObjectPath, context: HasEffectsContext): boolean {
return !this.variable || this.variable.hasEffectsWhenAssignedAtPath(path, context);
}
hasEffectsWhenCalledAtPath(
path: ObjectPath,
callOptions: CallOptions,
context: HasEffectsContext
) {
return !this.variable || this.variable.hasEffectsWhenCalledAtPath(path, callOptions, context);
}
include() {
if (!this.included) {
this.included = true;
if (this.variable !== null) {
this.context.includeVariableInModule(this.variable);
}
}
}
includeCallArguments(context: InclusionContext, args: (ExpressionNode | SpreadElement)[]): void {
this.variable!.includeCallArguments(context, args);
}
mayModifyThisWhenCalledAtPath(path: ObjectPath, recursionTracker: PathTracker) {
return this.variable
? this.variable.mayModifyThisWhenCalledAtPath(path, recursionTracker)
: true;
}
render(
code: MagicString,
_options: RenderOptions,
{ renderedParentType, isCalleeOfRenderedParent, isShorthandProperty }: NodeRenderOptions = BLANK
) {
if (this.variable) {
const name = this.variable.getName();
if (name !== this.name) {
code.overwrite(this.start, this.end, name, {
contentOnly: true,
storeName: true
});
if (isShorthandProperty) {
code.prependRight(this.start, `${this.name}: `);
}
}
// In strict mode, any variable named "eval" must be the actual "eval" function
if (
name === 'eval' &&
renderedParentType === NodeType.CallExpression &&
isCalleeOfRenderedParent
) {
code.appendRight(this.start, '0, ');
}
}
}
private disallowImportReassignment() {
return this.context.error(
{
code: 'ILLEGAL_REASSIGNMENT',
message: `Illegal reassignment to import '${this.name}'`
},
this.start
);
}
}