postcss: `Result.root` that is typed as `Document | Root` breaks the semantics of the `root` property
Ever since the addition of Document I’ve been “arguing” with the types of PostCSS a lot more.
I’ve also been unable to add meaningful support for any syntax that makes use of the Document node because those syntax plugins have blocking issues that prevent real support.
Any plugin works equally bad or well as it did before this addition and none leverage this new node type.
So from my perspective the addition of Document isn’t beneficial.
I feel that the current implementation isn’t quite right.
I don’t know what should be changed to fix this.
This specific issue is about this code :
What I want to do :
- parse a stylesheet that is a
foo.cssfile - access the
Rootnode
I already know that it is a Root node but now the type system is complaining and saying that it can be a Document node.
I can override this and tell TypeScript that it really is a Root node.
But what if someone uses one of the syntaxes that does return a Document in this case.
Which steps should I take :
- warn on
Documentnodes inResult.root? - throw an error?
- traverse the
Documentnodes in search ofRootnodes?
I think result.root must always return a Root node.
Never a Document node.
Maybe a new property can be added to expose Document nodes?
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 17 (17 by maintainers)
The
process().rootfix was released in 8.4.28.Yes, in
Oncethere is no other way 😅Sorry, it is not very popular to fix (and I don’t know simple solution).
I will release fix to
process().rootand close the issue.Unfortunately, it will be to big change for small use case.
Where exactly for you have a problem with
result.rootcase? Maybe I can suggest a better way to handle it if you will provide me more context.I added
Result#rootcasting fromprocess({ parser })type 7d51976aIt will be
Rootif we didn’t passparseroption and will haveDocumenttype only ifopts.parsertypes returnDocument.By if we don’t know source of the
Result({ result }in plugin) it will continue to beRoot | Document.What do you think? Will it simplify PostCSS API for you?
Nope, we are planning to use
Documentand don’t plan many changes.