From ebc995158bbde89f0f8e5ad5f1dbcca5e06eb03d Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 25 Jun 2026 10:10:01 +0800 Subject: [PATCH] Add more tests for "assignment like" node (#19447) --- .../attach/handle-if-statement-comments.js | 2 +- src/language-js/comments/attach/union-type.js | 6 +- src/language-js/comments/attach/utilities.js | 18 ++++- .../__snapshots__/format.test.js.snap | 4 +- .../__snapshots__/format.test.js.snap | 65 +++++++++++++++++++ .../js/comments/assignment/format.test.js | 1 + .../assignment/variable-declarator.js | 27 ++++++++ ...e_declarator.js => variable-declarator.js} | 0 8 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 tests/format/js/comments/assignment/__snapshots__/format.test.js.snap create mode 100644 tests/format/js/comments/assignment/format.test.js create mode 100644 tests/format/js/comments/assignment/variable-declarator.js rename tests/format/js/comments/{variable_declarator.js => variable-declarator.js} (100%) diff --git a/src/language-js/comments/attach/handle-if-statement-comments.js b/src/language-js/comments/attach/handle-if-statement-comments.js index 74379b456..9bb0a7a22 100644 --- a/src/language-js/comments/attach/handle-if-statement-comments.js +++ b/src/language-js/comments/attach/handle-if-statement-comments.js @@ -100,7 +100,7 @@ function handleCommentsBetween({ // - treat as a dangling comment otherwise if ( !isConsequentBlockStatement && - isSingleLineComment && + isSingleLineComment(comment, text) && // Comment and `precedingNode` are on same line !hasNewlineInRange(text, locEnd(precedingNode), locStart(comment)) ) { diff --git a/src/language-js/comments/attach/union-type.js b/src/language-js/comments/attach/union-type.js index 6bc654ea3..24f47512a 100644 --- a/src/language-js/comments/attach/union-type.js +++ b/src/language-js/comments/attach/union-type.js @@ -1,16 +1,14 @@ import { addLeadingComment } from "../../../main/comments/utilities.js"; import { locEnd, locStart } from "../../location/index.js"; -import { isBlockComment } from "../../utilities/comment-types.js"; import { isPrettierIgnoreComment } from "../../utilities/is-prettier-ignore-comment.js"; import { isUnionType } from "../../utilities/node-types.js"; import { stripComments } from "../../utilities/strip-comments.js"; -import { isSingleLineComment } from "../attach/utilities.js"; +import { isSingleLineBlockComment } from "../attach/utilities.js"; function shouldAttachToUnionTypeFirstElement(node, { comment, text, options }) { if ( isUnionType(node) && - isBlockComment(comment) && - isSingleLineComment(comment, text) && + isSingleLineBlockComment(comment, text) && !isPrettierIgnoreComment(comment) ) { const text = stripComments(options); diff --git a/src/language-js/comments/attach/utilities.js b/src/language-js/comments/attach/utilities.js index 826520f6c..0013d8417 100644 --- a/src/language-js/comments/attach/utilities.js +++ b/src/language-js/comments/attach/utilities.js @@ -4,7 +4,10 @@ import { } from "../../../main/comments/utilities.js"; import hasNewlineInRange from "../../../utilities/has-newline-in-range.js"; import { locEnd, locStart } from "../../location/index.js"; -import { isLineComment } from "../../utilities/comment-types.js"; +import { + isBlockComment, + isLineComment, +} from "../../utilities/comment-types.js"; /** @import {Node, Comment, NodeMap} from "../../types/estree.js"; @@ -15,10 +18,18 @@ import { isLineComment } from "../../utilities/comment-types.js"; @param {string} text @returns {boolean} */ -const isSingleLineComment = (comment, text) => - isLineComment(comment) || +const isSingleLineBlockComment = (comment, text) => + isBlockComment(comment) && !hasNewlineInRange(text, locStart(comment), locEnd(comment)); +/** +@param {Comment} comment +@param {string} text +@returns {boolean} +*/ +const isSingleLineComment = (comment, text) => + isLineComment(comment) || isSingleLineBlockComment(comment, text); + /** * @param {Node} node * @returns {void} @@ -50,5 +61,6 @@ function addBlockStatementFirstComment(node, comment) { export { addBlockOrNotComment, addBlockStatementFirstComment, + isSingleLineBlockComment, isSingleLineComment, }; diff --git a/tests/format/js/comments/__snapshots__/format.test.js.snap b/tests/format/js/comments/__snapshots__/format.test.js.snap index b1c6bb994..67004e2c4 100644 --- a/tests/format/js/comments/__snapshots__/format.test.js.snap +++ b/tests/format/js/comments/__snapshots__/format.test.js.snap @@ -7216,7 +7216,7 @@ try { ================================================================================ `; -exports[`variable_declarator.js - {"semi":false} format 1`] = ` +exports[`variable-declarator.js - {"semi":false} format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] semi: false @@ -7366,7 +7366,7 @@ const foo3 = 123 ================================================================================ `; -exports[`variable_declarator.js format 1`] = ` +exports[`variable-declarator.js format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 (default) | diff --git a/tests/format/js/comments/assignment/__snapshots__/format.test.js.snap b/tests/format/js/comments/assignment/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..7786f4462 --- /dev/null +++ b/tests/format/js/comments/assignment/__snapshots__/format.test.js.snap @@ -0,0 +1,65 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`variable-declarator.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +let a1 // commment + = 1; +let a12 +// commment + = 1; +let a2 /* commment */ + = 1; +let a22 +/* commment */ + = 1; +let a3 /* +commment */ + = 1; +let a32 +/* +commment */ + = 1; +let a4 = // comment +1; +let a42 = +// comment +1; +let a5 = /* commment */ +1; +let a52 = +/* commment */ +1; + +=====================================output===================================== +let a1 = 1; // commment +let a12 = + // commment + 1; +let a2 = /* commment */ 1; +let a22 = + /* commment */ + 1; +let a3 = + /* +commment */ + 1; +let a32 = + /* +commment */ + 1; +let a4 = + // comment + 1; +let a42 = + // comment + 1; +let a5 = /* commment */ 1; +let a52 = + /* commment */ + 1; + +================================================================================ +`; diff --git a/tests/format/js/comments/assignment/format.test.js b/tests/format/js/comments/assignment/format.test.js new file mode 100644 index 000000000..67c251cce --- /dev/null +++ b/tests/format/js/comments/assignment/format.test.js @@ -0,0 +1 @@ +runFormatTest(import.meta, ["babel", "flow", "typescript"]); diff --git a/tests/format/js/comments/assignment/variable-declarator.js b/tests/format/js/comments/assignment/variable-declarator.js new file mode 100644 index 000000000..d390c830e --- /dev/null +++ b/tests/format/js/comments/assignment/variable-declarator.js @@ -0,0 +1,27 @@ +let a1 // commment + = 1; +let a12 +// commment + = 1; +let a2 /* commment */ + = 1; +let a22 +/* commment */ + = 1; +let a3 /* +commment */ + = 1; +let a32 +/* +commment */ + = 1; +let a4 = // comment +1; +let a42 = +// comment +1; +let a5 = /* commment */ +1; +let a52 = +/* commment */ +1; diff --git a/tests/format/js/comments/variable_declarator.js b/tests/format/js/comments/variable-declarator.js similarity index 100% rename from tests/format/js/comments/variable_declarator.js rename to tests/format/js/comments/variable-declarator.js