-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Include all type parameters in completions within type parameters' constraints #56543
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
iisaduan
merged 6 commits into
microsoft:main
from
Andarist:fix/completions-between-type-params
Sep 9, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
52d24de
Include all type parameters in completions within type parameters' co…
Andarist 9412d6e
Merge remote-tracking branch 'origin/main' into fix/completions-betwe…
Andarist 756569c
filter out directly self-recursive type parameters in their constraints
Andarist 4efc823
use a better strategy
Andarist 70a7b15
fixed `isInTypeParameterDefault`
Andarist 4b174f4
fixed test case
Andarist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2666,7 +2666,7 @@ export function getCompletionEntriesFromSymbols( | |
includeSymbol = false, | ||
): UniqueNameSet { | ||
const start = timestamp(); | ||
const variableOrParameterDeclaration = getVariableOrParameterDeclaration(contextToken, location); | ||
const closestSymbolDeclaration = getClosestSymbolDeclaration(contextToken, location); | ||
const useSemicolons = probablyUsesSemicolons(sourceFile); | ||
const typeChecker = program.getTypeChecker(); | ||
// Tracks unique names. | ||
|
@@ -2745,26 +2745,33 @@ export function getCompletionEntriesFromSymbols( | |
} | ||
// Filter out variables from their own initializers | ||
// `const a = /* no 'a' here */` | ||
if (tryCast(variableOrParameterDeclaration, isVariableDeclaration) && symbol.valueDeclaration === variableOrParameterDeclaration) { | ||
if (tryCast(closestSymbolDeclaration, isVariableDeclaration) && symbol.valueDeclaration === closestSymbolDeclaration) { | ||
return false; | ||
} | ||
|
||
// Filter out parameters from their own initializers | ||
// Filter out current and latter parameters from defaults | ||
// `function f(a = /* no 'a' and 'b' here */, b) { }` or | ||
// `function f<T = /* no 'T' here */>(a: T) { }` | ||
// `function f<T = /* no 'T' and 'T2' here */>(a: T, b: T2) { }` | ||
const symbolDeclaration = symbol.valueDeclaration ?? symbol.declarations?.[0]; | ||
if ( | ||
variableOrParameterDeclaration && symbolDeclaration && ( | ||
(isTypeParameterDeclaration(variableOrParameterDeclaration) && isTypeParameterDeclaration(symbolDeclaration)) || | ||
(isParameter(variableOrParameterDeclaration) && isParameter(symbolDeclaration)) | ||
) | ||
) { | ||
const symbolDeclarationPos = symbolDeclaration.pos; | ||
const parameters = isParameter(variableOrParameterDeclaration) ? variableOrParameterDeclaration.parent.parameters : | ||
isInferTypeNode(variableOrParameterDeclaration.parent) ? undefined : | ||
variableOrParameterDeclaration.parent.typeParameters; | ||
if (symbolDeclarationPos >= variableOrParameterDeclaration.pos && parameters && symbolDeclarationPos < parameters.end) { | ||
return false; | ||
if (closestSymbolDeclaration && symbolDeclaration) { | ||
if (isParameter(closestSymbolDeclaration) && isParameter(symbolDeclaration)) { | ||
const parameters = closestSymbolDeclaration.parent.parameters; | ||
if (symbolDeclaration.pos >= closestSymbolDeclaration.pos && symbolDeclaration.pos < parameters.end) { | ||
return false; | ||
} | ||
} | ||
else if (isTypeParameterDeclaration(closestSymbolDeclaration) && isTypeParameterDeclaration(symbolDeclaration)) { | ||
if (closestSymbolDeclaration === symbolDeclaration && contextToken?.kind === SyntaxKind.ExtendsKeyword) { | ||
// filter out the directly self-recursive type parameters | ||
// `type A<K extends /* no 'K' here*/> = K` | ||
return false; | ||
} | ||
if (isInTypeParameterDefault(contextToken) && !isInferTypeNode(closestSymbolDeclaration.parent)) { | ||
const typeParameters = closestSymbolDeclaration.parent.typeParameters; | ||
if (typeParameters && symbolDeclaration.pos >= closestSymbolDeclaration.pos && symbolDeclaration.pos < typeParameters.end) { | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
@@ -6001,20 +6008,39 @@ function isModuleSpecifierMissingOrEmpty(specifier: ModuleReference | Expression | |
return !tryCast(isExternalModuleReference(specifier) ? specifier.expression : specifier, isStringLiteralLike)?.text; | ||
} | ||
|
||
function getVariableOrParameterDeclaration(contextToken: Node | undefined, location: Node) { | ||
function getClosestSymbolDeclaration(contextToken: Node | undefined, location: Node) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since |
||
if (!contextToken) return; | ||
|
||
const possiblyParameterDeclaration = findAncestor(contextToken, node => | ||
let closestDeclaration = findAncestor(contextToken, node => | ||
isFunctionBlock(node) || isArrowFunctionBody(node) || isBindingPattern(node) | ||
? "quit" | ||
: ((isParameter(node) || isTypeParameterDeclaration(node)) && !isIndexSignatureDeclaration(node.parent))); | ||
|
||
const possiblyVariableDeclaration = findAncestor(location, node => | ||
isFunctionBlock(node) || isArrowFunctionBody(node) || isBindingPattern(node) | ||
? "quit" | ||
: isVariableDeclaration(node)); | ||
if (!closestDeclaration) { | ||
closestDeclaration = findAncestor(location, node => | ||
isFunctionBlock(node) || isArrowFunctionBody(node) || isBindingPattern(node) | ||
? "quit" | ||
: isVariableDeclaration(node)); | ||
} | ||
return closestDeclaration as ParameterDeclaration | TypeParameterDeclaration | VariableDeclaration | undefined; | ||
} | ||
|
||
function isInTypeParameterDefault(contextToken: Node | undefined) { | ||
if (!contextToken) { | ||
return false; | ||
} | ||
|
||
return (possiblyParameterDeclaration || possiblyVariableDeclaration) as ParameterDeclaration | TypeParameterDeclaration | VariableDeclaration | undefined; | ||
let node = contextToken; | ||
let parent = contextToken.parent; | ||
while (parent) { | ||
if (isTypeParameterDeclaration(parent)) { | ||
return parent.default === node || node.kind === SyntaxKind.EqualsToken; | ||
} | ||
node = parent; | ||
parent = parent.parent; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function isArrowFunctionBody(node: Node) { | ||
|
17 changes: 17 additions & 0 deletions
17
tests/cases/fourslash/completionsForLatterTypeParametersInConstraints1.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// // https://github.com/microsoft/TypeScript/issues/56474 | ||
//// function test<First extends S/*1*/, Second>(a: First, b: Second) {} | ||
//// type A1<K extends /*2*/, L> = K | ||
|
||
verify.completions({ | ||
marker: ["1"], | ||
includes: ["Second"], | ||
excludes: ["First"], | ||
}); | ||
|
||
verify.completions({ | ||
marker: ["2"], | ||
includes: ["L"], | ||
excludes: ["K"], | ||
}); |
15 changes: 15 additions & 0 deletions
15
tests/cases/fourslash/completionsForSelfTypeParameterInConstraint1.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// type StateMachine<Config> = { | ||
//// initial?: "states" extends keyof Config ? keyof Config["states"] : never; | ||
//// states?: Record<string, {}>; | ||
//// }; | ||
|
||
//// declare function createMachine<Config extends StateMachine</*1*/>>( | ||
//// config: Config, | ||
//// ): void; | ||
|
||
verify.completions({ | ||
marker: ["1"], | ||
includes: ["Config"], | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The only real change here is the addition of
isInTypeParameterDefault
check. I decided to refactor this slightly though as I find handling theparameters
andtypeParameters
cases through those separate (although similar) code paths cleaner/more readable