Skip to content

Commit

Permalink
Warn when defining a method with same name as a prop (#4950)
Browse files Browse the repository at this point in the history
* Warn when defining a method with same name as a prop

* update error message
  • Loading branch information
chriscasola authored and yyx990803 committed Feb 18, 2017
1 parent 9ccffe7 commit 3dc9338
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,23 @@ function createComputedGetter (key) {
}

function initMethods (vm: Component, methods: Object) {
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' && methods[key] == null) {
warn(
`method "${key}" has an undefined value in the component definition. ` +
`Did you reference the function correctly?`,
vm
)
if (process.env.NODE_ENV !== 'production') {
if (methods[key] == null) {
warn(
`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.`,
vm
)
}
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/unit/features/options/props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,27 @@ describe('Options props', () => {
expect('already declared as a prop').toHaveBeenWarned()
})

it('should warn methods already defined as a prop', () => {
new Vue({
template: '<test a="1"></test>',
components: {
test: {
template: '<div></div>',
props: {
a: null
},
methods: {
a () {

}
}
}
}
}).$mount()
expect(`method "a" has already been defined as a prop`).toHaveBeenWarned()
expect(`Avoid mutating a prop directly`).toHaveBeenWarned()
})

it('treat boolean props properly', () => {
const vm = new Vue({
template: '<comp ref="child" prop-a prop-b="prop-b"></comp>',
Expand Down

0 comments on commit 3dc9338

Please sign in to comment.