Report dougJSONParse errors in same format as other parse errors

This commit is contained in:
Jonathan Palardy
2020-09-10 20:44:35 -07:00
parent d506cf924b
commit b4cf02bd80
2 changed files with 12 additions and 3 deletions

View File

@@ -67,7 +67,12 @@ var options = require("./nomnom")
if (options.compact) {
var fileName = options.file? options.file + ': ' : '';
parser.parseError = parser.lexer.parseError = function(str, hash) {
console.error(fileName + 'line '+ hash.loc.first_line +', col '+ hash.loc.last_column +', found: \''+ hash.token +'\' - expected: '+ hash.expected.join(', ') +'.');
// error from dougJSONParse
if (hash.message) {
console.error(fileName + 'line '+ hash.line +', col '+ hash.col +',', hash.message + '.');
} else {
console.error(fileName + 'line '+ hash.loc.first_line +', col '+ hash.loc.last_column +', found: \''+ hash.token +'\' - expected: '+ hash.expected.join(', ') +'.');
}
throw new Error(str);
};
}

View File

@@ -422,8 +422,12 @@ jsonlint.parse = function(input) {
dougJSONParse(input);
} catch(e) {
if(/Duplicate key|Bad string|Unexpected/.test(e.message)) {
var lineNumber = input.substring(0, e.at).split('\n').length;
throw SyntaxError(e.message + ' on line ' + lineNumber);
var linesUntilError = input.substring(0, e.at).split('\n');
var line = linesUntilError.length;
var col = linesUntilError[line - 1].length - 1;
this.parseError(e.message, {line: line, col: col, message: e.message.replace(/./, function(l) { return l.toLowerCase(); })});
throw SyntaxError(e.message + ' on line ' + line);
}
}