Open Bug 1733813 Opened 8 months ago Updated 3 months ago

Replace usages of JSON.parse(JSON.stringify(x)) with structuredClone(x)

Categories

(Firefox :: General, task)

task

Tracking

()

ASSIGNED

People

(Reporter: mconley, Assigned: Kwan)

References

Details

Bug 1722576 introduced the structuredClone global. According to searchfox, [we've got a few usages of JSON.parse(JSON.stringify(x)) in the tree](https://searchfox.org/mozilla-central/search?q=JSON.parse(JSON.stringify&path=). We might be able to replace them with using structuredClone(x) instead.

If so, then we might want to also introduce an ESLint rule to sniff out and discourage future usage of JSON.parse(JSON.stringify(x)).

I think this is a good idea for most "simple" objects. You do have to be careful, because structuredClone and JSON.parse(JSON.stringify(x)) behave differently in some ways.

For example:

JSON.parse(JSON.stringify({x: function() {}})) // { }
structuredClone({x: function() {}}) // Uncaught DOMException: The object could not be cloned.
Assignee: nobody → moz-ian
Status: NEW → ASSIGNED

The Bugbug bot thinks this bug should belong to the 'DevTools::General' component, and is moving the bug to that component. Please revert this change in case you think the bot is wrong.

Product: Firefox → DevTools

The original SearchFox link is not correct. So here an updated list of entries:
https://searchfox.org/mozilla-central/search?q=JSON.parse%28JSON.stringify%28&path=&case=false&regexp=false

This is not just devtools, as such moving back to Firefox :: General.

Product: DevTools → Firefox

Ian, are you actually working on a patch for this? If not, I'd like to jump in here.

Sebastian

Flags: needinfo?(moz-ian)

Hi Sebo, yes, sorry for the delay. I wrote an eslint rule and its associated tests for this right after taking it, but unfortunately didn't get to finishing the actual replacement (since it seemed like one needed to be careful to check for places where the other behaviour was required, as in comment #1, it wouldn't surprise me if that's used in devtools code somewhere) and it fell to the wayside. Looking back it would have made more sense to just land it with exclusions for the existing uses and then go back and remove them.
I have the patches still and will revive them, but I don't think I'll be able to properly get to it until next week.

Flags: needinfo?(moz-ian)
You need to log in before you can comment on or make changes to this bug.