From 6241740d3ea9d63c92f26b33794946c4ab0aff84 Mon Sep 17 00:00:00 2001 From: yikenman <17548672+yikenman@users.noreply.github.com> Date: Thu, 6 Mar 2025 23:42:11 +0800 Subject: [PATCH] docs(changeset): feat: use Reflect in Proxy --- .changeset/spicy-worms-invent.md | 5 +++++ packages/use-echarts-react/src/utils.spec.ts | 8 +++++--- packages/use-echarts-react/src/utils.ts | 9 ++++----- 3 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 .changeset/spicy-worms-invent.md diff --git a/.changeset/spicy-worms-invent.md b/.changeset/spicy-worms-invent.md new file mode 100644 index 0000000..a732e73 --- /dev/null +++ b/.changeset/spicy-worms-invent.md @@ -0,0 +1,5 @@ +--- +"use-echarts-react": patch +--- + +feat: use Reflect in Proxy diff --git a/packages/use-echarts-react/src/utils.spec.ts b/packages/use-echarts-react/src/utils.spec.ts index 3746ce5..4830fea 100644 --- a/packages/use-echarts-react/src/utils.spec.ts +++ b/packages/use-echarts-react/src/utils.spec.ts @@ -312,8 +312,11 @@ describe('utils', () => { jest.spyOn(console, 'warn').mockImplementation(jest.fn); mockEChartsInstance = { + id: `${Math.random()}`, setOption: jest.fn(), - getOption: jest.fn(), + getOption: jest.fn(function () { + return this.id; + }), resize: jest.fn(), isDisposed: jest.fn().mockReturnValue(false) } as unknown as EChartsType; @@ -326,8 +329,7 @@ describe('utils', () => { it('should allow accessing allowed properties', () => { const proxy = createProxyEChartsInstance(mockEChartsInstance); - - proxy.getOption(); + expect(proxy.getOption()).toBe(mockEChartsInstance.id); expect(mockEChartsInstance.getOption).toHaveBeenCalled(); }); diff --git a/packages/use-echarts-react/src/utils.ts b/packages/use-echarts-react/src/utils.ts index dc22848..2f1a3a9 100644 --- a/packages/use-echarts-react/src/utils.ts +++ b/packages/use-echarts-react/src/utils.ts @@ -93,7 +93,7 @@ export const createProxyEChartsInstance = (instance?: EChartsType | null, impera return null as any; } return new Proxy(instance, { - get(target, p) { + get(target, p, receiver) { if (FORBIT_PROP_SET.has(p)) { console.warn(`Prop '${String(p)}' is now managed by useECharts. This method will be no longer applicable.`); return undefined; @@ -102,16 +102,15 @@ export const createProxyEChartsInstance = (instance?: EChartsType | null, impera console.warn(`Method '${String(p)}' is now managed by useECharts. This method will be no longer applicable.`); return noop; } - return target[p as keyof typeof target]; + return Reflect.get(target, p, receiver); }, - set(target, p, value) { + set(target, p, value, receiver) { if (READONLY_PROP_SET.has(p)) { console.warn(`Prop '${String(p)}' is protected and cannot be modified.`); // not throw error. return true; } - target[p as keyof typeof target] = value; - return true; + return Reflect.set(target, p, value, receiver); } }); };