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