-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Generate names for type parameter declarations in inferred types #23902
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
Generate names for type parameter declarations in inferred types #23902
Conversation
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.
Should get another review though since I'm not very familiar with the emitter
@@ -3031,7 +3031,7 @@ namespace ts { | |||
// Options | |||
NoTruncation = 1 << 0, // Don't truncate result | |||
WriteArrayAsGenericType = 1 << 1, // Write Array<T> instead T[] | |||
// empty space | |||
GenerateNamesForShadowedTypeParams = 1 << 2, // When a type parameter T is shadowing another T, generate a name for it so it can still be referenced |
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.
Why does this need a flag?
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.
We don't wanna bother renaming the conflicts in quickinfo and signature help, at least in my opinion.
@@ -3444,6 +3445,7 @@ declare namespace ts { | |||
function createFileLevelUniqueName(text: string): Identifier; | |||
/** Create a unique name generated for a node. */ | |||
function getGeneratedNameForNode(node: Node): Identifier; | |||
function getOptimisticScopedGeneratedNameForName(node: Identifier): Identifier; |
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.
Does this need to be public?
if (optimistic) { | ||
if (checkFn(baseName)) { | ||
generatedNames.set(baseName, true); | ||
if (scoped) { |
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.
Anything that goes in generatedNames
is already reserved throughout the entire generated output, so scoping these doesn't seem to make any sense. Why was this needed?
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.
scoped
causes it to go into reservedNames
instead of generatedNames
; this allows, eg, T_1
to be reused in adjacent scopes.
src/compiler/factory.ts
Outdated
@@ -197,6 +197,15 @@ namespace ts { | |||
return name; | |||
} | |||
|
|||
export function getOptimisticScopedGeneratedNameForName(node: Identifier): Identifier { |
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.
Do we need a special factory function for this? Couldn't we just add the extra flags to autoGenerateflags
after calling one of the existing factory functions?
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.
Probably? But I'd also probably still write that into a function so it stays as an inline call as the use site.
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.
I'd rather we just change the signature of getGeneratedNameForNode
to something like:
export function getGeneratedNameForNode(node: Node, flags?: GeneratedIdentifierFlags): Identifier
Also, the existing getGeneratedNameForNode
actually defers reading the text of the node until emit. This is important so you could correctly handle a case where the node you are generating a name from is itself a generated name. What happens if you need to end up with <T>(x: T) => <T_1>(y: T_1) => T_1_1
?
src/compiler/factory.ts
Outdated
@@ -197,6 +197,15 @@ namespace ts { | |||
return name; | |||
} | |||
|
|||
export function getOptimisticScopedGeneratedNameForName(node: Identifier): Identifier { |
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.
I'd rather we just change the signature of getGeneratedNameForNode
to something like:
export function getGeneratedNameForNode(node: Node, flags?: GeneratedIdentifierFlags): Identifier
Also, the existing getGeneratedNameForNode
actually defers reading the text of the node until emit. This is important so you could correctly handle a case where the node you are generating a name from is itself a generated name. What happens if you need to end up with <T>(x: T) => <T_1>(y: T_1) => T_1_1
?
@rbuckton Do you wanna update your status and give more comments? |
Fixes #16313