Skip to content

Commit

Permalink
chore: warn methods that conflict with internals
Browse files Browse the repository at this point in the history
close #6312
  • Loading branch information
yyx990803 committed Sep 1, 2017
1 parent 02f8b80 commit 8fc6bc8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function initData (vm: Component) {
if (process.env.NODE_ENV !== 'production') {
if (methods && hasOwn(methods, key)) {
warn(
`method "${key}" has already been defined as a data property.`,
`Method "${key}" has already been defined as a data property.`,
vm
)
}
Expand Down Expand Up @@ -260,22 +260,28 @@ 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)
if (process.env.NODE_ENV !== 'production') {
if (methods[key] == null) {
warn(
`method "${key}" has an undefined value in the component definition. ` +
`Method "${key}" has an undefined value in the component definition. ` +
`Did you reference the function correctly?`,
vm
)
}
if (props && hasOwn(props, key)) {
warn(
`method "${key}" has already been defined as a prop.`,
`Method "${key}" has already been defined as a prop.`,
vm
)
}
if ((key in vm) && isReserved(key)) {
warn(
`Method "${key}" conflicts with an existing Vue instance method. ` +
`Avoid defining component methods that start with _ or $.`
)
}
}
vm[key] = methods[key] == null ? noop : bind(methods[key], vm)
}
}

Expand Down
13 changes: 11 additions & 2 deletions test/unit/features/options/methods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Options methods', () => {
hello: undefined
}
})
expect(`method "hello" has an undefined value in the component definition`).toHaveBeenWarned()
expect(`Method "hello" has an undefined value in the component definition`).toHaveBeenWarned()
})

it('should warn methods conflicting with data', () => {
Expand All @@ -37,6 +37,15 @@ describe('Options methods', () => {
foo () {}
}
})
expect(`method "foo" has already been defined as a data property`).toHaveBeenWarned()
expect(`Method "foo" has already been defined as a data property`).toHaveBeenWarned()
})

it('should warn methods conflicting with internal methods', () => {
new Vue({
methods: {
_update () {}
}
})
expect(`Method "_update" conflicts with an existing Vue instance method`).toHaveBeenWarned()
})
})
2 changes: 1 addition & 1 deletion test/unit/features/options/props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ describe('Options props', () => {
}
}
}).$mount()
expect(`method "a" has already been defined as a prop`).toHaveBeenWarned()
expect(`Method "a" has already been defined as a prop`).toHaveBeenWarned()
expect(`Avoid mutating a prop directly`).toHaveBeenWarned()
})

Expand Down

0 comments on commit 8fc6bc8

Please sign in to comment.