0

WebUI: Use ESLint to disallow JSON.parse(JSON.stringify()) to clone.

When ESLint encounters this pattern, it will throw the following error
"Don't use JSON.parse(JSON.stringify(...)) to clone objects. Use
structuredClone() instead"

Bug: 720034,1491559
Change-Id: I377639ec257640a5ff10e5f6258c6af87d60c260
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5005072
Reviewed-by: Mitsuru Oshima <oshima@chromium.org>
Reviewed-by: Rebekah Potter <rbpotter@chromium.org>
Commit-Queue: Demetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1220531}
This commit is contained in:
dpapad
2023-11-06 21:42:06 +00:00
committed by Chromium LUCI CQ
parent 8cc31d38d2
commit b9eb38f18e
2 changed files with 6 additions and 0 deletions

@ -63,6 +63,10 @@ module.exports = {
'message': 'Use ES modules or cr.define() instead',
},
],
'no-restricted-syntax': ['error', {
'selector': 'CallExpression[callee.object.name=JSON][callee.property.name=parse] > CallExpression[callee.object.name=JSON][callee.property.name=stringify]',
'message': 'Don\'t use JSON.parse(JSON.stringify(...)) to clone objects. Use structuredClone() instead.',
}],
'no-throw-literal': 'error',
'no-trailing-spaces': 'error',
'no-var': 'error',

@ -1638,6 +1638,7 @@ const lottiejs = (function(window) {
comps[i].layers.__used = true;
return comps[i].layers;
}
// eslint-disable-next-line no-restricted-syntax
return JSON.parse(JSON.stringify(comps[i].layers));
}
i += 1;
@ -4417,6 +4418,7 @@ const lottiejs = (function(window) {
RepeaterModifier.prototype.cloneElements = function(elements) {
let i;
const len = elements.length;
// eslint-disable-next-line no-restricted-syntax
const newElements = JSON.parse(JSON.stringify(elements));
this.resetElements(newElements);
return newElements;