-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathnew-directive-api.ts
68 lines (61 loc) · 1.8 KB
/
new-directive-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import wrap from '../src/wrapAstTransformation'
import type { ASTTransformation } from '../src/wrapAstTransformation'
const hookNameMap: { [key: string]: string } = {
bind: 'beforeMount',
inserted: 'mounted',
componentUpdated: 'updated',
unbind: 'unmounted',
}
export const transformAST: ASTTransformation = ({ root, j }) => {
const directiveRegistration = root.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
object: {
name: 'Vue',
},
property: {
name: 'directive',
},
},
})
directiveRegistration.forEach(({ node }) => {
if (
node.arguments.length === 2 &&
j.ObjectExpression.check(node.arguments[1])
) {
const directiveOptions = node.arguments[1]
let updateIndex = -1
directiveOptions.properties.forEach((prop, index) => {
if (
j.SpreadElement.check(prop) ||
j.SpreadProperty.check(prop) ||
!j.Identifier.check(prop.key)
) {
return
}
if (hookNameMap[prop.key.name]) {
prop.key.name = hookNameMap[prop.key.name]
}
if (prop.key.name === 'update') {
updateIndex = index
}
})
if (updateIndex !== -1) {
const nextProp =
directiveOptions.properties[updateIndex + 1] ||
// if `update` is the last property
directiveOptions.properties[updateIndex - 1]
nextProp.comments = nextProp.comments || []
nextProp.comments.push(
j.commentBlock(
` __REMOVED__: In Vue 3, there's no 'update' hook for directives `
)
)
directiveOptions.properties.splice(updateIndex, 1)
// TODO: should warn user in the console
}
}
})
}
export default wrap(transformAST)
export const parser = 'babylon'