Skip to content

Handle non-reactive nulls (#231) #241

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

Merged
merged 8 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 7 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import Vue from 'vue';

const toString = (x: any) => Object.prototype.toString.call(x);

export function isNative (Ctor: any): boolean {
return typeof Ctor === 'function' && /native code/.test(Ctor.toString())
export function isNative(Ctor: any): boolean {
return typeof Ctor === 'function' && /native code/.test(Ctor.toString());
}

export const hasSymbol =
typeof Symbol !== 'undefined' && isNative(Symbol) &&
typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)
typeof Symbol !== 'undefined' &&
isNative(Symbol) &&
typeof Reflect !== 'undefined' &&
isNative(Reflect.ownKeys);

export const noopFn: any = (_: any) => _;

Expand All @@ -34,9 +36,8 @@ export function def(obj: Object, key: string, val: any, enumerable?: boolean) {
});
}

const hasOwnProperty = Object.prototype.hasOwnProperty;
export function hasOwn(obj: Object | any[], key: string): boolean {
return hasOwnProperty.call(obj, key);
return obj !== null && Object.hasOwnProperty.call(obj, key);
Copy link
Member

@haoqunjiang haoqunjiang Feb 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better do the obj !== null test in the isReactive function.
And change the obj: Object to obj: object, to make the code clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sodatea thanks for the feedback!

}

export function assert(condition: any, msg: string) {
Expand Down
11 changes: 11 additions & 0 deletions test/setup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ describe('setup', () => {
expect(vm.a).toBe(1);
});

it('should work with non reactive null', () => {
const vm = new Vue({
setup() {
return {
a: null,
};
},
}).$mount();
expect(vm.a).toBe(null);
});

it('should be overrided by data option of plain object', () => {
const vm = new Vue({
setup() {
Expand Down