Skip to content

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

Merged
merged 3 commits into from
Jul 14, 2021

Conversation

gabritto
Copy link
Member

Fixes #44267

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Jul 14, 2021
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)) {
Copy link
Member Author

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();
Copy link
Member Author

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 };

@gabritto gabritto changed the title Gabritto/issue44267 Fix namespace name conflict detection in "Convert named imports to namespace import" action Jul 14, 2021
Copy link
Member

@andrewbranch andrewbranch left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Convert named imports to namespace import" adds redundant underscore suffix
3 participants