Skip to content
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

Provide string completions for in keyword checks #60272

Merged
merged 2 commits into from
Feb 20, 2025

Conversation

Andarist
Copy link
Contributor

closes #52736

@typescript-bot
Copy link
Collaborator

The TypeScript team hasn't accepted the linked issue #52736. If you can get it accepted, this PR will have a better chance of being reviewed.

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Oct 18, 2024
});
verify.completions({
marker: "7",
exact: ["bar", "foo"],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This includes a completion from private bar = 0 but apparently that kind of a check is valid 🤷‍♂️ TS playground

Copy link
Member

Choose a reason for hiding this comment

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

That is cursed and I hate it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Amen.

if ((parent as BinaryExpression).operatorToken.kind === SyntaxKind.InKeyword) {
const { left, right } = parent as BinaryExpression;
const leftOrRight = findAncestor(node, n => n.parent.kind === SyntaxKind.BinaryExpression);
const type = typeChecker.getTypeAtLocation(leftOrRight === right ? left : right);
Copy link
Member

Choose a reason for hiding this comment

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

What exactly is the purpose of getting the type on the right side? Where is that tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code has to handle both:

  • key in obj
  • obj in key

Those new completions are trying to suggest available keys. So to do that we obtain "the other" type and based on it we compute properties (one line below this) based on which those are computed.

Copy link
Member

Choose a reason for hiding this comment

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

What language construct is obj in key supposed to correspond to though? Isn't that just someone writing a construct backwards?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a new construct coming in the next edition of JabaScript.

You are, of course, right - this was nonsense. I'm sometimes a dumdum when producing code in short 5min spans of time I have at hand.

I just pushed out a commit removing this bit.

});
verify.completions({
marker: "7",
exact: ["bar", "foo"],
Copy link
Member

Choose a reason for hiding this comment

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

That is cursed and I hate it

return {
kind: StringLiteralCompletionKind.Properties,
symbols: properties.filter(prop => !prop.valueDeclaration || !isPrivateIdentifierClassElementDeclaration(prop.valueDeclaration)),
hasIndexSignature: false,
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean that we will aggressively complete in this position? Does this need to return true if any type has a string/number index signature?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is only used to return value for isNewIdentifierLocation and I don't quite understand what this is meant to represent. IIRC it, at times, works inconsistently anyway. I'd love to be able to tell what should be returned for it here but I simply just don't know ;p

Copy link
Member

@DanielRosenwasser DanielRosenwasser left a comment

Choose a reason for hiding this comment

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

Probably fine, but I'd take my last comment into account.

@jakebailey jakebailey merged commit b95187d into microsoft:main Feb 20, 2025
32 checks passed
@Andarist Andarist deleted the in-completions branch February 21, 2025 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

in keyword completions
4 participants