less / less.js

Less. The dynamic stylesheet language.
http://lesscss.org
Apache License 2.0
17.02k stars 3.41k forks source link

Prevent same row property value lookup #3503

Closed mik13ST closed 3 years ago

mik13ST commented 4 years ago

I wish Less property value lookup would begin the lookup on the previous row instead of the current row. It would make something like this possible:

.button {
  background-color: whitesmoke;

  &:hover {
    background-color: darken($background-color, 10%);
  }
}

Currently Less rejects to compile this with Recursive property reference for $background-color.

I think that delaying the lookup to exclude the current line would make things like this possible. I also believe this is a desirable thing.

I want to achieve this without using any variables.

I tried asking a question on StackOverflow first to see if I am not missing something.

matthew-dean commented 3 years ago

I don't think this is going to be added, because it's the same reason you can't do:

@var: 2;
.button {
  @var: @var + 2;
}

That is, Less's variables (and therefore properties) are declarative, and have a final value per scope. So they can't reference themselves, because that would set their final value, which is illogical.

You can, of course, do:

.button {
  @color: whitesmoke;
  background-color: @color;

  &:hover {
    background-color: darken(@color, 10%);
  }
}