-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Fix namespace name conflict detection in "Convert named imports to namespace import" action #45019
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
Conversation
if (!neededNamedImports.some(n => n.name === element.name)) { | ||
neededNamedImports.push(factory.createImportSpecifier(element.propertyName && factory.createIdentifier(element.propertyName.text), factory.createIdentifier(element.name.text))); | ||
} | ||
else if (isExportSpecifier(id.parent)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old code disregarded named export clauses that aliased the reference to the named import, so code such as
import { a } from "m";
export { a as b };
was in fact incorrectly refactored into
import { a } from "m";
export { m.a as b };
const neededNamedImports: ImportSpecifier[] = []; | ||
// Imports that need to be kept as named imports in the refactored code, to avoid changing the semantics. | ||
// More specifically, those are named imports that appear in named exports in the original code, e.g. `a` in `import { a } from "m"; export { a }`. | ||
const neededNamedImports: Set<ImportSpecifier> = new Set(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to use a set instead of an array. The old code used the array as a set by comparing the named imports' name
properties (of type Identifiers
) (element.name
in for loop below), but it pushed into the array a node with a newly-created Identifier
as name
, so in fact this new node's name
would never be equal to the named imports' name
, and therefore the same named import could be repeated, as in:
import { a, b } from "m";
export { b };
export { b as c };
would become
import * as m from "m";
import { b, b } from "m"; // Duplicate b import
export { b };
export { b as c };
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🌟 Thanks for the great code comments! Made it really easy to understand.
Fixes #44267