Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (!node) {
return undefined;
}
if (contextFlags! & ContextFlags.Completions) {
if (contextFlags! & ContextFlags.IgnoreNodeInferences) {
return runWithInferenceBlockedFromSourceNode(node, () => getContextualType(node, contextFlags));
}
return getContextualType(node, contextFlags);
Expand Down Expand Up @@ -32827,7 +32827,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function getContextualJsxElementAttributesType(node: JsxOpeningLikeElement, contextFlags: ContextFlags | undefined) {
if (isJsxOpeningElement(node) && contextFlags !== ContextFlags.Completions) {
if (isJsxOpeningElement(node) && contextFlags !== ContextFlags.IgnoreNodeInferences) {
const index = findContextualNode(node.parent, /*includeCaches*/ !contextFlags);
if (index >= 0) {
// Contextually applied type is moved from attributes up to the outer jsx attributes so when walking up from the children they get hit
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5524,11 +5524,11 @@ export const enum IntersectionFlags {
// dprint-ignore
/** @internal */
export const enum ContextFlags {
None = 0,
Signature = 1 << 0, // Obtaining contextual signature
NoConstraints = 1 << 1, // Don't obtain type variable constraints
Completions = 1 << 2, // Ignore inference to current node and parent nodes out to the containing call for completions
SkipBindingPatterns = 1 << 3, // Ignore contextual types applied by binding patterns
None = 0,
Signature = 1 << 0, // Obtaining contextual signature
NoConstraints = 1 << 1, // Don't obtain type variable constraints
IgnoreNodeInferences = 1 << 2, // Ignore inference to current node and parent nodes out to the containing call for, for example, completions
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As shown by this PR, this mechanism is handy for other things than just completions - so I decided to rename it.

SkipBindingPatterns = 1 << 3, // Ignore contextual types applied by binding patterns
}

// NOTE: If modifying this enum, must modify `TypeFormatFlags` too!
Expand Down
6 changes: 3 additions & 3 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3281,7 +3281,7 @@ function getContextualType(previousToken: Node, position: number, sourceFile: So
isEqualityOperatorKind(previousToken.kind) && isBinaryExpression(parent) && isEqualityOperatorKind(parent.operatorToken.kind) ?
// completion at `x ===/**/` should be for the right side
checker.getTypeAtLocation(parent.left) :
checker.getContextualType(previousToken as Expression, ContextFlags.Completions) || checker.getContextualType(previousToken as Expression);
checker.getContextualType(previousToken as Expression, ContextFlags.IgnoreNodeInferences) || checker.getContextualType(previousToken as Expression);
}
}

Expand Down Expand Up @@ -3964,7 +3964,7 @@ function getCompletionData(
// Cursor is inside a JSX self-closing element or opening element
const attrsType = jsxContainer && typeChecker.getContextualType(jsxContainer.attributes);
if (!attrsType) return GlobalsSearch.Continue;
const completionsType = jsxContainer && typeChecker.getContextualType(jsxContainer.attributes, ContextFlags.Completions);
const completionsType = jsxContainer && typeChecker.getContextualType(jsxContainer.attributes, ContextFlags.IgnoreNodeInferences);
symbols = concatenate(symbols, filterJsxAttributes(getPropertiesForObjectExpression(attrsType, completionsType, jsxContainer.attributes, typeChecker), jsxContainer.attributes.properties));
setSortTextToOptionalMember();
completionKind = CompletionKind.MemberLike;
Expand Down Expand Up @@ -4562,7 +4562,7 @@ function getCompletionData(
}
return GlobalsSearch.Continue;
}
const completionsType = typeChecker.getContextualType(objectLikeContainer, ContextFlags.Completions);
const completionsType = typeChecker.getContextualType(objectLikeContainer, ContextFlags.IgnoreNodeInferences);
const hasStringIndexType = (completionsType || instantiatedType).getStringIndexType();
const hasNumberIndextype = (completionsType || instantiatedType).getNumberIndexType();
isNewIdentifierLocation = !!hasStringIndexType || !!hasNumberIndextype;
Expand Down
18 changes: 17 additions & 1 deletion src/services/goToDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CallLikeExpression,
canHaveSymbol,
concatenate,
ContextFlags,
createTextSpan,
createTextSpanFromBounds,
createTextSpanFromNode,
Expand Down Expand Up @@ -72,6 +73,8 @@ import {
isNameOfFunctionDeclaration,
isNewExpressionTarget,
isObjectBindingPattern,
isObjectLiteralElementLike,
isObjectLiteralExpression,
isPropertyName,
isRightSideOfPropertyAccess,
isStaticModifier,
Expand Down Expand Up @@ -312,7 +315,20 @@ function getDefinitionFromObjectLiteralElement(typeChecker: TypeChecker, node: N
if (element) {
const contextualType = element && typeChecker.getContextualType(element.parent);
if (contextualType) {
return flatMap(getPropertySymbolsFromContextualType(element, typeChecker, contextualType, /*unionSymbolOk*/ false), propertySymbol => getDefinitionFromSymbol(typeChecker, propertySymbol, node));
let properties = getPropertySymbolsFromContextualType(element, typeChecker, contextualType, /*unionSymbolOk*/ false);
if (properties.length === 1) {
const declaration = properties[0].valueDeclaration;
const withoutNodeInferencesType = declaration && isObjectLiteralExpression(declaration.parent) && isObjectLiteralElementLike(declaration) && declaration.name === node ?
typeChecker.getContextualType(element.parent, ContextFlags.IgnoreNodeInferences) :
undefined;
if (withoutNodeInferencesType) {
const withoutNodeInferencesProperties = getPropertySymbolsFromContextualType(element, typeChecker, withoutNodeInferencesType, /*unionSymbolOk*/ false);
if (withoutNodeInferencesProperties.length) {
properties = withoutNodeInferencesProperties;
}
}
}
return flatMap(properties, propertySymbol => getDefinitionFromSymbol(typeChecker, propertySymbol, node));
}
}
return emptyArray;
Expand Down
4 changes: 2 additions & 2 deletions src/services/stringCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ function getStringLiteralCompletionEntries(sourceFile: SourceFile, node: StringL
}
}

function fromContextualType(contextFlags: ContextFlags = ContextFlags.Completions): StringLiteralCompletionsFromTypes | undefined {
function fromContextualType(contextFlags: ContextFlags = ContextFlags.IgnoreNodeInferences): StringLiteralCompletionsFromTypes | undefined {
// Get completion for string literal from string literal type
// i.e. var x: "hi" | "hello" = "/*completion position*/"
const types = getStringLiteralTypes(getContextualTypeFromParent(node, typeChecker, contextFlags));
Expand Down Expand Up @@ -603,7 +603,7 @@ function stringLiteralCompletionsForObjectLiteral(checker: TypeChecker, objectLi
const contextualType = checker.getContextualType(objectLiteralExpression);
if (!contextualType) return undefined;

const completionsType = checker.getContextualType(objectLiteralExpression, ContextFlags.Completions);
const completionsType = checker.getContextualType(objectLiteralExpression, ContextFlags.IgnoreNodeInferences);
const symbols = getPropertiesForObjectExpression(
contextualType,
completionsType,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// === goToDefinition ===
// === /tests/cases/fourslash/goToDefinitionObjectLiteralProperties2.ts ===
// type C = {
// <|[|foo|]: string;|>
// bar: number;
// };
//
// declare function fn<T extends C>(arg: T): T;
//
// fn({
// foo/*GOTO DEF*/: "",
// bar: true,
// });
//
// --- (line: 13) skipped ---

// === Details ===
[
{
"kind": "property",
"name": "foo",
"containerName": "__type",
"isLocal": false,
"isAmbient": false,
"unverified": false
}
]



// === goToDefinition ===
// === /tests/cases/fourslash/goToDefinitionObjectLiteralProperties2.ts ===
// type C = {
// foo: string;
// <|[|bar|]: number;|>
// };
//
// declare function fn<T extends C>(arg: T): T;
//
// fn({
// foo: "",
// bar/*GOTO DEF*/: true,
// });
//
// const result = fn({
// --- (line: 14) skipped ---

// === Details ===
[
{
"kind": "property",
"name": "bar",
"containerName": "__type",
"isLocal": false,
"isAmbient": false,
"unverified": false
}
]



// === goToDefinition ===
// === /tests/cases/fourslash/goToDefinitionObjectLiteralProperties2.ts ===
// type C = {
// <|[|foo|]: string;|>
// bar: number;
// };
//
// --- (line: 6) skipped ---

// --- (line: 10) skipped ---
// });
//
// const result = fn({
// foo/*GOTO DEF*/: "",
// bar: 1,
// });
//
// // this one shouldn't go to the constraint type
// result.foo;

// === Details ===
[
{
"kind": "property",
"name": "foo",
"containerName": "__type",
"isLocal": false,
"isAmbient": false,
"unverified": false
}
]



// === goToDefinition ===
// === /tests/cases/fourslash/goToDefinitionObjectLiteralProperties2.ts ===
// type C = {
// foo: string;
// <|[|bar|]: number;|>
// };
//
// declare function fn<T extends C>(arg: T): T;
// --- (line: 7) skipped ---

// --- (line: 11) skipped ---
//
// const result = fn({
// foo: "",
// bar/*GOTO DEF*/: 1,
// });
//
// // this one shouldn't go to the constraint type
// result.foo;

// === Details ===
[
{
"kind": "property",
"name": "bar",
"containerName": "__type",
"isLocal": false,
"isAmbient": false,
"unverified": false
}
]



// === goToDefinition ===
// === /tests/cases/fourslash/goToDefinitionObjectLiteralProperties2.ts ===
// --- (line: 10) skipped ---
// });
//
// const result = fn({
// <|[|foo|]: ""|>,
// bar: 1,
// });
//
// // this one shouldn't go to the constraint type
// result.foo/*GOTO DEF*/;

// === Details ===
[
{
"kind": "property",
"name": "foo",
"containerName": "__object",
"isLocal": false,
"isAmbient": false,
"unverified": false,
"failedAliasResolution": false
}
]
23 changes: 23 additions & 0 deletions tests/cases/fourslash/goToDefinitionObjectLiteralProperties2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path='fourslash.ts'/>

//// type C = {
//// foo: string;
//// bar: number;
//// };
////
//// declare function fn<T extends C>(arg: T): T;
////
//// fn({
//// foo/*1*/: "",
//// bar/*2*/: true,
//// });
////
//// const result = fn({
//// foo/*3*/: "",
//// bar/*4*/: 1,
//// });
////
//// // this one shouldn't go to the constraint type
//// result.foo/*5*/;

verify.baselineGoToDefinition("1", "2", "3", "4", "5");
Loading