Skip to content

Commit

Permalink
show source location for the strict lookup exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sohibegit authored and nknapp committed Oct 9, 2019
1 parent 0b593bf commit feb60f8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/handlebars/compiler/javascript-compiler.js
Expand Up @@ -1091,6 +1091,7 @@ JavaScriptCompiler.prototype = {

setupHelperArgs: function(helper, paramSize, params, useRegister) {
let options = this.setupParams(helper, paramSize, params);
options.loc = JSON.stringify(this.source.currentLocation);
options = this.objectLiteral(options);
if (useRegister) {
this.useRegister('options');
Expand Down Expand Up @@ -1150,7 +1151,7 @@ function strictLookup(requireTerminal, compiler, parts, type) {
}

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 {
return stack;
}
Expand Down
15 changes: 13 additions & 2 deletions lib/handlebars/exception.js
@@ -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) {
let loc = node && node.loc,
line,
column;
endLineNumber,
column,
endColumn;

if (loc) {
line = loc.start.line;
endLineNumber = loc.end.line;
column = loc.start.column;
endColumn = loc.end.column;

message += ' - ' + line + ':' + column;
}
Expand All @@ -27,6 +32,7 @@ function Exception(message, node) {
try {
if (loc) {
this.lineNumber = line;
this.endLineNumber = endLineNumber;

// Work around issue under safari where we can't directly set the column value
/* istanbul ignore next */
Expand All @@ -35,8 +41,13 @@ function Exception(message, node) {
value: column,
enumerable: true
});
Object.defineProperty(this, 'endColumn', {
value: endColumn,
enumerable: true
});
} else {
this.column = column;
this.endColumn = endColumn;
}
}
} catch (nop) {
Expand Down
6 changes: 3 additions & 3 deletions lib/handlebars/runtime.js
Expand Up @@ -79,9 +79,9 @@ export function template(templateSpec, env) {

// Just add water
let container = {
strict: function(obj, name) {
if (!(name in obj)) {
throw new Exception('"' + name + '" not defined in ' + obj);
strict: function(obj, name, loc) {
if (!obj || !(name in obj)) {
throw new Exception('"' + name + '" not defined in ' + obj, { loc: loc });
}
return obj[name];
},
Expand Down
19 changes: 19 additions & 0 deletions spec/strict.js
Expand Up @@ -95,6 +95,25 @@ describe('strict', function() {
};
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() {
Expand Down

0 comments on commit feb60f8

Please sign in to comment.