Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/live-proxy-delete-define.md
Original file line number Diff line number Diff line change
@@ -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.
23 changes: 23 additions & 0 deletions packages/svelte-table/src/live-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ export function createLiveProxy<T extends object>(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()
},
Expand Down
157 changes: 157 additions & 0 deletions packages/svelte-table/tests/live-proxy.test.ts
Original file line number Diff line number Diff line change
@@ -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'])
})
})
23 changes: 23 additions & 0 deletions packages/vue-table/src/live-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ export function createLiveProxy<T extends object>(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()
},
Expand Down
157 changes: 157 additions & 0 deletions packages/vue-table/tests/unit/live-proxy.test.ts
Original file line number Diff line number Diff line change
@@ -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'])
})
})