Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement cycle detection for mixins. #25

Open
SomMeri opened this issue Oct 8, 2012 · 3 comments
Open

Implement cycle detection for mixins. #25

SomMeri opened this issue Oct 8, 2012 · 3 comments

Comments

@SomMeri
Copy link
Owner

SomMeri commented Oct 8, 2012

If variables or mixins reference each other in cycle, current version behavior is undefined. That means, that it will crash with stack overflow error.

@SomMeri
Copy link
Owner Author

SomMeri commented Dec 21, 2012

Cycle detection for variables moved to separate issue: #61

@SomMeri
Copy link
Owner Author

SomMeri commented Dec 21, 2012

Cycle detection for mixins is not implemented and the implementation is postponed. Discussion:

Mixins can lead into cycles too:

.mixin {
  .mixin();
}

Detecting mixins cycles is harder then detecting variables cycles. Mixins can and do influence the state, so mixin can call itself without being in cycle:

.conditional(@a) {
  color: blue;
}

// mixin calls itself, but there is no cycle
.conditional(@a) when (@a > 10) {
  padding: @a;
  .conditional(@a - 1);
}

#usePlace {
  .conditional(13);
}

compiles into:

#usePlace {
  padding: 13;
  padding: 12;
  padding: 11;
  color: blue;
}

However, very similar less would cycle:

.conditional(@a) {
  color: blue;
}

// cycle
.conditional(@a) when (@a > 10) {
  padding: @a;
  .conditional(@a + 1);
}

#usePlace {
  .conditional(13);
}

In addition, each mixin has access to all variables defined in its caller, so all caller variables act like additional "hidden parameters":

.button() when (@a>10) {
  .button();
}

#maybeCycle {
  @a: 11; // causes stact overflow
  // @a: 2; // uncomment to remove stack overflow
 .button();
}

It is possible to detect cycles in some special situations e.g. if the cycle repeats the same state multiple times, but detecting them in all cases is most likely NP-hard problem.

Possible solution would be to detect unusually long mixins chains or measure memory and stop generation prior to stack overflow. We could then make some attempt to detect the cycle and print the most likely candidate.

@SomMeri
Copy link
Owner Author

SomMeri commented Feb 28, 2013

Less.js has limited support for this, it was removed from 1.3.3 unit tests file mixins.less. Needs to be added before closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant