[6a3a178] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | const hexify = char => {
|
---|
| 4 | const h = char.charCodeAt(0).toString(16).toUpperCase()
|
---|
| 5 | return '0x' + (h.length % 2 ? '0' : '') + h
|
---|
| 6 | }
|
---|
| 7 |
|
---|
| 8 | const parseError = (e, txt, context) => {
|
---|
| 9 | if (!txt) {
|
---|
| 10 | return {
|
---|
| 11 | message: e.message + ' while parsing empty string',
|
---|
| 12 | position: 0,
|
---|
| 13 | }
|
---|
| 14 | }
|
---|
| 15 | const badToken = e.message.match(/^Unexpected token (.) .*position\s+(\d+)/i)
|
---|
| 16 | const errIdx = badToken ? +badToken[2]
|
---|
| 17 | : e.message.match(/^Unexpected end of JSON.*/i) ? txt.length - 1
|
---|
| 18 | : null
|
---|
| 19 |
|
---|
| 20 | const msg = badToken ? e.message.replace(/^Unexpected token ./, `Unexpected token ${
|
---|
| 21 | JSON.stringify(badToken[1])
|
---|
| 22 | } (${hexify(badToken[1])})`)
|
---|
| 23 | : e.message
|
---|
| 24 |
|
---|
| 25 | if (errIdx !== null && errIdx !== undefined) {
|
---|
| 26 | const start = errIdx <= context ? 0
|
---|
| 27 | : errIdx - context
|
---|
| 28 |
|
---|
| 29 | const end = errIdx + context >= txt.length ? txt.length
|
---|
| 30 | : errIdx + context
|
---|
| 31 |
|
---|
| 32 | const slice = (start === 0 ? '' : '...') +
|
---|
| 33 | txt.slice(start, end) +
|
---|
| 34 | (end === txt.length ? '' : '...')
|
---|
| 35 |
|
---|
| 36 | const near = txt === slice ? '' : 'near '
|
---|
| 37 |
|
---|
| 38 | return {
|
---|
| 39 | message: msg + ` while parsing ${near}${JSON.stringify(slice)}`,
|
---|
| 40 | position: errIdx,
|
---|
| 41 | }
|
---|
| 42 | } else {
|
---|
| 43 | return {
|
---|
| 44 | message: msg + ` while parsing '${txt.slice(0, context * 2)}'`,
|
---|
| 45 | position: 0,
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | class JSONParseError extends SyntaxError {
|
---|
| 51 | constructor (er, txt, context, caller) {
|
---|
| 52 | context = context || 20
|
---|
| 53 | const metadata = parseError(er, txt, context)
|
---|
| 54 | super(metadata.message)
|
---|
| 55 | Object.assign(this, metadata)
|
---|
| 56 | this.code = 'EJSONPARSE'
|
---|
| 57 | this.systemError = er
|
---|
| 58 | Error.captureStackTrace(this, caller || this.constructor)
|
---|
| 59 | }
|
---|
| 60 | get name () { return this.constructor.name }
|
---|
| 61 | set name (n) {}
|
---|
| 62 | get [Symbol.toStringTag] () { return this.constructor.name }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | const kIndent = Symbol.for('indent')
|
---|
| 66 | const kNewline = Symbol.for('newline')
|
---|
| 67 | // only respect indentation if we got a line break, otherwise squash it
|
---|
| 68 | // things other than objects and arrays aren't indented, so ignore those
|
---|
| 69 | // Important: in both of these regexps, the $1 capture group is the newline
|
---|
| 70 | // or undefined, and the $2 capture group is the indent, or undefined.
|
---|
| 71 | const formatRE = /^\s*[{\[]((?:\r?\n)+)([\s\t]*)/
|
---|
| 72 | const emptyRE = /^(?:\{\}|\[\])((?:\r?\n)+)?$/
|
---|
| 73 |
|
---|
| 74 | const parseJson = (txt, reviver, context) => {
|
---|
| 75 | const parseText = stripBOM(txt)
|
---|
| 76 | context = context || 20
|
---|
| 77 | try {
|
---|
| 78 | // get the indentation so that we can save it back nicely
|
---|
| 79 | // if the file starts with {" then we have an indent of '', ie, none
|
---|
| 80 | // otherwise, pick the indentation of the next line after the first \n
|
---|
| 81 | // If the pattern doesn't match, then it means no indentation.
|
---|
| 82 | // JSON.stringify ignores symbols, so this is reasonably safe.
|
---|
| 83 | // if the string is '{}' or '[]', then use the default 2-space indent.
|
---|
| 84 | const [, newline = '\n', indent = ' '] = parseText.match(emptyRE) ||
|
---|
| 85 | parseText.match(formatRE) ||
|
---|
| 86 | [, '', '']
|
---|
| 87 |
|
---|
| 88 | const result = JSON.parse(parseText, reviver)
|
---|
| 89 | if (result && typeof result === 'object') {
|
---|
| 90 | result[kNewline] = newline
|
---|
| 91 | result[kIndent] = indent
|
---|
| 92 | }
|
---|
| 93 | return result
|
---|
| 94 | } catch (e) {
|
---|
| 95 | if (typeof txt !== 'string' && !Buffer.isBuffer(txt)) {
|
---|
| 96 | const isEmptyArray = Array.isArray(txt) && txt.length === 0
|
---|
| 97 | throw Object.assign(new TypeError(
|
---|
| 98 | `Cannot parse ${isEmptyArray ? 'an empty array' : String(txt)}`
|
---|
| 99 | ), {
|
---|
| 100 | code: 'EJSONPARSE',
|
---|
| 101 | systemError: e,
|
---|
| 102 | })
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | throw new JSONParseError(e, parseText, context, parseJson)
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
|
---|
| 110 | // because the buffer-to-string conversion in `fs.readFileSync()`
|
---|
| 111 | // translates it to FEFF, the UTF-16 BOM.
|
---|
| 112 | const stripBOM = txt => String(txt).replace(/^\uFEFF/, '')
|
---|
| 113 |
|
---|
| 114 | module.exports = parseJson
|
---|
| 115 | parseJson.JSONParseError = JSONParseError
|
---|
| 116 |
|
---|
| 117 | parseJson.noExceptions = (txt, reviver) => {
|
---|
| 118 | try {
|
---|
| 119 | return JSON.parse(stripBOM(txt), reviver)
|
---|
| 120 | } catch (e) {}
|
---|
| 121 | }
|
---|