Skip to content

Commit cefd898

Browse files
authored
Handle escaped double quotes in JSON (#33)
* Add test case for escaped double quotes * Validate syntax in tests * Produce valid escaped JSON
1 parent a04122a commit cefd898

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ module.exports = function (source) {
3333
return `module.exports = ${value}`
3434
}
3535

36-
return `module.exports = JSON.parse(\`${JSON.stringify(value)}\`)`
36+
// the outer JSON.stringify is parsed by JavaScript
37+
// the inner JSON.stringify is parsed by JSON.parse
38+
return `module.exports = JSON.parse(${JSON.stringify(JSON.stringify(value))})`
3739
}
3840

3941
exports.raw = true

test/fixtures/file-big.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,8 @@
13301330
},
13311331
{
13321332
"with-single-quotes": "this 'should' also work"
1333+
},
1334+
{
1335+
"with-double-\"quotes\"": "this \"should\" also work"
13331336
}
1334-
]
1335-
1337+
]

test/loader-big.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@ describe('Loader', () => {
1515

1616
// this will fail when there is an error e.g: `throw new Error...`
1717
expect(source.startsWith('module.exports = JSON.parse')).toEqual(true)
18+
19+
// validate syntax
20+
expect(() => eval(source)).not.toThrow()
1821
})
1922
})

0 commit comments

Comments
 (0)