Skip to content

Commit

Permalink
Merge pull request #176 from ember-cli/make-ensure-import-lazy
Browse files Browse the repository at this point in the history
Make ensureImport lazy
  • Loading branch information
rwjblue authored Feb 22, 2021
2 parents 88b8048 + 6a9a9d7 commit 3b7873d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
2 changes: 1 addition & 1 deletion __tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ describe('options', () => {
it(`adds the ember import when used in sub-modules`, () => {
let input = `import Component from '@ember/component';export default class extends Component {}`;
let actual = transform(input, [[Plugin, { useEmberModule: true }]]);
let expected = `import _Ember from 'ember';\nexport default class extends _Ember.Component {}`;
let expected = `import _ember from 'ember';\nexport default class extends _ember.Component {}`;

expect(actual).toEqual(expected);
});
Expand Down
82 changes: 52 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,39 +90,64 @@ module.exports = function (babel) {
Program(path, state) {
let options = state.opts || {};
let useEmberModule = Boolean(options.useEmberModule);
let allAddedImports = {};

let preexistingEmberImportDeclaration = path
.get('body')
.filter((n) => n.type === 'ImportDeclaration')
.find((n) => n.get('source').get('value').node === 'ember');

if (
// an import was found
preexistingEmberImportDeclaration &&
// this accounts for `import from 'ember'` without a local identifier
preexistingEmberImportDeclaration.node.specifiers.length > 0
) {
state.emberIdentifier =
preexistingEmberImportDeclaration.node.specifiers[0].local;
}
state.ensureImport = (exportName, moduleName) => {
let addedImports = (allAddedImports[moduleName] =
allAddedImports[moduleName] || {});

state.ensureEmberImport = () => {
if (!useEmberModule) {
// ensures that we can always assume `state.emberIdentifier` is set
state.emberIdentifier = t.identifier('Ember');
return;
}
if (addedImports[exportName]) return addedImports[exportName];

if (state.emberIdentifier) return;
if (
exportName === 'default' &&
moduleName === 'ember' &&
!useEmberModule
) {
addedImports[exportName] = t.identifier('Ember');
return addedImports[exportName];
}

state.emberIdentifier = path.scope.generateUidIdentifier('Ember');
let importDeclarations = path
.get('body')
.filter((n) => n.type === 'ImportDeclaration');

let emberImport = t.importDeclaration(
[t.importDefaultSpecifier(state.emberIdentifier)],
t.stringLiteral('ember')
let preexistingImportDeclaration = importDeclarations.find(
(n) => n.get('source').get('value').node === moduleName
);

path.unshiftContainer('body', emberImport);
if (preexistingImportDeclaration) {
let importSpecifier = preexistingImportDeclaration
.get('specifiers')
.find(({ node }) => {
return exportName === 'default'
? t.isImportDefaultSpecifier(node)
: node.imported.name === exportName;
});

if (importSpecifier) {
addedImports[exportName] = importSpecifier.node.local;
}
}

if (!addedImports[exportName]) {
let uid = path.scope.generateUidIdentifier(
exportName === 'default' ? moduleName : exportName
);
addedImports[exportName] = uid;

let newImportSpecifier =
exportName === 'default'
? t.importDefaultSpecifier(uid)
: t.importSpecifier(uid, t.identifier(exportName));

let newImport = t.importDeclaration(
[newImportSpecifier],
t.stringLiteral(moduleName)
);
path.unshiftContainer('body', newImport);
}

return addedImports[exportName];
};
},

Expand Down Expand Up @@ -216,9 +241,6 @@ module.exports = function (babel) {

removals.push(specifierPath);

// ensure that the Ember global is imported if needed
state.ensureEmberImport();

if (
path.scope.bindings[local.name].referencePaths.find(
(rp) => rp.parent.type === 'ExportSpecifier'
Expand Down Expand Up @@ -268,7 +290,7 @@ module.exports = function (babel) {
if (!isTypescriptNode(referencePath.parentPath)) {
const memberExpression = getMemberExpressionFor(
global,
state.emberIdentifier
state.ensureImport('default', 'ember')
);

try {
Expand Down

0 comments on commit 3b7873d

Please sign in to comment.