specificity icon indicating copy to clipboard operation
specificity copied to clipboard

Consider exposing `calculateSpecificityOfSelectorObject`

Open bramus opened this issue 2 years ago • 4 comments

As per request by @bartveneman on Twitter:

Would be nice to expose calculateSpecificityOfSelectorObject so we can avoid convertToAST -> typeof source -> source instanceof Object -> source.type == 'Selector'

https://github.com/bramus/specificity/blob/12557b78948bb4cb6438ac51e4c81c850cde5a4e/src/core/calculate.js#L5

This adds up quickly on several thousands of selectors like on our website.

Considered this in the past but hadn’t done set yet because I’m not too happy with the name of the function. Open to suggestions for a shorter - yet also clear - name. If no alternative can be found, then I’m fine with exposing as is.

bramus avatar Dec 30 '22 15:12 bramus

Maybe calculateFromAstNode or calculateSelectorNode, calculateNode? The return type is Specificity, so no real need to include that in the function name.

bartveneman avatar Dec 30 '22 17:12 bartveneman

1 more way to improve this function is to allow our own csstree walker. Project Wallace is powered by CSSTree too, so we already have a walker function available, next to the full AST. If we could expose a function that would accept a walk function and a Selector Node, that'd be 💯 , because there will not be conflict issues when our versions of CSSTree are different. function getSpeficiticity({ walker, node }) {}

bartveneman avatar Jan 01 '23 09:01 bartveneman

If we could expose a function that would accept a walk function and a Selector Node, that'd be 💯

What would that look like? Got some pseudo/theoretical code to clarify?

bramus avatar Jan 17 '23 08:01 bramus

Ah, sorry, misread the code: it doesn't use an AST walker, it just loops over some of the nodes 😬. So that already makes it pretty lightweight. The only dependency it seems to have is the max function and it returns a new Specificity at the end, which we maybe can skip?

bartveneman avatar Jan 17 '23 10:01 bartveneman