Skip to content

Commit

Permalink
Fix replacing Python constants in arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Feb 16, 2020
1 parent 30fcdf5 commit 582645d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ export function repair (jsString) {
c = curr()
}

if (specialValues.indexOf(key) === -1) {
if (key in pythonConstants) {
return pythonConstants[key]
} else if (specialValues.indexOf(key) === -1) {
return '"' + key + '"'
} else {
return key
Expand Down Expand Up @@ -268,6 +270,7 @@ export function repair (jsString) {
i++
} else if (/[a-zA-Z_$]/.test(c) && ['{', ','].indexOf(lastNonWhitespace()) !== -1) {
// an unquoted object key (like a in '{a:2}')
// FIXME: array values are also parsed via parseKey, work this out properly
chars.push(parseKey())
} else if (/\w/.test(c)) {
chars.push(parseValue())
Expand Down
2 changes: 2 additions & 0 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,14 @@ describe('util', () => {
' "null": None,\n' +
' "true": True,\n' +
' "false": False\n' +
' "array": [1, foo, None, True, False]\n' +
'}'

const expectedJson = '{\n' +
' "null": null,\n' +
' "true": true,\n' +
' "false": false\n' +
' "array": [1, "foo", null, true, false]\n' +
'}'

assert.strictEqual(repair(pythonDocument), expectedJson)
Expand Down

0 comments on commit 582645d

Please sign in to comment.