Reuse type nodes from optional parameters even when not written as a union with undefined (#48605)

* Reuse type nodes from optional parameters and properties even when not written as a union with `undefined`

* Remove newly unneeded NodeBuilderFlag

* Update public API

* Update baselines from main
This commit is contained in:
Andrew Branch
2022-04-08 14:28:36 -07:00
committed by GitHub
parent 12c4ea8542
commit af3e1d4dfd
51 changed files with 246 additions and 113 deletions
+12 -10
View File
@@ -5915,9 +5915,6 @@ namespace ts {
if (parameterDeclaration && isRequiredInitializedParameter(parameterDeclaration)) {
parameterType = getOptionalType(parameterType);
}
if ((context.flags & NodeBuilderFlags.NoUndefinedOptionalParameterType) && parameterDeclaration && !isJSDocParameterTag(parameterDeclaration) && isOptionalUninitializedParameter(parameterDeclaration)) {
parameterType = getTypeWithFacts(parameterType, TypeFacts.NEUndefined);
}
const parameterTypeNode = serializeTypeForDeclaration(context, parameterType, parameterSymbol, context.enclosingDeclaration, privateSymbolVisitor, bundledImports);
const modifiers = !(context.flags & NodeBuilderFlags.OmitParameterModifiers) && preserveModifierFlags && parameterDeclaration && parameterDeclaration.modifiers ? parameterDeclaration.modifiers.map(factory.cloneNode) : undefined;
@@ -6544,7 +6541,7 @@ namespace ts {
if (declWithExistingAnnotation && !isFunctionLikeDeclaration(declWithExistingAnnotation) && !isGetAccessorDeclaration(declWithExistingAnnotation)) {
// try to reuse the existing annotation
const existing = getEffectiveTypeAnnotationNode(declWithExistingAnnotation)!;
if (getTypeFromTypeNode(existing) === type && existingTypeNodeIsNotReferenceOrIsReferenceWithCompatibleTypeArgumentCount(existing, type)) {
if (typeNodeIsEquivalentToType(existing, declWithExistingAnnotation, type) && existingTypeNodeIsNotReferenceOrIsReferenceWithCompatibleTypeArgumentCount(existing, type)) {
const result = serializeExistingTypeNode(context, existing, includePrivateSymbol, bundled);
if (result) {
return result;
@@ -6562,6 +6559,17 @@ namespace ts {
return result;
}
function typeNodeIsEquivalentToType(typeNode: TypeNode, annotatedDeclaration: Declaration, type: Type) {
const typeFromTypeNode = getTypeFromTypeNode(typeNode);
if (typeFromTypeNode === type) {
return true;
}
if (isParameter(annotatedDeclaration) && annotatedDeclaration.questionToken) {
return getTypeWithFacts(type, TypeFacts.NEUndefined) === typeFromTypeNode;
}
return false;
}
function serializeReturnTypeForSignature(context: NodeBuilderContext, type: Type, signature: Signature, includePrivateSymbol?: (s: Symbol) => void, bundled?: boolean) {
if (!isErrorType(type) && context.enclosingDeclaration) {
const annotation = signature.declaration && getEffectiveReturnTypeNode(signature.declaration);
@@ -42579,12 +42587,6 @@ namespace ts {
hasSyntacticModifier(parameter, ModifierFlags.ParameterPropertyModifier);
}
function isOptionalUninitializedParameter(parameter: ParameterDeclaration) {
return !!strictNullChecks &&
isOptionalParameter(parameter) &&
!parameter.initializer;
}
function isExpandoFunctionDeclaration(node: Declaration): boolean {
const declaration = getParseTreeNode(node, isFunctionDeclaration);
if (!declaration) {
-1
View File
@@ -4490,7 +4490,6 @@ namespace ts {
UseAliasDefinedOutsideCurrentScope = 1 << 14, // Allow non-visible aliases
UseSingleQuotesForStringLiteralType = 1 << 28, // Use single quotes for string literal type
NoTypeReduction = 1 << 29, // Don't call getReducedType
NoUndefinedOptionalParameterType = 1 << 30, // Do not add undefined to optional parameter type
// Error handling
AllowThisInObjectLiteral = 1 << 15,
+4
View File
@@ -7654,4 +7654,8 @@ namespace ts {
return state > States.NodeModules ? { topLevelNodeModulesIndex, topLevelPackageNameIndex, packageRootIndex, fileNameIndex } : undefined;
}
export function getParameterTypeNode(parameter: ParameterDeclaration | JSDocParameterTag) {
return parameter.kind === SyntaxKind.JSDocParameterTag ? parameter.typeExpression?.type : parameter.type;
}
}
-1
View File
@@ -194,7 +194,6 @@ namespace ts.codefix {
const scriptTarget = getEmitScriptTarget(program.getCompilerOptions());
const flags =
NodeBuilderFlags.NoTruncation
| NodeBuilderFlags.NoUndefinedOptionalParameterType
| NodeBuilderFlags.SuppressAnyReturnType
| NodeBuilderFlags.AllowEmptyTuple
| (quotePreference === QuotePreference.Single ? NodeBuilderFlags.UseSingleQuotesForStringLiteralType : NodeBuilderFlags.None);