Remove printFunctionTypeParameters (#18031)
This commit is contained in:
1 parent
7df3ed6e3a
commit
b85bff8d1f
9 files changed
+55
-62
No files matched your search
@@ -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,
|
||||
]),
|
||||
|
||||
@@ -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),
|
||||
];
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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 })
|
||||
|
||||
@@ -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 = [
|
||||
|
||||
@@ -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),
|
||||
],
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -317,8 +317,8 @@ function printFunctionType(path, options, print) {
|
||||
path,
|
||||
options,
|
||||
print,
|
||||
/* expandArg */ false,
|
||||
/* printTypeParams */ true,
|
||||
/* shouldExpandArgument */ false,
|
||||
/* shouldPrintTypeParameters */ true,
|
||||
);
|
||||
|
||||
const returnTypeDoc = [];
|
||||
|
||||
@@ -236,8 +236,8 @@ function printTypescript(path, options, print) {
|
||||
path,
|
||||
options,
|
||||
print,
|
||||
/* expandArg */ false,
|
||||
/* printTypeParams */ true,
|
||||
/* shouldExpandArgument */ false,
|
||||
/* shouldPrintTypeParameters */ true,
|
||||
);
|
||||
|
||||
const returnTypeDoc = printTypeAnnotationProperty(
|
||||
|
||||
Reference in new issue
Block a user