Fix adjacent block comments corrupted in less (#19573)

Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
Konv Suuandfisker Cheung authored and GitHub committed 2026-07-09 01:34:58 +08:00
1 parent e4dd78409f
commit b3973c02dd
6 files changed
+285 -10

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
#### Fix adjacent block comments being corrupted (#19574 by @kovsu)
<!-- prettier-ignore -->
```less
// Input
/* a *//* b */
/* a */* {
color: red;
}
// Prettier stable
/* a */
/* b */
/* a * {
color: red;
}
// Prettier main
/* a */ /* b */
/* a */
* {
color: red;
}
```
+10 -8
View File
@@ -446,14 +446,16 @@ function replaceQuotesInInlineComments(text) {
continue;
}
if (c === "*" && text[i - 1] === "/") {
state = "comment-block";
continue;
}
if (c === "/" && text[i - 1] === "/") {
state = "comment-inline";
inlineCommentStartIndex = i - 1;
if (c === "/") {
const nextCharacter = text[i + 1];
if (nextCharacter === "*") {
state = "comment-block";
i++;
} else if (nextCharacter === "/") {
state = "comment-inline";
inlineCommentStartIndex = i;
i++;
}
continue;
}
@@ -61,6 +61,26 @@ parsers: ["less"]
================================================================================
`;
exports[`adjacent.less format 1`] = `
====================================options=====================================
parsers: ["less"]
printWidth: 80 (default) |
=====================================input======================================
/* a *//* b */
/* a */* {
color: red;
}
=====================================output=====================================
/* a */ /* b */
/* a */
* {
color: red;
}
================================================================================
`;
exports[`between-decl.less format 1`] = `
====================================options=====================================
parsers: ["less"]
+4
View File
@@ -0,0 +1,4 @@
/* a *//* b */
/* a */* {
color: red;
}
@@ -6302,6 +6302,233 @@ parsers: ["less"]
================================================================================
`;
exports[`snippet: tests-unit/comments/comments.less format 1`] = `
====================================options=====================================
parsers: ["less"]
printWidth: 80 (default) |
=====================================input======================================
// Comprehensive comment handling tests
/******************\\
* *
* Comment Header *
* *
\\******************/
/*
Comment
*/
/*
* Comment Test
*
* - cloudhead (http://cloudhead.net)
*
*/
////////////////
@var: "content";
////////////////
/* Colors
* ------
* #EDF8FC (background blue)
* #166C89 (darkest blue)
*
* Text:
* #333 (standard text) // A comment within a comment!
* #1F9EC9 (standard link)
*
*/
/* @group Variables
------------------- */
#comments /* boo *//* boo again*/,
//.commented_out1
//.commented_out2
//.commented_out3
.comments //end of comments1
//end of comments2
{
/**/ // An empty comment
color: red; /* A C-style comment */ /* A C-style comment */
background-color: orange; // A little comment
font-size: 12px;
/* lost comment */ content: @var;
border: 1px solid black;
// padding & margin //
padding: 0; // }{ '"
margin: 2em;
} //
/* commented out
#more-comments {
color: grey;
}
*/
.selector /* .with */, .lots, /* of */ .comments {
color/* survive */ /* me too */: grey, /* blue */ orange;
-webkit-border-radius: 2px /* webkit only */;
-moz-border-radius: (2px * 4) /* moz only with operation */;
}
.mixin_def_with_colors(@a: white, // in
@b: 1px //put in @b - causes problems! --->
) // the
when (@a = white) {
.test-rule {
color: @b;
}
}
.mixin_def_with_colors();
// .s when
//R/2
.sr-only-focusable {
clip: auto;
}
@-webkit-keyframes /* Safari */ hover /* and Chrome */ {
0% {
color: red;
}
}
#last { color: blue }
//
/* *//* { *//* *//* *//* */#div { color:#A33; }/* } */
// line immediately followed
/*by block */
@string_w_comment: ~"/* // Not commented out // */";
#output-block { --comment: @string_w_comment; }
/*comment on last line*/
=====================================output=====================================
// Comprehensive comment handling tests
/******************\\
* *
* Comment Header *
* *
\\******************/
/*
Comment
*/
/*
* Comment Test
*
* - cloudhead (http://cloudhead.net)
*
*/
////////////////
@var: "content";
////////////////
/* Colors
* ------
* #EDF8FC (background blue)
* #166C89 (darkest blue)
*
* Text:
* #333 (standard text) // A comment within a comment!
* #1F9EC9 (standard link)
*
*/
/* @group Variables
------------------- */
#comments /* boo *//* boo again*/,
//.commented_out1
//.commented_out2
//.commented_out3
.comments //end of comments1
//end of comments2
{
/**/ // An empty comment
color: red; /* A C-style comment */ /* A C-style comment */
background-color: orange; // A little comment
font-size: 12px;
/* lost comment */
content: @var;
border: 1px solid black;
// padding & margin //
padding: 0; // }{ '"
margin: 2em;
} //
/* commented out
#more-comments {
color: grey;
}
*/
.selector /* .with */, .lots, /* of */ .comments {
color/* survive */ /* me too */:
grey,
/* blue */ orange;
-webkit-border-radius: 2px /* webkit only */;
-moz-border-radius: (2px * 4) /* moz only with operation */;
}
.mixin_def_with_colors(@a: white, // in
@b: 1px //put in @b - causes problems! --->
) // the
when (@a = white) {
.test-rule {
color: @b;
}
}
.mixin_def_with_colors();
// .s when
//R/2
.sr-only-focusable {
clip: auto;
}
@-webkit-keyframes /* Safari */ hover /* and Chrome */ {
0% {
color: red;
}
}
#last {
color: blue;
}
//
/* */ /* { */ /* */ /* */ /* */
#div {
color: #a33;
} /* } */
// line immediately followed
/*by block */
@string_w_comment: ~"/* // Not commented out // */";
#output-block {
--comment: @string_w_comment;
}
/*comment on last line*/
================================================================================
`;
exports[`snippet: tests-unit/comments/comments2.less format 1`] = `
====================================options=====================================
parsers: ["less"]
@@ -10,11 +10,9 @@ const BUGS = new Set([
"tests-unit/permissive-parse/permissive-parse.less",
"tests-unit/parser-property-interp/parser-property-interp.less",
"tests-unit/mixins-interpolated/mixins-interpolated.less",
"tests-unit/import/import/invalid-css.less",
"tests-unit/functions/functions.less",
"tests-unit/extract-and-length/extract-and-length.less",
"tests-unit/css-escapes/css-escapes.less",
"tests-unit/comments/comments.less",
]);
/* spell-checker: enable */