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 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
13 changes: 13 additions & 0 deletions src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ const isReservedProp = {
slot: 1
}

function checkOptionType (vm: Component, name: string) {
const option = vm.$options[name]
if (!isPlainObject(option)) {
warn(
`component option "${name}" should be an object.`,
vm
)
}
}

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

function initComputed (vm: Component, computed: Object) {
process.env.NODE_ENV !== 'production' && checkOptionType(vm, 'computed')
const watchers = vm._computedWatchers = Object.create(null)

for (const key in computed) {
Expand Down Expand Up @@ -213,6 +224,7 @@ function createComputedGetter (key) {
}

function initMethods (vm: Component, methods: Object) {
process.env.NODE_ENV !== 'production' && checkOptionType(vm, '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) {
process.env.NODE_ENV !== 'production' && checkOptionType(vm, 'watch')
for (const key in watch) {
const handler = watch[key]
if (Array.isArray(handler)) {
Expand Down
17 changes: 17 additions & 0 deletions test/helpers/test-object-option.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Vue from 'vue'

export default function testObjectOption (name: string) {
it('should warn non object', () => {
const options = {}
options[name] = () => {}
new Vue(options)
expect(`component option "${name}" should be an object`).toHaveBeenWarned()
})

it('don\'t warn when is an object', () => {
const options = {}
options[name] = {}
new Vue(options)
expect(`component option "${name}" should be an object`).not.toHaveBeenWarned()
})
}
3 changes: 3 additions & 0 deletions test/unit/features/options/computed.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vue from 'vue'
import testObjectOption from '../../../helpers/test-object-option'

describe('Options computed', () => {
it('basic usage', done => {
Expand Down Expand Up @@ -48,6 +49,8 @@ describe('Options computed', () => {
}).then(done)
})

testObjectOption('computed')

it('warn with setter and no getter', () => {
const vm = new Vue({
template: `
Expand Down
3 changes: 3 additions & 0 deletions test/unit/features/options/methods.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vue from 'vue'
import testObjectOption from '../../../helpers/test-object-option'

describe('Options methods', () => {
it('should have correct context', () => {
Expand All @@ -16,6 +17,8 @@ describe('Options methods', () => {
expect(vm.a).toBe(2)
})

testObjectOption('methods')

it('should warn undefined methods', () => {
new Vue({
methods: {
Expand Down
3 changes: 3 additions & 0 deletions test/unit/features/options/watch.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vue from 'vue'
import testObjectOption from '../../../helpers/test-object-option'

describe('Options watch', () => {
let spy
Expand All @@ -23,6 +24,8 @@ describe('Options watch', () => {
}).then(done)
})

testObjectOption('watch')

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