10000 fix Runtime error reports from imported files · recomputing/less.js@aefd310 · GitHub
[go: up one dir, main page]

Skip to content

Commit aefd310

Browse files
committed
fix Runtime error reports from imported files
1 parent 2cc1b01 commit aefd310

File tree

5 files changed

+35
-20
lines changed

5 files changed

+35
-20
lines changed

lib/less/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ less.Parser.importer = function (file, paths, callback) {
110110
paths: [path.dirname(pathname)].concat(paths),
111111
filename: pathname
112112
}).parse(data, function (e, root) {
113-
callback(e, root);
113+
callback(e, root, data);
114114
});
115115
});
116116
} else {

lib/less/parser.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ less.Parser = function Parser(env) {
7373
paths: env && env.paths || [], // Search paths, when importing
7474
queue: [], // Files which haven't been imported yet
7575
files: {}, // Holds the imported parse trees
76+
contents: {}, // Holds the imported file contents
7677
mime: env && env.mime, // MIME type of .less files
7778
error: null, // Error in parsing/evaluating an import
7879
push: function (path, callback) {
@@ -82,9 +83,10 @@ less.Parser = function Parser(env) {
8283
//
8384
// Import a file asynchronously
8485
//
85-
less.Parser.importer(path, this.paths, function (e, root) {
86+
less.Parser.importer(path, this.paths, function (e, root, contents) {
8687
that.queue.splice(that.queue.indexOf(path), 1); // Remove the path from the queue
8788
that.files[path] = root; // Store the root
89+
that.contents[path] = contents;
8890

8991
if (e && !that.error) { that.error = e }
9092
callback(e, root);
@@ -189,7 +191,15 @@ less.Parser = function Parser(env) {
189191
}
190192
}
191193

192-
function getLocation(index) {
194+
function getInput(e, env) {
195+
if (e.filename && env.filename && (e.filename !== env.filename)) {
196+
return parser.imports.contents[e.filename];
197+
} else {
198+
return input;
199+
}
200+
}
201+
202+
function getLocation(index, input) {
193203
for (var n = index, column = -1;
194204
n >= 0 && input.charAt(n) !== '\n';
195205
n--) { column++ }
@@ -199,18 +209,19 @@ less.Parser = function Parser(env) {
199209
}
200210

201211
function LessError(e, env) {
202-
var lines = input.split('\n'),
203-
loc = getLocation(e.index),
212+
var input = getInput(e, env),
213+
loc = getLocation(e.index, input),
204214
line = loc.line,
205-
col = loc.column;
215+
col = loc.column,
216+
lines = input.split('\n');
206217

207-
this.type = e.type || 'SyntaxError';
218+
this.type = e.type || 'Syntax';
208219
this.message = e.message;
209220
this.filename = e.filename || env.filename;
210221
this.index = e.index;
211222
this.line = typeof(line) === 'number' ? line + 1 : null;
212-
this.callLine = e.call && (getLocation(e.call) + 1);
213-
this.callExtract = lines[getLocation(e.call)];
223+
this.callLine = e.call && (getLocation(e.call, input) + 1);
224+
this.callExtract = lines[getLocation(e.call, input)];
214225
this.stack = e.stack;
215226
this.column = col;
216227
this.extract = [
@@ -559,7 +570,7 @@ less.Parser = function Parser(env) {
559570

560571
if (! $(')')) return;
561572

562-
if (name) { return new(tree.Call)(name, args, index) }
573+
if (name) { return new(tree.Call)(name, args, index, env.filename) }
563574
},
564575
arguments: function () {
565576
var args = [], arg;
@@ -635,7 +646,7 @@ less.Parser = function Parser(env) {
635646
var name, index = i;
636647

637648
if (input.charAt(i) === '@' && (name = $(/^@@?[\w-]+/))) {
638-
return new(tree.Variable)(name, index);
649+
return new(tree.Variable)(name, index, env.filename);
639650
}
640651
},
641652

@@ -746,7 +757,7 @@ less.Parser = function Parser(env) {
746757
}
747758

748759
if (elements.length > 0 && ($(';') || peek('}'))) {
749-
return new(tree.mixin.Call)(elements, args, index, important);
760+
return new(tree.mixin.Call)(elements, args, index, env.filename, important);
750761
}
751762
},
752763

lib/less/tree/call.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
//
44
// A function call node.
55
//
6-
tree.Call = function (name, args, index) {
6+
tree.Call = function (name, args, index, filename) {
77
this.name = name;
88
this.args = args;
99
this.index = index;
10+
this.filename = filename;
1011
};
1112
tree.Call.prototype = {
1213
//
@@ -31,7 +32,7 @@ tree.Call.prototype = {
3132
throw { type: e.type || "Runtime",
3233
message: "error evaluating function `" + this.name + "`" +
3334
(e.message ? ': ' + e.message : ''),
34-
index: this.index };
35+
index: this.index, filename: this.filename };
3536
}
3637
} else { // 2.
3738
return new(tree.Anonymous)(this.name +

lib/less/tree/mixin.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
(function (tree) {
22

33
tree.mixin = {};
4-
tree.mixin.Call = function (elements, args, index, important) {
4+
tree.mixin.Call = function (elements, args, index, filename, important) {
55
this.selector = new(tree.Selector)(elements);
66
this.arguments = args;
77
this.index = index;
8+
this.filename = filename;
89
this.important = important;
910
};
1011
tree.mixin.Call.prototype = {
@@ -21,7 +22,7 @@ tree.mixin.Call.prototype = {
2122
rules, mixins[m].eval(env, this.arguments, this.important).rules);
2223
match = true;
2324
} catch (e) {
24-
throw { message: e.message, index: e.index, stack: e.stack, call: this.index };
25+
throw { message: e.message, index: e.index, filename: this.filename, stack: e.stack, call: this.index };
2526
}
2627
}
2728
}
@@ -34,13 +35,13 @@ tree.mixin.Call.prototype = {
3435
this.arguments.map(function (a) {
3536
return a.toCSS();
3637
}).join(', ') + ")`",
37-
index: this.index };
38+
index: this.index, filename: this.filename };
3839
}
3940
}
4041
}
4142
throw { type: 'Name',
4243
message: this.selector.toCSS().trim() + " is undefined",
43-
index: this.index };
44+
index: this.index, filename: this.filename };
4445
}
4546
};
4647

lib/less/tree/variable.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(function (tree) {
22

3-
tree.Variable = function (name, index) { this.name = name, this.index = index };
3+
tree.Variable = function (name, index, file) { this.name = name, this.index = index, this.file = file };
44
tree.Variable.prototype = {
55
eval: function (env) {
66
var variable, v, name = this.name;
@@ -15,7 +15,9 @@ tree.Variable.prototype = {
1515
}
1616
})) { return variable }
1717
else {
18-
throw { message: "variable " + name + " is undefined",
18+
throw { type: 'Name',
19+
message: "variable " + name + " is undefined",
20+
filename: this.file,
1921
index: this.index };
2022
}
2123
}

0 commit comments

Comments
 (0)
0