Skip to content
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

Warn and handle missing get in computed (fix #5265) #5267

Merged
merged 1 commit into from
Mar 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,16 @@ function initComputed (vm: Component, computed: Object) {

for (const key in computed) {
const userDef = computed[key]
const getter = typeof userDef === 'function' ? userDef : userDef.get
let getter = typeof userDef === 'function' ? userDef : userDef.get
if (process.env.NODE_ENV !== 'production') {
Copy link
Member

Choose a reason for hiding this comment

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

if(process.env.NODE_ENV !== 'production' && !getter) {
  // warning
}

might be better

Copy link
Member

Choose a reason for hiding this comment

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

I don't think so. Former is better as define plugin would replace process.env.NODE_ENV with 'production' and it can be stripped off.

Copy link
Member

@posva posva Mar 24, 2017

Choose a reason for hiding this comment

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

both are stripped off actually

if (getter === undefined) {
warn(
`No getter function has been defined for computed property "${key}".`,
vm
)
getter = noop
}
}
// create internal watcher for the computed property.
watchers[key] = new Watcher(vm, getter, noop, computedWatcherOptions)

Expand Down
27 changes: 27 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,33 @@ describe('Options computed', () => {
}).then(done)
})

it('warn with setter and no getter', () => {
const vm = new Vue({
template: `
<div>
<test></test>
</div>
`,
components: {
test: {
data () {
return {
a: 1
}
},
computed: {
b: {
set (v) { this.a = v }
}
},
template: `<div>{{a}}</div>`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<div>1</div>')
expect('No getter function has been defined for computed property "b".').toHaveBeenWarned()
})

it('watching computed', done => {
const spy = jasmine.createSpy('watch computed')
const vm = new Vue({
Expand Down