Merge printHook into printFunction (#18632)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-01-15 04:04:03 +08:00
1 parent b0ffc1208d
commit d40da4a260
4 files changed
+18 -61

No files matched your search

+1 -1
View File
@@ -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 = {
+3 -6
View File
@@ -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":
+13 -15
View File
@@ -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) ? ";" : "",
];
}
/*
+1 -39
View File
@@ -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<Doc>} */
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 };