From b3973c02dd0eb4e55651b27710cfc3aab3d05023 Mon Sep 17 00:00:00 2001 From: Konv Suu <2583695112@qq.com> Date: Thu, 9 Jul 2026 01:34:58 +0800 Subject: [PATCH] Fix adjacent block comments corrupted in less (#19573) Co-authored-by: fisker Cheung --- changelog_unreleased/less/19574.md | 24 ++ src/language-css/loc.js | 18 +- .../__snapshots__/format.test.js.snap | 20 ++ tests/format/less/comments/adjacent.less | 4 + .../__snapshots__/format.test.js.snap | 227 ++++++++++++++++++ .../less/less-test-suite/format.test.js | 2 - 6 files changed, 285 insertions(+), 10 deletions(-) create mode 100644 changelog_unreleased/less/19574.md create mode 100644 tests/format/less/comments/adjacent.less diff --git a/changelog_unreleased/less/19574.md b/changelog_unreleased/less/19574.md new file mode 100644 index 000000000..b5c1f060c --- /dev/null +++ b/changelog_unreleased/less/19574.md @@ -0,0 +1,24 @@ +#### Fix adjacent block comments being corrupted (#19574 by @kovsu) + + +```less +// Input +/* a *//* b */ +/* a */* { + color: red; +} + +// Prettier stable +/* a */ +/* b */ +/* a * { + color: red; +} + +// Prettier main +/* a */ /* b */ +/* a */ +* { + color: red; +} +``` diff --git a/src/language-css/loc.js b/src/language-css/loc.js index db73dbede..2264391ad 100644 --- a/src/language-css/loc.js +++ b/src/language-css/loc.js @@ -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; } diff --git a/tests/format/less/comments/__snapshots__/format.test.js.snap b/tests/format/less/comments/__snapshots__/format.test.js.snap index a13f448ed..a0b1c4759 100644 --- a/tests/format/less/comments/__snapshots__/format.test.js.snap +++ b/tests/format/less/comments/__snapshots__/format.test.js.snap @@ -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"] diff --git a/tests/format/less/comments/adjacent.less b/tests/format/less/comments/adjacent.less new file mode 100644 index 000000000..a89525195 --- /dev/null +++ b/tests/format/less/comments/adjacent.less @@ -0,0 +1,4 @@ +/* a *//* b */ +/* a */* { + color: red; +} diff --git a/tests/format/less/less-test-suite/__snapshots__/format.test.js.snap b/tests/format/less/less-test-suite/__snapshots__/format.test.js.snap index 30af9f65c..0b34a9c27 100644 --- a/tests/format/less/less-test-suite/__snapshots__/format.test.js.snap +++ b/tests/format/less/less-test-suite/__snapshots__/format.test.js.snap @@ -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"] diff --git a/tests/format/less/less-test-suite/format.test.js b/tests/format/less/less-test-suite/format.test.js index 9e844b7c1..d61763858 100644 --- a/tests/format/less/less-test-suite/format.test.js +++ b/tests/format/less/less-test-suite/format.test.js @@ -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 */