From 8322fc75dadcb656cab46e1804ca7aafd47851c8 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 12 Jun 2020 15:27:11 +0800 Subject: [PATCH] fix: unwrapRefProxy native objects handling (#376) * fix: unwrapRefProxy, resolve #375 * add test --- src/reactivity/unwrap.ts | 31 +++++++---------------- test/setup.spec.js | 54 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/src/reactivity/unwrap.ts b/src/reactivity/unwrap.ts index bf306ac1..a59fb29d 100644 --- a/src/reactivity/unwrap.ts +++ b/src/reactivity/unwrap.ts @@ -1,29 +1,16 @@ import { isRef } from './ref' -import { proxy, isFunction, isObject, isArray } from '../utils' +import { proxy, isFunction, isPlainObject, isArray } from '../utils' import { isReactive } from './reactive' export function unwrapRefProxy(value: any) { - if (isFunction(value)) { - return value - } - - if (isRef(value)) { - return value - } - - if (isArray(value)) { - return value - } - - if (isReactive(value)) { - return value - } - - if (!isObject(value)) { - return value - } - - if (!Object.isExtensible(value)) { + if ( + isFunction(value) || + isRef(value) || + isArray(value) || + isReactive(value) || + !isPlainObject(value) || + !Object.isExtensible(value) + ) { return value } diff --git a/test/setup.spec.js b/test/setup.spec.js index e273de06..98e50fcd 100644 --- a/test/setup.spec.js +++ b/test/setup.spec.js @@ -568,6 +568,60 @@ describe('setup', () => { ).toMatchObject([{ value: 1 }]) }) + it('should not unwrap built-in objects on the template', () => { + const date = new Date('2020-01-01') + const regex = /a(b).*/ + const dateString = date.toString() + const regexString = regex.toString() + const mathString = Math.toString() + + const vm = new Vue({ + setup() { + return { + raw_date: date, + nested_date: { + a: date, + b: date, + }, + raw_regex: regex, + nested_regex: { + a: regex, + b: regex, + }, + math: Math, + } + }, + template: `
+

{{raw_date}}

+

{{nested_date}}

+

{{raw_regex}}

+

{{nested_regex.a}}

+

{{nested_regex.b}}

+

{{math}}

+
`, + }).$mount() + + expect(vm.$el.querySelector('#raw_date').textContent).toBe(dateString) + expect( + JSON.parse(vm.$el.querySelector('#nested_date').textContent) + ).toMatchObject( + JSON.parse( + JSON.stringify({ + a: date, + b: date, + }) + ) + ) + expect(vm.$el.querySelector('#raw_regex').textContent).toBe(regexString) + expect(vm.$el.querySelector('#nested_regex_a').textContent).toBe( + regexString + ) + expect(vm.$el.querySelector('#nested_regex_b').textContent).toBe( + regexString + ) + expect(vm.$el.querySelector('#math').textContent).toBe(mathString) + }) + describe('Methods', () => { it('binds methods when calling with parenthesis', async () => { let context = null