Can't use color function on variable defined with javascript expression
With less 1.1.3 and 1.1.4, I can't write the following:
@mainColor: ~`myJavascriptVariable`;
button {
background-color: lighten(@mainColor, 30%);
}
without getting:
error evaluating function `lighten`
test.less on line 3, column 20:
myJavascriptVariable is defined in header before stylesheet and less script:
var myJavascriptVariable= "#ff0000";
myJavascriptVariable is defined correctly and it works well if we don't use color functions. Cheers.
The problem caused by the fact that any javascript variable become tree.Anonymous class instance under the hood. This class doesn't provide implementation of 'operate' method, so any operations are impossible. To fix it please, apply following code: less-1.1.3.js lines 1833-1839 Before:
if (typeof(result) === 'string') {
return new(tree.Quoted)('"' + result + '"', result, this.escaped, this.index);
} else if (Array.isArray(result)) {
return new(tree.Anonymous)(result.join(', '));
} else {
return new(tree.Anonymous)(result);
}
After:
if (typeof(result) === 'string') {
var match;
if (match = /^(-?\d*\.?\d+)(px|%|em|pc|ex|in|deg|s|ms|pt|cm|mm|rad|grad|turn)?/.exec(result)) {
return new (tree.Dimension)(match[1], match[2]);
} else if (match = /^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})/.exec(result)) {
return new(tree.Color)(match[1]);
}
return new(tree.Quoted)('"' + result + '"', result, this.escaped, this.index);
} else if (Array.isArray(result)) {
return new(tree.Anonymous)(result.join(', '));
} else {
return new(tree.Anonymous)(result);
}
Now we're checking whether passed js string matches color or dimension declaration and instantiate corresponding instance. It works quite well for me.
from the docs
If you want to parse a JavaScript string as a hex color, you may use the color function:
@color: color(`window.colors.baseColor`);