parse-json
Advanced tools
Comparing version 4.0.0 to 5.0.0
41
index.js
'use strict'; | ||
const errorEx = require('error-ex'); | ||
const fallback = require('json-parse-better-errors'); | ||
const {default: LinesAndColumns} = require('lines-and-columns'); | ||
const {codeFrameColumns} = require('@babel/code-frame'); | ||
const JSONError = errorEx('JSONError', { | ||
fileName: errorEx.append('in %s') | ||
fileName: errorEx.append('in %s'), | ||
codeFrame: errorEx.append('\n\n%s\n') | ||
}); | ||
module.exports = (input, reviver, filename) => { | ||
module.exports = (string, reviver, filename) => { | ||
if (typeof reviver === 'string') { | ||
@@ -17,18 +20,32 @@ filename = reviver; | ||
try { | ||
return JSON.parse(input, reviver); | ||
} catch (err) { | ||
fallback(input, reviver); | ||
throw err; | ||
return JSON.parse(string, reviver); | ||
} catch (error) { | ||
fallback(string, reviver); | ||
throw error; | ||
} | ||
} catch (err) { | ||
err.message = err.message.replace(/\n/g, ''); | ||
} catch (error) { | ||
error.message = error.message.replace(/\n/g, ''); | ||
const indexMatch = error.message.match(/in JSON at position (\d+) while parsing near/); | ||
const jsonErr = new JSONError(err); | ||
const jsonError = new JSONError(error); | ||
if (filename) { | ||
jsonErr.fileName = filename; | ||
jsonError.fileName = filename; | ||
} | ||
throw jsonErr; | ||
if (indexMatch && indexMatch.length > 0) { | ||
const lines = new LinesAndColumns(string); | ||
const index = Number(indexMatch[1]); | ||
const location = lines.locationForIndex(index); | ||
const codeFrame = codeFrameColumns( | ||
string, | ||
{start: {line: location.line + 1, column: location.column + 1}}, | ||
{highlightCode: true} | ||
); | ||
jsonError.codeFrame = codeFrame; | ||
} | ||
throw jsonError; | ||
} | ||
}; |
{ | ||
"name": "parse-json", | ||
"version": "4.0.0", | ||
"description": "Parse JSON with more helpful errors", | ||
"license": "MIT", | ||
"repository": "sindresorhus/parse-json", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && nyc ava" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"vendor" | ||
], | ||
"keywords": [ | ||
"parse", | ||
"json", | ||
"graceful", | ||
"error", | ||
"message", | ||
"humanize", | ||
"friendly", | ||
"helpful", | ||
"string", | ||
"str" | ||
], | ||
"dependencies": { | ||
"error-ex": "^1.3.1", | ||
"json-parse-better-errors": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"nyc": "^11.2.1", | ||
"xo": "*" | ||
} | ||
"name": "parse-json", | ||
"version": "5.0.0", | ||
"description": "Parse JSON with more helpful errors", | ||
"license": "MIT", | ||
"repository": "sindresorhus/parse-json", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && nyc ava" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"vendor" | ||
], | ||
"keywords": [ | ||
"parse", | ||
"json", | ||
"graceful", | ||
"error", | ||
"message", | ||
"humanize", | ||
"friendly", | ||
"helpful", | ||
"string" | ||
], | ||
"dependencies": { | ||
"@babel/code-frame": "^7.0.0", | ||
"error-ex": "^1.3.1", | ||
"json-parse-better-errors": "^1.0.1", | ||
"lines-and-columns": "^1.1.6" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"nyc": "^14.1.1", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -17,2 +17,3 @@ # parse-json [![Build Status](https://travis-ci.org/sindresorhus/parse-json.svg?branch=master)](https://travis-ci.org/sindresorhus/parse-json) | ||
const parseJson = require('parse-json'); | ||
const json = '{\n\t"foo": true,\n}'; | ||
@@ -32,5 +33,8 @@ | ||
/* | ||
JSONError: Trailing comma in object at 3:1 | ||
} | ||
^ | ||
JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' | ||
1 | { | ||
2 | "foo": true, | ||
> 3 | } | ||
| ^ | ||
*/ | ||
@@ -41,5 +45,8 @@ | ||
/* | ||
JSONError: Trailing comma in object in foo.json:3:1 | ||
} | ||
^ | ||
JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json | ||
1 | { | ||
2 | "foo": true, | ||
> 3 | } | ||
| ^ | ||
*/ | ||
@@ -51,10 +58,13 @@ | ||
parseJson(json); | ||
} catch (err) { | ||
err.fileName = 'foo.json'; | ||
throw err; | ||
} catch (error) { | ||
error.fileName = 'foo.json'; | ||
throw error; | ||
} | ||
/* | ||
JSONError: Trailing comma in object in foo.json:3:1 | ||
} | ||
^ | ||
JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json | ||
1 | { | ||
2 | "foo": true, | ||
> 3 | } | ||
| ^ | ||
*/ | ||
@@ -65,5 +75,5 @@ ``` | ||
### parseJson(input, [reviver], [filename]) | ||
### parseJson(string, reviver?, filename?) | ||
#### input | ||
#### string | ||
@@ -86,4 +96,12 @@ Type: `string` | ||
## License | ||
--- | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) | ||
<div align="center"> | ||
<b> | ||
<a href="https://tidelift.com/subscription/pkg/npm-parse-json?utm_source=npm-parse-json&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> | ||
</b> | ||
<br> | ||
<sub> | ||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. | ||
</sub> | ||
</div> |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
5067
42
102
0
4
+ Added@babel/code-frame@^7.0.0
+ Addedlines-and-columns@^1.1.6
+ Added@babel/code-frame@7.24.7(transitive)
+ Added@babel/helper-validator-identifier@7.24.7(transitive)
+ Added@babel/highlight@7.24.7(transitive)
+ Addedansi-styles@3.2.1(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedlines-and-columns@1.2.4(transitive)
+ Addedpicocolors@1.1.0(transitive)
+ Addedsupports-color@5.5.0(transitive)