Deep copy through JSON
This is how the comment reads inline, under the flagged line.
Why it matters
The JSON round-trip is a slow, lossy deep copy that silently changes types.
What to do
structuredClone(x) in JavaScript, copy.deepcopy(x) in Python, or don't copy at all.
Examples
const copy = JSON.parse(JSON.stringify(state));
const copy = structuredClone(state);
What it catches
It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript, Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
const c = JSON.parse(JSON.stringify(s));
Left alone
const c = structuredClone(s);
Research
Deep copy through `JSON.parse(JSON.stringify(x))`. It is slow and silently drops or changes Dates, Maps, `undefined` and `NaN`.
- no outside source yet
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"json-clone"
],
"severity": {
"json-clone": "low"
}
}
}
JSON.parse(JSON.stringifycopies by serialising to a string and back. It's slow and drops Dates, Maps, undefined and NaN.