diff --git a/.changeset/live-proxy-delete-define.md b/.changeset/live-proxy-delete-define.md new file mode 100644 index 0000000000..2feeb4a7b7 --- /dev/null +++ b/.changeset/live-proxy-delete-define.md @@ -0,0 +1,6 @@ +--- +'@tanstack/svelte-table': patch +'@tanstack/vue-table': patch +--- + +`delete` and `Object.defineProperty` on `useCellContext()`/`useHeaderContext()` values are now forwarded to the current cell or header. Previously `delete` left the property in place and `Object.defineProperty` wrote to the proxy's internal object, so later reads could return stale values or throw. New properties are defined as configurable, explicitly non-configurable definitions are rejected, and `Object.preventExtensions` is refused, because the underlying cell or header can change. diff --git a/packages/svelte-table/src/live-proxy.ts b/packages/svelte-table/src/live-proxy.ts index c3801e40a6..959b229d04 100644 --- a/packages/svelte-table/src/live-proxy.ts +++ b/packages/svelte-table/src/live-proxy.ts @@ -33,6 +33,29 @@ export function createLiveProxy(getTarget: () => T): T { set(_, key, value) { return Reflect.set(getTarget(), key, value) }, + deleteProperty(_, key) { + return Reflect.deleteProperty(getTarget(), key) + }, + defineProperty(_, key, descriptor) { + // The proxy reports every property as configurable because the target + // can change, so it rejects explicitly non-configurable definitions and + // makes new definitions configurable to keep that descriptor truthful. + if (descriptor.configurable === false) { + return false + } + const target = getTarget() + return Reflect.defineProperty( + target, + key, + Object.hasOwn(target, key) + ? descriptor + : { ...descriptor, configurable: true }, + ) + }, + preventExtensions() { + // The target can change, so the proxy cannot promise a fixed key set. + return false + }, has(_, key) { return key in getTarget() }, diff --git a/packages/svelte-table/tests/live-proxy.test.ts b/packages/svelte-table/tests/live-proxy.test.ts new file mode 100644 index 0000000000..d19d818a20 --- /dev/null +++ b/packages/svelte-table/tests/live-proxy.test.ts @@ -0,0 +1,157 @@ +import { describe, expect, test } from 'vitest' +import { createLiveProxy } from '../src/live-proxy' + +type Target = { custom?: string; label: string } + +describe('createLiveProxy', () => { + test('delete removes the property from the current target', () => { + const target: Target = { custom: 'old', label: 'a' } + const context = createLiveProxy(() => target) + + expect(delete context.custom).toBe(true) + expect(Object.hasOwn(target, 'custom')).toBe(false) + expect('custom' in context).toBe(false) + expect(context.custom).toBeUndefined() + }) + + test('defineProperty defines the property on the current target', () => { + const target: Target = { label: 'a' } + const context = createLiveProxy(() => target) + + Object.defineProperty(context, 'custom', { value: 'new' }) + + expect(target.custom).toBe('new') + expect(context.custom).toBe('new') + }) + + test('defineProperty updates an existing configurable property', () => { + const target: Target = { custom: 'old', label: 'a' } + const context = createLiveProxy(() => target) + + Object.defineProperty(context, 'custom', { + value: 'new', + configurable: true, + enumerable: true, + writable: true, + }) + + expect(target.custom).toBe('new') + expect(context.custom).toBe('new') + }) + + test('new definitions are configurable so the reported descriptor stays truthful', () => { + const target: Target = { label: 'a' } + const context = createLiveProxy(() => target) + + Object.defineProperty(context, 'custom', { value: 'new' }) + + expect(Object.getOwnPropertyDescriptor(target, 'custom')).toEqual({ + value: 'new', + writable: false, + enumerable: false, + configurable: true, + }) + const reported = Object.getOwnPropertyDescriptor(context, 'custom')! + expect(() => + Object.defineProperty(context, 'custom', reported), + ).not.toThrow() + expect(delete context.custom).toBe(true) + expect(Object.hasOwn(target, 'custom')).toBe(false) + }) + + test('new accessor definitions are configurable', () => { + const target: Target = { label: 'a' } + const context = createLiveProxy(() => target) + + Object.defineProperty(context, 'custom', { get: () => 'computed' }) + + expect( + Object.getOwnPropertyDescriptor(target, 'custom')?.configurable, + ).toBe(true) + expect(context.custom).toBe('computed') + const reported = Object.getOwnPropertyDescriptor(context, 'custom')! + expect(() => + Object.defineProperty(context, 'custom', reported), + ).not.toThrow() + expect(delete context.custom).toBe(true) + expect(Object.hasOwn(target, 'custom')).toBe(false) + }) + + test('redefining an existing property keeps its own attributes', () => { + const target: Target = { label: 'a' } + Object.defineProperty(target, 'custom', { + value: 'old', + configurable: true, + enumerable: false, + writable: true, + }) + const context = createLiveProxy(() => target) + + Object.defineProperty(context, 'custom', { value: 'new' }) + + expect(Object.getOwnPropertyDescriptor(target, 'custom')).toEqual({ + value: 'new', + writable: true, + enumerable: false, + configurable: true, + }) + }) + + test('rejects explicitly non-configurable definitions without touching the target', () => { + const target: Target = { label: 'a' } + const context = createLiveProxy(() => target) + + expect(() => + Object.defineProperty(context, 'custom', { + value: 'new', + configurable: false, + }), + ).toThrow(TypeError) + expect(Object.hasOwn(target, 'custom')).toBe(false) + }) + + test('delete and defineProperty follow target changes', () => { + let target: Target = { custom: 'first', label: 'a' } + const context = createLiveProxy(() => target) + const first = target + + target = { custom: 'second', label: 'b' } + expect(delete context.custom).toBe(true) + expect(target.custom).toBeUndefined() + expect(first.custom).toBe('first') + + Object.defineProperty(context, 'custom', { value: 'third' }) + expect(target.custom).toBe('third') + expect(Object.hasOwn(first, 'custom')).toBe(true) + expect(first.custom).toBe('first') + }) + + test('refuses preventExtensions so later definitions do not mutate the target', () => { + const target: Target = { label: 'a' } + const context = createLiveProxy(() => target) + + expect(() => Object.preventExtensions(context)).toThrow(TypeError) + expect(Reflect.preventExtensions(context)).toBe(false) + expect(Object.isExtensible(context)).toBe(true) + expect(Object.isExtensible(target)).toBe(true) + + expect(Reflect.defineProperty(context, 'custom', { value: 'new' })).toBe( + true, + ) + expect(target.custom).toBe('new') + expect(context.custom).toBe('new') + }) + + test('reads, writes, and key checks still forward to the current target', () => { + let target: Target = { label: 'a' } + const context = createLiveProxy(() => target) + + context.label = 'updated' + expect(target.label).toBe('updated') + + target = { custom: 'x', label: 'b' } + expect(context.label).toBe('b') + expect('custom' in context).toBe(true) + expect(Object.keys(context)).toEqual(['custom', 'label']) + }) +}) diff --git a/packages/vue-table/src/live-proxy.ts b/packages/vue-table/src/live-proxy.ts index 9a972e15fb..32293dbdbf 100644 --- a/packages/vue-table/src/live-proxy.ts +++ b/packages/vue-table/src/live-proxy.ts @@ -33,6 +33,29 @@ export function createLiveProxy(getTarget: () => T): T { set(_, key, value) { return Reflect.set(getTarget(), key, value) }, + deleteProperty(_, key) { + return Reflect.deleteProperty(getTarget(), key) + }, + defineProperty(_, key, descriptor) { + // The proxy reports every property as configurable because the target + // can change, so it rejects explicitly non-configurable definitions and + // makes new definitions configurable to keep that descriptor truthful. + if (descriptor.configurable === false) { + return false + } + const target = getTarget() + return Reflect.defineProperty( + target, + key, + Object.hasOwn(target, key) + ? descriptor + : { ...descriptor, configurable: true }, + ) + }, + preventExtensions() { + // The target can change, so the proxy cannot promise a fixed key set. + return false + }, has(_, key) { return key in getTarget() }, diff --git a/packages/vue-table/tests/unit/live-proxy.test.ts b/packages/vue-table/tests/unit/live-proxy.test.ts new file mode 100644 index 0000000000..22e31bdaca --- /dev/null +++ b/packages/vue-table/tests/unit/live-proxy.test.ts @@ -0,0 +1,157 @@ +import { describe, expect, test } from 'vitest' +import { createLiveProxy } from '../../src/live-proxy' + +type Target = { custom?: string; label: string } + +describe('createLiveProxy', () => { + test('delete removes the property from the current target', () => { + const target: Target = { custom: 'old', label: 'a' } + const context = createLiveProxy(() => target) + + expect(delete context.custom).toBe(true) + expect(Object.hasOwn(target, 'custom')).toBe(false) + expect('custom' in context).toBe(false) + expect(context.custom).toBeUndefined() + }) + + test('defineProperty defines the property on the current target', () => { + const target: Target = { label: 'a' } + const context = createLiveProxy(() => target) + + Object.defineProperty(context, 'custom', { value: 'new' }) + + expect(target.custom).toBe('new') + expect(context.custom).toBe('new') + }) + + test('defineProperty updates an existing configurable property', () => { + const target: Target = { custom: 'old', label: 'a' } + const context = createLiveProxy(() => target) + + Object.defineProperty(context, 'custom', { + value: 'new', + configurable: true, + enumerable: true, + writable: true, + }) + + expect(target.custom).toBe('new') + expect(context.custom).toBe('new') + }) + + test('new definitions are configurable so the reported descriptor stays truthful', () => { + const target: Target = { label: 'a' } + const context = createLiveProxy(() => target) + + Object.defineProperty(context, 'custom', { value: 'new' }) + + expect(Object.getOwnPropertyDescriptor(target, 'custom')).toEqual({ + value: 'new', + writable: false, + enumerable: false, + configurable: true, + }) + const reported = Object.getOwnPropertyDescriptor(context, 'custom')! + expect(() => + Object.defineProperty(context, 'custom', reported), + ).not.toThrow() + expect(delete context.custom).toBe(true) + expect(Object.hasOwn(target, 'custom')).toBe(false) + }) + + test('new accessor definitions are configurable', () => { + const target: Target = { label: 'a' } + const context = createLiveProxy(() => target) + + Object.defineProperty(context, 'custom', { get: () => 'computed' }) + + expect( + Object.getOwnPropertyDescriptor(target, 'custom')?.configurable, + ).toBe(true) + expect(context.custom).toBe('computed') + const reported = Object.getOwnPropertyDescriptor(context, 'custom')! + expect(() => + Object.defineProperty(context, 'custom', reported), + ).not.toThrow() + expect(delete context.custom).toBe(true) + expect(Object.hasOwn(target, 'custom')).toBe(false) + }) + + test('redefining an existing property keeps its own attributes', () => { + const target: Target = { label: 'a' } + Object.defineProperty(target, 'custom', { + value: 'old', + configurable: true, + enumerable: false, + writable: true, + }) + const context = createLiveProxy(() => target) + + Object.defineProperty(context, 'custom', { value: 'new' }) + + expect(Object.getOwnPropertyDescriptor(target, 'custom')).toEqual({ + value: 'new', + writable: true, + enumerable: false, + configurable: true, + }) + }) + + test('rejects explicitly non-configurable definitions without touching the target', () => { + const target: Target = { label: 'a' } + const context = createLiveProxy(() => target) + + expect(() => + Object.defineProperty(context, 'custom', { + value: 'new', + configurable: false, + }), + ).toThrow(TypeError) + expect(Object.hasOwn(target, 'custom')).toBe(false) + }) + + test('delete and defineProperty follow target changes', () => { + let target: Target = { custom: 'first', label: 'a' } + const context = createLiveProxy(() => target) + const first = target + + target = { custom: 'second', label: 'b' } + expect(delete context.custom).toBe(true) + expect(target.custom).toBeUndefined() + expect(first.custom).toBe('first') + + Object.defineProperty(context, 'custom', { value: 'third' }) + expect(target.custom).toBe('third') + expect(Object.hasOwn(first, 'custom')).toBe(true) + expect(first.custom).toBe('first') + }) + + test('refuses preventExtensions so later definitions do not mutate the target', () => { + const target: Target = { label: 'a' } + const context = createLiveProxy(() => target) + + expect(() => Object.preventExtensions(context)).toThrow(TypeError) + expect(Reflect.preventExtensions(context)).toBe(false) + expect(Object.isExtensible(context)).toBe(true) + expect(Object.isExtensible(target)).toBe(true) + + expect(Reflect.defineProperty(context, 'custom', { value: 'new' })).toBe( + true, + ) + expect(target.custom).toBe('new') + expect(context.custom).toBe('new') + }) + + test('reads, writes, and key checks still forward to the current target', () => { + let target: Target = { label: 'a' } + const context = createLiveProxy(() => target) + + context.label = 'updated' + expect(target.label).toBe('updated') + + target = { custom: 'x', label: 'b' } + expect(context.label).toBe('b') + expect('custom' in context).toBe(true) + expect(Object.keys(context)).toEqual(['custom', 'label']) + }) +})