Skip to content

Commit c45eec2

Browse files
committed
fix: node 22 behavior
1 parent 3c7bd80 commit c45eec2

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ const parseError = (e, txt, context = 20) => {
5252
let errIdx
5353
if (badIndexMatch) {
5454
errIdx = +badIndexMatch[1]
55-
} else if (msg.match(/^Unexpected end of JSON.*/i)) {
55+
} else /* istanbul ignore next - doesnt happen in Node 22 */ if (
56+
msg.match(/^Unexpected end of JSON.*/i)
57+
) {
5658
errIdx = txt.length - 1
5759
}
5860

test/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ t.test('throws SyntaxError for unexpected end of JSON', (t) => {
171171
20: /Unterminated string in JSON at position \d+/,
172172
default: /Unexpected end of JSON input/,
173173
},
174-
/ while parsing "{\\"foo: bar}"/
174+
/.* while parsing "{\\"foo: bar}"/
175175
),
176176
code: 'EJSONPARSE',
177177
position: getLatestMatchingNode({ 20: 11, default: 10 }),
@@ -207,7 +207,7 @@ t.test('SyntaxError with less context (limited start)', (t) => {
207207
20: 'Unterminated string in JSON at position 9',
208208
default: 'Unexpected end of JSON input',
209209
},
210-
' while parsing near "...',
210+
/.* while parsing near "\.\.\./,
211211
{
212212
20: '210',
213213
default: '3210',
@@ -230,7 +230,7 @@ t.test('SyntaxError with less context (limited end)', (t) => {
230230
20: ', "abcde" is not valid JSON',
231231
default: ' in JSON at position 0',
232232
},
233-
' while parsing ',
233+
/.* while parsing .*/,
234234
{
235235
20: "'abcd'",
236236
default: 'near "ab..."',
@@ -247,9 +247,12 @@ t.test('SyntaxError with less context (limited end)', (t) => {
247247
t.test('throws for end of input', (t) => {
248248
const data = '{"a":1,""'
249249
jsonThrows(t, data, 2, {
250-
message: expectMessage('Unexpected end of JSON input while parsing'),
250+
message: expectMessage({
251+
22: `Expected ':' after property name in JSON at`,
252+
default: 'Unexpected end of JSON input while parsing',
253+
}),
251254
code: 'EJSONPARSE',
252-
position: 8,
255+
position: getLatestMatchingNode({ 22: 9, default: 8 }),
253256
name: 'JSONParseError',
254257
systemError: SyntaxError,
255258
})

0 commit comments

Comments
 (0)