Skip to content

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.

@gabritto gabritto merged commit 3358f13 into main Jul 14, 2021
@gabritto gabritto deleted the gabritto/issue44267 branch July 14, 2021 18:07
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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

4 participants