Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(50224): Intellisense for strings within a type's Union doesn't wo…
…rk properly for JSX (#50231)

* fix(50224): show string literal completions in JsxAttributeInitializer

* add feedback changes
  • Loading branch information
a-tarasyuk committed Aug 23, 2022
1 parent 6ee5db9 commit 44ce3cf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/services/stringCompletions.ts
Expand Up @@ -241,7 +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*/")
return argumentInfo ? getStringLiteralCompletionsFromSignature(argumentInfo.invocation, node, argumentInfo, typeChecker) : fromContextualType();
return argumentInfo && getStringLiteralCompletionsFromSignature(argumentInfo.invocation, node, argumentInfo, typeChecker) || fromContextualType();
}
// falls through (is `require("")` or `require(""` or `import("")`)

Expand Down Expand Up @@ -283,7 +283,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<string, true>();
const candidates: Signature[] = [];
Expand All @@ -301,8 +301,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 {
Expand Down
Expand Up @@ -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 });
@@ -0,0 +1,11 @@
/// <reference path="fourslash.ts" />

// @jsx: preserve
// @filename: /a.tsx
////type Props = { a: number } | { b: "somethingelse" };
////declare function Foo(args: Props): any
////
////const a1 = <Foo b={"/*1*/"} />
////const a2 = <Foo b="/*2*/" />

verify.completions({ marker: ["1", "2"], exact: ["somethingelse"] });

0 comments on commit 44ce3cf

Please sign in to comment.