Skip to content

fix(reactivity): align the get/set operations to ECMA spec #3025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
79 changes: 79 additions & 0 deletions packages/reactivity/__tests__/reactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,83 @@ describe('reactivity/reactive', () => {
const observed = reactive(original)
expect(isReactive(observed)).toBe(false)
})

describe('with data property', () => {
test('should return the original value if a property is non-configurable, non-writable', () => {
const obj: any = {}
const inner = { foo: 1 }
Object.defineProperty(obj, 'foo', {
value: inner,
writable: false,
configurable: false
})

const observed = reactive(obj)
expect(isReactive(observed.foo)).toBe(false)
expect(observed.foo).toBe(inner)
})

test('should return proxy value if a property is configurable or writable', () => {
const obj: any = {}
Object.defineProperty(obj, 'foo', {
value: { foo: 1 },
writable: true,
configurable: false
})
Object.defineProperty(obj, 'bar', {
value: { foo: 1 },
writable: false,
configurable: true
})
Object.defineProperty(obj, 'baz', {
value: { foo: 1 },
writable: true,
configurable: true
})

const observed = reactive(obj)
expect(isReactive(observed.foo)).toBe(true)
expect(isReactive(observed.bar)).toBe(true)
expect(isReactive(observed.baz)).toBe(true)
})

// https://www.ecma-international.org/ecma-262/11.0/index.html#sec-invariants-of-the-essential-internal-methods
// https://www.ecma-international.org/ecma-262/11.0/index.html#sec-samevalue
test('should be able to set the value if they are the SameValue', () => {
const inner = { foo: 1 }
const outer: any = {}
Object.defineProperty(outer, 'key', {
value: inner,
configurable: false,
writable: false
})

const observed = reactive(outer)

observed.key = inner

expect(() => {
observed.key = 'other value'
}).toThrow(`'set' on proxy: trap returned falsish for property 'key'`)
})
})

describe('with accessor property', () => {
test('should return proxy value if a property is an accessor property', () => {
const inner = { foo: 1 }
const outer: any = {}
Object.defineProperty(outer, 'key', {
get() {
return inner
}
})

const observed = reactive(outer)
expect(observed).not.toBe(outer)
expect(isReactive(observed)).toBe(true)
expect(isReactive(outer)).toBe(false)

expect(isReactive(observed.key)).toBe(true)
})
})
})
26 changes: 26 additions & 0 deletions packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ function createGetter(isReadonly = false, shallow = false) {
}

if (isObject(res)) {
// #3024
// https://www.ecma-international.org/ecma-262/11.0/index.html#sec-invariants-of-the-essential-internal-methods
const descriptor = Reflect.getOwnPropertyDescriptor(target, key)
if (descriptor) {
if (
// make sure it's a data property
!(descriptor.get || descriptor.set) &&
// non-configurable, non-writable
(!descriptor.configurable && !descriptor.writable)
) {
return res
}
}
// Convert returned value into a proxy as well. we do the isObject check
// here to avoid invalid value warning. Also need to lazy access readonly
// and reactive here to avoid circular dependency.
Expand All @@ -147,6 +160,19 @@ function createSetter(shallow = false) {
receiver: object
): boolean {
let oldValue = (target as any)[key]

const descriptor = Reflect.getOwnPropertyDescriptor(target, key)
if (descriptor) {
if (
// make sure it's a data property
!(descriptor.get || descriptor.set) &&
// non-configurable, non-writable
(!descriptor.configurable && !descriptor.writable)
) {
return Object.is(oldValue, value)
}
}

if (!shallow) {
value = toRaw(value)
oldValue = toRaw(oldValue)
Expand Down