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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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', 'vue2-essential'],
  12. url: 'https://eslint.vuejs.org/rules/no-arrow-functions-in-watch.html'
  13. },
  14. fixable: null,
  15. schema: [],
  16. messages: {
  17. noArrowFunctionsInWatch:
  18. 'You should not use an arrow function to define a watcher.'
  19. }
  20. },
  21. /** @param {RuleContext} context */
  22. create(context) {
  23. return utils.executeOnVue(context, (obj) => {
  24. const watchNode = utils.findProperty(obj, 'watch')
  25. if (watchNode == null) {
  26. return
  27. }
  28. const watchValue = watchNode.value
  29. if (watchValue.type !== 'ObjectExpression') {
  30. return
  31. }
  32. for (const property of watchValue.properties) {
  33. if (property.type !== 'Property') {
  34. continue
  35. }
  36. for (const handler of utils.iterateWatchHandlerValues(property)) {
  37. if (handler.type === 'ArrowFunctionExpression') {
  38. context.report({
  39. node: handler,
  40. messageId: 'noArrowFunctionsInWatch'
  41. })
  42. }
  43. }
  44. }
  45. })
  46. }
  47. }