Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Commit

Permalink
[BUGFIX] Use a unique identifier for each reference
Browse files Browse the repository at this point in the history
Currently we reuse the same identifier for each reference to an imported
value. Apparently this is not the way the scope system is meant to work,
and instead we should be creating a new id node for each reference.
  • Loading branch information
Chris Garrett committed Mar 17, 2021
1 parent e2ba193 commit 01ff127
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
24 changes: 24 additions & 0 deletions __tests__/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,30 @@ describe('htmlbars-inline-precompile', function () {
`);
});

it('works properly when used along with modules transform multiple times', function () {
plugins.push([TransformModules]);
let transformed = transform(
"import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;\nvar otherCompiled = hbs`hello`;"
);

expect(transformed).toMatchInlineSnapshot(`
"define([\\"@ember/template-factory\\"], function (_templateFactory) {
\\"use strict\\";
var compiled = (0, _templateFactory.createTemplateFactory)(
/*
hello
*/
\\"precompiled(hello)\\");
var otherCompiled = (0, _templateFactory.createTemplateFactory)(
/*
hello
*/
\\"precompiled(hello)\\");
});"
`);
});

it('works properly when used after modules transform', function () {
plugins.unshift([TransformModules]);
let transformed = transform(
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ module.exports = function (babel) {
let addedImports = (state.allAddedImports[moduleName] =
state.allAddedImports[moduleName] || {});

if (addedImports[exportName]) return addedImports[exportName].id;
if (addedImports[exportName]) return t.identifier(addedImports[exportName].id.name);

if (moduleOverrides) {
let glimmerModule = moduleOverrides[moduleName];
Expand All @@ -205,6 +205,7 @@ module.exports = function (babel) {

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

return addedImports[exportName].id;
}

Expand All @@ -218,7 +219,7 @@ module.exports = function (babel) {
let importSpecifier = preexistingImportDeclaration.get('specifiers').find(({ node }) => {
return exportName === 'default'
? t.isImportDefaultSpecifier(node)
: node.imported.name === exportName;
: node.imported && node.imported.name === exportName;
});

if (importSpecifier) {
Expand Down Expand Up @@ -246,7 +247,7 @@ module.exports = function (babel) {
};
}

return addedImports[exportName].id;
return t.identifier(addedImports[exportName].id.name);
};

// Setup other module options and create cache for values
Expand Down

0 comments on commit 01ff127

Please sign in to comment.