From 5fff8e7959c6d4a46270c8ab0587fec7e71f3014 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 14 Jan 2026 05:10:23 +0800 Subject: [PATCH] Extract `printDanglingCommentsInList` (#18626) --- src/language-js/print/array.js | 24 +++---------- src/language-js/print/call-arguments.js | 18 ++-------- src/language-js/print/function-parameters.js | 27 +++----------- src/language-js/print/miscellaneous.js | 38 +++++++++++++++++--- src/language-js/print/object.js | 17 +++------ 5 files changed, 51 insertions(+), 73 deletions(-) diff --git a/src/language-js/print/array.js b/src/language-js/print/array.js index b0d60bc8e..93cdde860 100644 --- a/src/language-js/print/array.js +++ b/src/language-js/print/array.js @@ -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 = diff --git a/src/language-js/print/call-arguments.js b/src/language-js/print/call-arguments.js index 01bce68b5..e1e7a14e2 100644 --- a/src/language-js/print/call-arguments.js +++ b/src/language-js/print/call-arguments.js @@ -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; diff --git a/src/language-js/print/function-parameters.js b/src/language-js/print/function-parameters.js index 0e19a95f2..763c10384 100644 --- a/src/language-js/print/function-parameters.js +++ b/src/language-js/print/function-parameters.js @@ -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, - ] - : [], + ), ")", ]; } diff --git a/src/language-js/print/miscellaneous.js b/src/language-js/print/miscellaneous.js index a6a78174f..b5aa5d616 100644 --- a/src/language-js/print/miscellaneous.js +++ b/src/language-js/print/miscellaneous.js @@ -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, diff --git a/src/language-js/print/object.js b/src/language-js/print/object.js index 3e0ab1334..417770b81 100644 --- a/src/language-js/print/object.js +++ b/src/language-js/print/object.js @@ -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),