From f6a038ce0e38f8e81f1f2ac2796bc4a408ef36e5 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Tue, 9 Aug 2022 10:54:24 +0300 Subject: [PATCH 1/2] fix(50224): show string literal completions in JsxAttributeInitializer --- src/services/stringCompletions.ts | 9 +++++---- .../completionForStringLiteralFromSignature2.ts | 2 +- ...ringLiteralCompletionsInJsxAttributeInitializer.ts | 11 +++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 tests/cases/fourslash/stringLiteralCompletionsInJsxAttributeInitializer.ts diff --git a/src/services/stringCompletions.ts b/src/services/stringCompletions.ts index cb7260f6d8068..467af149330a3 100644 --- a/src/services/stringCompletions.ts +++ b/src/services/stringCompletions.ts @@ -241,7 +241,9 @@ namespace ts.Completions.StringCompletions { // Get string literal completions from specialized signatures of the target // i.e. declare function f(a: 'A'); // f("/*completion position*/") - return argumentInfo ? getStringLiteralCompletionsFromSignature(argumentInfo.invocation, node, argumentInfo, typeChecker) : fromContextualType(); + if (argumentInfo) { + return getStringLiteralCompletionsFromSignature(argumentInfo.invocation, node, argumentInfo, typeChecker) || fromContextualType(); + } } // falls through (is `require("")` or `require(""` or `import("")`) @@ -283,7 +285,7 @@ namespace ts.Completions.StringCompletions { type !== current && isLiteralTypeNode(type) && isStringLiteral(type.literal) ? type.literal.text : undefined); } - function getStringLiteralCompletionsFromSignature(call: CallLikeExpression, arg: StringLiteralLike, argumentInfo: SignatureHelp.ArgumentInfoForCompletions, checker: TypeChecker): StringLiteralCompletionsFromTypes { + function getStringLiteralCompletionsFromSignature(call: CallLikeExpression, arg: StringLiteralLike, argumentInfo: SignatureHelp.ArgumentInfoForCompletions, checker: TypeChecker): StringLiteralCompletionsFromTypes | undefined { let isNewIdentifier = false; const uniques = new Map(); const candidates: Signature[] = []; @@ -301,8 +303,7 @@ namespace ts.Completions.StringCompletions { isNewIdentifier = isNewIdentifier || !!(type.flags & TypeFlags.String); return getStringLiteralTypes(type, uniques); }); - - return { kind: StringLiteralCompletionKind.Types, types, isNewIdentifier }; + return length(types) ? { kind: StringLiteralCompletionKind.Types, types, isNewIdentifier } : undefined; } function stringLiteralCompletionsFromProperties(type: Type | undefined): StringLiteralCompletionsFromProperties | undefined { diff --git a/tests/cases/fourslash/completionForStringLiteralFromSignature2.ts b/tests/cases/fourslash/completionForStringLiteralFromSignature2.ts index f070d7e238902..4cc472ba173d0 100644 --- a/tests/cases/fourslash/completionForStringLiteralFromSignature2.ts +++ b/tests/cases/fourslash/completionForStringLiteralFromSignature2.ts @@ -4,4 +4,4 @@ ////declare function f(a: string, b: number): void; ////f("/**/", 0); -verify.completions({ marker: "", exact: [], isNewIdentifierLocation: true }); +verify.completions({ marker: "", exact: [], isNewIdentifierLocation: false }); diff --git a/tests/cases/fourslash/stringLiteralCompletionsInJsxAttributeInitializer.ts b/tests/cases/fourslash/stringLiteralCompletionsInJsxAttributeInitializer.ts new file mode 100644 index 0000000000000..2e3363728db4a --- /dev/null +++ b/tests/cases/fourslash/stringLiteralCompletionsInJsxAttributeInitializer.ts @@ -0,0 +1,11 @@ +/// + +// @jsx: preserve +// @filename: /a.tsx +////type Props = { a: number } | { b: "b" }; +////declare function Foo(args: Props): any +//// +////const a1 = +////const a2 = + +verify.completions({ marker: ["1", "2"], exact: ["b"] }); From 7ded27043345887591e1173de333c9265d194507 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Thu, 18 Aug 2022 22:30:06 +0300 Subject: [PATCH 2/2] add feedback changes --- src/services/stringCompletions.ts | 4 +--- .../stringLiteralCompletionsInJsxAttributeInitializer.ts | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/services/stringCompletions.ts b/src/services/stringCompletions.ts index 467af149330a3..922e50654bbe1 100644 --- a/src/services/stringCompletions.ts +++ b/src/services/stringCompletions.ts @@ -241,9 +241,7 @@ namespace ts.Completions.StringCompletions { // Get string literal completions from specialized signatures of the target // i.e. declare function f(a: 'A'); // f("/*completion position*/") - if (argumentInfo) { - return getStringLiteralCompletionsFromSignature(argumentInfo.invocation, node, argumentInfo, typeChecker) || fromContextualType(); - } + return argumentInfo && getStringLiteralCompletionsFromSignature(argumentInfo.invocation, node, argumentInfo, typeChecker) || fromContextualType(); } // falls through (is `require("")` or `require(""` or `import("")`) diff --git a/tests/cases/fourslash/stringLiteralCompletionsInJsxAttributeInitializer.ts b/tests/cases/fourslash/stringLiteralCompletionsInJsxAttributeInitializer.ts index 2e3363728db4a..6e19605d7d064 100644 --- a/tests/cases/fourslash/stringLiteralCompletionsInJsxAttributeInitializer.ts +++ b/tests/cases/fourslash/stringLiteralCompletionsInJsxAttributeInitializer.ts @@ -2,10 +2,10 @@ // @jsx: preserve // @filename: /a.tsx -////type Props = { a: number } | { b: "b" }; +////type Props = { a: number } | { b: "somethingelse" }; ////declare function Foo(args: Props): any //// ////const a1 = ////const a2 = -verify.completions({ marker: ["1", "2"], exact: ["b"] }); +verify.completions({ marker: ["1", "2"], exact: ["somethingelse"] });