Skip to content

Commit a419e50

Browse files
committed
fix: parse "/*/" as an unclosed comment
The comment branch searches for the closing "*/" with value.indexOf("*/", pos), starting at the same index as the opening "/*". For the input "/*/" that search matches the "*" of the opening delimiter plus the following "/", so the parser reports a closed empty comment and never sets the unclosed flag. On stringify it then emits "/**/", so the round trip is not lossless. "/*/" is an unterminated comment, which postcss core also reports as an unclosed comment. The parser already handles this for other inputs such as "/*x"; only the slash right after the opener slipped through. Start the closing search at pos + 2 so the opening "/*" cannot be matched as its own terminator.
1 parent 898fdfe commit a419e50

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

lib/parse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module.exports = function (input) {
103103

104104
// Comments
105105
} else if (code === slash && value.charCodeAt(pos + 1) === star) {
106-
next = value.indexOf("*/", pos);
106+
next = value.indexOf("*/", pos + 2);
107107

108108
token = {
109109
type: "comment",

test/parse.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,19 @@ const tests = [
15531553
},
15541554
],
15551555
},
1556+
{
1557+
message: "should not treat the opening slash-star as its own terminator",
1558+
fixture: "/*/",
1559+
expected: [
1560+
{
1561+
type: "comment",
1562+
sourceIndex: 0,
1563+
sourceEndIndex: 3,
1564+
value: "/",
1565+
unclosed: true,
1566+
},
1567+
],
1568+
},
15561569
{
15571570
message: "should respect escape character",
15581571
fixture: "Hawaii \\35 -0",

0 commit comments

Comments
 (0)