Skip to content

Commit

Permalink
fixed stacktraces line number when used multiline js expressions
Browse files Browse the repository at this point in the history
Signed-off-by: Tj Holowaychuk <tj@vision-media.ca>
  • Loading branch information
Yuriy Bogdanov authored and tj committed Jun 20, 2011
1 parent ff1b737 commit 101bc69
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ejs.js
Expand Up @@ -190,6 +190,11 @@ var parse = exports.parse = function(str, options){
var start = i;
var end = str.indexOf(close, i);
var js = str.substring(i, end);
var n = 0;
while ((n = js.indexOf("\n", n)) > -1) {
n++;
lineno++;
}
if (js[0] == ':') js = filtered(js);
buf.push(prefix, js, postfix);
i += end - start + close.length - 1;
Expand Down
5 changes: 5 additions & 0 deletions lib/ejs.js
Expand Up @@ -141,6 +141,11 @@ var parse = exports.parse = function(str, options){
var start = i;
var end = str.indexOf(close, i);
var js = str.substring(i, end);
var n = 0;
while ((n = js.indexOf("\n", n)) > -1) {
n++;
lineno++;
}
if (js[0] == ':') js = filtered(js);
buf.push(prefix, js, postfix);
i += end - start + close.length - 1;
Expand Down
23 changes: 23 additions & 0 deletions test/ejs.test.js
Expand Up @@ -241,5 +241,28 @@ module.exports = {
var lineno = parseInt(err.toString().match(/ejs:(\d+)\n/)[1]);
assert.eql(lineno,3,"Error should been thrown on line 3, was thrown on line "+lineno);
}
},

'test useful stack traces multiline': function(assert){
var str = [
"A little somethin'",
"somethin'",
"<% var some = 'pretty';",
" var multiline = 'javascript';",
"%>",
"<% if (name) { %>", // Failing line
" <p><%= name %></p>",
" <p><%= email %></p>",
"<% } %>"
].join("\n");

try {
ejs.render(str)
} catch( err ){
assert.includes(err.message,"name is not defined");
assert.eql(err.name,"ReferenceError");
var lineno = parseInt(err.toString().match(/ejs:(\d+)\n/)[1]);
assert.eql(lineno,6,"Error should been thrown on line 3, was thrown on line "+lineno);
}
}
};

0 comments on commit 101bc69

Please sign in to comment.