Skip to content

fix(completions): exclude default keyword from expression-body arrow function completions#63604

Open
tsushanth wants to merge 1 commit into
microsoft:mainfrom
tsushanth:fix/no-default-keyword-in-expression-body-completions
Open

fix(completions): exclude default keyword from expression-body arrow function completions#63604
tsushanth wants to merge 1 commit into
microsoft:mainfrom
tsushanth:fix/no-default-keyword-in-expression-body-completions

Conversation

@tsushanth

Copy link
Copy Markdown

Fixes #63463

Problem

default appeared as a keyword completion inside expression-bodied arrow functions:

(d, defaultIsAnInvalidParameterName) => d
//                                       ^ `default` offered here — invalid

Two existing fourslash tests already acknowledged this with TODO and "probably stop working" comments.

Root cause

getRelevantTokens sets contextToken to the => token (the preceding token before the identifier/keyword being typed). Inside tryGetFunctionLikeBodyCompletionContainer, findAncestor starts at contextToken.parent (the ArrowFunction) with prev = undefined, so the check prev === node.body never matches on the first iteration. The function returns undefined, and getGlobalCompletions falls back to KeywordCompletionFilters.All, which includes every isFunctionLikeBodyKeyword hit — and default passes that test because it is neither a contextual keyword nor a class-member keyword.

Fix

  1. Detect the expression-body => context directly in tryGetFunctionLikeBodyCompletionContainer: when contextToken is EqualsGreaterThanToken whose parent is an ArrowFunction with a non-block body, return that arrow function as the container.

  2. Add FunctionLikeExpressionBodyKeywords filter that delegates to isFunctionLikeBodyKeyword but excludes DefaultKeyword.

  3. In getGlobalCompletions, use the new filter when the detected container has a non-block body, so default is omitted only in expression-body contexts. Block bodies (including switch/default: inside functions) are unaffected.

  4. Update the two fourslash tests to assert excludes: ["default"] instead of including the TODO/known-bad expectation.

Tests

  • completionListAtEndOfWordInArrowFunction02.ts — updated, passes
  • completionListAtEndOfWordInArrowFunction03.ts — updated, passes
  • All 307 completionList* fourslash tests pass
  • completionListKeywords.ts (global scope default) continues to pass

…w function completions

`default` is not a valid expression so it should not appear in keyword
completions when completing inside an expression-bodied arrow function
(`(x) => <here>`). It was being included because:

1. `tryGetFunctionLikeBodyCompletionContainer` missed the expression-body
   case: `getRelevantTokens` sets `contextToken` to the `=>` token (the
   preceding token before the identifier/keyword being typed), so `prev`
   is `undefined` on the first `findAncestor` iteration and the check
   `prev === node.body` never matches.

2. Without a detected container, `getGlobalCompletions` fell back to
   `KeywordCompletionFilters.All`, which includes every
   `isFunctionLikeBodyKeyword` — and `default` passed that test because
   it is neither a contextual keyword nor a class-member keyword.

Fix:
- Detect the expression-body `=>` context directly in
  `tryGetFunctionLikeBodyCompletionContainer`: when `contextToken` is
  `EqualsGreaterThanToken` whose parent is an `ArrowFunction` with a
  non-block body, return that arrow function as the container.
- Add `FunctionLikeExpressionBodyKeywords` filter that delegates to
  `isFunctionLikeBodyKeyword` but excludes `DefaultKeyword`.
- In `getGlobalCompletions`, use the new filter when the detected
  container has a non-block body, so `default` is omitted only in
  expression contexts (block bodies inside switch statements are
  unaffected).
- Update the two fourslash tests that had TODOs acknowledging this bug.

Fixes microsoft#63463
Copilot AI review requested due to automatic review settings July 1, 2026 20:19
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Jul 1, 2026
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Jul 1, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a keyword-completion edge case in the language service where default was incorrectly suggested within expression-bodied arrow functions, by refining how the completion context is detected and applying a more appropriate keyword filter for concise arrow bodies.

Changes:

  • Teach tryGetFunctionLikeBodyCompletionContainer to recognize the => token context for expression-bodied arrows and treat the containing ArrowFunction as the completion container.
  • Add a new keyword filter (FunctionLikeExpressionBodyKeywords) that excludes default while keeping existing block-body behavior unchanged.
  • Update two fourslash tests to assert "default" is excluded (removing prior TODO/known-bad expectations).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/services/completions.ts Detect expression-bodied arrow => context and apply a new keyword filter that omits default only for concise bodies.
tests/cases/fourslash/completionListAtEndOfWordInArrowFunction02.ts Update completion assertions to exclude "default" in expression-bodied arrow completion context.
tests/cases/fourslash/completionListAtEndOfWordInArrowFunction03.ts Update completion assertions to exclude "default" (matching the corrected keyword filtering behavior).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

default keyword is offered in expression-bodied arrow function completions

2 participants