-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
341 lines (290 loc) · 11.5 KB
/
index.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
'use strict';
const path = require('path');
const mapping = require('ember-rfc176-data');
function isIgnored(ignore, importPath, exportName) {
if (Array.isArray(ignore)) {
return ignore.indexOf(importPath) > -1;
} else {
let ignoredExports = ignore[importPath];
return ignoredExports && ignoredExports.indexOf(exportName) > -1;
}
}
function isDecorator(moduleName, importName) {
switch (moduleName) {
case '@ember/service':
return importName === 'inject';
case '@ember/controller':
return importName === 'inject';
case '@glimmer/tracking':
return importName === 'tracked';
case '@ember/object/compat':
return importName === 'dependentKeyCompat';
case '@ember/object':
return ['action', 'computed'].includes(importName);
case '@ember/object/computed':
// only the default import of this module is not a decorator
return importName !== 'default';
}
}
module.exports = function (babel) {
const t = babel.types;
const TSTypesRequiringModification = [
'TSAsExpression',
'TSTypeAssertion',
'TSNonNullExpression',
];
const isTypescriptNode = (node) =>
node.type.startsWith('TS') &&
!TSTypesRequiringModification.includes(node.type);
// Flips the ember-rfc176-data mapping into an 'import' indexed object, that exposes the
// default import as well as named imports, e.g. import {foo} from 'bar'
const reverseMapping = {};
mapping.forEach((exportDefinition) => {
const imported = exportDefinition.global;
const importRoot = exportDefinition.module;
let importName = exportDefinition.export;
if (!reverseMapping[importRoot]) {
reverseMapping[importRoot] = {};
}
reverseMapping[importRoot][importName] = imported;
});
function getMemberExpressionFor(global) {
let parts = global.split('.');
let object = parts.shift();
let property = parts.shift();
let memberExpression = t.MemberExpression(
t.identifier(object),
t.identifier(property)
);
while (parts.length > 0) {
let property = parts.shift();
memberExpression = t.MemberExpression(
memberExpression,
t.identifier(property)
);
}
return memberExpression;
}
return {
name: 'ember-modules-api-polyfill',
visitor: {
ImportDeclaration(path, state) {
let ignore = (state.opts && state.opts.ignore) || [];
let node = path.node;
let declarations = [];
let removals = [];
let specifiers = path.get('specifiers');
let importPath = node.source.value;
if (importPath === 'ember') {
// For `import Ember from 'ember'`, we can just remove the import
// and change `Ember` usage to to global Ember object.
let specifierPath = specifiers.find((specifierPath) => {
if (specifierPath.isImportDefaultSpecifier()) {
return true;
}
// TODO: Use the nice Babel way to throw
throw new Error(`Unexpected non-default import from 'ember'`);
});
if (specifierPath) {
let local = specifierPath.node.local;
if (local.name !== 'Ember') {
path.scope.rename(local.name, 'Ember');
}
removals.push(specifierPath);
} else {
// import 'ember';
path.remove();
}
}
// This is the mapping to use for the import statement
const mapping = reverseMapping[importPath];
// Only walk specifiers if this is a module we have a mapping for
if (mapping) {
// Iterate all the specifiers and attempt to locate their mapping
specifiers.forEach((specifierPath) => {
let specifier = specifierPath.node;
let importName;
// imported is the name of the module being imported, e.g. import foo from bar
const imported = specifier.imported;
// local is the name of the module in the current scope, this is usually the same
// as the imported value, unless the module is aliased
const local = specifier.local;
// We only care about these 2 specifiers
if (
specifier.type !== 'ImportDefaultSpecifier' &&
specifier.type !== 'ImportSpecifier'
) {
if (specifier.type === 'ImportNamespaceSpecifier') {
throw new Error(
`Using \`import * as ${specifier.local.name} from '${importPath}'\` is not supported.`
);
}
return;
}
// Determine the import name, either default or named
if (specifier.type === 'ImportDefaultSpecifier') {
importName = 'default';
} else {
importName = imported.name;
}
if (isIgnored(ignore, importPath, importName)) {
return;
}
// Extract the global mapping
const global = mapping[importName];
// Ensure the module being imported exists
if (!global) {
throw path.buildCodeFrameError(
`${importPath} does not have a ${importName} export`
);
}
removals.push(specifierPath);
if (
path.scope.bindings[local.name].referencePaths.find(
(rp) => rp.parent.type === 'ExportSpecifier'
)
) {
// not safe to use path.scope.rename directly
declarations.push(
t.variableDeclaration('var', [
t.variableDeclarator(
t.identifier(local.name),
t.identifier(global)
),
])
);
} else {
// Replace the occurences of the imported name with the global name.
let binding = path.scope.getBinding(local.name);
let referencePaths = binding.referencePaths;
if (isDecorator(importPath, importName)) {
// tldr; decorator paths are not always included in `path.scope.getBinding(local.name)`
//
// In some circumstances, decorators are not included in the
// reference paths for a local binding when the decorator
// identifier name is also defined _within_ the method being
// decorated. This is likely a bug in Babel, that should be
// reported and fixed.
//
// in order to fix that, we have to manually traverse to gather
// the decorator references **before** the
// @babel/plugin-proposal-decorators runs (because it removes
// them)
path.parentPath.traverse({
Decorator(decoratorPath) {
if (
decoratorPath.node.expression.type === 'Identifier' &&
decoratorPath.node.expression.name === local.name
) {
referencePaths.push(decoratorPath.get('expression'));
}
},
});
}
// Replace the occurrences of the imported name with the global name.
referencePaths.forEach((referencePath) => {
if (!isTypescriptNode(referencePath.parentPath)) {
const memberExpression = getMemberExpressionFor(global);
try {
referencePath.replaceWith(memberExpression);
} catch (e) {
referencePath.scope.crawl();
referencePath.replaceWith(memberExpression);
}
}
});
}
});
}
if (removals.length > 0 || mapping) {
if (removals.length === node.specifiers.length) {
path.replaceWithMultiple(declarations);
} else {
removals.forEach((specifierPath) => specifierPath.remove());
path.insertAfter(declarations);
}
}
},
ExportNamedDeclaration(path, state) {
let ignore = (state.opts && state.opts.ignore) || [];
let node = path.node;
if (!node.source) {
return;
}
let replacements = [];
let removals = [];
let specifiers = path.get('specifiers');
let importPath = node.source.value;
// This is the mapping to use for the import statement
const mapping = reverseMapping[importPath];
// Only walk specifiers if this is a module we have a mapping for
if (mapping) {
// Iterate all the specifiers and attempt to locate their mapping
specifiers.forEach((specifierPath) => {
let specifier = specifierPath.node;
// exported is the name of the module being export,
// e.g. `foo` in `export { computed as foo } from '@ember/object';`
const exported = specifier.exported;
// local is the original name of the module, this is usually the same
// as the exported value, unless the module is aliased
const local = specifier.local;
// We only care about the ExportSpecifier
if (specifier.type !== 'ExportSpecifier') {
return;
}
// Determine the import name, either default or named
let importName = local.name;
if (isIgnored(ignore, importPath, importName)) {
return;
}
// Extract the global mapping
const global = mapping[importName];
// Ensure the module being imported exists
if (!global) {
throw path.buildCodeFrameError(
`${importPath} does not have a ${importName} export`
);
}
removals.push(specifierPath);
let declaration;
const globalAsIdentifier = t.identifier(global);
if (exported.name === 'default') {
declaration = t.exportDefaultDeclaration(globalAsIdentifier);
} else {
// Replace the node with a new `var name = Ember.something`
declaration = t.exportNamedDeclaration(
t.variableDeclaration('var', [
t.variableDeclarator(exported, globalAsIdentifier),
]),
[],
null
);
}
replacements.push(declaration);
});
}
if (removals.length > 0 && removals.length === node.specifiers.length) {
path.replaceWithMultiple(replacements);
} else if (replacements.length > 0) {
removals.forEach((specifierPath) => specifierPath.remove());
path.insertAfter(replacements);
}
},
ExportAllDeclaration(path) {
let node = path.node;
let importPath = node.source.value;
// This is the mapping to use for the import statement
const mapping = reverseMapping[importPath];
// Only walk specifiers if this is a module we have a mapping for
if (mapping) {
throw path.buildCodeFrameError(
`Wildcard exports from ${importPath} are currently not possible`
);
}
},
},
};
};
// Provide the path to the package's base directory for caching with broccoli
// Ref: https://github.com/babel/broccoli-babel-transpiler#caching
module.exports.baseDir = () => path.resolve(__dirname, '..');