question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Detached Rulesets and local scope protection

See original GitHub issue

Variables 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:closed
  • Created 9 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
seven-phases-maxcommented, May 31, 2018

@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.

0reactions
JawsomeJasoncommented, May 30, 2018

I’d like to offer a valid use case for this: themed/mapped variables:

// MIXINS FOR SETTINGS/GETTING THEMES
#THEMES {
  .set(@name, @vars) {
    #THEMES {
      .get(@@name) {
        @vars();
      }
    }
  }
}


// ESTABLISH THEMES
#THEMES.set(default, {
  @primary: black;
  @contrast: white;
});

#THEMES.set(inverted, {
  @primary: white;
  @contrast: black;
});

#THEMES.set(error, {
  @primary: white;
  @contrast: red;
});

// use themes
body {
  #THEMES.get(default);
  background-color: @primary;
  color: @contrast;
}

.error {
  #THEMES.get(error);

  color: @contrast;
  background-color: lighten(@contrast, 95%);
  border: 1px solid @contrast;
}

IMO, this is far better and cleaner than the redundancy of @primary-default: ..., @primary-inverted: ... variables.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found