Skip to content

Commit d6bd667

Browse files
maciej-kayyx990803
authored andcommitted
Warn when component option should be an object, but is not (#5605) (#5642)
* warn when component should be an object, but is not * remarks * remarks * remarks * rename to checkOptionType and guard production * typo * Update state.js * Update test-object-option.js
1 parent 92d6192 commit d6bd667

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

Diff for: src/core/instance/state.js

+13
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ const isReservedProp = {
6060
slot: 1
6161
}
6262

63+
function checkOptionType (vm: Component, name: string) {
64+
const option = vm.$options[name]
65+
if (!isPlainObject(option)) {
66+
warn(
67+
`component option "${name}" should be an object.`,
68+
vm
69+
)
70+
}
71+
}
72+
6373
function initProps (vm: Component, propsOptions: Object) {
6474
const propsData = vm.$options.propsData || {}
6575
const props = vm._props = {}
@@ -148,6 +158,7 @@ function getData (data: Function, vm: Component): any {
148158
const computedWatcherOptions = { lazy: true }
149159

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

153164
for (const key in computed) {
@@ -213,6 +224,7 @@ function createComputedGetter (key) {
213224
}
214225

215226
function initMethods (vm: Component, methods: Object) {
227+
process.env.NODE_ENV !== 'production' && checkOptionType(vm, 'methods')
216228
const props = vm.$options.props
217229
for (const key in methods) {
218230
vm[key] = methods[key] == null ? noop : bind(methods[key], vm)
@@ -235,6 +247,7 @@ function initMethods (vm: Component, methods: Object) {
235247
}
236248

237249
function initWatch (vm: Component, watch: Object) {
250+
process.env.NODE_ENV !== 'production' && checkOptionType(vm, 'watch')
238251
for (const key in watch) {
239252
const handler = watch[key]
240253
if (Array.isArray(handler)) {

Diff for: test/helpers/test-object-option.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Vue from 'vue'
2+
3+
export default function testObjectOption (name: string) {
4+
it('should warn non object', () => {
5+
const options = {}
6+
options[name] = () => {}
7+
new Vue(options)
8+
expect(`component option "${name}" should be an object`).toHaveBeenWarned()
9+
})
10+
11+
it('don\'t warn when is an object', () => {
12+
const options = {}
13+
options[name] = {}
14+
new Vue(options)
15+
expect(`component option "${name}" should be an object`).not.toHaveBeenWarned()
16+
})
17+
}

Diff for: test/unit/features/options/computed.spec.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from 'vue'
2+
import testObjectOption from '../../../helpers/test-object-option'
23

34
describe('Options computed', () => {
45
it('basic usage', done => {
@@ -48,6 +49,8 @@ describe('Options computed', () => {
4849
}).then(done)
4950
})
5051

52+
testObjectOption('computed')
53+
5154
it('warn with setter and no getter', () => {
5255
const vm = new Vue({
5356
template: `

Diff for: test/unit/features/options/methods.spec.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from 'vue'
2+
import testObjectOption from '../../../helpers/test-object-option'
23

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

20+
testObjectOption('methods')
21+
1922
it('should warn undefined methods', () => {
2023
new Vue({
2124
methods: {

Diff for: test/unit/features/options/watch.spec.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from 'vue'
2+
import testObjectOption from '../../../helpers/test-object-option'
23

34
describe('Options watch', () => {
45
let spy
@@ -23,6 +24,8 @@ describe('Options watch', () => {
2324
}).then(done)
2425
})
2526

27+
testObjectOption('watch')
28+
2629
it('string method name', done => {
2730
const vm = new Vue({
2831
data: {

0 commit comments

Comments
 (0)