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

Conversation

kmc059000
Copy link
Contributor

@kmc059000 kmc059000 commented Mar 23, 2017

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

If adding a new feature, the PR's description includes:

  • A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:

I ran all test locally. I'm having some failures related to SSR and selenium which appear to be completely unrelated. I've confirmed the new test works by reverting my change and adding it back in.

I am using noop for the getter if one does not exist to avoid exceptions when calling functions on the getter.

@kmc059000
Copy link
Contributor Author

#5265

let getter = typeof userDef === 'function' ? userDef : userDef.get
if (getter === undefined) {
warn(
`computed "${key}" is requires a "get" to be defined if a "set" is defined.` +
Copy link
Contributor

Choose a reason for hiding this comment

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

What about saying: "No getter function has been defined for computed property ${key}."

if (getter === undefined) {
warn(
`computed "${key}" is requires a "get" to be defined if a "set" is defined.` +
`Did you define or misspell the get?`,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need this.

@@ -147,7 +147,15 @@ 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 (getter === undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you wrap the if in a non-prod if please? (if (process.env.NODE_ENV !== 'production'))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should only the warning be wrapped? I think getter should be assigned to noop regardless of whether it is in production mode or not, so that the behavior is consistent.

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 it's necessary to have a fallback on production because we'd get the error at runtime, so wrapping the whole if looks better imo. It'll also prevent an unnecessary check for all computed properties

@kmc059000 kmc059000 force-pushed the 5265-warn-computed-missing-get branch from 8ad8be5 to dd14280 Compare March 23, 2017 22:37
@@ -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

@kmc059000
Copy link
Contributor Author

kmc059000 commented Mar 24, 2017

Here is the change that changed the behavior. Notice on this line that noop is used if get is falsy. Does anybody think that this original behavior is preferable to the warning?

const getter = typeof userDef === 'function' ? userDef : (userDef.get || noop)

This would be consistent with how defineComputed() is defined here when the get is undefined.

IMO this is better because it would not cause errors in production mode, avoid a new warning, keep the
2.1.* behavior, and make the behavior between production and non-production modes consistent.


Alternatively, I think it may be possible to avoid creating a Watcher if get is not defined, although I think this is a edge case.

watchers[key] = getter ? new Watcher(vm, getter, noop, computedWatcherOptions) : null

@yyx990803 yyx990803 merged commit 6fcfdbd into vuejs:dev Mar 27, 2017
awamwang pushed a commit to awamwang/vue that referenced this pull request Jun 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants