feature-request: allow specificity calculation of AST's of popular parsers
- Dominant language
- JavaScript
- Stars
- 97
- Forks
- 7
- PR merge metrics
- No merged PRs in 30d
Description
TIL that [this semi-copy exists (14M weekly downloads)](https://github.com/csstools/postcss-plugins/tree/682ce345f5834d50328374572892555998e8e529/packages/selector-specificity) for the sole reason that PostCSS AST's can be used to calculate specificity instead of CSSTree. Unfortunately I ran into the same issue for Project Wallace where I re-implemented specificity to cater for `@projectwallace/css-parser`'s AST.
It'd be nice to have a more generic calculator, so I want to use this issue for some bikeshedding and gathering ideas. I'm going to make some proposals as a starting point:
```ts
calculate(selector: string): Specificity
// which calls CSSTree by default, as it does now
import type { Selector } from 'csstree'
calculateForAST(node: Selector): Specificity
// But then we could also expose
import type { Node } from 'postcss'
calculatePostCSS(node): Specificity
import type { Selector } from '@projectwallace/css-parser'
calculateProjectWallace(node: Selector): Specificity
```
The hard-to-design part here is to make one function that handles the specificity calculation, regardless of parser or node types:
```ts
// all above calculate() methods call this under the hood:
calculateGeneric()
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.