-
Notifications
You must be signed in to change notification settings - Fork 12.8k
fix(41259) : JS autocomplete doesn't work for object literal shorthands #41539
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
Conversation
@typescript-bot pack this |
Heya @andrewbranch, I've started to run the tarball bundle task on this PR at 6d3db10. You can monitor the build here. |
It seems that all tests should be passed to pack. I've just updated the failing one to fit the current build. |
Oh, that's annoying. I thought it just had to build cleanly. Thanks! @typescript-bot pack this again pls |
Heya @andrewbranch, I've started to run the tarball bundle task on this PR at 0e6b319. You can monitor the build here. |
Hey @andrewbranch, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build. |
A couple notes:
Thanks for working on this! |
It looks like you've sent a pull request to update our 'lib' files. These files aren't meant to be edited by hand, as they consist of last-known good states of the compiler and are generated from 'src'. Unless this is necessary, consider closing the pull request and sending a separate PR to update 'src'. |
Thanks for the much detailed feedback! It was very helpful.
IMHO, offering all the completions would be better even if some of are wrong. The difference between the contextual type case is that dot can't be followed in shorthand completion so there's no chance that ending up with different type. Even though, as you mentioned, since checking all the assignability of the list is expensive, just offering them all rather than nothing could give more good user experience, I think.
declare const foo: number;
interface Empty {}
interface Typed { typed: number; }
declare function f10<T extends Object>(obj: T): void;
declare function f13<T extends (Empty | Object | Typed)>(obj: T): void;
f10({foo});
f13({foo}); Playground, v4.0.5
I'm not sure if it is legal to do this only for my intention.
Thanks ! |
I wonder if you can avoid all this by seeing if Edit: To clarify, I’m not 100% confident that |
Oh actually I didn't think about that! I agree with that. |
I realized that there's already a code which is getting properties of the type. Still, one thing that blocks me from implementing it is distinguishing the |
I don’t think we should do anything special for capital-O |
That actually makes a lot easier to me ;) Just fixed it! |
verify.completions( | ||
{ marker: ["1", "2", "4", "6", "10", "11", "13", "14", "15"], includes: ["foo"]}, | ||
{ marker: ["5", "7", "8"], excludes: ["foo"], isNewIdentifierLocation: true}, | ||
{ marker: ["3", "9", "12", "16"], excludes: ["foo"]}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I can nitpick the format of this test a little bit, could we rename/reorder the function declarations such that they’re obviously grouped by expected behavior? So you can quickly scan that e.g. f1–f10 should show foo
in completions, then there’s a blank line, and the next group doesn’t show foo
, etc... We also want to add a case where any
is the contextual type, and a case where there is no contextual type, like const x = { /** }
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I had also considered about this and I think you're right. any
and non-contextual type are also added. Thanks!
@typescript-bot pack this again please and thank you |
Heya @andrewbranch, I've started to run the tarball bundle task on this PR at a0f2476. You can monitor the build here. |
Hey @andrewbranch, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build. |
Thanks for your work on this, @orange4glace. If you’re ready for review, push the button and I’ll get some other team members to experiment with it on the playground. |
I'm much appreciated that I can contribute to the one of my favorite language with your lots of help :) |
We very much appreciate the contribution! Lots of people are on vacation this week, so it might not get another review until next week. This should be on track for 4.2 regardless though. |
I just played with this:
Given const variable1 = 1
const variable2 = 2 // nothing after a missing comma
const o = {
variable1
va/**/
}
// nothing in an unclosed literal
const p = { var/**/
interface A {
// ...
} I didn't read the original bug too closely so I'm not sure if fixing that is feature creep or not. |
Thanks for the feedback @sandersn!
Actually it was my mistake. I was checking
I agree that it should be worked either since autocomplete also works in the case which is, const o = {
variable1: variable1
variable2: var/**/
} I've also applied this to the latest commit. Please leave comments for any thoughts for the code or other things! |
@@ -9,7 +9,10 @@ | |||
|
|||
verify.completions({ | |||
marker: "1", | |||
exact: [] | |||
includes: [{ | |||
name: "Object", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a bit of a pain, but can we try to keep using exact
in all these tests? Hopefully in this case it’s just exact: completion.globals
. exact
is useful in some of these other tests for ensuring that locals get sorted before globals, which is important to this feature. I’m almost on the fence about whether globals should show up here at all (who wants to declare something like const obj = { Object, Date, setTimeout }
?), but I think as long as they get sorted below locals, it’s fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! I'll fix it after I find out how I can do this elegantly. :)
I’m almost on the fence about whether globals should show up here at all
I agree with that by the way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed! except it uses globalsPlus
for this case (#41731)
707e026#diff-758264b55071844e413a511bae1269991faea7f964e4d03cebb9a021ff42eb50R12
It’s probably worth discussing the broader design point of whether globals should be offered as shorthand properties, or just locals: const salamander = 0;
const o = { s/**/ };
// Completions:
// - salamander 👍
// - setTimeout 🤔 I think it’s a bit odd for globals to show up, but I’m not sure it’s harmful, because
These two points mean that the globals are not crowding out better suggestions, even if they’re rarely useful themselves. Open to hearing other takes, though. |
I just thought of something else—assuming we decide to keep globals in the completions here, can we add a test that ensures that we don’t trigger auto-imports here? I think it’d be over the top to get shorthand property completions for things that aren’t even currently in scope at all. Something like this // @module: esnext
// @Filename: /a.ts
//// export const exportedConstant = 0;
// @Filename: /b.ts
//// const obj = { exp/**/
verify.completions({
marker: "",
exact: completion.globals, // I think?
preferences: { includeCompletionsForModuleExports: true }
}); |
I agree that offering auto-imports to completions might be annoying 👍 const obj = {
o/**/
~~~~ obj
} It is obviously a mistake to use its container variable (and which finally emits an error const obj = {
value: o/**/
~~~~ obj
} I don't know whether this is intended or not (like implementing it is kinda cumbersome? 🤔)
👍 for not offering global symbols since as you said, literally no one wants to re-declare such symbols which can be accessed globally. |
I don’t think it should be difficult to fix; I’m guessing it has just gone unnoticed. But I don’t think you need to worry about it in this PR since it appears unrelated. I can open a new bug for it. |
@sandersn @DanielRosenwasser either of you want to weigh in on whether these completions should include globals (#41539 (comment))? I don’t feel super strongly about it for the reasons I outlined in that comment. But I want to make a decision and get this merged soon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @orange4glace!
Oh, I haven't noticed that this merged. |
Fixes #41259
Still have to work with failed testcase
completionListAtIdentifierDefinitionLocations_destructuring