From d40da4a260d35d9f0ea8208c2cc77204f8d3e5f8 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 15 Jan 2026 04:04:03 +0800 Subject: [PATCH] Merge `printHook` into `printFunction` (#18632) --- src/language-js/comments/handle-comments.js | 2 +- src/language-js/print/flow.js | 9 ++--- src/language-js/print/function.js | 28 +++++++-------- src/language-js/print/hook.js | 40 +-------------------- 4 files changed, 18 insertions(+), 61 deletions(-) diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index b2c245c94..d9ef50790 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -664,7 +664,6 @@ function handleCommentInEmptyParens({ comment, enclosingNode, options }) { const functionNode = isRealFunctionLikeNode(enclosingNode) || - enclosingNode.type === "HookDeclaration" || enclosingNode.type === "HookTypeAnnotation" ? enclosingNode : enclosingNode.type === "MethodDefinition" || @@ -1256,6 +1255,7 @@ const isRealFunctionLikeNode = createTypeCheckFunction([ "TSConstructorType", "TSFunctionType", "TSDeclareMethod", + "HookDeclaration", ]); const handleComments = { diff --git a/src/language-js/print/flow.js b/src/language-js/print/flow.js index c92bb8a69..380df9270 100644 --- a/src/language-js/print/flow.js +++ b/src/language-js/print/flow.js @@ -25,12 +25,9 @@ import { printEnumMember, printFlowEnumBody, } from "./enum.js"; +import { printFunction } from "./function.js"; import { printFunctionType } from "./function-type.js"; -import { - printDeclareHook, - printHook, - printHookTypeAnnotation, -} from "./hook.js"; +import { printDeclareHook, printHookTypeAnnotation } from "./hook.js"; import { printIndexedAccessType } from "./indexed-access-type.js"; import { printInferType } from "./infer-type.js"; import { printIntersectionType } from "./intersection-type.js"; @@ -74,7 +71,7 @@ function printFlow(path, options, print) { case "ComponentTypeParameter": return printComponentTypeParameter(path, options, print); case "HookDeclaration": - return printHook(path, options, print); + return printFunction(path, options, print); case "DeclareHook": return printDeclareHook(path, options, print); case "HookTypeAnnotation": diff --git a/src/language-js/print/function.js b/src/language-js/print/function.js index ead2b8d1b..49f93db18 100644 --- a/src/language-js/print/function.js +++ b/src/language-js/print/function.js @@ -36,6 +36,7 @@ const isMethodValue = ({ node, key, parent }) => /* - `FunctionDeclaration` - `FunctionExpression` +- `HookDeclaration` (Flow) - `TSDeclareFunction`(TypeScript) */ function printFunction(path, options, print, args) { @@ -63,13 +64,6 @@ function printFunction(path, options, print, args) { } } - const parts = [ - printDeclareToken(path), - node.async ? "async " : "", - `function${node.generator ? "*" : ""} `, - node.id ? print("id") : "", - ]; - const parametersDoc = printFunctionParameters( path, options, @@ -82,7 +76,16 @@ function printFunction(path, options, print, args) { returnTypeDoc, ); - parts.push( + const isFlowHookDeclaration = node.type === "HookDeclaration"; + const keyword = isFlowHookDeclaration ? "hook" : "function"; + + return [ + printDeclareToken(path), + node.async ? "async " : "", + keyword, + node.generator ? "*" : "", + " ", + node.id ? print("id") : "", print("typeParameters"), group([ shouldGroupParameters ? group(parametersDoc) : parametersDoc, @@ -90,13 +93,8 @@ function printFunction(path, options, print, args) { ]), node.body ? " " : "", print("body"), - ); - - if (options.semi && (node.declare || !node.body)) { - parts.push(";"); - } - - return parts; + options.semi && (node.declare || !node.body) ? ";" : "", + ]; } /* diff --git a/src/language-js/print/hook.js b/src/language-js/print/hook.js index 24e4850f6..ba81917d6 100644 --- a/src/language-js/print/hook.js +++ b/src/language-js/print/hook.js @@ -1,5 +1,4 @@ import { group } from "../../document/index.js"; -import { printReturnType } from "./function.js"; import { printFunctionParameters, shouldGroupFunctionParameters, @@ -11,43 +10,6 @@ import { printDeclareToken } from "./miscellaneous.js"; * @import {Doc} from "../../document/index.js" */ -/* -- `HookDeclaration` (Flow) -*/ -function printHook(path, options, print) { - const { node } = path; - - /** @type {Array} */ - const parts = ["hook"]; - if (node.id) { - parts.push(" ", print("id")); - } - - const parametersDoc = printFunctionParameters( - path, - options, - print, - /* shouldExpandArgument */ false, - /* shouldPrintTypeParameters*/ true, - ); - const returnTypeDoc = printReturnType(path, print); - const shouldGroupParameters = shouldGroupFunctionParameters( - node, - returnTypeDoc, - ); - - parts.push( - group([ - shouldGroupParameters ? group(parametersDoc) : parametersDoc, - returnTypeDoc, - ]), - node.body ? " " : "", - print("body"), - ); - - return parts; -} - /* - "DeclareHook" */ @@ -107,4 +69,4 @@ function printHookTypeAnnotation(path, options, print) { ]); } -export { printDeclareHook, printHook, printHookTypeAnnotation }; +export { printDeclareHook, printHookTypeAnnotation };