blakeembrey / blakeembrey/javascript-stringify

Repeated references in Maps & Sets produce broken output

Open
#34 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
146
Forks
16
PR merge metrics
No merged PRs in 30d

Description

Briefly, this happens:

```js
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.