Reuse logic for function parameters in flow components (#18633)
This commit is contained in:
1 parent
cc35f506e2
commit
f48f89e182
4 files changed
+34
-137
No files matched your search
@@ -14,7 +14,6 @@ import getTextWithoutComments from "../utilities/get-text-without-comments.js";
|
||||
import {
|
||||
createTypeCheckFunction,
|
||||
getCallArguments,
|
||||
getComponentParameters,
|
||||
getFunctionParameters,
|
||||
isBinaryCastExpression,
|
||||
isCallExpression,
|
||||
@@ -67,7 +66,6 @@ function handleOwnLineComment(context) {
|
||||
handleIgnoreComments,
|
||||
handleConditionalExpressionComments,
|
||||
handleLastFunctionParameterComments,
|
||||
handleLastComponentArgComments,
|
||||
handleMemberExpressionComments,
|
||||
handleIfStatementComments,
|
||||
handleWhileComments,
|
||||
@@ -666,20 +664,12 @@ function handleCommentInEmptyParens({ comment, enclosingNode, options }) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
isFlowComponent(enclosingNode) &&
|
||||
getComponentParameters(enclosingNode).length === 0 &&
|
||||
isInArgumentOrParameterParentheses(enclosingNode, comment, options)
|
||||
) {
|
||||
addDanglingComment(enclosingNode, comment);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Only add dangling comments to fix the case when no params are present,
|
||||
// i.e. a function without any argument.
|
||||
|
||||
const functionNode =
|
||||
isRealFunctionLikeNode(enclosingNode) ||
|
||||
isFlowComponent(enclosingNode) ||
|
||||
enclosingNode.type === "HookTypeAnnotation"
|
||||
? enclosingNode
|
||||
: enclosingNode.type === "MethodDefinition" ||
|
||||
@@ -700,39 +690,6 @@ function handleCommentInEmptyParens({ comment, enclosingNode, options }) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function handleLastComponentArgComments({
|
||||
comment,
|
||||
precedingNode,
|
||||
enclosingNode,
|
||||
followingNode,
|
||||
text,
|
||||
}) {
|
||||
// "DeclareComponent" and "ComponentTypeAnnotation" definitions
|
||||
if (
|
||||
precedingNode?.type === "ComponentTypeParameter" &&
|
||||
(enclosingNode?.type === "DeclareComponent" ||
|
||||
enclosingNode?.type === "ComponentTypeAnnotation") &&
|
||||
followingNode?.type !== "ComponentTypeParameter"
|
||||
) {
|
||||
addTrailingComment(precedingNode, comment);
|
||||
return true;
|
||||
}
|
||||
|
||||
// "ComponentParameter" definitions
|
||||
if (
|
||||
(precedingNode?.type === "ComponentParameter" ||
|
||||
precedingNode?.type === "RestElement") &&
|
||||
(enclosingNode?.type === "ComponentDeclaration" ||
|
||||
enclosingNode?.type === "DeclareComponent") &&
|
||||
getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) === ")"
|
||||
) {
|
||||
addTrailingComment(precedingNode, comment);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function handleLastFunctionParameterComments({
|
||||
comment,
|
||||
precedingNode,
|
||||
@@ -750,6 +707,17 @@ function handleLastFunctionParameterComments({
|
||||
return true;
|
||||
}
|
||||
|
||||
// "DeclareComponent" and "ComponentTypeAnnotation" definitions
|
||||
if (
|
||||
precedingNode?.type === "ComponentTypeParameter" &&
|
||||
(enclosingNode?.type === "DeclareComponent" ||
|
||||
enclosingNode?.type === "ComponentTypeAnnotation") &&
|
||||
followingNode?.type !== "ComponentTypeParameter"
|
||||
) {
|
||||
addTrailingComment(precedingNode, comment);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Real functions and TypeScript function type definitions
|
||||
if (
|
||||
(precedingNode?.type === "Identifier" ||
|
||||
@@ -765,6 +733,18 @@ function handleLastFunctionParameterComments({
|
||||
return true;
|
||||
}
|
||||
|
||||
// "ComponentParameter" definitions
|
||||
if (
|
||||
(precedingNode?.type === "ComponentParameter" ||
|
||||
precedingNode?.type === "RestElement") &&
|
||||
(enclosingNode?.type === "ComponentDeclaration" ||
|
||||
enclosingNode?.type === "DeclareComponent") &&
|
||||
getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) === ")"
|
||||
) {
|
||||
addTrailingComment(precedingNode, comment);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Comment between function parameters parentheses and function body
|
||||
if (
|
||||
!isBlockComment(comment) &&
|
||||
|
||||
@@ -1,21 +1,6 @@
|
||||
import {
|
||||
group,
|
||||
hardline,
|
||||
ifBreak,
|
||||
indent,
|
||||
line,
|
||||
softline,
|
||||
} from "../../document/index.js";
|
||||
import {
|
||||
getComponentParameters,
|
||||
isNextLineEmpty,
|
||||
iterateComponentParametersPath,
|
||||
shouldPrintComma,
|
||||
} from "../utilities/index.js";
|
||||
import {
|
||||
printDanglingCommentsInList,
|
||||
printDeclareToken,
|
||||
} from "./miscellaneous.js";
|
||||
import { group } from "../../document/index.js";
|
||||
import { printFunctionParameters } from "./function-parameters.js";
|
||||
import { printDeclareToken } from "./miscellaneous.js";
|
||||
|
||||
/**
|
||||
* @import AstPath from "../../common/ast-path.js"
|
||||
@@ -23,9 +8,9 @@ import {
|
||||
*/
|
||||
|
||||
/*
|
||||
- "ComponentDeclaration"
|
||||
- "DeclareComponent"
|
||||
- "ComponentTypeAnnotation"
|
||||
- `ComponentDeclaration` (Flow)
|
||||
- `DeclareComponent` (Flow)
|
||||
- `ComponentTypeAnnotation` (Flow)
|
||||
*/
|
||||
function printComponent(path, options, print) {
|
||||
const { node } = path;
|
||||
@@ -37,7 +22,7 @@ function printComponent(path, options, print) {
|
||||
|
||||
parts.push(print("typeParameters"));
|
||||
|
||||
const parametersDoc = printComponentParameters(path, options, print);
|
||||
const parametersDoc = printFunctionParameters(path, options, print);
|
||||
if (node.rendersType) {
|
||||
parts.push(group([parametersDoc, " ", print("rendersType")]));
|
||||
} else {
|
||||
@@ -55,50 +40,6 @@ function printComponent(path, options, print) {
|
||||
return parts;
|
||||
}
|
||||
|
||||
function printComponentParameters(path, options, print) {
|
||||
const { node: componentNode } = path;
|
||||
const parameters = getComponentParameters(componentNode);
|
||||
|
||||
if (parameters.length === 0) {
|
||||
return ["(", printDanglingCommentsInList(path, options), ")"];
|
||||
}
|
||||
|
||||
const printed = [];
|
||||
iterateComponentParametersPath(path, (parameterPath, index) => {
|
||||
const isLastParameter = index === parameters.length - 1;
|
||||
if (isLastParameter && componentNode.rest) {
|
||||
printed.push("...");
|
||||
}
|
||||
printed.push(print());
|
||||
if (isLastParameter) {
|
||||
return;
|
||||
}
|
||||
printed.push(",");
|
||||
if (isNextLineEmpty(parameters[index], options)) {
|
||||
printed.push(hardline, hardline);
|
||||
} else {
|
||||
printed.push(line);
|
||||
}
|
||||
});
|
||||
|
||||
return [
|
||||
"(",
|
||||
indent([softline, ...printed]),
|
||||
ifBreak(
|
||||
shouldPrintComma(options, "all") &&
|
||||
!hasRestParameter(componentNode, parameters)
|
||||
? ","
|
||||
: "",
|
||||
),
|
||||
softline,
|
||||
")",
|
||||
];
|
||||
}
|
||||
|
||||
function hasRestParameter(componentNode, parameters) {
|
||||
return componentNode.rest || parameters.at(-1)?.type === "RestElement";
|
||||
}
|
||||
|
||||
/*
|
||||
- `ComponentParameter` (Flow)
|
||||
*/
|
||||
|
||||
@@ -55,6 +55,9 @@ const functionParameterDanglingCommentFilter = (comment) =>
|
||||
- `FunctionTypeAnnotation` (Flow)
|
||||
- `HookDeclaration` (Flow)
|
||||
- `HookTypeAnnotation` (Flow)
|
||||
- `ComponentDeclaration` (Flow)
|
||||
- `DeclareComponent` (Flow)
|
||||
- `ComponentTypeAnnotation` (Flow)
|
||||
*/
|
||||
function printFunctionParameters(
|
||||
path,
|
||||
|
||||
@@ -849,31 +849,6 @@ function iterateFunctionParametersPath(path, iteratee) {
|
||||
}
|
||||
}
|
||||
|
||||
const componentParametersCache = new WeakMap();
|
||||
function getComponentParameters(node) {
|
||||
if (componentParametersCache.has(node)) {
|
||||
return componentParametersCache.get(node);
|
||||
}
|
||||
|
||||
const parameters = [...node.params];
|
||||
if (node.rest) {
|
||||
parameters.push(node.rest);
|
||||
}
|
||||
|
||||
componentParametersCache.set(node, parameters);
|
||||
return parameters;
|
||||
}
|
||||
|
||||
function iterateComponentParametersPath(path, iteratee) {
|
||||
const { node } = path;
|
||||
let index = 0;
|
||||
const callback = () => iteratee(path, index++);
|
||||
path.each(callback, "params");
|
||||
if (node.rest) {
|
||||
path.call(callback, "rest");
|
||||
}
|
||||
}
|
||||
|
||||
const callArgumentsCache = new WeakMap();
|
||||
function getCallArguments(node) {
|
||||
if (callArgumentsCache.has(node)) {
|
||||
@@ -1144,7 +1119,6 @@ export {
|
||||
getCallArguments,
|
||||
getCallArgumentSelector,
|
||||
getComments,
|
||||
getComponentParameters,
|
||||
getFunctionParameters,
|
||||
getLeftSide,
|
||||
getLeftSidePathName,
|
||||
@@ -1200,7 +1174,6 @@ export {
|
||||
isTypeAnnotationAFunction,
|
||||
isUnionType,
|
||||
iterateCallArgumentsPath,
|
||||
iterateComponentParametersPath,
|
||||
iterateFunctionParametersPath,
|
||||
needsHardlineAfterDanglingComment,
|
||||
shouldFlatten,
|
||||
|
||||
Reference in new issue
Block a user