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

Move to file: fix detection of references to globals that shouldn't be moved #60450

Merged
merged 10 commits into from
Nov 15, 2024

Conversation

andrewbranch
Copy link
Member

Fixes #59799
Fixes #60408
Closes #60173
Closes #60410

isGlobalType was a red herring; it was implemented incorrectly but it never mattered if something was a global type. What we actually care about is whether the thing we’re moving was merging with declarations in other files. If we check that after ruling out that it was in an import, then it must be a global, and if the thing has a global definition in another file, we can’t start exporting it without breaking that merge:

// This merges with the global in lib.d.ts
interface String {
  reverse(): string;
}

// This is a reference to the value meaning of the same global in lib.d.ts
String.prototype.reverse = function (): string {
    return this.split("").reverse().join("");
}

Why don’t we want to export interface String when we move the prototype method definition here?

  • Not because String.prototype... refers to a value while interface String refers to a type—this feels important, but they’re actually two different meanings of the same symbol!
  • Not because String is a global type—if we had instead written interface Foo {} and moved the code type Bar = Foo, it would be true that Foo is a global type, but we make an assumption that most people want modules most of the time, and it’s probably a coincidence that they hadn’t made this file a module yet. In this case, we would actually start exporting interface Foo {}, converting it from a global type to an exported type. (Also, the same argument can be made for values, so being a type has nothing to do with it.)
  • It’s because we can’t convert String from a global to an export by making modifications in this file alone—there are other definitions of String in other files, which increases our confidence that it’s actually intended to be a global.

@typescript-bot typescript-bot added For Milestone Bug PRs that fix a bug with a specific milestone labels Nov 7, 2024
@andrewbranch andrewbranch changed the title Bug/59799 Move to file: fix detection of references to globals that shouldn't be moved Nov 7, 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.

👍

@andrewbranch andrewbranch merged commit b3c67d3 into microsoft:main Nov 15, 2024
30 checks passed
@andrewbranch
Copy link
Member Author

@typescript-bot cherry-pick to release-5.7

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 15, 2024

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
cherry-pick to release-5.7 ✅ Started ✅ Results

@andrewbranch andrewbranch deleted the bug/59799 branch November 15, 2024 22:21
typescript-bot pushed a commit that referenced this pull request Nov 15, 2024
…e moved (#60450)

Co-authored-by: Isabel Duan <isabelduan@microsoft.com>
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
@typescript-bot
Copy link
Collaborator

Hey, @andrewbranch! I've created #60513 for you.

DanielRosenwasser pushed a commit that referenced this pull request Nov 18, 2024
…e-5.7 (#60513)

Co-authored-by: Andrew Branch <andrewbranch@users.noreply.github.com>
Co-authored-by: Isabel Duan <isabelduan@microsoft.com>
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
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: Done
4 participants