fix: don't treat a non-prefix token before | as a namespace#324
Open
spokodev wants to merge 1 commit into
Open
fix: don't treat a non-prefix token before | as a namespace#324spokodev wants to merge 1 commit into
| as a namespace#324spokodev wants to merge 1 commit into
Conversation
`namespace()` used the previous token's content as the namespace prefix whenever a `|` was seen. For an empty namespace written as a bare `|` (e.g. `|b`), the previous token is whatever precedes it — a comment, comma, combinator or whitespace — and that was emitted as the prefix. So `/* c */|b` round-tripped to `/* c */\/\*\ c\ \*\/|b`, `.a,|b` to `.a,\,|b`, `.a > |b` to `.a > |b`, and so on. Only use the previous token as a prefix when it can be one: a type/word, the universal `*`, or the nesting `&` (the tokens whose parsing hands off to `namespace()`). Otherwise the namespace is empty.
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
When a bare
|introduces an empty namespace, whatever token precedes it is emitted as the namespace prefix — corrupting the selector on a round-trip:These are all valid selectors that should round-trip unchanged.
Root cause
namespace()takes the prefix from the previous token unconditionally:namespace()is reached either after a real prefix has been consumed (*|,ns|,&|, where the previous token is that prefix) or directly fromcombinator()when a bare|starts a compound. In the second case the previous token is a comment, comma, combinator or whitespace — not a prefix — but it was still used as one.Fix
Only use the previous token as the prefix when it can actually be one — a word (type selector), the universal
*, or the nesting&(these are exactly the tokens whose parsing hands off tonamespace()). Otherwise the namespace is empty (true).*|a,ns|a,&|foo, and|aare unchanged.Test plan
main).eslint/tscclean.