/
namespace.js
199 lines (194 loc) 路 5.9 KB
/
namespace.js
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
188
189
190
191
192
193
194
195
196
197
198
199
const SHARED_NAMESPACE_ERRORS = {
TSEnumDeclaration: "An enum may not share the name of its parent namespace.",
FunctionDeclaration:
"A function may not share the name of its parent namespace.",
ClassDeclaration: "A class may not share the name of its parent namespace.",
VariableDeclaration:
"A variable may not share the name of its parent namespace.",
TSModuleDeclaration:
"A namespace may not share the name of its parent namespace.",
};
export default function transpileEnum(path, t) {
if (path.node.declare || path.node.id.type === "StringLiteral") {
path.remove();
return;
}
const name = path.node.id.name;
const value = handleNested(path, t, JSON.parse(JSON.stringify(path.node)));
if (path.parent.type === "ExportNamedDeclaration") {
path.parentPath.insertAfter(value);
path.replaceWith(getDeclaration(t, name));
path.scope.registerDeclaration(path.parentPath);
} else if (path.scope.hasOwnBinding(name)) {
path.replaceWith(value);
} else {
path.scope.registerDeclaration(
path.replaceWithMultiple([getDeclaration(t, name), value])[0],
);
}
}
function getDeclaration(t, name) {
return t.variableDeclaration("let", [
t.variableDeclarator(t.identifier(name)),
]);
}
function handleNested(path, t, node, parentExportName) {
const names = [];
const name = node.id.name;
const namespaceTopLevel = node.body.body;
for (let i = 0; i < namespaceTopLevel.length; i++) {
const subNode = namespaceTopLevel[i];
switch (subNode.type) {
case "TSModuleDeclaration": {
const moduleName = subNode.id.name;
if (moduleName === name) {
throw path.hub.file.buildCodeFrameError(
subNode,
SHARED_NAMESPACE_ERRORS.TSModuleDeclaration,
);
}
if (names[moduleName]) {
namespaceTopLevel[i] = handleNested(path, t, subNode);
} else {
names[moduleName] = true;
namespaceTopLevel.splice(
i++,
1,
getDeclaration(t, moduleName),
handleNested(path, t, subNode),
);
}
continue;
}
case "TSEnumDeclaration":
case "FunctionDeclaration":
case "ClassDeclaration": {
const itemName = subNode.id.name;
if (itemName === name) {
throw path.hub.file.buildCodeFrameError(
subNode,
SHARED_NAMESPACE_ERRORS[subNode.type],
);
}
names[itemName] = true;
continue;
}
case "VariableDeclaration":
for (const variable of subNode.declarations) {
const variableName = variable.id.name;
if (variableName === name) {
throw path.hub.file.buildCodeFrameError(
variable,
SHARED_NAMESPACE_ERRORS.VariableDeclaration,
);
}
names[variableName] = true;
}
continue;
default:
continue;
case "ExportNamedDeclaration":
}
switch (subNode.declaration.type) {
case "TSEnumDeclaration":
case "FunctionDeclaration":
case "ClassDeclaration": {
const itemName = subNode.declaration.id.name;
if (itemName === name) {
throw path.hub.file.buildCodeFrameError(
subNode.declaration,
SHARED_NAMESPACE_ERRORS[subNode.declaration.type],
);
}
names[itemName] = true;
namespaceTopLevel.splice(
i++,
1,
subNode.declaration,
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(t.identifier(name), t.identifier(itemName)),
t.identifier(itemName),
),
),
);
break;
}
case "VariableDeclaration":
if (subNode.declaration.kind !== "const") {
throw path.hub.file.buildCodeFrameError(
subNode.declaration,
"Namespaces exporting non-const are unsupported.",
);
}
for (const variable of subNode.declaration.declarations) {
const variableName = variable.id.name;
if (variableName === name) {
throw path.hub.file.buildCodeFrameError(
variable,
SHARED_NAMESPACE_ERRORS.VariableDeclaration,
);
}
variable.init = t.assignmentExpression(
"=",
t.memberExpression(t.identifier(name), t.identifier(variableName)),
variable.init,
);
}
namespaceTopLevel[i] = subNode.declaration;
break;
case "TSModuleDeclaration": {
const moduleName = subNode.declaration.id.name;
if (moduleName === name) {
throw path.hub.file.buildCodeFrameError(
subNode.declaration,
SHARED_NAMESPACE_ERRORS.TSModuleDeclaration,
);
}
if (names[moduleName]) {
namespaceTopLevel[i] = handleNested(
path,
t,
subNode.declaration,
name,
);
} else {
names[moduleName] = true;
namespaceTopLevel.splice(
i++,
1,
getDeclaration(t, moduleName),
handleNested(path, t, subNode.declaration, name),
);
}
}
}
}
const derivedParameter = t.logicalExpression(
"||",
t.identifier(name),
t.assignmentExpression("=", t.identifier(name), t.objectExpression([])),
);
return t.expressionStatement(
t.callExpression(
t.functionExpression(
null,
[t.identifier(name)],
t.blockStatement(namespaceTopLevel),
),
[
parentExportName
? t.assignmentExpression(
"=",
t.memberExpression(
t.identifier(parentExportName),
t.identifier(name),
),
derivedParameter,
)
: derivedParameter,
],
),
);
}