Skip to content

Commit eefffa6

Browse files
committed
feat: implement rename lifecycle function
1 parent bed4a49 commit eefffa6

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { defineInlineTest } from 'jscodeshift/src/testUtils'
2+
3+
const renameLifeCycle = require('../rename-lifecycle')
4+
5+
defineInlineTest(
6+
renameLifeCycle,
7+
{},
8+
`export default {
9+
destroyed: function () {
10+
console.log('foo')
11+
},
12+
beforeDestroy: function () {
13+
console.log('bar')
14+
},
15+
methods: {
16+
destroyed: function() {},
17+
beforeDestroy: function() {}
18+
}
19+
}
20+
`,
21+
`export default {
22+
unmounted: function () {
23+
console.log('foo')
24+
},
25+
beforeUnmount: function () {
26+
console.log('bar')
27+
},
28+
methods: {
29+
destroyed: function() {},
30+
beforeDestroy: function() {}
31+
}
32+
}`)

transformations/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const transformationMap: {
1717
'scoped-slots-to-slots': require('./scoped-slots-to-slots'),
1818
'new-directive-api': require('./new-directive-api'),
1919
'remove-vue-set-and-delete': require('./remove-vue-set-and-delete'),
20+
'rename-lifecycle': require('./rename-lifecycle'),
2021

2122
// atomic ones
2223
'remove-contextual-h-from-render': require('./remove-contextual-h-from-render'),

transformations/rename-lifecycle.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import wrap from '../src/wrapAstTransformation'
2+
import type { ASTTransformation } from '../src/wrapAstTransformation'
3+
4+
const lifecycleMap: { [key: string]: string } = {
5+
'destroyed': 'unmounted',
6+
'beforeDestroy': 'beforeUnmount'
7+
}
8+
9+
export const transformAST: ASTTransformation = ({ root, j }) => {
10+
const methodArray: any [] = [j.ObjectProperty, j.ObjectMethod, j.ClassProperty]
11+
methodArray.forEach(method => {
12+
const methods = root.find(method)
13+
if (methods.length) {
14+
methods.forEach((path) => {
15+
// @ts-ignore
16+
const beforeReplaceName = path.node.key.name
17+
const afterReplaceName = lifecycleMap[beforeReplaceName]
18+
if (afterReplaceName && path.parent?.parent?.value?.type === 'ExportDefaultDeclaration') {
19+
// @ts-ignore
20+
path.value.key.name = afterReplaceName
21+
}
22+
})
23+
}
24+
})
25+
}
26+
27+
export default wrap(transformAST)
28+
export const parser = 'babylon'

0 commit comments

Comments
 (0)