These are chat archives for less/less.js

14th
Jan 2016
Matthew Dean
@matthew-dean
Jan 14 2016 02:59
Could someone explain how / why the visitor pattern is used in Less?
Zephraph
@zephraph
Jan 14 2016 03:00
Well, I can probably speak to the why.
The visitor pattern is a common method used to visit nodes in a tree.
Essentially when less parses everything out I assume it creates an AST of sorts.
The visitor pattern is then used to traverse that tree and view/transform nodes.
Heh, that's probably an oversimplification, and you likely knew all that already... but yeah.
Matthew Dean
@matthew-dean
Jan 14 2016 03:07
No, I'm somewhat unfamiliar with the visitor pattern. I assume this is so you can have an API which can "add methods" to a type of Node without actually altering the Node prototype?
What's confusing to me is that the nodes have functions which seem to act upon the object properties / values.... Or maybe they don't, hmm.
Zephraph
@zephraph
Jan 14 2016 03:14
can you link me to an example of what you're seeing?
Matthew Dean
@matthew-dean
Jan 14 2016 03:16
@zephraph Nothing I'm seeing. Just reviewing the API in my head, because of this: less/less-meta#10. Thoughts welcome.
Part of me is wondering how familiar the average JS user is with that pattern and if that's another barrier to understanding plugin development.
Zephraph
@zephraph
Jan 14 2016 03:18
Well, it's a well established pattern.
The thing is, it gives you a lot of power.
Matthew Dean
@matthew-dean
Jan 14 2016 03:18
I don't doubt it's well-established, but I don't know about well-established in JS.
What kind of power does it give you?
That's a pretty good article, it hopefully can provide a bit of insight.
A simplistic (and probably wrong) explanation is that it allows you to plug into a node without actually modifying the node's interface.
Granted, it's very complicated. >_>
The only thing I would say is to avoid a straight pipeline approach like postcss.
Zephraph
@zephraph
Jan 14 2016 03:26
The problem postcss has is that every plugin (potentially) parses navigates through the entire tree.
With the visitor API you visit a node, all interested parties do what they need to do, then you visit the next node.
Again, I'm no expert, but that's my understanding of it.
I think really it should probably just be cleaned up and documented better.
As you pointed out in that issue, having good documentation, examples, and best practices goes a long way to making your API more approachable.
Matthew Dean
@matthew-dean
Jan 14 2016 03:50
Yeah, I think there should be a simple explanation, maybe just in the form of what your plugin structure needs to be, without just saying it's a "visitor" and hope someone will understand what that means.
@zephraph Hey, btw, while you're here (possibly), did I tell you that started a postcss-less plugin? I got stuck when I was trying to get tests to work.
But I was successfully converting an evaluated Less tree into a PostCSS tree. You'd probably be interested in looking at that.
Matthew Dean
@matthew-dean
Jan 14 2016 03:55
The hardest part: Less's evaluated tree is not as flat as PostCSS's node tree. And further flattening those nodes in Less is, frustratingly, extremely non-trivial.
You can't just do Selector.toCSS() or something nice like that.
There's someone trying to explain it with javascript.
A bit easier to see I think.
Yeah, I think you told me you were working on that.
I'm not sure what the goal is really.
When I was working on that, I was trying to plug it into https://github.com/stylelint/stylelint
My biggest problem with less in general is the lack of a good linting solution.
Zephraph
@zephraph
Jan 14 2016 05:15
During my search for solutions (and alternatives) I ran across sass-lint which referenced their AST: https://github.com/tonyganch/gonzales-pe/tree/dev
Interestingly, it supports Less... I'm not sure to what degree, but I'd recommend you taking a look.