forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Variable.ts
108 lines (94 loc) · 3.15 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 type ExternalModule from '../../ExternalModule';
import type Module from '../../Module';
import type { RenderOptions } from '../../utils/renderHelpers';
import type { HasEffectsContext } from '../ExecutionContext';
import type { NodeInteraction } from '../NodeInteractions';
import { INTERACTION_ACCESSED } from '../NodeInteractions';
import type Identifier from '../nodes/Identifier';
import { ExpressionEntity } from '../nodes/shared/Expression';
import type { NodeBase } from '../nodes/shared/Node';
import type { VariableKind } from '../nodes/shared/VariableKinds';
import type { ObjectPath } from '../utils/PathTracker';
export default class Variable extends ExpressionEntity {
alwaysRendered = false;
forbiddenNames: Set<string> | null = null;
initReached = false;
isId = false;
// both NamespaceVariable and ExternalVariable can be namespaces
declare isNamespace?: boolean;
isReassigned = false;
kind: VariableKind | null = null;
declare module?: Module | ExternalModule;
renderBaseName: string | null = null;
renderName: string | null = null;
private renderedLikeHoisted?: Variable;
constructor(public name: string) {
super();
}
/**
* Binds identifiers that reference this variable to this variable.
* Necessary to be able to change variable names.
*/
addReference(_identifier: Identifier): void {}
AllUsedPlaces: NodeBase[] = [];
addUsedPlace(identifier: NodeBase): void {
this.AllUsedPlaces.push(identifier);
}
/**
* Prevent this variable from being renamed to this name to avoid name
* collisions
*/
forbidName(name: string) {
(this.forbiddenNames ||= new Set()).add(name);
}
getBaseVariableName(): string {
return (
this.renderedLikeHoisted?.getBaseVariableName() ||
this.renderBaseName ||
this.renderName ||
this.name
);
}
getName(
getPropertyAccess: (name: string) => string,
useOriginalName?: RenderOptions['useOriginalName']
): string {
if (useOriginalName?.(this)) {
return this.name;
}
if (this.renderedLikeHoisted) {
return this.renderedLikeHoisted.getName(getPropertyAccess, useOriginalName);
}
const name = this.renderName || this.name;
return this.renderBaseName ? `${this.renderBaseName}${getPropertyAccess(name)}` : name;
}
hasEffectsOnInteractionAtPath(
path: ObjectPath,
{ type }: NodeInteraction,
_context: HasEffectsContext
): boolean {
return type !== INTERACTION_ACCESSED || path.length > 0;
}
/**
* 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(): void {
this.included = true;
this.renderedLikeHoisted?.include();
}
/**
* Links the rendered name of this variable to another variable and includes
* this variable if the other variable is included.
*/
renderLikeHoisted(variable: Variable): void {
this.renderedLikeHoisted = variable;
}
markCalledFromTryStatement(): void {}
setRenderNames(baseName: string | null, name: string | null): void {
this.renderBaseName = baseName;
this.renderName = name;
}
}