Skip to content
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

fix: unwrapRefProxy native objects handling #376

Merged
merged 3 commits into from
Jun 12, 2020
Merged
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dist"
],
"scripts": {
"start": "cross-env TARGET=es rollup -c -w",
"start": "concurrently \"tsc --emitDeclarationOnly -w\" \"cross-env TARGET=es rollup -c -w\"",
pikax marked this conversation as resolved.
Show resolved Hide resolved
"build": "rimraf dist typings && tsc --emitDeclarationOnly && rollup -c",
"lint": "prettier --write --parser typescript \"{src,test,test-dts}/*.ts?(x)\" && prettier --write \"{src,test}/*.js\"",
"test": "yarn test-dts && yarn test-unit",
Expand All @@ -47,6 +47,7 @@
"@rollup/plugin-replace": "^2.3.2",
"@types/jest": "^25.2.3",
"@types/node": "^14.0.6",
"concurrently": "^5.2.0",
"conventional-changelog-cli": "^2.0.31",
"conventional-github-releaser": "^3.1.3",
"cross-env": "^7.0.2",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function genConfig({ outFile, format, mode }) {
let buildConfig

if (process.env.TARGET) {
buildConfig = genConfig(builds[process.env.TARGET])
buildConfig = [genConfig(builds[process.env.TARGET])]
} else {
buildConfig = getAllBuilds()
}
Expand Down
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
Loading