parse-json
Advanced tools
Comparing version 8.0.0 to 8.0.1
45
index.js
@@ -6,29 +6,24 @@ import fallback from 'json-parse-even-better-errors'; | ||
export class JSONError extends Error { | ||
name = 'JSONError'; | ||
fileName; | ||
codeFrame; | ||
rawCodeFrame; | ||
#message; | ||
constructor(message) { | ||
super(message); | ||
// We cannot pass message to `super()`, otherwise the message accessor will be overridden. | ||
// https://262.ecma-international.org/14.0/#sec-error-message | ||
super(); | ||
let _message = message instanceof Error | ||
? message.message | ||
: message; | ||
this.#message = message; | ||
Error.captureStackTrace?.(this, JSONError); | ||
} | ||
Object.defineProperty(this, 'message', { | ||
configurable: true, | ||
enumerable: false, | ||
get() { | ||
return `${_message}${this.fileName ? ` in ${this.fileName}` : ''}${this.codeFrame ? `\n\n${this.codeFrame}\n` : ''}`; | ||
}, | ||
set(value) { | ||
_message = value; | ||
}, | ||
}); | ||
get message() { | ||
const {fileName, codeFrame} = this; | ||
return `${this.#message}${fileName ? ` in ${fileName}` : ''}${codeFrame ? `\n\n${codeFrame}\n` : ''}`; | ||
} | ||
this.name = 'JSONError'; | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, JSONError); | ||
} | ||
set message(message) { | ||
this.#message = message; | ||
} | ||
@@ -47,3 +42,3 @@ } | ||
const {index, line, column} = match.groups; | ||
let {index, line, column} = match.groups; | ||
@@ -54,3 +49,11 @@ if (line && column) { | ||
return indexToPosition(string, Number(index), {oneBased: true}); | ||
index = Number(index); | ||
// The error location can be out of bounds. | ||
if (index === string.length) { | ||
const {line, column} = indexToPosition(string, string.length - 1, {oneBased: true}); | ||
return {line, column: column + 1}; | ||
} | ||
return indexToPosition(string, index, {oneBased: true}); | ||
}; | ||
@@ -57,0 +60,0 @@ |
{ | ||
"name": "parse-json", | ||
"version": "8.0.0", | ||
"version": "8.0.1", | ||
"description": "Parse JSON with more helpful errors", | ||
@@ -14,3 +14,7 @@ "license": "MIT", | ||
"type": "module", | ||
"exports": "./index.js", | ||
"exports": { | ||
"types": "./index.d.ts", | ||
"default": "./index.js" | ||
}, | ||
"sideEffects": false, | ||
"engines": { | ||
@@ -39,5 +43,5 @@ "node": ">=18" | ||
"@babel/code-frame": "^7.22.13", | ||
"index-to-position": "^0.1.0", | ||
"index-to-position": "^0.1.1", | ||
"json-parse-even-better-errors": "^3.0.0", | ||
"type-fest": "^4.6.0" | ||
"type-fest": "^4.7.1" | ||
}, | ||
@@ -44,0 +48,0 @@ "devDependencies": { |
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
9311
177
Updatedindex-to-position@^0.1.1
Updatedtype-fest@^4.7.1