Skip to content

Commit

Permalink
🤖 Pick PR #54656 (fix: no variable suggestions withou...) into releas…
Browse files Browse the repository at this point in the history
…e-5.1 (#54782)

Co-authored-by: Vitaly Turovsky <[email protected]>
  • Loading branch information
TypeScript Bot and zardoy authored Jun 26, 2023
1 parent e6ceba0 commit 2d031ce
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2394,7 +2394,7 @@ export function getCompletionEntriesFromSymbols(
includeSymbol = false
): UniqueNameSet {
const start = timestamp();
const variableOrParameterDeclaration = getVariableOrParameterDeclaration(contextToken);
const variableOrParameterDeclaration = getVariableOrParameterDeclaration(contextToken, location);
const useSemicolons = probablyUsesSemicolons(sourceFile);
const typeChecker = program.getTypeChecker();
// Tracks unique names.
Expand Down Expand Up @@ -5464,14 +5464,20 @@ function isModuleSpecifierMissingOrEmpty(specifier: ModuleReference | Expression
return !tryCast(isExternalModuleReference(specifier) ? specifier.expression : specifier, isStringLiteralLike)?.text;
}

function getVariableOrParameterDeclaration(contextToken: Node | undefined) {
function getVariableOrParameterDeclaration(contextToken: Node | undefined, location: Node) {
if (!contextToken) return;

const declaration = findAncestor(contextToken, node =>
const possiblyParameterDeclaration = findAncestor(contextToken, node =>
isFunctionBlock(node) || isArrowFunctionBody(node) || isBindingPattern(node)
? "quit"
: isVariableDeclaration(node) || ((isParameter(node) || isTypeParameterDeclaration(node)) && !isIndexSignatureDeclaration(node.parent)));
return declaration as ParameterDeclaration | TypeParameterDeclaration | VariableDeclaration | undefined;
: ((isParameter(node) || isTypeParameterDeclaration(node)) && !isIndexSignatureDeclaration(node.parent)));

const possiblyVariableDeclaration = findAncestor(location, node =>
isFunctionBlock(node) || isArrowFunctionBody(node) || isBindingPattern(node)
? "quit"
: isVariableDeclaration(node));

return (possiblyParameterDeclaration || possiblyVariableDeclaration) as ParameterDeclaration | TypeParameterDeclaration | VariableDeclaration | undefined;
}

function isArrowFunctionBody(node: Node) {
Expand Down
10 changes: 8 additions & 2 deletions tests/cases/fourslash/completionEntryForConst.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
///<reference path="fourslash.ts" />

////const c = "s";
/////**/
/////*1*/
////const d = 1
////d/*2*/
////const e = 1
/////*3*/

verify.completions({ marker: "", includes: { name: "c", text: 'const c: "s"', kind: "const" } });
verify.completions({ marker: ["1"], includes: { name: "c", text: 'const c: "s"', kind: "const" } });
verify.completions({ marker: ["2"], includes: { name: "d", text: 'const d: 1', kind: "const" } });
verify.completions({ marker: ["3"], includes: { name: "e", text: 'const e: 1', kind: "const" } });

0 comments on commit 2d031ce

Please sign in to comment.