fix: parse "/*/" as an unclosed comment#101
Open
greymoth-jp wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
valueParser("/*/").toString()returns/**/, so the round trip is not lossless for this input.The comment branch looks for the closing
*/withvalue.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 theunclosedflag. 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 reportsUnclosed commentfor the same input. The parser already handles unterminated comments correctly for other inputs (for example/*xyields{ 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.