Skip to content

Fix codefix-triggered auto import of aliased exports #60260

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

andrewbranch
Copy link
Member

Fixes #59583

@andrewbranch andrewbranch requested a review from iisaduan October 17, 2024 19:51
@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Oct 17, 2024
Copy link
Member

@iisaduan iisaduan left a comment

Choose a reason for hiding this comment

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

Do we need an extra importadder function? It seems like this could be done by doing the tryGetAutoImportableReference... call/logic from within addImportFromExportedSymbol (or an update to exportinfomap?). The functions seem too similar to justify a new one, this function one skips a lot of the resolving in the existing function, and any other codefix that tries to import Alias from a symbol reference still crashes after this PR.

(I also think this is an issue with export aliases in general, not just types)

* @internal
* Information about an `import("...").SomeType` replaced by an auto-importable type reference.
*/
export interface ImportTypeReplacement {
Copy link
Member

Choose a reason for hiding this comment

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

"replacement" is a little confusing to me--does this refer to export aliases in general?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it refers to the fact that an ImportType has been replaced by a TypeReference plus a top-level import.

@andrewbranch
Copy link
Member Author

@iisaduan what’s happening here is that the add-missing-members codefix generates code like

someMethod(p: import("./one").SomeType, q: import("./two").default) {}

which first gets transformed into

someMethod(p: SomeType, q: BetterNameForDefault) {}

and then we ask to auto-import the symbols for those types. But we were only passing the symbols in, which means we were losing important pieces of information:

  1. The imports we generate must expose SomeType and BetterNameForDefault as the resulting identifiers.
  2. The export names we should be looking to match are SomeType and default.
  3. The symbols must be types, so are valid to be imported as type-only
  4. The file we’re importing into is TypeScript

addImportFromExportedSymbol doesn’t receive any of that information as parameters and does a bunch of work to determine it, and wasn’t always coming up with the right result. I first tried to expand the existing signature, but it was getting unwieldy and felt like it was trying to stretch it too far. But you’re right; extracting it out meant I didn’t fix a similar crash from occurring in the addImportFromExportedSymbol code path 😕

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

Successfully merging this pull request may close these issues.

autoimports crashes with aliased export
4 participants