1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-22 08:20:11 +00:00

Always use JSONLint for SyntaxError post-processing.

Remove the check for SyntaxError's built-in line and character
indicators and always re-parse on error to grab the line/column.
This commit is contained in:
hacksalot 2016-01-10 05:17:28 -05:00
parent bd8b587c5b
commit fee21a7b17

View File

@ -11,31 +11,25 @@ Definition of the SyntaxErrorEx class.
Represents a SyntaxError exception with line and column info. Represents a SyntaxError exception with line and column info.
Collect syntax error information from the provided exception object. The Collect syntax error information from the provided exception object. The
JavaScript `SyntaxError` exception isn't interpreted uniformly across environ- JavaScript `SyntaxError` exception isn't interpreted uniformly across environ-
ments, so we first check for a .lineNumber and .columnNumber and, if that's ments, so we reparse on error to grab the line and column.
not present, fall back to the JSONLint library, which provides that info.
See: http://stackoverflow.com/q/13323356 See: http://stackoverflow.com/q/13323356
@class SyntaxErrorEx @class SyntaxErrorEx
*/ */
module.exports = function SyntaxErrorEx( ex, rawData ) { function SyntaxErrorEx( ex, rawData ) {
var lineNum = null, colNum = null; var lineNum = null, colNum = null;
if( ex.lineNumber !== undefined && ex.lineNumber !== null ) { var JSONLint = require('json-lint');
lineNum = ex.lineNumber; var lint = JSONLint( rawData, { comments: false } );
} this.line = (lint.error ? lint.line : '???');
if( ex.columnNumber !== undefined && ex.columnNumber !== null ) { this.col = (lint.error ? lint.character : '???');
colNum = ex.columnNumber;
}
if( lineNum === null || colNum === null ) {
var JSONLint = require('json-lint'); // TODO: json-lint or is-my-json-valid?
var lint = JSONLint( rawData, { comments: false } );
if( lineNum === null ) lineNum = (lint.error ? lint.line : '???');
if( colNum === null ) colNum = (lint.error ? lint.character : '???');
}
this.line = lineNum;
this.col = colNum;
}
SyntaxErrorEx.is = function( ex ) {
return ex instanceof SyntaxError;
}; };
module.exports = SyntaxErrorEx;
}()); }());