Skip to content

Commit 448f2f7

Browse files
committed
feat: rename deprecated lifecycle
1 parent da31b58 commit 448f2f7

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Diff for: generator/codemods/vue/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = function(fileInfo, api) {
66

77
require('./add-emit-declaration')(context)
88
require('./add-watch-deep')(context)
9+
require('./rename-lifecycle')(context)
910

1011
return root.toSource({ lineTerminator: '\n' })
1112
}

Diff for: generator/codemods/vue/rename-lifecycle.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/** @type {import('jscodeshift').Transform} */
2+
3+
const DEPRECATED_LIFECYCLE = Object.create(null)
4+
DEPRECATED_LIFECYCLE.destroyed = 'unmounted'
5+
DEPRECATED_LIFECYCLE.beforeDestroy = 'beforeUnmount'
6+
7+
module.exports = function renameLifecycle(context) {
8+
const { j, root } = context
9+
10+
const renameDeprecatedLifecycle = path => {
11+
const name = path.node.key.name
12+
13+
if (
14+
DEPRECATED_LIFECYCLE[name] &&
15+
path.parent &&
16+
path.parent.parent &&
17+
path.parent.parent.value.type === 'ExportDefaultDeclaration'
18+
) {
19+
path.value.key.name = DEPRECATED_LIFECYCLE[name]
20+
}
21+
}
22+
23+
root.find(j.ObjectProperty).forEach(renameDeprecatedLifecycle)
24+
root.find(j.ObjectMethod).forEach(renameDeprecatedLifecycle)
25+
root.find(j.ClassProperty).forEach(renameDeprecatedLifecycle)
26+
27+
return root.toSource({ lineTerminator: '\n' })
28+
}

0 commit comments

Comments
 (0)