Extract printDanglingCommentsInList (#18626)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-01-14 05:10:23 +08:00
1 parent 9c13ac1572
commit 5fff8e7959
5 files changed
+51 -73

No files matched your search

+5 -19
View File
@@ -23,28 +23,14 @@ import {
isTupleType,
shouldPrintComma,
} from "../utilities/index.js";
import { printOptionalToken } from "./miscellaneous.js";
import {
printDanglingCommentsInList,
printOptionalToken,
} from "./miscellaneous.js";
import { printTypeAnnotationProperty } from "./type-annotation.js";
/** @import {Doc} from "../../document/index.js" */
function printEmptyArrayElements(path, options) {
const { node } = path;
return group([
"[",
hasComment(node, CommentCheckFlags.Dangling)
? [
indent([softline, printDanglingComments(path, options)]),
hasComment(node, CommentCheckFlags.Dangling | CommentCheckFlags.Line)
? hardline
: softline,
]
: [],
"]",
]);
}
/*
- `ArrayExpression`
- `ArrayPattern`
@@ -59,7 +45,7 @@ function printArray(path, options, print) {
const elementsProperty = isTupleType(node) ? "elementTypes" : "elements";
const elements = node[elementsProperty];
if (elements.length === 0 && !node.inexact) {
parts.push(printEmptyArrayElements(path, options));
parts.push(group(["[", printDanglingCommentsInList(path, options), "]"]));
} else {
const lastElem = elements.at(-1);
const canHaveTrailingComma =
+2 -16
View File
@@ -10,7 +10,6 @@ import {
softline,
willBreak,
} from "../../document/index.js";
import { printDanglingComments } from "../../main/comments/print.js";
import {
CommentCheckFlags,
getCallArguments,
@@ -36,6 +35,7 @@ import {
shouldPrintComma,
} from "../utilities/index.js";
import { isConciselyPrintedArray } from "./array.js";
import { printDanglingCommentsInList } from "./miscellaneous.js";
/*
- `NewExpression`
@@ -51,21 +51,7 @@ function printCallArguments(path, options, print) {
const args = getCallArguments(node);
if (args.length === 0) {
return group([
"(",
hasComment(node, CommentCheckFlags.Dangling)
? [
indent([softline, printDanglingComments(path, options)]),
hasComment(
node,
CommentCheckFlags.Dangling | CommentCheckFlags.Line,
)
? hardline
: softline,
]
: [],
")",
]);
return group(["(", printDanglingCommentsInList(path, options), ")"]);
}
const lastArgIndex = args.length - 1;
+5 -22
View File
@@ -9,10 +9,8 @@ import {
softline,
willBreak,
} from "../../document/index.js";
import { printDanglingComments } from "../../main/comments/print.js";
import isNonEmptyArray from "../../utilities/is-non-empty-array.js";
import {
CommentCheckFlags,
getFunctionParameters,
hasComment,
hasRestParameter,
@@ -27,6 +25,7 @@ import {
iterateFunctionParametersPath,
shouldPrintComma,
} from "../utilities/index.js";
import { printDanglingCommentsInList } from "./miscellaneous.js";
/** @import AstPath from "../../common/ast-path.js" */
@@ -75,27 +74,11 @@ function printFunctionParameters(
return [
typeParametersDoc,
"(",
hasComment(
functionNode,
CommentCheckFlags.Dangling,
printDanglingCommentsInList(
path,
options,
functionParameterDanglingCommentFilter,
)
? [
indent([
softline,
printDanglingComments(path, options, {
filter: functionParameterDanglingCommentFilter,
}),
]),
hasComment(
functionNode,
CommentCheckFlags.Dangling | CommentCheckFlags.Line,
functionParameterDanglingCommentFilter,
)
? hardline
: softline,
]
: [],
),
")",
];
}
+34 -4
View File
@@ -1,4 +1,11 @@
import { group, indent, line, softline } from "../../document/index.js";
import {
group,
hardline,
indent,
line,
softline,
} from "../../document/index.js";
import { printDanglingComments } from "../../main/comments/print.js";
import {
CommentCheckFlags,
createTypeCheckFunction,
@@ -8,9 +15,10 @@ import {
} from "../utilities/index.js";
/**
* @import AstPath from "../../common/ast-path.js"
* @import {Doc} from "../../document/index.js"
*/
@import AstPath from "../../common/ast-path.js"
@import {Doc} from "../../document/index.js"
@import {Comment} from "../types/estree.js"
*/
/**
* @param {AstPath} path
@@ -164,9 +172,31 @@ function printIfOrWhileCondition(path, options, print) {
return group([indent([softline, conditionDoc]), softline]);
}
/**
@param {(comment: Comment) => boolean} [filter]
@returns {Doc}
*/
function printDanglingCommentsInList(path, options, filter) {
const { node } = path;
return hasComment(node, CommentCheckFlags.Dangling, filter)
? [
indent([softline, printDanglingComments(path, options, { filter })]),
hasComment(
node,
CommentCheckFlags.Dangling | CommentCheckFlags.Line,
filter,
)
? hardline
: softline,
]
: "";
}
export {
adjustClause,
printAbstractToken,
printDanglingCommentsInList,
printDeclareToken,
printDefiniteToken,
printIfOrWhileCondition as printDoWhileStatementCondition,
+5 -12
View File
@@ -23,7 +23,10 @@ import {
shouldPrintComma,
} from "../utilities/index.js";
import { shouldHugTheOnlyParameter } from "./function-parameters.js";
import { printOptionalToken } from "./miscellaneous.js";
import {
printDanglingCommentsInList,
printOptionalToken,
} from "./miscellaneous.js";
import { printTypeAnnotationProperty } from "./type-annotation.js";
/** @import {Doc} from "../../document/index.js" */
@@ -136,17 +139,7 @@ function printObject(path, options, print) {
if (parts.length === 0) {
content = group([
"{",
hasComment(node, CommentCheckFlags.Dangling)
? [
indent([softline, printDanglingComments(path, options)]),
hasComment(
node,
CommentCheckFlags.Dangling | CommentCheckFlags.Line,
)
? hardline
: softline,
]
: [],
printDanglingCommentsInList(path, options),
"}",
printOptionalToken(path),
printTypeAnnotationProperty(path, print),