no-arrow-functions-in-watch.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * @author Sosuke Suzuki
  3. */
  4. 'use strict'
  5. const utils = require('../utils')
  6. module.exports = {
  7. meta: {
  8. type: 'problem',
  9. docs: {
  10. description: 'disallow using arrow functions to define watcher',
  11. categories: ['vue3-essential', 'essential'],
  12. url: 'https://eslint.vuejs.org/rules/no-arrow-functions-in-watch.html'
  13. },
  14. fixable: null,
  15. schema: []
  16. },
  17. /** @param {RuleContext} context */
  18. create(context) {
  19. return utils.executeOnVue(context, (obj) => {
  20. const watchNode = utils.findProperty(obj, 'watch')
  21. if (watchNode == null) {
  22. return
  23. }
  24. const watchValue = watchNode.value
  25. if (watchValue.type !== 'ObjectExpression') {
  26. return
  27. }
  28. for (const property of watchValue.properties) {
  29. if (property.type !== 'Property') {
  30. continue
  31. }
  32. for (const handler of utils.iterateWatchHandlerValues(property)) {
  33. if (handler.type === 'ArrowFunctionExpression') {
  34. context.report({
  35. node: handler,
  36. message:
  37. 'You should not use an arrow function to define a watcher.'
  38. })
  39. }
  40. }
  41. }
  42. })
  43. }
  44. }