TypeScript: Smart select does not select TypeScript object type

  • VSCode Version: latest
  • OS Version: macOS latest

Steps to Reproduce:

  1. Paste this into a TypeScript file
    type Foo = {
      /** comment */
      status: number;
    };
    
  2. Place cursor just before status
  3. Run command “expand selection” 4 times

Expected: only the object should be selected:

image

Actual: the whole type is selected:

image

gif

Does this issue occur when all extensions are disabled?: Yes

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 17 (10 by maintainers)

Commits related to this issue

Most upvoted comments

@saigeethakandepallicherukuru please go ahead!

Everyone else: Sorry to have not responded before, but No Assignee (up at the top on the right) means no TS team member is working on the bug and it’s up for grabs. Unless there’s already an open PR, it’s a pretty safe bet that no one else is working on any particular bug.

Yep, that confirms the problem is ours. All that code is here. If you add a new test copying the format of this one, you should be able to debug that code and see what’s going on:

I tried to trace the problem on the VS Code side and found some clues that the problem lies within the TS language server.

Problematic case

This is the input code block that I used:

test ts

I put the caret at the beginning of the status field (position 5:3) and invoked the Expand Selection command. This is the (flattened) tree of ranges and their parents returned from the TypeScript language server (for any range its parent sits at the next line):

5:3 >> 5:9
4:3 >> 5:9
5:3 >> 5:18 (*)
4:3 >> 5:18
3:13 >> 6:1
3:12 >> 6:2
3:1 >> 6:3
1:1 >> 8:15

VS Code editor expects parents to include their child, but this is broken at the third line (here indicated by the *). VS Code’s behavior is to drop all ranges due to this inconsistency.

Healthy case

I also tried this with the healthy code sample below:

code ts-ok

Again, I put the caret at the beginning of the status field (position 4:3) and invoked the Expand Selection command. This is the array of resulting ranges, which satisfies the parent-child criterion:

4:3 >> 4:9
4:3 >> 4:18
3:13 >> 5:1
3:12 >> 5:2
3:1 >> 5:3
1:1 >> 7:15

Another healthy case

As I checked, the problem does not raise when the commented line begins with double-slash (//), like this:

code-ok-2

This is the resulting ranges:

5:3 >> 5:9
5:3 >> 5:18
3:13 >> 6:1
3:12 >> 6:2
3:1 >> 6:3
1:1 >> 8:15

@andrewbranch If the fix should be done on TS, I’d appreciate any hints to begin with since I’m new to the codebase.

TypeScript is not specific to TS files. We power all of the JavaScript features in VS Code. Smart select should work exactly the same between JS and TS files. However, VS Code has multiple smart selection providers—we are just one of them for JS and TS—and it merges their results, which is why I said it might end up being a VS Code bug. But the fact that it happens in JS doesn’t tell us anything about who the bug belongs to.