less / less.ruby

Less Ruby — now at http://github.com/cowboyd/less.rb
Apache License 2.0
956 stars 69 forks source link

Odd scope behavior in nested mixins. #103

Open xdissent opened 14 years ago

xdissent commented 14 years ago

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.

daz4126 commented 14 years ago

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;
  }
}