Skip to content

Commit

Permalink
Merge pull request #51 from emberjs/remove-extra-import-from@ember-te…
Browse files Browse the repository at this point in the history
…mplate-compiler

Remove extraneous import from `@ember/template-compiler`
  • Loading branch information
ef4 authored May 8, 2024
2 parents 010bec7 + 587cc90 commit e043d54
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 7 deletions.
60 changes: 60 additions & 0 deletions __tests__/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,66 @@ describe('htmlbars-inline-precompile', function () {
`);
});

it('cleans up leftover imports when there is more than one template', function () {
plugins = [
[
HTMLBarsInlinePrecompile,
{
targetFormat: 'hbs',
},
],
];
let code = `
import { template } from "@ember/template-compiler";
import Component from '@glimmer/component';
export default class Test extends Component {
foo = 1;
static{
template("<Icon />", {
component: this,
eval () {
return eval(arguments[0]);
}
});
}
}
const Icon = template("Icon", {
eval () {
return eval(arguments[0]);
}
});
`;

let transformed = transform(code);

expect(transformed).toEqualCode(`
import Component from "@glimmer/component";
import { precompileTemplate } from "@ember/template-compilation";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
export default class Test extends Component {
foo = 1;
static {
setComponentTemplate(
precompileTemplate("<Icon />", {
strictMode: true,
scope: () => ({
Icon,
}),
}),
this
);
}
}
const Icon = setComponentTemplate(
precompileTemplate("Icon", {
strictMode: true,
}),
templateOnly()
);
`);
});

it("respects user's strict option on template()", function () {
plugins = [
[
Expand Down
21 changes: 14 additions & 7 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,6 @@ function updateCallForm<EnvSpecificOptions>(
//
target = target.get('arguments.0') as NodePath<t.CallExpression>;
}

// We deliberately do updateScope at the end so that when it updates
// references, those references will point to the accurate paths in the
// final AST.
Expand Down Expand Up @@ -764,18 +763,26 @@ function maybePruneImport(
return;
}
let binding = identifier.scope.getBinding(identifier.node.name);
// this checks if the identifier (that we're about to remove) is used in
// exactly one place.
if (
binding?.referencePaths.reduce((count, path) => (path.removed ? count : count + 1), 0) === 1
) {

if (!binding) {
return;
}

let found = binding.referencePaths.find((path) => path.node === identifier.node);
if (!found) {
return;
}

binding.referencePaths.splice(binding.referencePaths.indexOf(found), 1);
binding.references--;

if (binding.references === 0) {
let specifier = binding.path;
if (specifier.isImportSpecifier()) {
let declaration = specifier.parentPath as NodePath<t.ImportDeclaration>;
util.removeImport(declaration.node.source.value, name(specifier.node.imported));
}
}
identifier.removed = true;
}

function precompileTemplate(i: Importer) {
Expand Down

0 comments on commit e043d54

Please sign in to comment.