Skip to content

Commit 2149198

Browse files
committed
fix: Ensure, that tokens and keys in error messages are enclosed in quotation marks
1 parent f11f20c commit 2149198

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Example of an error message:
4545
Parse error on line 1, column 14:
4646
{"creative": ?}
4747
-------------^
48-
Unexpected token ?
48+
Unexpected token "?"
4949

5050
## Command-line Interface
5151

@@ -198,7 +198,7 @@ The following code logs twice the following message:
198198
Parse error on line 1, column 14:
199199
{"creative": ?}
200200
-------------^
201-
Unexpected token ?
201+
Unexpected token "?"
202202

203203
```js
204204
const { parse } = require('@prantlf/jsonlint')

src/custom-parser.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,7 @@ function parseCustom (input, options) { // eslint-disable-line no-unused-vars
6161
function generateMessage () {
6262
var message
6363
if (position < inputLength) {
64-
var token = '\'' +
65-
JSON
66-
.stringify(input[position])
67-
.replace(/^"|"$/g, '')
68-
.replace(/'/g, "\\'")
69-
.replace(/\\"/g, '"') +
70-
'\''
64+
var token = JSON.stringify(input[position])
7165
message = 'Unexpected token ' + token
7266
} else {
7367
message = 'Unexpected end of input'
@@ -229,7 +223,7 @@ function parseCustom (input, options) { // eslint-disable-line no-unused-vars
229223
skipWhiteSpace()
230224
var key = parseKey()
231225
if (allowDuplicateObjectKeys === false && result[key]) {
232-
fail('Duplicate key: ' + key)
226+
fail('Duplicate key: "' + key + '"')
233227
}
234228
skipWhiteSpace()
235229

@@ -246,10 +240,10 @@ function parseCustom (input, options) { // eslint-disable-line no-unused-vars
246240
var value = parseGeneric()
247241
stack.pop()
248242

249-
if (value === undefined) fail('No value found for key ' + key)
243+
if (value === undefined) fail('No value found for key "' + key + '"')
250244
if (typeof key !== 'string') {
251245
if (!json5 || typeof key !== 'number') {
252-
fail('Wrong key type: ' + key)
246+
fail('Wrong key type: "' + key + '"')
253247
}
254248
}
255249

test/parse3.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ function addTest (arg, row, col, errRegExp) {
1111
} catch (err) {
1212
if (row !== undefined) assert.equal(err.location.start.line, row, 'wrong row: ' + err.location.start.line)
1313
if (col !== undefined) assert.equal(err.location.start.column, col, 'wrong column: ' + err.location.start.column)
14-
if (errRegExp) assert(errRegExp.exec(err.message))
14+
try {
15+
if (errRegExp) assert(errRegExp.exec(err.message))
16+
} catch (error) {
17+
console.log('Message:', err.message)
18+
console.log('RegExp: ', errRegExp)
19+
throw error
20+
}
1521
return
1622
}
1723
throw Error('no error')
@@ -47,7 +53,7 @@ addTest('nulz', 1, 1)
4753

4854
// no data
4955
addTest(' ', 1, 3, /No data.*whitespace/)
50-
addTest('blah', 1, 1, /Unexpected token 'b'/)
56+
addTest('blah', 1, 1, /Unexpected token "b"/)
5157
addTest('', 1, 1, /No data.*empty input/)
5258

5359
try {

0 commit comments

Comments
 (0)