show source location for the strict lookup exceptions

This commit is contained in:
sohibe
2019-05-29 03:39:03 +03:00
committed by Nils Knappmeier
parent 0b593bfe5d
commit feb60f85c9
4 changed files with 37 additions and 6 deletions
@@ -1091,6 +1091,7 @@ JavaScriptCompiler.prototype = {
setupHelperArgs: function(helper, paramSize, params, useRegister) { setupHelperArgs: function(helper, paramSize, params, useRegister) {
let options = this.setupParams(helper, paramSize, params); let options = this.setupParams(helper, paramSize, params);
options.loc = JSON.stringify(this.source.currentLocation);
options = this.objectLiteral(options); options = this.objectLiteral(options);
if (useRegister) { if (useRegister) {
this.useRegister('options'); this.useRegister('options');
@@ -1150,7 +1151,7 @@ function strictLookup(requireTerminal, compiler, parts, type) {
} }
if (requireTerminal) { if (requireTerminal) {
return [compiler.aliasable('container.strict'), '(', stack, ', ', compiler.quotedString(parts[i]), ')']; return [compiler.aliasable('container.strict'), '(', stack, ', ', compiler.quotedString(parts[i]), ', ', JSON.stringify(compiler.source.currentLocation), ' )'];
} else { } else {
return stack; return stack;
} }
+13 -2
View File
@@ -1,13 +1,18 @@
const errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack']; const errorProps = ['description', 'fileName', 'lineNumber', 'endLineNumber', 'message', 'name', 'number', 'stack'];
function Exception(message, node) { function Exception(message, node) {
let loc = node && node.loc, let loc = node && node.loc,
line, line,
column; endLineNumber,
column,
endColumn;
if (loc) { if (loc) {
line = loc.start.line; line = loc.start.line;
endLineNumber = loc.end.line;
column = loc.start.column; column = loc.start.column;
endColumn = loc.end.column;
message += ' - ' + line + ':' + column; message += ' - ' + line + ':' + column;
} }
@@ -27,6 +32,7 @@ function Exception(message, node) {
try { try {
if (loc) { if (loc) {
this.lineNumber = line; this.lineNumber = line;
this.endLineNumber = endLineNumber;
// Work around issue under safari where we can't directly set the column value // Work around issue under safari where we can't directly set the column value
/* istanbul ignore next */ /* istanbul ignore next */
@@ -35,8 +41,13 @@ function Exception(message, node) {
value: column, value: column,
enumerable: true enumerable: true
}); });
Object.defineProperty(this, 'endColumn', {
value: endColumn,
enumerable: true
});
} else { } else {
this.column = column; this.column = column;
this.endColumn = endColumn;
} }
} }
} catch (nop) { } catch (nop) {
+3 -3
View File
@@ -79,9 +79,9 @@ export function template(templateSpec, env) {
// Just add water // Just add water
let container = { let container = {
strict: function(obj, name) { strict: function(obj, name, loc) {
if (!(name in obj)) { if (!obj || !(name in obj)) {
throw new Exception('"' + name + '" not defined in ' + obj); throw new Exception('"' + name + '" not defined in ' + obj, { loc: loc });
} }
return obj[name]; return obj[name];
}, },
+19
View File
@@ -95,6 +95,25 @@ describe('strict', function() {
}; };
equals(template({}, {helpers: helpers}), 'success'); equals(template({}, {helpers: helpers}), 'success');
}); });
it('should show error location on missing property lookup', function() {
shouldThrow(function() {
var template = CompilerContext.compile('\n\n\n {{hello}}', {strict: true});
template({});
}, Exception, '"hello" not defined in [object Object] - 4:5');
});
it('should error contains correct location properties on missing property lookup', function() {
try {
var template = CompilerContext.compile('\n\n\n {{hello}}', {strict: true});
template({});
} catch (error) {
equals(error.lineNumber, 4);
equals(error.endLineNumber, 4);
equals(error.column, 5);
equals(error.endColumn, 10);
}
});
}); });
describe('assume objects', function() { describe('assume objects', function() {