Marko v3: Improved error message for invalid JS expressions

This commit is contained in:
Patrick Steele-Idem 2016-01-10 17:52:14 -07:00
parent 08b63b535a
commit d403308c21

View File

@ -1,4 +1,5 @@
'use strict';
var charProps = require('char-props');
const esprima = require('esprima');
const Expression = require('../ast/Expression');
@ -201,7 +202,27 @@ function convert(node) {
}
function parseExpression(src) {
let jsAST = esprima.parse('(' + src + ')');
let jsAST;
try {
jsAST = esprima.parse('(' + src + ')');
} catch(e) {
var errorIndex = e.index;
var errorMessage = e.toString();
if (errorIndex != null && errorIndex >= 0) {
var srcCharProps = charProps(src);
errorIndex--; // Account for extra paren added to start
let line = srcCharProps.lineAt(errorIndex)+1;
let column = srcCharProps.columnAt(errorIndex)+1;
errorMessage += ' [Line ' + line + ', Col: ' + column + ']';
}
var wrappedError = new Error('Invalid JavaScript expression: ' + src + ' - ' + errorMessage);
wrappedError.index = errorIndex;
wrappedError.src = src;
wrappedError.code = 'ERR_INVALID_JAVASCRIPT_EXPRESSION';
throw wrappedError;
}
var converted = convert(jsAST);
if (converted == null) {
converted = new Expression({value: src});