Skip to content

Rule proposal: no-arrow-functions-in-watch #1113

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

Closed
1 of 4 tasks
alert-debug opened this issue May 5, 2020 · 2 comments · Fixed by #1155
Closed
1 of 4 tasks

Rule proposal: no-arrow-functions-in-watch #1113

alert-debug opened this issue May 5, 2020 · 2 comments · Fixed by #1155

Comments

@alert-debug
Copy link

alert-debug commented May 5, 2020

Please describe what the rule should do:
According to the Vue docs, "you should not use an arrow function to define a watcher". The rule should detect when a component uses an arrow function inside the watch section. Code that doesn't follow this requirement leads to confusing errors about this not being defined.

What category should the rule belong to?

  • Enforces code style
  • Warns about a potential error
  • Suggests an alternate way of doing something
  • Other (please specify:)

Provide 2-3 code examples that this rule should warn about:

watch: {
    propShow: {
        handler: (val, oldVal) => {
            this.show = val;
        }
    }
}

(The above example is taken from an actual question asked on StackOverflow.)

watch: {
    dialog: (newState) => {
        this.$nextTick(() => {
            console.log('Tick')
        })
    }
}
@ota-meshi
Copy link
Member

Thank you for suggesting the this rule.

I like this rule 👍

@nikkipelchat
Copy link

Hey, two questions about this rule:

  1. Should this rule also apply to methods? The docs say you can't use an arrow function in them either. Maybe that would even be a new rule no-arrow-functions-in-methods.
  2. The rule implemented doesn't do a deep search of the whole watcher functions. It checks if the immediate watchers are arrow functions but it won't check inside the watchers. Should it catch all arrow functions inside watchers? Examples below.

Things it will catch:

watch: {
    validation: () => { // will catch this
        this.something = 'hi'
    }
}

Things it won't catch at the moment:

watch: {
    propShow: {
        handler: (val, oldVal) => { // won't catch this
            this.show = val
        }
    }
}
watch: {
    validation() {
        this.$nextTick(() => { // won't catch this
            console.log('Tick')
        })
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants