no-deprecated-events-api.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * @fileoverview disallow using deprecated events api
  3. * @author yoyo930021
  4. */
  5. 'use strict'
  6. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const utils = require('../utils')
  10. // ------------------------------------------------------------------------------
  11. // Rule Definition
  12. // ------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: 'problem',
  16. docs: {
  17. description: 'disallow using deprecated events api (in Vue.js 3.0.0+)',
  18. categories: ['vue3-essential'],
  19. url: 'https://eslint.vuejs.org/rules/no-deprecated-events-api.html'
  20. },
  21. fixable: null,
  22. schema: [],
  23. messages: {
  24. noDeprecatedEventsApi:
  25. 'The Events api `$on`, `$off` `$once` is deprecated. Using external library instead, for example mitt.'
  26. }
  27. },
  28. /** @param {RuleContext} context */
  29. create(context) {
  30. return utils.defineVueVisitor(context, {
  31. /** @param {MemberExpression & ({parent: CallExpression} | {parent: ChainExpression & {parent: CallExpression}})} node */
  32. 'CallExpression > MemberExpression, CallExpression > ChainExpression > MemberExpression'(
  33. node
  34. ) {
  35. const call =
  36. node.parent.type === 'ChainExpression'
  37. ? node.parent.parent
  38. : node.parent
  39. if (call.optional) {
  40. // It is OK because checking whether it is deprecated.
  41. // e.g. `this.$on?.()`
  42. return
  43. }
  44. if (
  45. utils.skipChainExpression(call.callee) !== node ||
  46. !['$on', '$off', '$once'].includes(
  47. utils.getStaticPropertyName(node) || ''
  48. )
  49. ) {
  50. return
  51. }
  52. if (!utils.isThis(node.object, context)) {
  53. return
  54. }
  55. context.report({
  56. node: node.property,
  57. messageId: 'noDeprecatedEventsApi'
  58. })
  59. }
  60. })
  61. }
  62. }