no-deprecated-destroyed-lifecycle.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @author Yosuke Ota
  3. * See LICENSE file in root directory for full license.
  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:
  18. 'disallow using deprecated `destroyed` and `beforeDestroy` lifecycle hooks (in Vue.js 3.0.0+)',
  19. categories: ['vue3-essential'],
  20. url:
  21. 'https://eslint.vuejs.org/rules/no-deprecated-destroyed-lifecycle.html'
  22. },
  23. fixable: null,
  24. schema: [],
  25. messages: {
  26. deprecatedDestroyed:
  27. 'The `destroyed` lifecycle hook is deprecated. Use `unmounted` instead.',
  28. deprecatedBeforeDestroy:
  29. 'The `beforeDestroy` lifecycle hook is deprecated. Use `beforeUnmount` instead.',
  30. insteadUnmounted: 'Instead, change to `unmounted`.',
  31. insteadBeforeUnmount: 'Instead, change to `beforeUnmount`.'
  32. }
  33. },
  34. /** @param {RuleContext} context */
  35. create(context) {
  36. return utils.executeOnVue(context, (obj) => {
  37. const destroyed = utils.findProperty(obj, 'destroyed')
  38. if (destroyed) {
  39. context.report({
  40. node: destroyed.key,
  41. messageId: 'deprecatedDestroyed',
  42. // I don't know if they have exactly the same function, so don't do autofix.
  43. suggest: [
  44. {
  45. messageId: 'insteadUnmounted',
  46. fix(fixer) {
  47. return fix(fixer, destroyed, 'unmounted')
  48. }
  49. }
  50. ]
  51. })
  52. }
  53. const beforeDestroy = utils.findProperty(obj, 'beforeDestroy')
  54. if (beforeDestroy) {
  55. context.report({
  56. node: beforeDestroy.key,
  57. messageId: 'deprecatedBeforeDestroy',
  58. // I don't know if they have exactly the same function, so don't do autofix.
  59. suggest: [
  60. {
  61. messageId: 'insteadBeforeUnmount',
  62. fix(fixer) {
  63. return fix(fixer, beforeDestroy, 'beforeUnmount')
  64. }
  65. }
  66. ]
  67. })
  68. }
  69. /**
  70. * @param {RuleFixer} fixer
  71. * @param {Property} property
  72. * @param {string} newName
  73. */
  74. function fix(fixer, property, newName) {
  75. if (property.computed) {
  76. if (
  77. property.key.type === 'Literal' ||
  78. property.key.type === 'TemplateLiteral'
  79. ) {
  80. return fixer.replaceTextRange(
  81. [property.key.range[0] + 1, property.key.range[1] - 1],
  82. newName
  83. )
  84. }
  85. return null
  86. }
  87. if (property.shorthand) {
  88. return fixer.insertTextBefore(property.key, `${newName}:`)
  89. }
  90. return fixer.replaceText(property.key, newName)
  91. }
  92. })
  93. }
  94. }