-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathv-model.ts
106 lines (92 loc) · 3.35 KB
/
v-model.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import wrap from '../src/wrapAstTransformation'
import type { ASTTransformation } from '../src/wrapAstTransformation'
import type { ObjectProperty } from 'jscodeshift'
export const transformAST: ASTTransformation = ({ j, root }) => {
// find model option
const modelCollection = root
.find(j.ObjectProperty, node => node.key.name === 'model')
.filter(path => path.parent.parent.node.type == 'ExportDefaultDeclaration')
if (!modelCollection.length) return
// find prop option which is in model
const propPath = modelCollection.find(j.ObjectProperty, (node: ObjectProperty) => {
// @ts-ignore
return node.key.name === 'prop'
}).get(0)
// find event option which is in model
const eventPath = modelCollection.find(j.ObjectProperty, (node: ObjectProperty) => {
// @ts-ignore
return node.key.name === 'event'
}).get(0)
const propName = propPath.node.value.value
const propEvent = eventPath.node.value.value
// find the props option
const propsCollections = root
.find(j.ObjectProperty, node => node.key.name === 'props')
.filter(path => path.parent.parent.node.type === 'ExportDefaultDeclaration')
if (!propsCollections.length) return
// find the value which is in props
const valueNode: ObjectProperty = propsCollections
.find(j.ObjectProperty, node => node.key.name === 'value')
.filter(path => path.parent.parent.node.key.name === 'props')
.get(0)
.node
// replace the value with modelValue
// @ts-ignore
valueNode?.key.name = 'modelValue'
// remove model option
modelCollection.remove()
// find the methods option
const methodsCollections = root
.find(j.ObjectProperty, node => node.key.name === 'methods')
.filter(nodePath => nodePath.parent.parent.node.type === 'ExportDefaultDeclaration')
const methodName = `${propEvent}${propName[0].toUpperCase().concat(propName.slice(1))}`
const methodBodyNode = j(`
export default {
${methodName} (${propName}){
this.$emit('update:modelValue', ${propName})
}}`).find(j.ObjectMethod).get(0).node
if (!methodsCollections.length) {
// method option dont exists ,push a method option
propsCollections.get(0)
.parent
.value
.properties
.push(j.objectProperty(j.identifier('methods'), j.objectExpression([methodBodyNode])))
} else {
// method option existed ,push method body
methodsCollections.get(0)
.node
.value
.properties
.push(methodBodyNode)
}
// replace all this.emit(eventName , prop) with ${methodName}(prop)
const callMethods = root.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
object: {
type: 'ThisExpression'
},
property: {
type: 'Identifier',
name: '$emit'
}
}
}).filter(nodePath => {
const methodArgs = nodePath.node.arguments
return methodArgs.length === 2
&& methodArgs[0].type === 'StringLiteral'
&& methodArgs[0].value === propEvent
&& methodArgs[1].type === 'Identifier'
})
if (callMethods.length) {
// get the second param name
callMethods.forEach(nodePath => {
// @ts-ignore
const paramName = nodePath.node.arguments[1].name
nodePath.parentPath.replace(j.expressionStatement(j.callExpression(j.identifier(methodName), [j.identifier(paramName)])))
})
}
}
export default wrap(transformAST)
export const parser = 'babylon'