Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const { normalizeOptions } = require('../utils')
module.exports = json

/**
* RegExp to match the first non-space in a string.
* RegExp to match the first non-whitespace character in a string.
*
* Allowed whitespace is defined in RFC 7159:
*
Expand All @@ -33,7 +33,7 @@ module.exports = json
* %x0A / ; Line feed or New line
* %x0D ) ; Carriage return
*/
const FIRST_CHAR_REGEXP = /^[\x20\x09\x0a\x0d]*([^\x20\x09\x0a\x0d])/ // eslint-disable-line no-control-regex
const FIRST_NON_WHITESPACE_REGEXP = /^[\x20\x09\x0a\x0d]*([^\x20\x09\x0a\x0d]|$)/ // eslint-disable-line no-control-regex

@bjohansebas bjohansebas Jul 9, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, for future reviewers, here's the regex change.

" {json}"
master=match[1]="{"
PR=match[1]="{"
"123" (primitivo)
master=match[1]="1"
PR=match[1]="1"
" " (solo whitespace)
master=NULL (no match)
PR=match[1]=""
"" (vacio)
master=NULL (no match)
PR=match[1]=""


const JSON_SYNTAX_CHAR = '#'
const JSON_SYNTAX_REGEXP = /#+/g
Expand Down Expand Up @@ -80,10 +80,12 @@ function createJsonParser (options) {
return {}
}

const first = firstchar(body)
if (first !== '{' && first !== '[') {
debug('strict violation')
throw createStrictSyntaxError(body, first)
if (body[0] !== '{' && body[0] !== '[') {
const first = firstNonWhitespaceChar(body)
if (first !== '{' && first !== '[') {
debug('strict violation')
throw createStrictSyntaxError(body, first)
}
}

try {
Expand Down Expand Up @@ -152,12 +154,10 @@ function createStrictSyntaxError (str, char) {
* @returns {string|undefined}
* @private
*/
function firstchar (str) {
const match = FIRST_CHAR_REGEXP.exec(str)
function firstNonWhitespaceChar (str) {
const match = FIRST_NON_WHITESPACE_REGEXP.exec(str)

return match
? match[1]
: undefined
return match && match[1] ? match[1] : undefined
}

/**
Expand Down