Skip to content

Commit

Permalink
fix: node 22 behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed May 4, 2024
1 parent 3c7bd80 commit c45eec2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ const parseError = (e, txt, context = 20) => {
let errIdx
if (badIndexMatch) {
errIdx = +badIndexMatch[1]
} else if (msg.match(/^Unexpected end of JSON.*/i)) {
} else /* istanbul ignore next - doesnt happen in Node 22 */ if (
msg.match(/^Unexpected end of JSON.*/i)
) {
errIdx = txt.length - 1
}

Expand Down
13 changes: 8 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ t.test('throws SyntaxError for unexpected end of JSON', (t) => {
20: /Unterminated string in JSON at position \d+/,
default: /Unexpected end of JSON input/,
},
/ while parsing "{\\"foo: bar}"/
/.* while parsing "{\\"foo: bar}"/
),
code: 'EJSONPARSE',
position: getLatestMatchingNode({ 20: 11, default: 10 }),
Expand Down Expand Up @@ -207,7 +207,7 @@ t.test('SyntaxError with less context (limited start)', (t) => {
20: 'Unterminated string in JSON at position 9',
default: 'Unexpected end of JSON input',
},
' while parsing near "...',
/.* while parsing near "\.\.\./,
{
20: '210',
default: '3210',
Expand All @@ -230,7 +230,7 @@ t.test('SyntaxError with less context (limited end)', (t) => {
20: ', "abcde" is not valid JSON',
default: ' in JSON at position 0',
},
' while parsing ',
/.* while parsing .*/,
{
20: "'abcd'",
default: 'near "ab..."',
Expand All @@ -247,9 +247,12 @@ t.test('SyntaxError with less context (limited end)', (t) => {
t.test('throws for end of input', (t) => {
const data = '{"a":1,""'
jsonThrows(t, data, 2, {
message: expectMessage('Unexpected end of JSON input while parsing'),
message: expectMessage({
22: `Expected ':' after property name in JSON at`,
default: 'Unexpected end of JSON input while parsing',
}),
code: 'EJSONPARSE',
position: 8,
position: getLatestMatchingNode({ 22: 9, default: 8 }),
name: 'JSONParseError',
systemError: SyntaxError,
})
Expand Down

0 comments on commit c45eec2

Please sign in to comment.