/
Variable.ts
78 lines (68 loc) · 2.4 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
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 { 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: string | null = null;
declare module?: Module | ExternalModule;
renderBaseName: string | null = null;
renderName: string | null = null;
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 {}
/**
* 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.renderBaseName || this.renderName || this.name;
}
getName(
getPropertyAccess: (name: string) => string,
useOriginalName?: RenderOptions['useOriginalName']
): string {
const name = useOriginalName?.(this) ? this.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;
}
markCalledFromTryStatement(): void {}
setRenderNames(baseName: string | null, name: string | null): void {
this.renderBaseName = baseName;
this.renderName = name;
}
}