From b85bff8d1f01095a7bdc717a8763486c2ca036e4 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 10 Oct 2025 21:20:07 +0800 Subject: [PATCH] Remove `printFunctionTypeParameters` (#18031) --- src/language-js/print/arrow-function.js | 8 ++--- src/language-js/print/call-expression.js | 18 ++++++++++-- src/language-js/print/function-parameters.js | 31 ++++++++++++-------- src/language-js/print/function.js | 12 ++++---- src/language-js/print/hook.js | 10 +++---- src/language-js/print/member-chain.js | 10 ++----- src/language-js/print/misc.js | 20 ------------- src/language-js/print/type-annotation.js | 4 +-- src/language-js/print/typescript.js | 4 +-- 9 files changed, 55 insertions(+), 62 deletions(-) diff --git a/src/language-js/print/arrow-function.js b/src/language-js/print/arrow-function.js index 8f7069fd0..32ddedc25 100644 --- a/src/language-js/print/arrow-function.js +++ b/src/language-js/print/arrow-function.js @@ -177,9 +177,9 @@ function printArrowFunctionSignature(path, options, print, args) { if (shouldPrintParamsWithoutParens(path, options)) { parts.push(print(["params", 0])); } else { - const expandArg = args.expandLastArg || args.expandFirstArg; + const shouldExpandArgument = args.expandLastArg || args.expandFirstArg; let returnTypeDoc = printReturnType(path, print); - if (expandArg) { + if (shouldExpandArgument) { if (willBreak(returnTypeDoc)) { throw new ArgExpansionBailout(); } @@ -191,8 +191,8 @@ function printArrowFunctionSignature(path, options, print, args) { path, options, print, - expandArg, - /* printTypeParams */ true, + shouldExpandArgument, + /* shouldPrintTypeParameters */ true, ), returnTypeDoc, ]), diff --git a/src/language-js/print/call-expression.js b/src/language-js/print/call-expression.js index 4a70dd5a9..c86399dbc 100644 --- a/src/language-js/print/call-expression.js +++ b/src/language-js/print/call-expression.js @@ -13,14 +13,26 @@ import { } from "../utils/index.js"; import printCallArguments from "./call-arguments.js"; import printMemberChain from "./member-chain.js"; -import { printFunctionTypeParameters, printOptionalToken } from "./misc.js"; +import { printOptionalToken } from "./misc.js"; +/* +- `NewExpression` +- `ImportExpression` +- `OptionalCallExpression` +- `CallExpression` +- `TSImportType` (TypeScript) +*/ function printCallExpression(path, options, print) { const { node } = path; const isNewExpression = node.type === "NewExpression"; const optional = printOptionalToken(path); const args = getCallArguments(node); + // `TSImportType.typeArguments` is after `qualifier`, not before the "arguments" + const typeArgumentsDoc = + node.type !== "TSImportType" && node.typeArguments + ? print("typeArguments") + : ""; const isTemplateLiteralSingleArg = args.length === 1 && isTemplateOnItsOwnLine(args[0], options.originalText); @@ -47,7 +59,7 @@ function printCallExpression(path, options, print) { isNewExpression ? "new " : "", printCallee(path, print), optional, - printFunctionTypeParameters(path, options, print), + typeArgumentsDoc, "(", join(", ", printed), ")", @@ -77,7 +89,7 @@ function printCallExpression(path, options, print) { isNewExpression ? "new " : "", printCallee(path, print), optional, - printFunctionTypeParameters(path, options, print), + typeArgumentsDoc, printCallArguments(path, options, print), ]; diff --git a/src/language-js/print/function-parameters.js b/src/language-js/print/function-parameters.js index 50e9d8b27..a4340b1e3 100644 --- a/src/language-js/print/function-parameters.js +++ b/src/language-js/print/function-parameters.js @@ -27,7 +27,6 @@ import { iterateFunctionParametersPath, shouldPrintComma, } from "../utils/index.js"; -import { printFunctionTypeParameters } from "./misc.js"; /** @import AstPath from "../../common/ast-path.js" */ @@ -35,18 +34,19 @@ function printFunctionParameters( path, options, print, - expandArg, - printTypeParams, + shouldExpandArgument, + shouldPrintTypeParameters, ) { const functionNode = path.node; const parameters = getFunctionParameters(functionNode); - const typeParams = printTypeParams - ? printFunctionTypeParameters(path, options, print) - : ""; + const typeParametersDoc = + shouldPrintTypeParameters && functionNode.typeParameters + ? print("typeParameters") + : ""; if (parameters.length === 0) { return [ - typeParams, + typeParametersDoc, "(", printDanglingComments(path, options, { filter: (comment) => @@ -92,12 +92,17 @@ function printFunctionParameters( // } b, // ) ) => { // }) - if (expandArg && !isDecoratedFunction(path)) { - if (willBreak(typeParams) || willBreak(printed)) { + if (shouldExpandArgument && !isDecoratedFunction(path)) { + if (willBreak(typeParametersDoc) || willBreak(printed)) { // Removing lines in this case leads to broken or ugly output throw new ArgExpansionBailout(); } - return group([removeLines(typeParams), "(", removeLines(printed), ")"]); + return group([ + removeLines(typeParametersDoc), + "(", + removeLines(printed), + ")", + ]); } // Single object destructuring should hug @@ -111,12 +116,12 @@ function printFunctionParameters( (node) => !isNonEmptyArray(node.decorators), ); if (shouldHugParameters && hasNotParameterDecorator) { - return [typeParams, "(", ...printed, ")"]; + return [typeParametersDoc, "(", ...printed, ")"]; } // don't break in specs, eg; `it("should maintain parens around done even when long", (done) => {})` if (isParametersInTestCall) { - return [typeParams, "(", ...printed, ")"]; + return [typeParametersDoc, "(", ...printed, ")"]; } const isFlowShorthandWithOneArg = @@ -147,7 +152,7 @@ function printFunctionParameters( } return [ - typeParams, + typeParametersDoc, "(", indent([softline, ...printed]), ifBreak( diff --git a/src/language-js/print/function.js b/src/language-js/print/function.js index 163c511b1..b4d9d5eaa 100644 --- a/src/language-js/print/function.js +++ b/src/language-js/print/function.js @@ -27,7 +27,7 @@ import { shouldBreakFunctionParameters, shouldGroupFunctionParameters, } from "./function-parameters.js"; -import { printDeclareToken, printFunctionTypeParameters } from "./misc.js"; +import { printDeclareToken } from "./misc.js"; import { printPropertyKey } from "./property.js"; import { printTypeAnnotationProperty } from "./type-annotation.js"; @@ -59,7 +59,7 @@ function printFunction(path, options, print, args) { const { node } = path; - let expandArg = false; + let shouldExpandArgument = false; if ( (node.type === "FunctionDeclaration" || node.type === "FunctionExpression") && @@ -73,7 +73,7 @@ function printFunction(path, options, print, args) { (param) => param.type === "Identifier" && !param.typeAnnotation, )) ) { - expandArg = true; + shouldExpandArgument = true; } } @@ -88,7 +88,7 @@ function printFunction(path, options, print, args) { path, options, print, - expandArg, + shouldExpandArgument, ); const returnTypeDoc = printReturnType(path, print); const shouldGroupParameters = shouldGroupFunctionParameters( @@ -97,7 +97,7 @@ function printFunction(path, options, print, args) { ); parts.push( - printFunctionTypeParameters(path, options, print), + print("typeParameters"), group([ shouldGroupParameters ? group(parametersDoc) : parametersDoc, returnTypeDoc, @@ -163,7 +163,7 @@ function printMethodValue(path, options, print) { returnTypeDoc, ); const parts = [ - printFunctionTypeParameters(path, options, print), + print("typeParameters"), group([ shouldBreakParameters ? group(parametersDoc, { shouldBreak: true }) diff --git a/src/language-js/print/hook.js b/src/language-js/print/hook.js index fd295056b..f66547203 100644 --- a/src/language-js/print/hook.js +++ b/src/language-js/print/hook.js @@ -27,8 +27,8 @@ function printHook(path, options, print) { path, options, print, - false, - true, + /* shouldExpandArgument */ false, + /* shouldPrintTypeParameters*/ true, ); const returnTypeDoc = printReturnType(path, print); const shouldGroupParameters = shouldGroupFunctionParameters( @@ -80,7 +80,7 @@ function isDeclareHookTypeAnnotation(path) { } /* -- "HookTypeAnnotation" +- `HookTypeAnnotation` (Flow) */ function printHookTypeAnnotation(path, options, print) { const { node } = path; @@ -89,8 +89,8 @@ function printHookTypeAnnotation(path, options, print) { path, options, print, - /* expandArg */ false, - /* printTypeParams */ true, + /* shouldExpandArgument */ false, + /* shouldPrintTypeParameters */ true, ); const returnTypeDoc = [ diff --git a/src/language-js/print/member-chain.js b/src/language-js/print/member-chain.js index 57c6d1592..a9f12b1e8 100644 --- a/src/language-js/print/member-chain.js +++ b/src/language-js/print/member-chain.js @@ -27,11 +27,7 @@ import { } from "../utils/index.js"; import printCallArguments from "./call-arguments.js"; import { printMemberLookup } from "./member.js"; -import { - printBindExpressionCallee, - printFunctionTypeParameters, - printOptionalToken, -} from "./misc.js"; +import { printBindExpressionCallee, printOptionalToken } from "./misc.js"; /** * @import {Doc} from "../../document/builders.js" @@ -114,7 +110,7 @@ function printMemberChain(path, options, print) { path, [ printOptionalToken(path), - printFunctionTypeParameters(path, options, print), + print("typeArguments"), printCallArguments(path, options, print), ], options, @@ -157,7 +153,7 @@ function printMemberChain(path, options, print) { node, printed: [ printOptionalToken(path), - printFunctionTypeParameters(path, options, print), + print("typeArguments"), printCallArguments(path, options, print), ], }); diff --git a/src/language-js/print/misc.js b/src/language-js/print/misc.js index e2a8d284b..bf08be750 100644 --- a/src/language-js/print/misc.js +++ b/src/language-js/print/misc.js @@ -90,25 +90,6 @@ function printAbstractToken({ node }) { return node.abstract || tsAbstractNodeTypes.has(node.type) ? "abstract " : ""; } -function printFunctionTypeParameters(path, options, print) { - const { node } = path; - - // `TSImportType.typeArguments` is after `qualifier`, not before the "arguments" - if (node.type === "TSImportType") { - return ""; - } - - if (node.typeArguments) { - return print("typeArguments"); - } - - if (node.typeParameters) { - return print("typeParameters"); - } - - return ""; -} - function printBindExpressionCallee(path, options, print) { return ["::", print("callee")]; } @@ -139,7 +120,6 @@ export { printBindExpressionCallee, printDeclareToken, printDefiniteToken, - printFunctionTypeParameters, printOptionalToken, printRestSpread, printTypeScriptAccessibilityToken, diff --git a/src/language-js/print/type-annotation.js b/src/language-js/print/type-annotation.js index 0c267c08c..acf43974e 100644 --- a/src/language-js/print/type-annotation.js +++ b/src/language-js/print/type-annotation.js @@ -317,8 +317,8 @@ function printFunctionType(path, options, print) { path, options, print, - /* expandArg */ false, - /* printTypeParams */ true, + /* shouldExpandArgument */ false, + /* shouldPrintTypeParameters */ true, ); const returnTypeDoc = []; diff --git a/src/language-js/print/typescript.js b/src/language-js/print/typescript.js index f1a6ca9ce..57d02b9a9 100644 --- a/src/language-js/print/typescript.js +++ b/src/language-js/print/typescript.js @@ -236,8 +236,8 @@ function printTypescript(path, options, print) { path, options, print, - /* expandArg */ false, - /* printTypeParams */ true, + /* shouldExpandArgument */ false, + /* shouldPrintTypeParameters */ true, ); const returnTypeDoc = printTypeAnnotationProperty(