Add support for tailing comma

pull/251/head
Anton Medvedev 1 year ago
parent 686e9a8e3e
commit a1feb3c997
No known key found for this signature in database

@ -340,6 +340,10 @@ function* parseJson(stdin) {
} else if (lastChar === ',') {
next()
skipWhitespace()
if (lastChar === '}') {
next()
return obj
}
} else {
throw new SyntaxError(errorSnippet())
}
@ -366,6 +370,10 @@ function* parseJson(stdin) {
} else if (lastChar === ',') {
next()
skipWhitespace()
if (lastChar === ']') {
next()
return array
}
} else {
throw new SyntaxError(errorSnippet())
}

@ -23,7 +23,6 @@ void async function main() {
t.deepEqual(stdout, '[\n {\n "greeting": "hello world"\n }\n]\n')
})
await test('parseJson - valid json', async t => {
const obj = {a: 2.3e100, b: 'str', c: null, d: false, e: [1, 2, 3]}
const {stdout, stderr} = await run(obj)
@ -60,6 +59,16 @@ void async function main() {
t.equal((await run('2.3e-3')).stdout, '0.0023\n')
})
await test('parseJson - object tailing comma', async t => {
const {stdout} = await run('{"a": 1,}')
t.equal(stdout, '{\n "a": 1\n}\n')
})
await test('parseJson - array tailing comma', async t => {
const {stdout} = await run('[1,]')
t.equal(stdout, '[\n 1\n]\n')
})
await test('transform - anonymous function', async t => {
const {stdout} = await run({'key': 'value'}, '\'function (x) { return x.key }\'')
t.equal(stdout, 'value\n')

Loading…
Cancel
Save