@samitouri / QOS-React / commits / 0f97ea4c71

[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.

Sathya Gunasekaran committed Oct 4, 2023 at 12:36 UTC 0f97ea4c71f5f8db72221405a422d49a2c2644a3
3 files changed +16 -6
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-3.expect.md
+2 -2
@@ -9,7 +9,7 @@ function Component(a) {
9 let obj = {
10 method() {
11 mutate(x);
12 - return x.a;
12 + return x;
13 },
14 };
15 return obj.method();
@@ -32,7 +32,7 @@ function Component(a) {
32 const obj = {
33 method() {
34 mutate(x);
35 - return x.a;
35 + return x;
36 },
37 };
38 return obj.method();
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-3.js
+1 -1
@@ -5,7 +5,7 @@ function Component(a) {
5 let obj = {
6 method() {
7 mutate(x);
8 - return x.a;
8 + return x;
9 },
10 };
11 return obj.method();
compiler/packages/sprout/src/shared-runtime.ts
+13 -3
@@ -34,11 +34,21 @@ export const CONST_NUMBER2 = 2;
34 export const CONST_TRUE = true;
35 export const CONST_FALSE = false;
36
37 -let count: number = 0;
37 export function mutate(arg: any): void {
38 // don't mutate primitive
40 - if (typeof arg != null && typeof arg === "object") {
41 - arg["wat" + count++] = "joe";
39 + if (typeof arg === null || typeof arg !== "object") {
40 + return;
41 + }
42 +
43 + let count: number = 0;
44 + let key;
45 + while (true) {
46 + key = "wat" + count;
47 + if (!Object.hasOwn(arg, key)) {
48 + arg[key] = "joe";
49 + return;
50 + }
51 + count++;
52 }
53 }
54