Skip to content

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 49 additions & 23 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Copy link
Contributor Author

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 the parameters and typeParameters cases through those separate (although similar) code paths cleaner/more readable

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;
}
}
}
}

Expand Down Expand Up @@ -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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

since Parameter has a specific meaning and commonly doesn't include TypeParameter I decided to rename this (as this function might return both of those types)

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) {
Expand Down
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"],
});
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"],
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
//// const f5 = (a, b = (c = /*7*/, e) => { }, d = b) => { }
////
//// type A1<K = /*T1*/, L> = K
//// type A2<K extends /*T2*/, L> = K

verify.completions({
marker: ["1", "2"],
Expand Down Expand Up @@ -42,6 +41,6 @@ verify.completions({
})

verify.completions({
marker: ["T1", "T2"],
marker: ["T1"],
excludes: ["K", "L"],
})