parse-json
Advanced tools
Comparing version 7.1.0 to 7.1.1
69
index.js
@@ -11,41 +11,56 @@ import errorEx from 'error-ex'; | ||
const generateCodeFrame = (string, location, highlightCode = true) => | ||
codeFrameColumns(string, {start: location}, {highlightCode}); | ||
const getErrorLocation = (string, message) => { | ||
const match = message.match(/in JSON at position (?<index>\d+)(?: \(line (?<line>\d+) column (?<column>\d+)\))? while parsing/); | ||
if (!match) { | ||
return; | ||
} | ||
let {index, line, column} = match.groups; | ||
if (line && column) { | ||
return {line: Number(line), column: Number(column)}; | ||
} | ||
({line, column} = new LinesAndColumns(string).locationForIndex(Number(index))); | ||
return {line: line + 1, column: column + 1}; | ||
}; | ||
export default function parseJson(string, reviver, filename) { | ||
if (typeof reviver === 'string') { | ||
filename = reviver; | ||
reviver = null; | ||
reviver = undefined; | ||
} | ||
let message; | ||
try { | ||
try { | ||
return JSON.parse(string, reviver); | ||
} catch (error) { | ||
fallback(string, reviver); | ||
throw error; | ||
} | ||
return JSON.parse(string, reviver); | ||
} catch (error) { | ||
error.message = error.message.replace(/\n/g, ''); | ||
const indexMatch = error.message.match(/in JSON at position (\d+) while parsing/); | ||
message = error.message; | ||
} | ||
const jsonError = new JSONError(error); | ||
if (filename) { | ||
jsonError.fileName = filename; | ||
} | ||
try { | ||
fallback(string, reviver); | ||
} catch (error) { | ||
message = error.message; | ||
} | ||
if (indexMatch && indexMatch.length > 0) { | ||
const lines = new LinesAndColumns(string); | ||
const index = Number(indexMatch[1]); | ||
const location = lines.locationForIndex(index); | ||
message = message.replace(/\n/g, ''); | ||
const jsonError = new JSONError(message); | ||
const generateCodeFrame = ({highlightCode}) => codeFrameColumns( | ||
string, | ||
{start: {line: location.line + 1, column: location.column + 1}}, | ||
{highlightCode}, | ||
); | ||
if (filename) { | ||
jsonError.fileName = filename; | ||
} | ||
jsonError.codeFrame = generateCodeFrame({highlightCode: true}); | ||
jsonError.rawCodeFrame = generateCodeFrame({highlightCode: false}); | ||
} | ||
const location = getErrorLocation(string, message); | ||
if (location) { | ||
jsonError.codeFrame = generateCodeFrame(string, location); | ||
jsonError.rawCodeFrame = generateCodeFrame(string, location, /* highlightCode */ false); | ||
} | ||
throw jsonError; | ||
} | ||
throw jsonError; | ||
} |
{ | ||
"name": "parse-json", | ||
"version": "7.1.0", | ||
"version": "7.1.1", | ||
"description": "Parse JSON with more helpful errors", | ||
@@ -47,2 +47,4 @@ "license": "MIT", | ||
"nyc": "^15.1.0", | ||
"outdent": "^0.8.0", | ||
"strip-ansi": "^7.1.0", | ||
"tsd": "^0.28.1", | ||
@@ -49,0 +51,0 @@ "xo": "^0.54.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8671
156
6