Add more tests for "assignment like" node (#19447)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-06-25 10:10:01 +08:00
1 parent 5c9fd27dfa
commit ebc995158b
8 files changed
+113 -10

No files matched your search

@@ -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))
) {
@@ -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);
+15 -3
View File
@@ -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,
};
@@ -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) |
@@ -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;
================================================================================
`;
@@ -0,0 +1 @@
runFormatTest(import.meta, ["babel", "flow", "typescript"]);
@@ -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;