Skip to content

Fix local function variables being suggested in parameter defaults #61926

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

Closed
wants to merge 6 commits into from
Closed
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
19 changes: 10 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"fast-xml-parser": "^4.5.2",
"glob": "^10.4.5",
"globals": "^15.15.0",
"hereby": "^1.10.0",
"hereby": "^1.11.0",
"jsonc-parser": "^3.3.1",
"knip": "^5.44.4",
"minimist": "^1.2.8",
Expand Down
55 changes: 44 additions & 11 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2805,12 +2805,25 @@ export function getCompletionEntriesFromSymbols(
return !!(allFlags & SymbolFlags.Namespace);
}

if (isTypeOnlyLocation) {
// It's a type, but you can reach it by namespace.type as well
return symbolCanBeReferencedAtTypeLocation(symbol, typeChecker);
}

// expressions are value space (which includes the value namespaces)
if (isTypeOnlyLocation) {
// It's a type, but you can reach it by namespace.type as well
return symbolCanBeReferencedAtTypeLocation(symbol, typeChecker);
}

// Filter out function body variables from parameter defaults
// `function f(a = /* no function body variables here */) { var x; }`
if (closestSymbolDeclaration && isParameter(closestSymbolDeclaration) && symbolDeclaration && isInParameterDefault(contextToken)) {
const functionNode = closestSymbolDeclaration.parent;
if (isFunctionLike(functionNode) && "body" in functionNode) {
const functionBody = functionNode.body;
if (functionBody && symbolDeclaration.pos > functionBody.pos && symbolDeclaration.pos < functionBody.end) {
// This symbol is declared inside the function body, exclude it
return false;
}
}
}

// expressions are value space (which includes the value namespaces)
return !!(allFlags & SymbolFlags.Value);
}

Expand Down Expand Up @@ -4037,8 +4050,8 @@ function getCompletionData(
symbolToOriginInfoMap[i] = origin;
}
}
}
}

// Need to insert 'this.' before properties of `this` type, so only do that if `includeInsertTextCompletions`
if (preferences.includeCompletionsWithInsertText && scopeNode.kind !== SyntaxKind.SourceFile) {
const thisType = typeChecker.tryGetThisTypeAt(scopeNode, /*includeGlobalThis*/ false, isClassLike(scopeNode.parent) ? scopeNode as ThisContainer : undefined);
Expand Down Expand Up @@ -6044,9 +6057,29 @@ function isInTypeParameterDefault(contextToken: Node | undefined) {
parent = parent.parent;
}

return false;
}

return false;
}

function isInParameterDefault(contextToken: Node | undefined) {
if (!contextToken) {
return false;
}

let node = contextToken;
while (node) {
if (isParameter(node.parent)) {
const param = node.parent;
// Check if we're in the initializer part after the equals sign
return param.initializer === node ||
(param.initializer && node.pos >= param.initializer.pos && node.pos < param.initializer.end);
}
node = node.parent;
if (!node) break;
}

return false;
}

function isArrowFunctionBody(node: Node) {
return node.parent && isArrowFunction(node.parent) &&
(node.parent.body === node ||
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/fourslash/noCompletionsForLocalVariablesInDefaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />

//// function test1(a: string, b = /*marker*/) {
//// var hoisted
//// let mutable
//// const readonly = 1
//// }

// Check that parameter 'a' is included but local variables are excluded
verify.completions({
marker: "marker",
includes: ["a"],
excludes: ["hoisted", "mutable", "readonly"],
});