Skip to content

Warn when component option should be an object, but is not (#5605) #5642

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
May 12, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 14 additions & 1 deletion src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ const isReservedProp = {
slot: 1
}

function parseObjectOption (vm: Component, option: Object, name: string) {
Copy link
Member

Choose a reason for hiding this comment

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

no need for option arg because you have name

Copy link
Member

Choose a reason for hiding this comment

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

better call it checkOptionType

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the result is used in assignment, to call it a "check" may be misleading

Copy link
Member

Choose a reason for hiding this comment

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

The assignment prevents runtime errors but the function is meant to do a check

if (!isPlainObject(option)) {
process.env.NODE_ENV !== 'production' && warn(
Copy link
Member

Choose a reason for hiding this comment

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

since this function is only called in dev mode, this check is unecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it may be good to provide a fallback (empty object) if a wrong type is used in prod mode

Copy link
Member

Choose a reason for hiding this comment

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

no, because it will never reach production code

Copy link
Member

Choose a reason for hiding this comment

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

The check must be removed in production

`${name} should be an object.`,
Copy link
Member

Choose a reason for hiding this comment

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

The message could be a bit more explicit: component option "${name}" should be an object

vm
)
return {}
}
return option
}

function initProps (vm: Component, propsOptions: Object) {
const propsData = vm.$options.propsData || {}
const props = vm._props = {}
Expand Down Expand Up @@ -148,8 +159,8 @@ function getData (data: Function, vm: Component): any {
const computedWatcherOptions = { lazy: true }

function initComputed (vm: Component, computed: Object) {
computed = parseObjectOption(vm, computed, 'computed')
Copy link
Member

Choose a reason for hiding this comment

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

This must be called only in non-production env (NODE_ENV !== 'production')

const watchers = vm._computedWatchers = Object.create(null)

for (const key in computed) {
const userDef = computed[key]
let getter = typeof userDef === 'function' ? userDef : userDef.get
Expand Down Expand Up @@ -213,6 +224,7 @@ function createComputedGetter (key) {
}

function initMethods (vm: Component, methods: Object) {
methods = parseObjectOption(vm, methods, 'methods')
const props = vm.$options.props
for (const key in methods) {
vm[key] = methods[key] == null ? noop : bind(methods[key], vm)
Expand All @@ -235,6 +247,7 @@ function initMethods (vm: Component, methods: Object) {
}

function initWatch (vm: Component, watch: Object) {
watch = parseObjectOption(vm, watch, 'watch')
for (const key in watch) {
const handler = watch[key]
if (Array.isArray(handler)) {
Expand Down
14 changes: 14 additions & 0 deletions test/unit/features/options/computed.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ describe('Options computed', () => {
}).then(done)
})

it('warn non object', () => {
new Vue({
computed: 'wrong'
})
expect('computed should be an object').toHaveBeenWarned()
})

it('don\'t warn when is an object', () => {
new Vue({
computed: {}
})
expect('computed should be an object').not.toHaveBeenWarned()
})

it('warn with setter and no getter', () => {
const vm = new Vue({
template: `
Expand Down
14 changes: 14 additions & 0 deletions test/unit/features/options/methods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,18 @@ describe('Options methods', () => {
})
expect(`method "hello" has an undefined value in the component definition`).toHaveBeenWarned()
})

it('should warn non object', () => {
Copy link
Member

Choose a reason for hiding this comment

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

These tests can also be refactored into a function. It should also test it doesn't print the warning in the right scenario

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should refactored function be a separate file in test/helpers/?

Copy link
Member

Choose a reason for hiding this comment

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

yes, after all you need to include it in different test files 🙂

new Vue({
methods: 'wrong'
})
expect('methods should be an object').toHaveBeenWarned()
})

it('don\'t warn when is an object', () => {
new Vue({
methods: {}
})
expect('computed should be an object').not.toHaveBeenWarned()
})
})
14 changes: 14 additions & 0 deletions test/unit/features/options/watch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ describe('Options watch', () => {
}).then(done)
})

it('warn non object', () => {
new Vue({
watch: 'wrong'
})
expect('watch should be an object').toHaveBeenWarned()
})

it('don\'t warn when is an object', () => {
new Vue({
watch: {}
})
expect('computed should be an object').not.toHaveBeenWarned()
})

it('string method name', done => {
const vm = new Vue({
data: {
Expand Down