forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentifier.ts
More file actions
107 lines (99 loc) · 3.38 KB
/
Identifier.ts
File metadata and controls
107 lines (99 loc) · 3.38 KB
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
import isReference, { type NodeWithFieldDefinition } from 'is-reference';
import type MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import type { NodeRenderOptions, RenderOptions } from '../../utils/renderHelpers';
import type FunctionScope from '../scopes/FunctionScope';
import type LocalVariable from '../variables/LocalVariable';
import type Variable from '../variables/Variable';
import * as NodeType from './NodeType';
import { type ExpressionEntity } from './shared/Expression';
import IdentifierBase from './shared/IdentifierBase';
import type { PatternNode } from './shared/Pattern';
import type { VariableKind } from './shared/VariableKinds';
export type IdentifierWithVariable = Identifier & { variable: Variable };
export default class Identifier extends IdentifierBase implements PatternNode {
name!: string;
type!: NodeType.tIdentifier;
variable: Variable | null = null;
addExportedVariables(
variables: Variable[],
exportNamesByVariable: ReadonlyMap<Variable, readonly string[]>
): void {
if (exportNamesByVariable.has(this.variable!)) {
variables.push(this.variable!);
}
}
bind(): void {
if (!this.variable && isReference(this, this.parent as NodeWithFieldDefinition)) {
this.variable = this.scope.findVariable(this.name);
this.variable.addReference(this);
this.isVariableReference = true;
}
}
declare(kind: VariableKind, init: ExpressionEntity): LocalVariable[] {
let variable: LocalVariable;
const { treeshake } = this.scope.context.options;
switch (kind) {
case 'var': {
variable = this.scope.addDeclaration(this, this.scope.context, init, kind);
if (treeshake && treeshake.correctVarValueBeforeDeclaration) {
// Necessary to make sure the init is deoptimized. We cannot call deoptimizePath here.
variable.markInitializersForDeoptimization();
}
break;
}
case 'function': {
// in strict mode, functions are only hoisted within a scope but not across block scopes
variable = this.scope.addDeclaration(this, this.scope.context, init, kind);
break;
}
case 'let':
case 'const':
case 'using':
case 'await using':
case 'class': {
variable = this.scope.addDeclaration(this, this.scope.context, init, kind);
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)];
}
markDeclarationReached(): void {
this.variable!.initReached = true;
}
render(
code: MagicString,
{ snippets: { getPropertyAccess }, useOriginalName }: RenderOptions,
{ renderedParentType, isCalleeOfRenderedParent, isShorthandProperty }: NodeRenderOptions = BLANK
): void {
if (this.variable) {
const name = this.variable.getName(getPropertyAccess, useOriginalName);
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, ');
}
}
}
}