From 893fd3b1203c2c2dc196aba55759dce6c3a53478 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 26 Aug 2026 03:29:23 +0800 Subject: [PATCH] Add `isTypeAnnotation` (#19921) --- src/language-js/comments/handle-comments.js | 4 ++-- src/language-js/print/function-parameters.js | 5 ++--- src/language-js/print/type-annotation.js | 11 ++++++----- .../utilities/is-type-annotation-a-function.js | 2 +- src/language-js/utilities/node-types.js | 5 +++++ 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index 019ec7f91..640e55285 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -28,6 +28,7 @@ import { isIntersectionType, isMemberExpression, isTypeAlias, + isTypeAnnotation, isUnionType, } from "../utilities/node-types.js"; import { stripComments } from "../utilities/strip-comments.js"; @@ -994,8 +995,7 @@ function handleCommentsInDestructuringPattern({ (enclosingNode.type === "ObjectPattern" || enclosingNode.type === "ArrayPattern") && enclosingNode.typeAnnotation === followingNode && - (followingNode.type === "TSTypeAnnotation" || - followingNode.type === "TypeAnnotation") + isTypeAnnotation(followingNode) ) { if ( getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) === diff --git a/src/language-js/print/function-parameters.js b/src/language-js/print/function-parameters.js index 4c92e632c..c9ba40182 100644 --- a/src/language-js/print/function-parameters.js +++ b/src/language-js/print/function-parameters.js @@ -23,6 +23,7 @@ import { isArrayExpression, isObjectExpression, isObjectType, + isTypeAnnotation, } from "../utilities/node-types.js"; import { isTestCall } from "../utilities/test-libraries.js"; import { @@ -209,9 +210,7 @@ function shouldHugTheOnlyFunctionParameter(node) { (parameter.type === "ObjectPattern" || parameter.type === "ArrayPattern" || (parameter.type === "Identifier" && - parameter.typeAnnotation && - (parameter.typeAnnotation.type === "TypeAnnotation" || - parameter.typeAnnotation.type === "TSTypeAnnotation") && + isTypeAnnotation(parameter.typeAnnotation) && isObjectType(parameter.typeAnnotation.typeAnnotation)) || (parameter.type === "FunctionTypeParam" && isObjectType(parameter.typeAnnotation) && diff --git a/src/language-js/print/type-annotation.js b/src/language-js/print/type-annotation.js index 384e970b6..a6ac57573 100644 --- a/src/language-js/print/type-annotation.js +++ b/src/language-js/print/type-annotation.js @@ -1,6 +1,10 @@ import { CommentCheckFlags, hasComment } from "../utilities/comments.js"; import { isSimpleType } from "../utilities/is-simple-type.js"; -import { isObjectType, isUnionType } from "../utilities/node-types.js"; +import { + isObjectType, + isTypeAnnotation, + isUnionType, +} from "../utilities/node-types.js"; import { shouldHugUnionType } from "./union-type.js"; /** @@ -40,10 +44,7 @@ function printTypeAnnotationProperty( let shouldPrintLeadingSpace = false; - if ( - typeAnnotation.type === "TSTypeAnnotation" || - typeAnnotation.type === "TypeAnnotation" - ) { + if (isTypeAnnotation(typeAnnotation)) { const firstToken = path.call(getTypeAnnotationFirstToken, propertyName); if ( diff --git a/src/language-js/utilities/is-type-annotation-a-function.js b/src/language-js/utilities/is-type-annotation-a-function.js index a56d3708c..e31870756 100644 --- a/src/language-js/utilities/is-type-annotation-a-function.js +++ b/src/language-js/utilities/is-type-annotation-a-function.js @@ -5,7 +5,7 @@ import { hasSameLocStart } from "../location/index.js"; // var f: (a) => void; function isTypeAnnotationAFunction(node) { return ( - (node.type === "TypeAnnotation" || node.type === "TSTypeAnnotation") && + node.type === "TypeAnnotation" && node.typeAnnotation.type === "FunctionTypeAnnotation" && !node.static && !hasSameLocStart(node, node.typeAnnotation) diff --git a/src/language-js/utilities/node-types.js b/src/language-js/utilities/node-types.js index ea8db0af5..f5c2040e6 100644 --- a/src/language-js/utilities/node-types.js +++ b/src/language-js/utilities/node-types.js @@ -142,4 +142,9 @@ export const isTypeParameterInstantiation = createTypeCheckFunction([ "TypeParameterInstantiation", ]); +export const isTypeAnnotation = createTypeCheckFunction([ + "TSTypeAnnotation", + "TypeAnnotation", +]); + export * from "./literal.js";