Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove extraneous import from @ember/template-compiler #51

Merged
merged 5 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 polyfilling rfc931 with hbs target', 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
10 changes: 10 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,13 +629,23 @@ function updateCallForm<EnvSpecificOptions>(
),
])
);

// we just wrapped the target callExpression in the call to
// setComponentTemplate. Adjust `target` back to point at the
// precompileTemplate call for the final updateScope below.
//
target = target.get('arguments.0') as NodePath<t.CallExpression>;
}

// Changing imports/identifiers doesn't automatically update the scope.
//
// NOTE: https://github.com/babel/babel/issues/7974
//
// We need to refresh ourselves
// This is expensive, but required so that maybePruneImport
// has a chance to prune imports when their specifiers are no longer used.
state.program.scope.crawl();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so the previous release just cleaned up all our need for scope.crawl. It appears that this one place was not covered.

The fix for this will go into babel-import-util. Using util.removeImport is supposed to correct the scope references.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking closer I didn't explain correctly in my comment above, but the basic point is still true: we don't need scope.crawl when we're the ones directly responsible for removing the reference. We should be updating the scope at that point so it stays correct.


// 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
Loading