mirror of
https://github.com/less/less.js.git
synced 2026-01-23 14:18:00 -05:00
Less's messages for JavaScript evaluation errors do not contain the actual error thrown, making them very hard to debug. This PR adds the error message to the log.
53 lines
1.6 KiB
JavaScript
53 lines
1.6 KiB
JavaScript
(function (tree) {
|
|
|
|
tree.JavaScript = function (string, index, escaped) {
|
|
this.escaped = escaped;
|
|
this.expression = string;
|
|
this.index = index;
|
|
};
|
|
tree.JavaScript.prototype = {
|
|
type: "JavaScript",
|
|
eval: function (env) {
|
|
var result,
|
|
that = this,
|
|
context = {};
|
|
|
|
var expression = this.expression.replace(/@\{([\w-]+)\}/g, function (_, name) {
|
|
return tree.jsify(new(tree.Variable)('@' + name, that.index).eval(env));
|
|
});
|
|
|
|
try {
|
|
expression = new(Function)('return (' + expression + ')');
|
|
} catch (e) {
|
|
throw { message: "JavaScript evaluation error: " + e.message + " from `" + expression + "`" ,
|
|
index: this.index };
|
|
}
|
|
|
|
for (var k in env.frames[0].variables()) {
|
|
context[k.slice(1)] = {
|
|
value: env.frames[0].variables()[k].value,
|
|
toJS: function () {
|
|
return this.value.eval(env).toCSS();
|
|
}
|
|
};
|
|
}
|
|
|
|
try {
|
|
result = expression.call(context);
|
|
} catch (e) {
|
|
throw { message: "JavaScript evaluation error: '" + e.name + ': ' + e.message + "'" ,
|
|
index: this.index };
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
};
|
|
|
|
})(require('../tree'));
|
|
|