Description
This is a continuation of the 3.0 Roadmap Proposal Discussion here: #10
One of my hopes was that after custom root functions were working well, which they are, I could start digging in and try all sorts of fun things with the API.
But the more I try to do something useful, the more the API keeps kicking me in the shins. One of the major issues that the Less API has - it's completely wrong. I don't mean wrongly constructed; I mean things aren't called what they are. Just as some examples:
- At-rules (or @-rules) are called Directives (?)
- Declarations are called rules (which is wrong - "rules" is another name for "rulesets")
- Rulesets contain collections of nodes, but nodes are called Rules, even though they might not be rules
To be fair, none of these things were named for an "API"; those are just the internal names originally assigned. Less didn't have plugins at that time, so it didn't matter. Moving on:
Node complexity
As @seven-phases-max noted in #10 (comment), Less, out of necessity, has a number of unique node types. That's fine, but what would be challenging for an average developer is that it's an unknown which node type they actually want to create, and a typical CSS value, for example, might be a collection of a number of different nested Less nodes.
For example: an Element node is a uniquely Less concept (I don't mean the concept of an element, I just mean the concept that a selector has sub-nodes and those sub-nodes have combinators), and it's not necessarily intuitive how one would construct a collection of Element nodes to represent a single selector. Or if one is creating a CSS value, which nodes might be created to exist as part of that value.
So, even if you could document the nodes, I'm not sure it would make any sense at first glance.
Proposal
I think there are 2 main things which could simply 99% of use cases.
- First, name things what they are. Change node types to be consistent to how they're documented in the CSS spec. Obviously, a concern is that this could be a breaking change to any plugins that add node visitors. But I don't think Less's plugin ecosystem has been that significant thus far partly for many reasons already listed. But someone could speak to that.
- I think it would be prudent to at least support parsing (in the API) of selectors and CSS values. That way you could write very simple statements like:
var decl = new less.Declaration("border": "1px solid black");
var rule = new less.Ruleset(".box", [ decl ]);
Right now something like that would take about 10x the code, and requires passing in information about index and fileinfo (the latter of which should never be needed). But I think it wouldn't take too much work to hook back in some of the parsing logic to make things a lot simpler.