Passing a variable list through .for loop using Le

2019-09-03 07:55发布

问题:

I have been using a Less mixin which makes looping in Less, less verbose and is really handy: https://github.com/seven-phases-max/less.curious/

I upgraded from Less 2.6.0 to Less 2.6.1 and now the Less will not compile.

Here's the Less code:

// ............................................................
// .for

.for(@i, @n) {.-each(@i)}
.for(@n)     when (isnumber(@n)) {.for(1, @n)}
.for(@i, @n) when not (@i = @n)  {
.for((@i + (@n - @i) / abs(@n - @i)), @n);
}

// ............................................................
// .for-each

.for(@array)   when (default()) {.for-impl_(length(@array))}
.for-impl_(@i) when (@i > 1)    {.for-impl_((@i - 1))}
.for-impl_(@i) when (@i > 0)    {.-each(extract(@array, @i))}

@gray-base: #000;
@gray-darker: #222;
@gray-dark: #333;
@gray: #555;
@gray-light: #777;
@gray-lighter: #eee;

@gray-type:
 gray,
 gray-base,
 gray-dark,
 gray-darker,
 gray-light,
 gray-lighter;

.gray-type {
    // Mixin to generate the gray font colours for p tags in the font stacks.
    .for(@gray-type); .-each(@name) {
        .@{name} {
            color: @@name;
        }
    }   
}

Here's the expected output:

.gray-type .gray {
  color: #555;
}
.gray-type .gray-base {
  color: #000;
}
.gray-type .gray-dark {
  color: #333;
}
.gray-type .gray-darker {
  color: #222;
}
.gray-type .gray-light {
  color: #777;
}
.gray-type .gray-lighter {
  color: #eee;
}

After upgrading to Less 2.6.1 I get the following error:

>> SyntaxError: variable @[object Object],[object Object] is undefined in less/style.less on line 41, column 3:
>> 40       // Mixin to generate the gray font colours for p tags in the font stacks.
>> 41       .for(@gray-type); .-each(@name) {
>> 42           .@{name} {
Warning: Error compiling less/style.less Use --force to continue.

I think the problem is related to the hyphens being stripped from the variables during compilation, therefore making the variables passed though the list invalid.

Any help would be greatly appreciated.

标签: less