Skip to content

Allows root (non-value) functions in Less - #2785

Merged
matthew-dean merged 9 commits into
masterfrom
root-functions
Mar 15, 2016
Merged

matthew-dean merged 9 commits into
masterfrom
root-functions

Conversation

@matthew-dean

Copy link
Copy Markdown
Member

This PR greatly increases the utility of @plugin in the following ways:

  1. Custom functions can be called from the root of a ruleset. This is so plugins can return any type of node, not just a Value node. Less still checks during evaluation if the node is allowed there. So, for example, if no custom function is defined, then Less will assume it's an unknown CSS function, which is not allowed in the root.
  2. Like mixins, custom functions can receive detached rulesets, to do more advanced transformations of rules via JavaScript, or: to simply pass "structured data" to a function. Or, to pass a Less list and another node type to do some kind of processing on each member of the list as it relates to that Node. There really isn't a limitation here.
  3. Functions now allow semi-colon separators. This addresses Allow Semicolon as Function Argument Separator #1505 and a number of the use cases in Allow parametrized mixins as detached rulesets to form 'lambdas' #2270.

I've added some additional tests demonstrating custom functions.

@matthew-dean

Copy link
Copy Markdown
Member Author

Just as an aside, I discovered from this that creating Less nodes is HARD. That is, there is very little "auto-conversion" of strings to node types, and Less nodes are very granular. So there's certainly still room for improvement on the API side.

@matthew-dean matthew-dean changed the title Root functions for custom plugins Allows root (non-value) functions in Less Jan 27, 2016
@matthew-dean

Copy link
Copy Markdown
Member Author

@seven-phases-max I probably went overkill on the number of tests I added, but I did one for every node type found in a ruleset root that should produce an error.

Take a look and let me know if you think it's safe to merge.

matthew-dean added a commit that referenced this pull request Mar 15, 2016
Allows root (non-value) functions in Less
@matthew-dean
matthew-dean merged commit 2f07cd7 into master Mar 15, 2016
@matthew-dean
matthew-dean deleted the root-functions branch March 15, 2016 16:52
wmfgerrit pushed a commit to wikimedia/less.php that referenced this pull request Nov 2, 2026
The guts of this change is in checkValidNodes. The rest is implementing
the new `allowRoot` field in Less_Tree classes, matching less-3.13.1.js
(ctrl-F for `.allowRoot =`).

```
$ git grep -F '.allowRoot =' | wc -l
12 # test/assets/less-3.13.1.js
$ git grep -F '>allowRoot =' lib/  | wc -l
12 # lib/Less/Tree/*.php
```

Most of this landed in upstream with less/less.js@b67403d3
in less/less.js#2785 which doesn't change
behavior but makes internal checks stricter to catch mistakes and
syntax errors, none of which we are currently triggering.

I also reviewed nearby regions of code for differences with
Less.js 3.13 and either:
* Port any minor differences now.
* Add specific TODOs for bigger changes to port later.
* Update `@see` from 2.x to 3.x.

Bug: T288498
Change-Id: Ib3e36c99e768540028c640e1a6a8d540288947b6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants