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++; } }