javascript-stringify icon indicating copy to clipboard operation
javascript-stringify copied to clipboard

Repeated references in Maps & Sets produce broken output

Open eemeli opened this issue 4 years ago • 3 comments

Briefly, this happens:

const obj = {}
const map = new Map([['a', obj], ['b', obj]])
const ok = stringify(map)
// "new Map([['a',{}],['b',{}]])"
const bad = stringify(map, null, null, { references: true })
// "(function(){var x=new Map([['a',{}],['b',undefined]]);x[1][1]=x[0][1];return x;}())"
eval(bad)
// Uncaught TypeError: Cannot read property '1' of undefined

The problem here is that the object type isn't tracked, and therefore isn't taken into account by stringifyPath. The . and [] forms work fine for arrays and objects, but don't for Maps and Sets. The path in those cases is referring to the Array.from() constructions, rather than the resulting objects.

eemeli avatar Apr 18 '20 13:04 eemeli

That makes sense, ES6 data structures were a more recent addition that I didn't think through entirely. I'll try and fix it over the weekend, thanks for the report!

blakeembrey avatar Apr 18 '20 17:04 blakeembrey

Would a PR on this be welcome? Not promising anything, mind...

eemeli avatar Sep 06 '20 06:09 eemeli

Sorry for the delay, PRs are always welcome on any repo I maintain 😄 I just always get busier than I expect, so I'm sorry 🙏

blakeembrey avatar Oct 12 '20 01:10 blakeembrey