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

fix: handle star import name collisions in module concatenation #11751

Merged
merged 2 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/optimize/ConcatenatedModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ class ConcatenatedModule extends Module {
const name = reference.identifier.name;
if (ConcatenationScope.isModuleReference(name)) {
const match = ConcatenationScope.matchModuleReference(name);
if (!match || match.ids.length < 1) continue;
Copy link

Choose a reason for hiding this comment

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

0a8529f reverts the changes in ConcatenatedModule.js from 95eabe4
but im curious why this change fixes the initial bug?

Copy link
Member

Choose a reason for hiding this comment

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

findNewName should already return a name that avoids collisions with other variables. But the actual bug was that in some cases potential collisions were not detected. This happened in the case when ids = [], which is the case for namespace objects or the externals (which are also treated as namespace objects).

The above snippet fixes the problem.

if (!match) continue;
const binding = getFinalBinding(
moduleGraph,
modulesWithInfo[match.index],
Expand Down
5 changes: 5 additions & 0 deletions test/cases/optimize/concat-star-import/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { foo } from "./module";

it("should handle star import with name collision", () => {
expect(foo()).toBe("1 21 2");
});
7 changes: 7 additions & 0 deletions test/cases/optimize/concat-star-import/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as c from "cmodule";

export function foo() {
// variable name matches the imported package name
const cmodule = c([1, 2]);
return cmodule + cmodule;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.