mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-10 18:04:18 -05:00
Handle literal string union complete pattern for JSX attribute snippets (<input type />) (#52562)
Co-authored-by: Andrew Branch <andrewbranch@users.noreply.github.com>
This commit is contained in:
@@ -1727,6 +1727,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
isArrayType,
|
||||
isTupleType,
|
||||
isArrayLikeType,
|
||||
isEmptyAnonymousObjectType,
|
||||
isTypeInvalidDueToUnionDiscriminant,
|
||||
getExactOptionalProperties,
|
||||
getAllPossiblePropertiesOfTypes,
|
||||
|
||||
@@ -4946,6 +4946,8 @@ export interface TypeChecker {
|
||||
getPromisedTypeOfPromise(promise: Type, errorNode?: Node): Type | undefined;
|
||||
/** @internal */
|
||||
getAwaitedType(type: Type): Type | undefined;
|
||||
/** @internal */
|
||||
isEmptyAnonymousObjectType(type: Type): boolean;
|
||||
getReturnTypeOfSignature(signature: Signature): Type;
|
||||
/**
|
||||
* Gets the type of a parameter at a given position in a signature.
|
||||
|
||||
@@ -218,6 +218,7 @@ import {
|
||||
isStatement,
|
||||
isStatic,
|
||||
isString,
|
||||
isStringAndEmptyAnonymousObjectIntersection,
|
||||
isStringANonContextualKeyword,
|
||||
isStringLiteralLike,
|
||||
isStringLiteralOrTemplate,
|
||||
@@ -1442,7 +1443,7 @@ 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))))) {
|
||||
if (type.flags & TypeFlags.StringLike || (type.flags & TypeFlags.Union && every((type as UnionType).types, type => !!(type.flags & (TypeFlags.StringLike | TypeFlags.Undefined) || isStringAndEmptyAnonymousObjectIntersection(type))))) {
|
||||
// If is string like or undefined use quotes
|
||||
insertText = `${escapeSnippetText(name)}=${quote(sourceFile, preferences, "$1")}`;
|
||||
isSnippet = true;
|
||||
|
||||
@@ -342,6 +342,7 @@ import {
|
||||
tryCast,
|
||||
Type,
|
||||
TypeChecker,
|
||||
TypeFlags,
|
||||
TypeFormatFlags,
|
||||
TypeNode,
|
||||
TypeOfExpression,
|
||||
@@ -2154,6 +2155,17 @@ export function isStringOrRegularExpressionOrTemplateLiteral(kind: SyntaxKind):
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function isStringAndEmptyAnonymousObjectIntersection(type: Type) {
|
||||
if (!type.isIntersection()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const { types, checker } = type;
|
||||
return types.length === 2
|
||||
&& (types[0].flags & TypeFlags.String) && checker.isEmptyAnonymousObjectType(types[1]);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function isPunctuation(kind: SyntaxKind): boolean {
|
||||
return SyntaxKind.FirstPunctuation <= kind && kind <= SyntaxKind.LastPunctuation;
|
||||
|
||||
Reference in New Issue
Block a user