From 0844e463c96d83daf1904753fe34da0701b92616 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 30 Jan 2026 10:55:52 +0800 Subject: [PATCH] Simplify `needsHardlineAfterDanglingComment` (#18797) Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- src/language-js/comments/handle-comments.js | 3 +-- src/language-js/parse/postprocess/index.js | 9 +++++++-- .../merge-nestled-jsdoc-comments.js | 6 ++++-- src/language-js/print/comment.js | 3 +-- src/language-js/print/mapped-type.js | 2 +- src/language-js/print/ternary.js | 2 +- src/language-js/print/try-statement.js | 2 +- src/language-js/printers.js | 2 +- .../{is-line-comment.js => comment-types.js} | 13 ++++++++++++- src/language-js/utilities/comments.js | 3 +-- src/language-js/utilities/is-block-comment.js | 18 ------------------ .../utilities/is-indentable-block-comment.js | 2 +- .../utilities/is-type-cast-comment.js | 2 +- .../needs-hardline-after-dangling-comment.js | 12 +++--------- 14 files changed, 35 insertions(+), 44 deletions(-) rename src/language-js/utilities/{is-line-comment.js => comment-types.js} (69%) delete mode 100644 src/language-js/utilities/is-block-comment.js diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index 44636a49d..17da7ef46 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -11,10 +11,9 @@ import isNonEmptyArray from "../../utilities/is-non-empty-array.js"; import { locEnd, locStart } from "../loc.js"; import { getCallArguments } from "../utilities/call-arguments.js"; import { isInsideCallOrNewExpressionParentheses } from "../utilities/call-or-new-expression-parentheses.js"; +import { isBlockComment, isLineComment } from "../utilities/comment-types.js"; import { createTypeCheckFunction } from "../utilities/create-type-check-function.js"; import { getFunctionParameters } from "../utilities/function-parameters.js"; -import { isBlockComment } from "../utilities/is-block-comment.js"; -import { isLineComment } from "../utilities/is-line-comment.js"; import { isMethod } from "../utilities/is-method.js"; import { isObjectProperty } from "../utilities/is-object-property.js"; import { isPrettierIgnoreComment } from "../utilities/is-prettier-ignore-comment.js"; diff --git a/src/language-js/parse/postprocess/index.js b/src/language-js/parse/postprocess/index.js index 8d519b66a..b5bd169d8 100644 --- a/src/language-js/parse/postprocess/index.js +++ b/src/language-js/parse/postprocess/index.js @@ -1,10 +1,12 @@ import * as assert from "#universal/assert"; import { commentsPropertyInOptions } from "../../../constants.js"; import { locEnd, locEndWithFullText, locStart } from "../../loc.js"; +import { + isBlockComment, + isLineComment, +} from "../../utilities/comment-types.js"; import { createTypeCheckFunction } from "../../utilities/create-type-check-function.js"; import { getRaw } from "../../utilities/get-raw.js"; -import { isBlockComment } from "../../utilities/is-block-comment.js"; -import { isLineComment } from "../../utilities/is-line-comment.js"; import { isTypeCastComment } from "../../utilities/is-type-cast-comment.js"; import { stripComments } from "../../utilities/strip-comments.js"; import { mergeNestledJsdocComments } from "./merge-nestled-jsdoc-comments.js"; @@ -252,7 +254,10 @@ function assertComments(comments, text) { // Flow const closingMark = commentText.endsWith("*-/") ? "*-/" : "*/"; assert.equal("/*" + comment.value + closingMark, commentText); + return; } + + throw new Error(`Unknown comment type '${comment.type}'.`); } } diff --git a/src/language-js/parse/postprocess/merge-nestled-jsdoc-comments.js b/src/language-js/parse/postprocess/merge-nestled-jsdoc-comments.js index 1b1002285..2052f164c 100644 --- a/src/language-js/parse/postprocess/merge-nestled-jsdoc-comments.js +++ b/src/language-js/parse/postprocess/merge-nestled-jsdoc-comments.js @@ -1,7 +1,9 @@ import { locEnd, locStart } from "../../loc.js"; -import { isBlockComment } from "../../utilities/is-block-comment.js"; +import { + isBlockComment, + isLineComment, +} from "../../utilities/comment-types.js"; import isIndentableBlockComment from "../../utilities/is-indentable-block-comment.js"; -import { isLineComment } from "../../utilities/is-line-comment.js"; function mergeNestledJsdocComments(comments) { if (comments.length < 2) { diff --git a/src/language-js/print/comment.js b/src/language-js/print/comment.js index c5d10577f..35897fb63 100644 --- a/src/language-js/print/comment.js +++ b/src/language-js/print/comment.js @@ -5,9 +5,8 @@ import { replaceEndOfLine, } from "../../document/index.js"; import { locEnd, locStart } from "../loc.js"; -import { isBlockComment } from "../utilities/is-block-comment.js"; +import { isBlockComment, isLineComment } from "../utilities/comment-types.js"; import isIndentableBlockComment from "../utilities/is-indentable-block-comment.js"; -import { isLineComment } from "../utilities/is-line-comment.js"; /** @import {Doc} from "../../document/index.js" diff --git a/src/language-js/print/mapped-type.js b/src/language-js/print/mapped-type.js index 4637a1d55..4c8b79f12 100644 --- a/src/language-js/print/mapped-type.js +++ b/src/language-js/print/mapped-type.js @@ -10,8 +10,8 @@ import { printDanglingComments } from "../../main/comments/print.js"; import hasNewline from "../../utilities/has-newline.js"; import hasNewlineInRange from "../../utilities/has-newline-in-range.js"; import { locEnd, locStart } from "../loc.js"; +import { isLineComment } from "../utilities/comment-types.js"; import { CommentCheckFlags, getComments } from "../utilities/comments.js"; -import { isLineComment } from "../utilities/is-line-comment.js"; import { stripComments } from "../utilities/strip-comments.js"; import { printClassMemberSemicolon } from "./class.js"; diff --git a/src/language-js/print/ternary.js b/src/language-js/print/ternary.js index b4ef17dba..c8600f777 100644 --- a/src/language-js/print/ternary.js +++ b/src/language-js/print/ternary.js @@ -12,12 +12,12 @@ import { printDanglingComments } from "../../main/comments/print.js"; import hasNewlineInRange from "../../utilities/has-newline-in-range.js"; import { locEnd, locStart } from "../loc.js"; import needsParentheses from "../parentheses/needs-parentheses.js"; +import { isBlockComment } from "../utilities/comment-types.js"; import { CommentCheckFlags, getComments, hasComment, } from "../utilities/comments.js"; -import { isBlockComment } from "../utilities/is-block-comment.js"; import { isLoneShortArgument } from "../utilities/is-lone-short-argument.js"; import { isSimpleExpressionByNodeCount } from "../utilities/is-simple-expression-by-node-count.js"; import { diff --git a/src/language-js/print/try-statement.js b/src/language-js/print/try-statement.js index 9f0c373db..787622582 100644 --- a/src/language-js/print/try-statement.js +++ b/src/language-js/print/try-statement.js @@ -1,8 +1,8 @@ import { indent, softline } from "../../document/index.js"; import hasNewline from "../../utilities/has-newline.js"; import { locEnd, locStart } from "../loc.js"; +import { isBlockComment } from "../utilities/comment-types.js"; import { hasComment } from "../utilities/comments.js"; -import { isBlockComment } from "../utilities/is-block-comment.js"; function printTryStatement(path, options, print) { const { node } = path; diff --git a/src/language-js/printers.js b/src/language-js/printers.js index ab186e280..120ff6b71 100644 --- a/src/language-js/printers.js +++ b/src/language-js/printers.js @@ -9,7 +9,7 @@ import { insertPragma } from "./pragma.js"; import { printComment } from "./print/comment.js"; import { printEstree } from "./print/index.js"; import getVisitorKeys from "./traverse/get-visitor-keys.js"; -import { isBlockComment } from "./utilities/is-block-comment.js"; +import { isBlockComment } from "./utilities/comment-types.js"; import hasPrettierIgnore from "./utilities/is-ignored.js"; const features = { diff --git a/src/language-js/utilities/is-line-comment.js b/src/language-js/utilities/comment-types.js similarity index 69% rename from src/language-js/utilities/is-line-comment.js rename to src/language-js/utilities/comment-types.js index 9ec70501e..37a803187 100644 --- a/src/language-js/utilities/is-line-comment.js +++ b/src/language-js/utilities/comment-types.js @@ -4,6 +4,17 @@ import { createTypeCheckFunction } from "./create-type-check-function.js"; @import {Comment} from "../types/estree.js"; */ +/** + * @param {Comment} comment + * @returns {boolean} + */ +const isBlockComment = createTypeCheckFunction([ + "Block", + "CommentBlock", + // `meriyah` + "MultiLine", +]); + const isLineComment = createTypeCheckFunction([ "Line", "CommentLine", @@ -18,4 +29,4 @@ const isLineComment = createTypeCheckFunction([ "InterpreterDirective", ]); -export { isLineComment }; +export { isBlockComment, isLineComment }; diff --git a/src/language-js/utilities/comments.js b/src/language-js/utilities/comments.js index ffc857bb0..9424628fd 100644 --- a/src/language-js/utilities/comments.js +++ b/src/language-js/utilities/comments.js @@ -1,6 +1,5 @@ import isNonEmptyArray from "../../utilities/is-non-empty-array.js"; -import { isBlockComment } from "./is-block-comment.js"; -import { isLineComment } from "./is-line-comment.js"; +import { isBlockComment, isLineComment } from "./comment-types.js"; import { isPrettierIgnoreComment } from "./is-prettier-ignore-comment.js"; /** diff --git a/src/language-js/utilities/is-block-comment.js b/src/language-js/utilities/is-block-comment.js deleted file mode 100644 index e2d98c0ec..000000000 --- a/src/language-js/utilities/is-block-comment.js +++ /dev/null @@ -1,18 +0,0 @@ -import { createTypeCheckFunction } from "./create-type-check-function.js"; - -/** -@import {Comment} from "../types/estree.js"; -*/ - -/** - * @param {Comment} comment - * @returns {boolean} - */ -const isBlockComment = createTypeCheckFunction([ - "Block", - "CommentBlock", - // `meriyah` - "MultiLine", -]); - -export { isBlockComment }; diff --git a/src/language-js/utilities/is-indentable-block-comment.js b/src/language-js/utilities/is-indentable-block-comment.js index e36ee8b32..8e84e5d10 100644 --- a/src/language-js/utilities/is-indentable-block-comment.js +++ b/src/language-js/utilities/is-indentable-block-comment.js @@ -1,4 +1,4 @@ -import { isBlockComment } from "./is-block-comment.js"; +import { isBlockComment } from "./comment-types.js"; function isIndentableBlockCommentInternal(comment) { /* diff --git a/src/language-js/utilities/is-type-cast-comment.js b/src/language-js/utilities/is-type-cast-comment.js index 707238c12..180ed0948 100644 --- a/src/language-js/utilities/is-type-cast-comment.js +++ b/src/language-js/utilities/is-type-cast-comment.js @@ -1,4 +1,4 @@ -import { isBlockComment } from "./is-block-comment.js"; +import { isBlockComment } from "./comment-types.js"; /** @import {Comment} from "../types/estree.js" diff --git a/src/language-js/utilities/needs-hardline-after-dangling-comment.js b/src/language-js/utilities/needs-hardline-after-dangling-comment.js index 223d51143..76511fd48 100644 --- a/src/language-js/utilities/needs-hardline-after-dangling-comment.js +++ b/src/language-js/utilities/needs-hardline-after-dangling-comment.js @@ -1,5 +1,5 @@ -import { CommentCheckFlags, getComments, hasComment } from "./comments.js"; -import { isBlockComment } from "./is-block-comment.js"; +import { isLineComment } from "./comment-types.js"; +import { CommentCheckFlags, getComments } from "./comments.js"; /** @import { @@ -12,13 +12,7 @@ import { isBlockComment } from "./is-block-comment.js"; * @returns {boolean} */ function needsHardlineAfterDanglingComment(node) { - if (!hasComment(node)) { - return false; - } - const lastDanglingComment = getComments(node, CommentCheckFlags.Dangling).at( - -1, - ); - return lastDanglingComment && !isBlockComment(lastDanglingComment); + return isLineComment(getComments(node, CommentCheckFlags.Dangling).at(-1)); } export { needsHardlineAfterDanglingComment };