Skip to content

Fix JSX attribute completion for union types containing string-like t… #62242

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
31 changes: 30 additions & 1 deletion src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,36 @@ function createCompletionEntry(
&& !(type.flags & TypeFlags.BooleanLike)
&& !(type.flags & TypeFlags.Union && find((type as UnionType).types, type => !!(type.flags & TypeFlags.BooleanLike)))
) {
if (type.flags & TypeFlags.StringLike || (type.flags & TypeFlags.Union && every((type as UnionType).types, type => !!(type.flags & (TypeFlags.StringLike | TypeFlags.Undefined) || isStringAndEmptyAnonymousObjectIntersection(type))))) {
// Check if we should use quotes for string-like types
let shouldUseQuotes = false;

if (type.flags & TypeFlags.StringLike) {
// Direct string-like type
shouldUseQuotes = true;
}
else if (type.flags & TypeFlags.Union) {
const unionType = type as UnionType;
// Check if all types are string-like or undefined (original logic)
const allTypesAreStringLikeOrUndefined = every(unionType.types, type => !!(type.flags & (TypeFlags.StringLike | TypeFlags.Undefined) || isStringAndEmptyAnonymousObjectIntersection(type)));

if (allTypesAreStringLikeOrUndefined) {
shouldUseQuotes = true;
}
else {
// Check if the union contains string-like types that users would typically provide as strings
// This handles cases like Preact's Signalish<string | undefined> = string | undefined | SignalLike<string | undefined>
const hasStringLikeTypes = some(unionType.types, type => !!(type.flags & TypeFlags.StringLike));
const hasNonObjectTypes = some(unionType.types, type => !!(type.flags & (TypeFlags.StringLike | TypeFlags.Undefined | TypeFlags.Null)));

// If the union has string-like types and at least some primitive types (not just objects),
// prefer quotes since users commonly want to provide string values
if (hasStringLikeTypes && hasNonObjectTypes) {
shouldUseQuotes = true;
}
}
}

if (shouldUseQuotes) {
// If is string like or undefined use quotes
insertText = `${escapeSnippetText(name)}=${quote(sourceFile, preferences, "$1")}`;
isSnippet = true;
Expand Down
52 changes: 52 additions & 0 deletions tests/cases/fourslash/jsxAttributeCompletionStyleAutoSignalish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/// <reference path="fourslash.ts" />

// @Filename: foo.tsx
//// declare namespace JSX {
//// interface Element { }
//// interface SignalLike<T> {
//// value: T;
//// peek(): T;
//// subscribe(fn: (value: T) => void): () => void;
//// }
//// type Signalish<T> = T | SignalLike<T>;
//// interface IntrinsicElements {
//// div: {
//// class?: Signalish<string | undefined>;
//// id?: Signalish<string | undefined>;
//// title?: Signalish<string | undefined>;
//// disabled?: Signalish<boolean | undefined>;
//// 'data-testid'?: Signalish<string | undefined>;
//// role?: Signalish<string | undefined>;
//// // For comparison - pure string type should still work
//// pureString?: string;
//// // Boolean-like should not get quotes
//// booleanProp?: boolean;
//// }
//// }
//// }
////
//// <div [|prop_/**/|] />

// Test that string-like Signalish types prefer quotes over braces
verify.completions({
marker: "",
includes: [
{
name: "class",
insertText: "class=\"$1\"",
isSnippet: true,
sortText: completion.SortText.OptionalMember,
},
{
name: "id",
insertText: "id=\"$1\"",
isSnippet: true,
sortText: completion.SortText.OptionalMember,
},
],
preferences: {
jsxAttributeCompletionStyle: "auto",
includeCompletionsWithSnippetText: true,
includeCompletionsWithInsertText: true,
}
});
Loading