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

Support completions contextual types in more places #20768

Merged
2 commits merged into from
Jan 3, 2018

Conversation

ghost
Copy link

@ghost ghost commented Dec 18, 2017

Fixes #20760

Sequel to #20020 that supports completions-specific contextual types on an identifier after a new expression. Moves this code from checker to a wrapper in completions.ts since some of these contextual types don't really make sense outside of completions. (E.g., we contextually type an identifier in const x: T = new iden| as T, whereas a correct contextual type would be { new(): T }.)

@ghost ghost requested a review from armanio123 December 18, 2017 22:13
@ghost ghost force-pushed the getRecommendedCompletions branch 3 times, most recently from a71011d to 374654b Compare December 21, 2017 16:18
@ghost ghost force-pushed the getRecommendedCompletions branch from 374654b to 7526d65 Compare January 3, 2018 17:11
Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

Seems fine except that I don't understand why removing the contextual typing from === and others doesn't change anything in the batch compilation tests. Can you explain?

@@ -13943,7 +13943,7 @@ namespace ts {
// the contextual type of an initializer expression is the type annotation of the containing declaration, if present.
function getContextualTypeForInitializerExpression(node: Expression): Type {
const declaration = <VariableLikeDeclaration>node.parent;
if (hasInitializer(declaration) && node === declaration.initializer || node.kind === SyntaxKind.EqualsToken) {
Copy link
Member

Choose a reason for hiding this comment

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

Why does = go away? I guess that it's because in batch compilation, = doesn't have a type, and node is in fact only ever = when called from services.

Copy link
Author

Choose a reason for hiding this comment

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

Right, this code was added in #20020 and this PR moves the special cases to completions.ts.

case SyntaxKind.ExclamationEqualsEqualsToken:
case SyntaxKind.ExclamationEqualsToken:
// For completions after `x === `
return node === operatorToken ? getTypeOfExpression(binaryExpression.left) : undefined;
Copy link
Member

Choose a reason for hiding this comment

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

is this really not used anywhere else besides services?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, this was also added by #20020.

case ts.SyntaxKind.EqualsToken:
return ts.isVariableDeclaration(parent)
? checker.getContextualType(parent.initializer)
: ts.isBinaryExpression(parent)
Copy link
Member

Choose a reason for hiding this comment

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

can you reformat like so?

return ts.isVariableDeclaration(parent) ? checker.getContextualType(parent.initializer) :
    ts.isBinaryExpression(parent) ? checker.getTypeAtLocation(parent.left) : undefined;

@ghost ghost merged commit a33f229 into master Jan 3, 2018
@ghost ghost deleted the getRecommendedCompletions branch January 3, 2018 21:51
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant