/
NamespaceVariable.ts
143 lines (125 loc) · 4.36 KB
/
NamespaceVariable.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
import type Module from '../../Module';
import type { AstContext } from '../../Module';
import { getToStringTagValue, MERGE_NAMESPACES_VARIABLE } from '../../utils/interopHelpers';
import type { RenderOptions } from '../../utils/renderHelpers';
import { getSystemExportStatement } from '../../utils/systemJsRendering';
import type Identifier from '../nodes/Identifier';
import type { LiteralValueOrUnknown } from '../nodes/shared/Expression';
import { UnknownValue } from '../nodes/shared/Expression';
import type ChildScope from '../scopes/ChildScope';
import type { ObjectPath } from '../utils/PathTracker';
import { SymbolToStringTag } from '../utils/PathTracker';
import Variable from './Variable';
export default class NamespaceVariable extends Variable {
context: AstContext;
declare isNamespace: true;
module: Module;
private memberVariables: { [name: string]: Variable } | null = null;
private mergedNamespaces: readonly Variable[] = [];
private referencedEarly = false;
private references: Identifier[] = [];
constructor(context: AstContext) {
super(context.getModuleName());
this.context = context;
this.module = context.module;
}
addReference(identifier: Identifier): void {
this.references.push(identifier);
this.name = identifier.name;
}
getLiteralValueAtPath(path: ObjectPath): LiteralValueOrUnknown {
if (path[0] === SymbolToStringTag) {
return 'Module';
}
return UnknownValue;
}
getMemberVariables(): { [name: string]: Variable } {
if (this.memberVariables) {
return this.memberVariables;
}
const memberVariables: { [name: string]: Variable } = Object.create(null);
for (const name of [...this.context.getExports(), ...this.context.getReexports()]) {
if (name[0] !== '*' && name !== this.module.info.syntheticNamedExports) {
const exportedVariable = this.context.traceExport(name);
if (exportedVariable) {
memberVariables[name] = exportedVariable;
}
}
}
return (this.memberVariables = memberVariables);
}
hasEffectsOnInteractionAtPath(): boolean {
return false;
}
include(): void {
this.included = true;
this.context.includeAllExports();
}
prepare(accessedGlobalsByScope: Map<ChildScope, Set<string>>): void {
if (this.mergedNamespaces.length > 0) {
this.module.scope.addAccessedGlobals([MERGE_NAMESPACES_VARIABLE], accessedGlobalsByScope);
}
}
renderBlock(options: RenderOptions): string {
const {
exportNamesByVariable,
format,
freeze,
indent: t,
namespaceToStringTag,
snippets: { _, cnst, getObject, getPropertyAccess, n, s }
} = options;
const memberVariables = this.getMemberVariables();
const members: [key: string | null, value: string][] = Object.entries(memberVariables).map(
([name, original]) => {
if (this.referencedEarly || original.isReassigned) {
return [
null,
`get ${name}${_}()${_}{${_}return ${original.getName(getPropertyAccess)}${s}${_}}`
];
}
return [name, original.getName(getPropertyAccess)];
}
);
members.unshift([null, `__proto__:${_}null`]);
let output = getObject(members, { lineBreakIndent: { base: '', t } });
if (this.mergedNamespaces.length > 0) {
const assignmentArguments = this.mergedNamespaces.map(variable =>
variable.getName(getPropertyAccess)
);
output = `/*#__PURE__*/${MERGE_NAMESPACES_VARIABLE}(${output},${_}[${assignmentArguments.join(
`,${_}`
)}])`;
} else {
// The helper to merge namespaces will also take care of freezing and toStringTag
if (namespaceToStringTag) {
output = `/*#__PURE__*/Object.defineProperty(${output},${_}Symbol.toStringTag,${_}${getToStringTagValue(
getObject
)})`;
}
if (freeze) {
output = `/*#__PURE__*/Object.freeze(${output})`;
}
}
const name = this.getName(getPropertyAccess);
output = `${cnst} ${name}${_}=${_}${output};`;
if (format === 'system' && exportNamesByVariable.has(this)) {
output += `${n}${getSystemExportStatement([this], options)};`;
}
return output;
}
renderFirst(): boolean {
return this.referencedEarly;
}
setMergedNamespaces(mergedNamespaces: readonly Variable[]): void {
this.mergedNamespaces = mergedNamespaces;
const moduleExecIndex = this.context.getModuleExecIndex();
for (const identifier of this.references) {
if (identifier.context.getModuleExecIndex() <= moduleExecIndex) {
this.referencedEarly = true;
break;
}
}
}
}
NamespaceVariable.prototype.isNamespace = true;