You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Global mixins and mixins inside namespaces handle scopes differently #1316
Scoping of global mixins is handled differently then scoping of mixin inside namespaces:
If I call mixin directly, it does not look into callers scope until it searched whole mixins scope. It will go through all parents, including the global scope.
If I call mixin from namespace, it searches only local scope and then proceeds to callers scope. Anything defined in definition parents scope is ignored.
Should they behave differently? If no, which version is correct?
Direct mixin call - mixin searches definition space first. It will never hit the caller:
/* define global mixin and variable */.content(){mixin:global;}@content: global;/* mixin to be called */.mobile(){
variable:@content;.content();}.big-desktop-button{/* define local mixin and variable */.content(){mixin:caller;}/* call mixin */@content: caller;.mobile();}
If I place exactly the same mixin into a namespace, output changes:
More over if you remove #namespace's parens (i.e. turn a parametric namespace into non-parametric) the result is global again :)
I was recently trying to make a sort of graphical cheat sheet for LESS 'scope precedence' (because sometimes it may be really complicated to retain which one overrides which other). Well, I did not finish it (because I did not manage to find a satisfactory visual representation - yet I hope), but here's at least a basic crib that covers most of typical scenarios, it's actually a two because parametric and non-parametric parent mixins (aka namespaces) result in a different precedence:
non-parametric parent: local > callee > parent > global > caller
parametric parent: local > callee > caller > global > parent
The code template I used (to illustrate what all those local/callee/parent/global/caller refer to):
.parent() {
.sub() {var-in-sub: @var}
.sup() {var-in-sup: @var}
}
.output {
@var: green;
.parent();
.sub(); // -> black
.parent.sup(); // -> green
}
// somewhere in a far far away galaxy:
.generate-some-classes() {
@var: black;
div {
color: @var;
}
}
.generate-some-classes();
Now thinking of it in context of #2212 (comment) it becomes to look even more tricky: the problem is that global scope is the "parent" of the .sub too and it may be not really possible to fix this w/o breaking the rest of things :( I.e. is it possible to have parent > caller > global at all (at least this is what I hoped to get everywhere eventually) since a parent scope is something quite abstract actually and in many cases the parentis the global...
Activity
seven-phases-max commentedon Oct 20, 2013
More over if you remove
#namespace's parens (i.e. turn a parametric namespace into non-parametric) the result isglobalagain :)I was recently trying to make a sort of graphical cheat sheet for LESS 'scope precedence' (because sometimes it may be really complicated to retain which one overrides which other). Well, I did not finish it (because I did not manage to find a satisfactory visual representation - yet I hope), but here's at least a basic crib that covers most of typical scenarios, it's actually a two because parametric and non-parametric parent mixins (aka namespaces) result in a different precedence:
local>callee>parent>global>callerlocal>callee>caller>global>parentThe code template I used (to illustrate what all those local/callee/parent/global/caller refer to):
p.s. edit: fixed parametric vs. non-parametric precedence sheet (accidentally swapped).
seven-phases-max commentedon Sep 30, 2014
Another curiouse example inspired by #2212 (comment):
Now thinking of it in context of #2212 (comment) it becomes to look even more tricky: the problem is that
globalscope is the "parent" of the.subtoo and it may be not really possible to fix this w/o breaking the rest of things :( I.e. is it possible to haveparent>caller>globalat all (at least this is what I hoped to get everywhere eventually) since aparentscope is something quite abstract actually and in many cases theparentis theglobal...