microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.94k stars 12.47k forks source link

Autocompletion did not work when the cursor was placed before the constructor() #53124

Open WayneCYChang opened 1 year ago

WayneCYChang commented 1 year ago

TS Template added by @mjbvz

TypeScript Version: 5.0.1

Search Terms

See https://github.com/microsoft/TypeScript/issues/53124#issuecomment-1457338704 for minimal repo


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

Description

Autocompletion did not work when the cursor was placed before the constructor()

Reproduction

Steps to reproduce the behavior:

  1. Go to 'the line before constructor(private service: Service)'
  2. Type 'a = this.service.'
  3. Autocompletion list doesn't show the method of service.

Expected behavior

Show method of service.

Screenshots

If applicable, add screenshots to help explain your problem.

image

Anything else relevant?

https://github.com/angular/vscode-ng-language-service/issues/1866#issue-1596155750

mjbvz commented 1 year ago

Please share a minimal, self-contained code example that demonstrates this issue

WayneCYChang commented 1 year ago

Please share a minimal, self-contained code example that demonstrates this issue

Thanks for the quick reply. Here is the code example. https://github.com/WayneCYChang/vscode-autocomplete-wrong

mjbvz commented 1 year ago

Thanks. Minimal repo:

class Foo {
    bar = this.foo // Type . here
    constructor(private foo: Foo) { }
}
mjbvz commented 1 year ago

Suggestions do start working after the first letter is typed. It's likely they are just broken while the file is in an invalid syntax state

WayneCYChang commented 1 year ago

Yes, you're right. An invalid syntax state is broken Suggestions. But I also found that is only broken before the constructor(). The suggestions will work normally if you have some words between '.' and the constructor().

I have committed a new code to the repo. You can try it out.

image

Andarist commented 1 year ago

The issue is not specific to the constructor. The same happens here:

class Foo {
    bar = this.
    other;
}

The parser continues to parse this as a property access so it doesn't parse other (or constructor in the other example) as a class member (or constructor). Thus the symbols for those members are not created.

In its essence the issue is very very similar to the one reported here