Detached Rulesets and local scope protection
See original GitHub issueVariables returned from detached ruleset behave differently then variables returned from mixins:
- Variables returned from mixins are copied into caller scope only if they are not present in local scope. However, there is no protection for parent scope.
- Variables returned from detached rulesets are copied into caller scope only if the caller scope does not contain them at all. Both local and parent scope are protected.
Example with detached rulesets:
.wrap-mixin(@ruleset) {
@b: non-local; // parent scope is protected
.wrap-selector {
@ruleset(); // variable returned from detached ruleset is ignored
visible-one: @b;
}
}
.wrap-mixin({
@b: returned from detached ruleset;
}); // call the whole thing
compiles into:
.wrap-selector {
visible-one: non-local;
}
Example with mixins:
.ruleset-mixin() {
@b-mixin: returned from mixin;
}
.wrap-mixin-mixin() {
@b-mixin: non-local; // parent scope - no protection
.wrap-selector {
.ruleset-mixin(); // variable returned from mixin wins over the one defined in parent scope
visible-one: @b-mixin;
}
}
.wrap-mixin-mixin(); // call the whole thing
compiles into:
.wrap-selector {
visible-one: returned from mixin;
}
Expected behavior: mixins and detached rulesets should implement the same caller scope protection. Since detached rulesets are new feature and mixins old one, mixins should win and both of them should protect only local scope.
Tested with lessc 1.7.0 on windows node-js.
Issue Analytics
- State:
- Created 9 years ago
- Comments:8 (7 by maintainers)
Top Results From Across the Web
Features In-Depth | Less.js
A detached ruleset is a group of css properties, nested rulesets, media declarations or anything else stored in a variable. You can include...
Read more >Scope modifiers
The scope modifier LOCAL declares a variable local to a function; it sets the value to zero/NULL. The scope modifier PRIVATE declares a...
Read more >8. Model Local Rules
This Model Local Rule covers the example of a dropping zone used as the only relief option available (other than stroke and distance)...
Read more >LESS - Passing Rulesets to Mixins
Detached ruleset contains rulesets such as properties, nested rulesets, variables declaration, mixins, etc. It is stored in a variable and included in ...
Read more >Demystifying JavaScript Variable Scope and Hoisting
Scoping rules vary from language to language. JavaScript has two scopes: global and local. Local scope has two variations: the old function ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@thejase You don’t need DRs for this. Mixins are capable of the same w/o any DRs (and with more clean syntax): https://github.com/less/less.js/issues/2442#issuecomment-73875782 (and here’s corresponding “no DR” equivalent of your snippet above: https://gist.github.com/seven-phases-max/1f917de67753385c920816740b60c39b).
As for the ticket itself: it’s still considered an issue in https://github.com/less/less-meta/issues/16 (just for language consistency reasons) - so I’ll modify the closing post here accordingly.
I’d like to offer a valid use case for this: themed/mapped variables:
IMO, this is far better and cleaner than the redundancy of
@primary-default: ..., @primary-inverted: ...
variables.