no-deprecated-destroyed-lifecycle.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @author Yosuke Ota
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. /**
  8. * @param {RuleFixer} fixer
  9. * @param {Property} property
  10. * @param {string} newName
  11. */
  12. function fix(fixer, property, newName) {
  13. if (property.computed) {
  14. if (
  15. property.key.type === 'Literal' ||
  16. property.key.type === 'TemplateLiteral'
  17. ) {
  18. return fixer.replaceTextRange(
  19. [property.key.range[0] + 1, property.key.range[1] - 1],
  20. newName
  21. )
  22. }
  23. return null
  24. }
  25. if (property.shorthand) {
  26. return fixer.insertTextBefore(property.key, `${newName}:`)
  27. }
  28. return fixer.replaceText(property.key, newName)
  29. }
  30. module.exports = {
  31. meta: {
  32. type: 'problem',
  33. docs: {
  34. description:
  35. 'disallow using deprecated `destroyed` and `beforeDestroy` lifecycle hooks (in Vue.js 3.0.0+)',
  36. categories: ['vue3-essential'],
  37. url: 'https://eslint.vuejs.org/rules/no-deprecated-destroyed-lifecycle.html'
  38. },
  39. fixable: 'code',
  40. schema: [],
  41. messages: {
  42. deprecatedDestroyed:
  43. 'The `destroyed` lifecycle hook is deprecated. Use `unmounted` instead.',
  44. deprecatedBeforeDestroy:
  45. 'The `beforeDestroy` lifecycle hook is deprecated. Use `beforeUnmount` instead.'
  46. }
  47. },
  48. /** @param {RuleContext} context */
  49. create(context) {
  50. return utils.executeOnVue(context, (obj) => {
  51. const destroyed = utils.findProperty(obj, 'destroyed')
  52. if (destroyed) {
  53. context.report({
  54. node: destroyed.key,
  55. messageId: 'deprecatedDestroyed',
  56. fix(fixer) {
  57. return fix(fixer, destroyed, 'unmounted')
  58. }
  59. })
  60. }
  61. const beforeDestroy = utils.findProperty(obj, 'beforeDestroy')
  62. if (beforeDestroy) {
  63. context.report({
  64. node: beforeDestroy.key,
  65. messageId: 'deprecatedBeforeDestroy',
  66. fix(fixer) {
  67. return fix(fixer, beforeDestroy, 'beforeUnmount')
  68. }
  69. })
  70. }
  71. })
  72. }
  73. }