Skip to content

Commit 7eb96f6

Browse files
Fix JSX attribute completion for union types containing string-like types
1 parent 3916165 commit 7eb96f6

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/services/completions.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,38 @@ function createCompletionEntry(
18601860
&& !(type.flags & TypeFlags.BooleanLike)
18611861
&& !(type.flags & TypeFlags.Union && find((type as UnionType).types, type => !!(type.flags & TypeFlags.BooleanLike)))
18621862
) {
1863-
if (type.flags & TypeFlags.StringLike || (type.flags & TypeFlags.Union && every((type as UnionType).types, type => !!(type.flags & (TypeFlags.StringLike | TypeFlags.Undefined) || isStringAndEmptyAnonymousObjectIntersection(type))))) {
1863+
// Check if we should use quotes for string-like types
1864+
let shouldUseQuotes = false;
1865+
1866+
if (type.flags & TypeFlags.StringLike) {
1867+
// Direct string-like type
1868+
shouldUseQuotes = true;
1869+
} else if (type.flags & TypeFlags.Union) {
1870+
const unionType = type as UnionType;
1871+
// Check if all types are string-like or undefined (original logic)
1872+
const allTypesAreStringLikeOrUndefined = every(unionType.types, type =>
1873+
!!(type.flags & (TypeFlags.StringLike | TypeFlags.Undefined) || isStringAndEmptyAnonymousObjectIntersection(type))
1874+
);
1875+
1876+
if (allTypesAreStringLikeOrUndefined) {
1877+
shouldUseQuotes = true;
1878+
} else {
1879+
// Check if the union contains string-like types that users would typically provide as strings
1880+
// This handles cases like Preact's Signalish<string | undefined> = string | undefined | SignalLike<string | undefined>
1881+
const hasStringLikeTypes = some(unionType.types, type => !!(type.flags & TypeFlags.StringLike));
1882+
const hasNonObjectTypes = some(unionType.types, type =>
1883+
!!(type.flags & (TypeFlags.StringLike | TypeFlags.Undefined | TypeFlags.Null))
1884+
);
1885+
1886+
// If the union has string-like types and at least some primitive types (not just objects),
1887+
// prefer quotes since users commonly want to provide string values
1888+
if (hasStringLikeTypes && hasNonObjectTypes) {
1889+
shouldUseQuotes = true;
1890+
}
1891+
}
1892+
}
1893+
1894+
if (shouldUseQuotes) {
18641895
// If is string like or undefined use quotes
18651896
insertText = `${escapeSnippetText(name)}=${quote(sourceFile, preferences, "$1")}`;
18661897
isSnippet = true;

0 commit comments

Comments
 (0)