fix: do not reuse the opening * when closing a comment#100
Open
spokodev wants to merge 1 commit into
Open
Conversation
`indexOf("*/", pos)` started searching at the opening `/`, so the `*` of
the opening `/*` could be matched as the `*` of a closing `*/`. As a
result `/*/` was parsed as a closed, empty comment instead of an unclosed
comment whose content is `/`, and stringifying it produced `/**/` —
breaking the lossless round-trip (`/*/` is also a known CSS comment hack).
Start the search past the opening delimiter (`pos + 2`).
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.
Problem
/*/does not round-trip — it comes back as/**/:/*/is an unclosed comment whose content is/, but it is parsed as a closed, empty comment ({ type: "comment", value: "" }), and stringifying that yields/**/. This breaks the lossless round-trip the parser is designed for, and/*/shows up in real stylesheets as a comment-toggle hack.Root cause
In
lib/parse.jsthe closing delimiter is located with:pospoints at the opening/, so the search range includes the opening/*. For/*/,indexOf("*/", 0)finds a*/at index 1 — i.e. it reuses the*of the opening/*as the*of the closing*/. The comment is then treated as closed with empty content.Fix
Start the search past the opening delimiter so the two
*s can't overlap:Now
/*/has no*/after the opening, so it is correctly an unclosed comment with content/. Already-closed comments are unaffected:/**/,/*c*/,/****/still find their real closing*/.Test plan
/*/parses to{ type: "comment", value: "/", unclosed: true }(fails onmain).i/otest (/*/,a /*/ b,/*/ comment hack /**/).node --test),oxlint/oxfmtclean.