From 7465f046666917f224f6435e489afd768fd1fa83 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Tue, 5 Apr 2022 00:05:20 +0300 Subject: [PATCH] fix(48542): omit parameter info of contextual union type (#48549) --- src/services/signatureHelp.ts | 9 +- .../signatureHelp_unionType.baseline | 419 ++++++++++++++++++ .../fourslash/signatureHelp_unionType.ts | 21 + 3 files changed, 446 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/signatureHelp_unionType.baseline create mode 100644 tests/cases/fourslash/signatureHelp_unionType.ts diff --git a/src/services/signatureHelp.ts b/src/services/signatureHelp.ts index e26b0f49425..eefc1c44e9c 100644 --- a/src/services/signatureHelp.ts +++ b/src/services/signatureHelp.ts @@ -307,10 +307,13 @@ namespace ts.SignatureHelp { // for optional function condition. const nonNullableContextualType = contextualType.getNonNullableType(); - const signatures = nonNullableContextualType.getCallSignatures(); - if (signatures.length !== 1) return undefined; + const symbol = nonNullableContextualType.symbol; + if (symbol === undefined) return undefined; - const invocation: ContextualInvocation = { kind: InvocationKind.Contextual, signature: first(signatures), node: startingToken, symbol: chooseBetterSymbol(nonNullableContextualType.symbol) }; + const signature = lastOrUndefined(nonNullableContextualType.getCallSignatures()); + if (signature === undefined) return undefined; + + const invocation: ContextualInvocation = { kind: InvocationKind.Contextual, signature, node: startingToken, symbol: chooseBetterSymbol(symbol) }; return { isTypeParameterList: false, invocation, argumentsSpan, argumentIndex, argumentCount }; } diff --git a/tests/baselines/reference/signatureHelp_unionType.baseline b/tests/baselines/reference/signatureHelp_unionType.baseline new file mode 100644 index 00000000000..17ba6f279be --- /dev/null +++ b/tests/baselines/reference/signatureHelp_unionType.baseline @@ -0,0 +1,419 @@ +[ + { + "marker": { + "fileName": "/tests/cases/fourslash/signatureHelp_unionType.ts", + "position": 296, + "name": "1" + }, + "signatureHelp": { + "items": [ + { + "isVariadic": false, + "prefixDisplayParts": [ + { + "text": "a", + "kind": "localName" + }, + { + "text": "(", + "kind": "punctuation" + } + ], + "suffixDisplayParts": [ + { + "text": ")", + "kind": "punctuation" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "void", + "kind": "keyword" + } + ], + "separatorDisplayParts": [ + { + "text": ",", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + } + ], + "parameters": [ + { + "name": "fn", + "documentation": [], + "displayParts": [ + { + "text": "fn", + "kind": "parameterName" + }, + { + "text": "?", + "kind": "punctuation" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "(", + "kind": "punctuation" + }, + { + "text": "(", + "kind": "punctuation" + }, + { + "text": "x", + "kind": "parameterName" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "string", + "kind": "keyword" + }, + { + "text": ")", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "=>", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "string", + "kind": "keyword" + }, + { + "text": ")", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "|", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "(", + "kind": "punctuation" + }, + { + "text": "(", + "kind": "punctuation" + }, + { + "text": "y", + "kind": "parameterName" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "number", + "kind": "keyword" + }, + { + "text": ")", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "=>", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "number", + "kind": "keyword" + }, + { + "text": ")", + "kind": "punctuation" + } + ], + "isOptional": true, + "isRest": false + } + ], + "documentation": [], + "tags": [] + } + ], + "applicableSpan": { + "start": 295, + "length": 31 + }, + "selectedItemIndex": 0, + "argumentIndex": 0, + "argumentCount": 1 + } + }, + { + "marker": { + "fileName": "/tests/cases/fourslash/signatureHelp_unionType.ts", + "position": 332, + "name": "2" + }, + "signatureHelp": { + "items": [ + { + "isVariadic": false, + "prefixDisplayParts": [ + { + "text": "b", + "kind": "localName" + }, + { + "text": "(", + "kind": "punctuation" + } + ], + "suffixDisplayParts": [ + { + "text": ")", + "kind": "punctuation" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "void", + "kind": "keyword" + } + ], + "separatorDisplayParts": [ + { + "text": ",", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + } + ], + "parameters": [ + { + "name": "x", + "documentation": [], + "displayParts": [ + { + "text": "x", + "kind": "parameterName" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "string", + "kind": "keyword" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "|", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "number", + "kind": "keyword" + } + ], + "isOptional": false, + "isRest": false + } + ], + "documentation": [], + "tags": [] + } + ], + "applicableSpan": { + "start": 332, + "length": 0 + }, + "selectedItemIndex": 0, + "argumentIndex": 0, + "argumentCount": 0 + } + }, + { + "marker": { + "fileName": "/tests/cases/fourslash/signatureHelp_unionType.ts", + "position": 339, + "name": "3" + }, + "signatureHelp": { + "items": [ + { + "isVariadic": false, + "prefixDisplayParts": [ + { + "text": "Callback", + "kind": "interfaceName" + }, + { + "text": "(", + "kind": "punctuation" + } + ], + "suffixDisplayParts": [ + { + "text": ")", + "kind": "punctuation" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "string", + "kind": "keyword" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "|", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "number", + "kind": "keyword" + } + ], + "separatorDisplayParts": [ + { + "text": ",", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + } + ], + "parameters": [ + { + "name": "x", + "documentation": [], + "displayParts": [ + { + "text": "x", + "kind": "parameterName" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "string", + "kind": "keyword" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "|", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "number", + "kind": "keyword" + } + ], + "isOptional": false, + "isRest": false + } + ], + "documentation": [], + "tags": [] + } + ], + "applicableSpan": { + "start": 339, + "length": 0 + }, + "selectedItemIndex": 0, + "argumentIndex": 0, + "argumentCount": 0 + } + } +] \ No newline at end of file diff --git a/tests/cases/fourslash/signatureHelp_unionType.ts b/tests/cases/fourslash/signatureHelp_unionType.ts new file mode 100644 index 00000000000..0548ac1e689 --- /dev/null +++ b/tests/cases/fourslash/signatureHelp_unionType.ts @@ -0,0 +1,21 @@ +/// + +////declare const a: (fn?: ((x: string) => string) | ((y: number) => number)) => void; +////declare const b: (x: string | number) => void; +//// +////interface Callback { +//// (x: string): string; +//// (x: number): number; +//// (x: string | number): string | number; +////} +////declare function c(callback: Callback): void; + +////a((/*1*/) => { +//// return undefined; +////}); +//// +////b(/*2*/); +//// +////c((/*3*/) => {}); + +verify.baselineSignatureHelp();