mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[sprout] Make mutate incrementally attempt to add property
Previously, we stored a global count variable that was updated every time we added a property to the `arg` object. This was added to prevent collisions, and make sure we do actually mutate the object. But the count value was shared by the forget compiled and uncompiled versions, so the same object mutated in either versions would result in having different properties leading to potential test failures. Instead, let's make count local and attempt to incrementally mutate the object with different keys.
This commit is contained in:
+2
-2
@@ -9,7 +9,7 @@ function Component(a) {
|
||||
let obj = {
|
||||
method() {
|
||||
mutate(x);
|
||||
return x.a;
|
||||
return x;
|
||||
},
|
||||
};
|
||||
return obj.method();
|
||||
@@ -32,7 +32,7 @@ function Component(a) {
|
||||
const obj = {
|
||||
method() {
|
||||
mutate(x);
|
||||
return x.a;
|
||||
return x;
|
||||
},
|
||||
};
|
||||
return obj.method();
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ function Component(a) {
|
||||
let obj = {
|
||||
method() {
|
||||
mutate(x);
|
||||
return x.a;
|
||||
return x;
|
||||
},
|
||||
};
|
||||
return obj.method();
|
||||
|
||||
@@ -34,11 +34,21 @@ export const CONST_NUMBER2 = 2;
|
||||
export const CONST_TRUE = true;
|
||||
export const CONST_FALSE = false;
|
||||
|
||||
let count: number = 0;
|
||||
export function mutate(arg: any): void {
|
||||
// don't mutate primitive
|
||||
if (typeof arg != null && typeof arg === "object") {
|
||||
arg["wat" + count++] = "joe";
|
||||
if (typeof arg === null || typeof arg !== "object") {
|
||||
return;
|
||||
}
|
||||
|
||||
let count: number = 0;
|
||||
let key;
|
||||
while (true) {
|
||||
key = "wat" + count;
|
||||
if (!Object.hasOwn(arg, key)) {
|
||||
arg[key] = "joe";
|
||||
return;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user