diff options
author | isaacs <i@izs.me> | 2020-03-05 09:42:29 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2020-05-08 04:12:27 +0300 |
commit | ed1bb5fdc8ed73eee7a32814d9852857585d4baa (patch) | |
tree | df4484d8637a57ff34a90642b54895af736d0a34 /node_modules | |
parent | a612b73f9f0cd6a191aad529dacf4969c1b00404 (diff) |
json-parse-even-better-errors@2.2.0
Diffstat (limited to 'node_modules')
-rw-r--r-- | node_modules/json-parse-even-better-errors/README.md | 10 | ||||
-rw-r--r-- | node_modules/json-parse-even-better-errors/index.js | 36 | ||||
-rw-r--r-- | node_modules/json-parse-even-better-errors/package.json | 28 |
3 files changed, 53 insertions, 21 deletions
diff --git a/node_modules/json-parse-even-better-errors/README.md b/node_modules/json-parse-even-better-errors/README.md index 631035f9a..d53853d50 100644 --- a/node_modules/json-parse-even-better-errors/README.md +++ b/node_modules/json-parse-even-better-errors/README.md @@ -20,13 +20,16 @@ getting nicer errors out of `JSON.parse()`, including context and position of th ```javascript const parseJson = require('json-parse-even-better-errors') -parseJson('"foo"') +parseJson('"foo"') // returns the string 'foo' parseJson('garbage') // more useful error message +parseJson.noExceptions('garbage') // returns undefined ``` ### Features * Like JSON.parse, but the errors are better. +* Strips a leading byte-order-mark that you sometimes get reading files. +* Has a `noExceptions` method that returns undefined rather than throwing. ### API @@ -35,6 +38,11 @@ parseJson('garbage') // more useful error message Works just like `JSON.parse`, but will include a bit more information when an error happens. This throws a `JSONParseError`. +#### <a name="parse"></a> `parse.noExceptions(txt, reviver = null)` + +Works just like `JSON.parse`, but will return `undefined` rather than +throwing an error. + #### <a name="jsonparseerror"></a> `class JSONParseError(er, text, context = 20, caller = null)` Extends the JavaScript `SyntaxError` class to parse the message and provide diff --git a/node_modules/json-parse-even-better-errors/index.js b/node_modules/json-parse-even-better-errors/index.js index a6354e81d..5e2006acb 100644 --- a/node_modules/json-parse-even-better-errors/index.js +++ b/node_modules/json-parse-even-better-errors/index.js @@ -1,5 +1,10 @@ 'use strict' +const hexify = char => { + const h = char.charCodeAt(0).toString(16).toUpperCase() + return '0x' + (h.length % 2 ? '0' : '') + h +} + const parseError = (e, txt, context) => { if (!txt) { return { @@ -7,11 +12,16 @@ const parseError = (e, txt, context) => { position: 0, } } - const badToken = e.message.match(/^Unexpected token.*position\s+(\d+)/i) - const errIdx = badToken ? +badToken[1] + const badToken = e.message.match(/^Unexpected token (.) .*position\s+(\d+)/i) + const errIdx = badToken ? +badToken[2] : e.message.match(/^Unexpected end of JSON.*/i) ? txt.length - 1 : null + const msg = badToken ? e.message.replace(/^Unexpected token ./, `Unexpected token ${ + JSON.stringify(badToken[1]) + } (${hexify(badToken[1])})`) + : e.message + if (errIdx !== null && errIdx !== undefined) { const start = errIdx <= context ? 0 : errIdx - context @@ -26,12 +36,12 @@ const parseError = (e, txt, context) => { const near = txt === slice ? '' : 'near ' return { - message: e.message + ` while parsing ${near}'${slice}'`, + message: msg + ` while parsing ${near}${JSON.stringify(slice)}`, position: errIdx, } } else { return { - message: e.message + ` while parsing '${txt.slice(0, context * 2)}'`, + message: msg + ` while parsing '${txt.slice(0, context * 2)}'`, position: 0, } } @@ -53,11 +63,12 @@ class JSONParseError extends SyntaxError { } const parseJson = (txt, reviver, context) => { + const parseText = stripBOM(txt) context = context || 20 try { - return JSON.parse(txt, reviver) + return JSON.parse(parseText, reviver) } catch (e) { - if (typeof txt !== 'string') { + if (typeof txt !== 'string' && !Buffer.isBuffer(txt)) { const isEmptyArray = Array.isArray(txt) && txt.length === 0 throw Object.assign(new TypeError( `Cannot parse ${isEmptyArray ? 'an empty array' : String(txt)}` @@ -67,9 +78,20 @@ const parseJson = (txt, reviver, context) => { }) } - throw new JSONParseError(e, txt, context, parseJson) + throw new JSONParseError(e, parseText, context, parseJson) } } +// Remove byte order marker. This catches EF BB BF (the UTF-8 BOM) +// because the buffer-to-string conversion in `fs.readFileSync()` +// translates it to FEFF, the UTF-16 BOM. +const stripBOM = txt => String(txt).replace(/^\uFEFF/, '') + module.exports = parseJson parseJson.JSONParseError = JSONParseError + +parseJson.noExceptions = (txt, reviver) => { + try { + return JSON.parse(stripBOM(txt), reviver) + } catch (e) {} +} diff --git a/node_modules/json-parse-even-better-errors/package.json b/node_modules/json-parse-even-better-errors/package.json index de383dac7..502d70756 100644 --- a/node_modules/json-parse-even-better-errors/package.json +++ b/node_modules/json-parse-even-better-errors/package.json @@ -1,27 +1,29 @@ { - "_from": "json-parse-even-better-errors@^2.0.1", - "_id": "json-parse-even-better-errors@2.0.1", + "_from": "json-parse-even-better-errors", + "_id": "json-parse-even-better-errors@2.2.0", "_inBundle": false, - "_integrity": "sha512-XFY2Mbnmg+8r7MRsxfArVkZcfjxGlF/NjM3LsPXVeCX/GBF/1FTCv+idHBYC4qLPtK7q8HC8bapLoWqnhP/bXw==", + "_integrity": "sha512-2tLgY7LRNZ9Hd6gmCuBG5/OjRHQpSgJQqJoYyLLOhUgn8LdOYrjaZLcxkWnDads+AD/haWWioPNziXQcgvQJ/g==", "_location": "/json-parse-even-better-errors", "_phantomChildren": {}, "_requested": { - "type": "range", + "type": "tag", "registry": true, - "raw": "json-parse-even-better-errors@^2.0.1", + "raw": "json-parse-even-better-errors", "name": "json-parse-even-better-errors", "escapedName": "json-parse-even-better-errors", - "rawSpec": "^2.0.1", + "rawSpec": "", "saveSpec": null, - "fetchSpec": "^2.0.1" + "fetchSpec": "latest" }, "_requiredBy": [ + "#USER", + "/", "/read-package-json-fast" ], - "_resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.0.1.tgz", - "_shasum": "ed0009e0f5e7eb21ae0675d0d34782cc7a53c60e", - "_spec": "json-parse-even-better-errors@^2.0.1", - "_where": "/Users/claudiahdz/npm/cli/node_modules/read-package-json-fast", + "_resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.2.0.tgz", + "_shasum": "304d29aa54bb01156a1328c454034ff0ac8a7bf4", + "_spec": "json-parse-even-better-errors", + "_where": "/Users/isaacs/dev/npm/cli", "author": { "name": "Kat Marchán", "email": "kzm@zkat.tech" @@ -51,8 +53,8 @@ "url": "git+https://github.com/npm/json-parse-even-better-errors.git" }, "scripts": { - "postpublish": "git push --follow-tags", "postversion": "npm publish", + "prepublishOnly": "git push --follow-tags", "preversion": "npm t", "snap": "tap", "test": "tap" @@ -60,5 +62,5 @@ "tap": { "check-coverage": true }, - "version": "2.0.1" + "version": "2.2.0" } |