less.ruby icon indicating copy to clipboard operation
less.ruby copied to clipboard

Odd scope behavior in nested mixins.

Open xdissent opened this issue 14 years ago • 1 comments

Nested mixins behave differently when the top level mixin is called with and without arguments. Here's an example:

@glob: red;

.setup(@outside: green) {
    color: @outside;
    background-color: @glob;

    .run(@inside: blue) {
        color: @inside;
        background-color: @outside;
    }
}

body {
    .setup(black);

    p {
        .run(white);
    }
}

Fails with: ! Variable Name Error: .run in p is undefined.. But when the .setup mixin is called without arguments, everything behaves perfectly:

@glob: red;

.setup(@outside: green) {
    color: @outside;
    background-color: @glob;

    .run(@inside: blue) {
        color: @inside;
        background-color: @outside;
    }
}

body {
    .setup;

    p {
        .run(white);
    }
}

becomes:

body {
  color: green;
  background-color: red;
}
body p {
  color: white;
  background-color: green;
}

If this worked with arguments, it would be ideal for pseudo namespacing of mixins. Since the scope resolution always falls back to the parent scope, really cool complex conditional styles could be created.

xdissent avatar Oct 12 '09 22:10 xdissent

This also doesn't work if the nested mixin does NOT have an argument (even though you'd expect it to default to blue), the error message is the same ! Variable Name Error: .run in p is undefined.:

@glob: red;

.setup(@outside: green) {
  color: @outside;
  background-color: @glob;

  .run(@inside: blue) {
    color: @inside;
    background-color: @outside;
  }
}

body {
  .setup;

p {
    .run;
  }
}

daz4126 avatar Oct 18 '09 10:10 daz4126