Skip to content

fix: parse "/*/" as an unclosed comment#101

Open
greymoth-jp wants to merge 1 commit into
postcss:mainfrom
greymoth-jp:fix-slash-star-comment
Open

fix: parse "/*/" as an unclosed comment#101
greymoth-jp wants to merge 1 commit into
postcss:mainfrom
greymoth-jp:fix-slash-star-comment

Conversation

@greymoth-jp

Copy link
Copy Markdown

valueParser("/*/").toString() returns /**/, so the round trip is not lossless for this input.

The comment branch looks for the closing */ with value.indexOf("*/", pos), which starts the search 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 ({ value: "", sourceEndIndex: 3 }) and never sets the unclosed flag. On stringify it then emits /* + "" + */, which is /**/.

/*/ is actually an unterminated comment: after /* the only content is / and there is no closing */. postcss core agrees and reports Unclosed comment for the same input. The parser already handles unterminated comments correctly for other inputs (for example /*x yields { value: "x", unclosed: true }); only the slash right after the opener slipped through.

Starting the closing search at pos + 2, where the comment body begins, stops the opening /* from being matched as its own terminator. /*/ now parses to { value: "/", unclosed: true } and round trips back to /*/. Empty and normal comments are unchanged.

Added a parse test for /*/. The existing suite still passes.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant