An object is copied by printing it as JSON and parsing it again

tx0/ts:json-deep-clonestrictwarningsmell

What it finds

JSON.parse(JSON.stringify(value)) copies an object by printing and reparsing it, which loses dates, undefined, maps, sets and anything else JSON cannot hold, and throws on cycles. structuredClone(value) copies those and says what is meant.

Reported are calls of JSON.parse on the global JSON whose only argument is a call of JSON.stringify with one argument.

From the rule's source, json_deep_clone.rs.

Example from the rule's tests
export function copy<T>(value: T): T {  return JSON.parse(JSON.stringify(value));tx0 reports: use structuredClone}export function parse(text: string) {  return JSON.parse(text);}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:json-deep-clone" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:json-deep-clone] reason="..."

tx0 explain tx0/ts:json-deep-clone shows the same in the terminal. Found a false positive? Report it.