Skip to content

Commit

Permalink
fix: unwrapRefProxy native objects handling (#376)
Browse files Browse the repository at this point in the history
* fix: unwrapRefProxy, resolve #375

* add test
  • Loading branch information
antfu authored Jun 12, 2020
1 parent ca4350b commit 8322fc7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 22 deletions.
31 changes: 9 additions & 22 deletions src/reactivity/unwrap.ts
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down
54 changes: 54 additions & 0 deletions test/setup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<div>
<p id="raw_date">{{raw_date}}</p>
<p id="nested_date">{{nested_date}}</p>
<p id="raw_regex">{{raw_regex}}</p>
<p id="nested_regex_a">{{nested_regex.a}}</p>
<p id="nested_regex_b">{{nested_regex.b}}</p>
<p id="math">{{math}}</p>
</div>`,
}).$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
Expand Down

0 comments on commit 8322fc7

Please sign in to comment.