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)) {
|
if (shouldPrintParamsWithoutParens(path, options)) {
|
||||||
parts.push(print(["params", 0]));
|
parts.push(print(["params", 0]));
|
||||||
} else {
|
} else {
|
||||||
const expandArg = args.expandLastArg || args.expandFirstArg;
|
const shouldExpandArgument = args.expandLastArg || args.expandFirstArg;
|
||||||
let returnTypeDoc = printReturnType(path, print);
|
let returnTypeDoc = printReturnType(path, print);
|
||||||
if (expandArg) {
|
if (shouldExpandArgument) {
|
||||||
if (willBreak(returnTypeDoc)) {
|
if (willBreak(returnTypeDoc)) {
|
||||||
throw new ArgExpansionBailout();
|
throw new ArgExpansionBailout();
|
||||||
}
|
}
|
||||||
@@ -191,8 +191,8 @@ function printArrowFunctionSignature(path, options, print, args) {
|
|||||||
path,
|
path,
|
||||||
options,
|
options,
|
||||||
print,
|
print,
|
||||||
expandArg,
|
shouldExpandArgument,
|
||||||
/* printTypeParams */ true,
|
/* shouldPrintTypeParameters */ true,
|
||||||
),
|
),
|
||||||
returnTypeDoc,
|
returnTypeDoc,
|
||||||
]),
|
]),
|
||||||
|
|||||||
@@ -13,14 +13,26 @@ import {
|
|||||||
} from "../utils/index.js";
|
} from "../utils/index.js";
|
||||||
import printCallArguments from "./call-arguments.js";
|
import printCallArguments from "./call-arguments.js";
|
||||||
import printMemberChain from "./member-chain.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) {
|
function printCallExpression(path, options, print) {
|
||||||
const { node } = path;
|
const { node } = path;
|
||||||
const isNewExpression = node.type === "NewExpression";
|
const isNewExpression = node.type === "NewExpression";
|
||||||
|
|
||||||
const optional = printOptionalToken(path);
|
const optional = printOptionalToken(path);
|
||||||
const args = getCallArguments(node);
|
const args = getCallArguments(node);
|
||||||
|
// `TSImportType.typeArguments` is after `qualifier`, not before the "arguments"
|
||||||
|
const typeArgumentsDoc =
|
||||||
|
node.type !== "TSImportType" && node.typeArguments
|
||||||
|
? print("typeArguments")
|
||||||
|
: "";
|
||||||
|
|
||||||
const isTemplateLiteralSingleArg =
|
const isTemplateLiteralSingleArg =
|
||||||
args.length === 1 && isTemplateOnItsOwnLine(args[0], options.originalText);
|
args.length === 1 && isTemplateOnItsOwnLine(args[0], options.originalText);
|
||||||
@@ -47,7 +59,7 @@ function printCallExpression(path, options, print) {
|
|||||||
isNewExpression ? "new " : "",
|
isNewExpression ? "new " : "",
|
||||||
printCallee(path, print),
|
printCallee(path, print),
|
||||||
optional,
|
optional,
|
||||||
printFunctionTypeParameters(path, options, print),
|
typeArgumentsDoc,
|
||||||
"(",
|
"(",
|
||||||
join(", ", printed),
|
join(", ", printed),
|
||||||
")",
|
")",
|
||||||
@@ -77,7 +89,7 @@ function printCallExpression(path, options, print) {
|
|||||||
isNewExpression ? "new " : "",
|
isNewExpression ? "new " : "",
|
||||||
printCallee(path, print),
|
printCallee(path, print),
|
||||||
optional,
|
optional,
|
||||||
printFunctionTypeParameters(path, options, print),
|
typeArgumentsDoc,
|
||||||
printCallArguments(path, options, print),
|
printCallArguments(path, options, print),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ import {
|
|||||||
iterateFunctionParametersPath,
|
iterateFunctionParametersPath,
|
||||||
shouldPrintComma,
|
shouldPrintComma,
|
||||||
} from "../utils/index.js";
|
} from "../utils/index.js";
|
||||||
import { printFunctionTypeParameters } from "./misc.js";
|
|
||||||
|
|
||||||
/** @import AstPath from "../../common/ast-path.js" */
|
/** @import AstPath from "../../common/ast-path.js" */
|
||||||
|
|
||||||
@@ -35,18 +34,19 @@ function printFunctionParameters(
|
|||||||
path,
|
path,
|
||||||
options,
|
options,
|
||||||
print,
|
print,
|
||||||
expandArg,
|
shouldExpandArgument,
|
||||||
printTypeParams,
|
shouldPrintTypeParameters,
|
||||||
) {
|
) {
|
||||||
const functionNode = path.node;
|
const functionNode = path.node;
|
||||||
const parameters = getFunctionParameters(functionNode);
|
const parameters = getFunctionParameters(functionNode);
|
||||||
const typeParams = printTypeParams
|
const typeParametersDoc =
|
||||||
? printFunctionTypeParameters(path, options, print)
|
shouldPrintTypeParameters && functionNode.typeParameters
|
||||||
: "";
|
? print("typeParameters")
|
||||||
|
: "";
|
||||||
|
|
||||||
if (parameters.length === 0) {
|
if (parameters.length === 0) {
|
||||||
return [
|
return [
|
||||||
typeParams,
|
typeParametersDoc,
|
||||||
"(",
|
"(",
|
||||||
printDanglingComments(path, options, {
|
printDanglingComments(path, options, {
|
||||||
filter: (comment) =>
|
filter: (comment) =>
|
||||||
@@ -92,12 +92,17 @@ function printFunctionParameters(
|
|||||||
// } b,
|
// } b,
|
||||||
// ) ) => {
|
// ) ) => {
|
||||||
// })
|
// })
|
||||||
if (expandArg && !isDecoratedFunction(path)) {
|
if (shouldExpandArgument && !isDecoratedFunction(path)) {
|
||||||
if (willBreak(typeParams) || willBreak(printed)) {
|
if (willBreak(typeParametersDoc) || willBreak(printed)) {
|
||||||
// Removing lines in this case leads to broken or ugly output
|
// Removing lines in this case leads to broken or ugly output
|
||||||
throw new ArgExpansionBailout();
|
throw new ArgExpansionBailout();
|
||||||
}
|
}
|
||||||
return group([removeLines(typeParams), "(", removeLines(printed), ")"]);
|
return group([
|
||||||
|
removeLines(typeParametersDoc),
|
||||||
|
"(",
|
||||||
|
removeLines(printed),
|
||||||
|
")",
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Single object destructuring should hug
|
// Single object destructuring should hug
|
||||||
@@ -111,12 +116,12 @@ function printFunctionParameters(
|
|||||||
(node) => !isNonEmptyArray(node.decorators),
|
(node) => !isNonEmptyArray(node.decorators),
|
||||||
);
|
);
|
||||||
if (shouldHugParameters && hasNotParameterDecorator) {
|
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) => {})`
|
// don't break in specs, eg; `it("should maintain parens around done even when long", (done) => {})`
|
||||||
if (isParametersInTestCall) {
|
if (isParametersInTestCall) {
|
||||||
return [typeParams, "(", ...printed, ")"];
|
return [typeParametersDoc, "(", ...printed, ")"];
|
||||||
}
|
}
|
||||||
|
|
||||||
const isFlowShorthandWithOneArg =
|
const isFlowShorthandWithOneArg =
|
||||||
@@ -147,7 +152,7 @@ function printFunctionParameters(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
typeParams,
|
typeParametersDoc,
|
||||||
"(",
|
"(",
|
||||||
indent([softline, ...printed]),
|
indent([softline, ...printed]),
|
||||||
ifBreak(
|
ifBreak(
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ import {
|
|||||||
shouldBreakFunctionParameters,
|
shouldBreakFunctionParameters,
|
||||||
shouldGroupFunctionParameters,
|
shouldGroupFunctionParameters,
|
||||||
} from "./function-parameters.js";
|
} from "./function-parameters.js";
|
||||||
import { printDeclareToken, printFunctionTypeParameters } from "./misc.js";
|
import { printDeclareToken } from "./misc.js";
|
||||||
import { printPropertyKey } from "./property.js";
|
import { printPropertyKey } from "./property.js";
|
||||||
import { printTypeAnnotationProperty } from "./type-annotation.js";
|
import { printTypeAnnotationProperty } from "./type-annotation.js";
|
||||||
|
|
||||||
@@ -59,7 +59,7 @@ function printFunction(path, options, print, args) {
|
|||||||
|
|
||||||
const { node } = path;
|
const { node } = path;
|
||||||
|
|
||||||
let expandArg = false;
|
let shouldExpandArgument = false;
|
||||||
if (
|
if (
|
||||||
(node.type === "FunctionDeclaration" ||
|
(node.type === "FunctionDeclaration" ||
|
||||||
node.type === "FunctionExpression") &&
|
node.type === "FunctionExpression") &&
|
||||||
@@ -73,7 +73,7 @@ function printFunction(path, options, print, args) {
|
|||||||
(param) => param.type === "Identifier" && !param.typeAnnotation,
|
(param) => param.type === "Identifier" && !param.typeAnnotation,
|
||||||
))
|
))
|
||||||
) {
|
) {
|
||||||
expandArg = true;
|
shouldExpandArgument = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@ function printFunction(path, options, print, args) {
|
|||||||
path,
|
path,
|
||||||
options,
|
options,
|
||||||
print,
|
print,
|
||||||
expandArg,
|
shouldExpandArgument,
|
||||||
);
|
);
|
||||||
const returnTypeDoc = printReturnType(path, print);
|
const returnTypeDoc = printReturnType(path, print);
|
||||||
const shouldGroupParameters = shouldGroupFunctionParameters(
|
const shouldGroupParameters = shouldGroupFunctionParameters(
|
||||||
@@ -97,7 +97,7 @@ function printFunction(path, options, print, args) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
parts.push(
|
parts.push(
|
||||||
printFunctionTypeParameters(path, options, print),
|
print("typeParameters"),
|
||||||
group([
|
group([
|
||||||
shouldGroupParameters ? group(parametersDoc) : parametersDoc,
|
shouldGroupParameters ? group(parametersDoc) : parametersDoc,
|
||||||
returnTypeDoc,
|
returnTypeDoc,
|
||||||
@@ -163,7 +163,7 @@ function printMethodValue(path, options, print) {
|
|||||||
returnTypeDoc,
|
returnTypeDoc,
|
||||||
);
|
);
|
||||||
const parts = [
|
const parts = [
|
||||||
printFunctionTypeParameters(path, options, print),
|
print("typeParameters"),
|
||||||
group([
|
group([
|
||||||
shouldBreakParameters
|
shouldBreakParameters
|
||||||
? group(parametersDoc, { shouldBreak: true })
|
? group(parametersDoc, { shouldBreak: true })
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ function printHook(path, options, print) {
|
|||||||
path,
|
path,
|
||||||
options,
|
options,
|
||||||
print,
|
print,
|
||||||
false,
|
/* shouldExpandArgument */ false,
|
||||||
true,
|
/* shouldPrintTypeParameters*/ true,
|
||||||
);
|
);
|
||||||
const returnTypeDoc = printReturnType(path, print);
|
const returnTypeDoc = printReturnType(path, print);
|
||||||
const shouldGroupParameters = shouldGroupFunctionParameters(
|
const shouldGroupParameters = shouldGroupFunctionParameters(
|
||||||
@@ -80,7 +80,7 @@ function isDeclareHookTypeAnnotation(path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
- "HookTypeAnnotation"
|
- `HookTypeAnnotation` (Flow)
|
||||||
*/
|
*/
|
||||||
function printHookTypeAnnotation(path, options, print) {
|
function printHookTypeAnnotation(path, options, print) {
|
||||||
const { node } = path;
|
const { node } = path;
|
||||||
@@ -89,8 +89,8 @@ function printHookTypeAnnotation(path, options, print) {
|
|||||||
path,
|
path,
|
||||||
options,
|
options,
|
||||||
print,
|
print,
|
||||||
/* expandArg */ false,
|
/* shouldExpandArgument */ false,
|
||||||
/* printTypeParams */ true,
|
/* shouldPrintTypeParameters */ true,
|
||||||
);
|
);
|
||||||
|
|
||||||
const returnTypeDoc = [
|
const returnTypeDoc = [
|
||||||
|
|||||||
@@ -27,11 +27,7 @@ import {
|
|||||||
} from "../utils/index.js";
|
} from "../utils/index.js";
|
||||||
import printCallArguments from "./call-arguments.js";
|
import printCallArguments from "./call-arguments.js";
|
||||||
import { printMemberLookup } from "./member.js";
|
import { printMemberLookup } from "./member.js";
|
||||||
import {
|
import { printBindExpressionCallee, printOptionalToken } from "./misc.js";
|
||||||
printBindExpressionCallee,
|
|
||||||
printFunctionTypeParameters,
|
|
||||||
printOptionalToken,
|
|
||||||
} from "./misc.js";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @import {Doc} from "../../document/builders.js"
|
* @import {Doc} from "../../document/builders.js"
|
||||||
@@ -114,7 +110,7 @@ function printMemberChain(path, options, print) {
|
|||||||
path,
|
path,
|
||||||
[
|
[
|
||||||
printOptionalToken(path),
|
printOptionalToken(path),
|
||||||
printFunctionTypeParameters(path, options, print),
|
print("typeArguments"),
|
||||||
printCallArguments(path, options, print),
|
printCallArguments(path, options, print),
|
||||||
],
|
],
|
||||||
options,
|
options,
|
||||||
@@ -157,7 +153,7 @@ function printMemberChain(path, options, print) {
|
|||||||
node,
|
node,
|
||||||
printed: [
|
printed: [
|
||||||
printOptionalToken(path),
|
printOptionalToken(path),
|
||||||
printFunctionTypeParameters(path, options, print),
|
print("typeArguments"),
|
||||||
printCallArguments(path, options, print),
|
printCallArguments(path, options, print),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -90,25 +90,6 @@ function printAbstractToken({ node }) {
|
|||||||
return node.abstract || tsAbstractNodeTypes.has(node.type) ? "abstract " : "";
|
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) {
|
function printBindExpressionCallee(path, options, print) {
|
||||||
return ["::", print("callee")];
|
return ["::", print("callee")];
|
||||||
}
|
}
|
||||||
@@ -139,7 +120,6 @@ export {
|
|||||||
printBindExpressionCallee,
|
printBindExpressionCallee,
|
||||||
printDeclareToken,
|
printDeclareToken,
|
||||||
printDefiniteToken,
|
printDefiniteToken,
|
||||||
printFunctionTypeParameters,
|
|
||||||
printOptionalToken,
|
printOptionalToken,
|
||||||
printRestSpread,
|
printRestSpread,
|
||||||
printTypeScriptAccessibilityToken,
|
printTypeScriptAccessibilityToken,
|
||||||
|
|||||||
@@ -317,8 +317,8 @@ function printFunctionType(path, options, print) {
|
|||||||
path,
|
path,
|
||||||
options,
|
options,
|
||||||
print,
|
print,
|
||||||
/* expandArg */ false,
|
/* shouldExpandArgument */ false,
|
||||||
/* printTypeParams */ true,
|
/* shouldPrintTypeParameters */ true,
|
||||||
);
|
);
|
||||||
|
|
||||||
const returnTypeDoc = [];
|
const returnTypeDoc = [];
|
||||||
|
|||||||
@@ -236,8 +236,8 @@ function printTypescript(path, options, print) {
|
|||||||
path,
|
path,
|
||||||
options,
|
options,
|
||||||
print,
|
print,
|
||||||
/* expandArg */ false,
|
/* shouldExpandArgument */ false,
|
||||||
/* printTypeParams */ true,
|
/* shouldPrintTypeParameters */ true,
|
||||||
);
|
);
|
||||||
|
|
||||||
const returnTypeDoc = printTypeAnnotationProperty(
|
const returnTypeDoc = printTypeAnnotationProperty(
|
||||||
|
|||||||
Reference in new issue
Block a user