From 0f97ea4c71f5f8db72221405a422d49a2c2644a3 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Wed, 4 Oct 2023 12:36:04 +0530 Subject: [PATCH] [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. --- .../compiler/object-method-shorthand-3.expect.md | 4 ++-- .../compiler/object-method-shorthand-3.js | 2 +- compiler/packages/sprout/src/shared-runtime.ts | 16 +++++++++++++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-3.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-3.expect.md index 37376a1c1f..1ebf15e389 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-3.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-3.expect.md @@ -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(); diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-3.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-3.js index 4222e39846..54c1edf116 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-3.js +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-3.js @@ -5,7 +5,7 @@ function Component(a) { let obj = { method() { mutate(x); - return x.a; + return x; }, }; return obj.method(); diff --git a/compiler/packages/sprout/src/shared-runtime.ts b/compiler/packages/sprout/src/shared-runtime.ts index 56cb5eaa08..752b1f590e 100644 --- a/compiler/packages/sprout/src/shared-runtime.ts +++ b/compiler/packages/sprout/src/shared-runtime.ts @@ -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++; } }