|
| 1 | +import wrap from '../src/wrapAstTransformation' |
| 2 | +import type { ASTTransformation } from '../src/wrapAstTransformation' |
| 3 | +import type { ObjectProperty } from 'jscodeshift' |
| 4 | + |
| 5 | +export const transformAST: ASTTransformation = ({ j, root }) => { |
| 6 | + // find model option |
| 7 | + const modelCollection = root |
| 8 | + .find(j.ObjectProperty, node => node.key.name === 'model') |
| 9 | + .filter(path => path.parent.parent.node.type == 'ExportDefaultDeclaration') |
| 10 | + if (!modelCollection.length) return |
| 11 | + |
| 12 | + // find prop option which is in model |
| 13 | + const propPath = modelCollection.find(j.ObjectProperty, (node: ObjectProperty) => { |
| 14 | + // @ts-ignore |
| 15 | + return node.key.name === 'prop' |
| 16 | + }).get(0) |
| 17 | + |
| 18 | + // find event option which is in model |
| 19 | + const eventPath = modelCollection.find(j.ObjectProperty, (node: ObjectProperty) => { |
| 20 | + // @ts-ignore |
| 21 | + return node.key.name === 'event' |
| 22 | + }).get(0) |
| 23 | + |
| 24 | + const propName = propPath.node.value.value |
| 25 | + const propEvent = eventPath.node.value.value |
| 26 | + |
| 27 | + // find the props option |
| 28 | + const propsCollections = root |
| 29 | + .find(j.ObjectProperty, node => node.key.name === 'props') |
| 30 | + .filter(path => path.parent.parent.node.type === 'ExportDefaultDeclaration') |
| 31 | + if (!propsCollections.length) return |
| 32 | + |
| 33 | + // find the value which is in props |
| 34 | + const valueNode: ObjectProperty = propsCollections |
| 35 | + .find(j.ObjectProperty, node => node.key.name === 'value') |
| 36 | + .filter(path => path.parent.parent.node.key.name === 'props') |
| 37 | + .get(0) |
| 38 | + .node |
| 39 | + |
| 40 | + // replace the value with modelValue |
| 41 | + // @ts-ignore |
| 42 | + valueNode?.key.name = 'modelValue' |
| 43 | + |
| 44 | + // remove model option |
| 45 | + modelCollection.remove() |
| 46 | + |
| 47 | + // find the methods option |
| 48 | + const methodsCollections = root |
| 49 | + .find(j.ObjectProperty, node => node.key.name === 'methods') |
| 50 | + .filter(nodePath => nodePath.parent.parent.node.type === 'ExportDefaultDeclaration') |
| 51 | + |
| 52 | + const methodName = `${propEvent}${propName[0].toUpperCase().concat(propName.slice(1))}` |
| 53 | + const methodBodyNode = j(` |
| 54 | + export default { |
| 55 | + ${methodName} (${propName}){ |
| 56 | + this.$emit('update:modelValue', ${propName}) |
| 57 | + }}`).find(j.ObjectMethod).get(0).node |
| 58 | + |
| 59 | + if (!methodsCollections.length) { |
| 60 | + // method option dont exists ,push a method option |
| 61 | + propsCollections.get(0) |
| 62 | + .parent |
| 63 | + .value |
| 64 | + .properties |
| 65 | + .push(j.objectProperty(j.identifier('methods'), j.objectExpression([methodBodyNode]))) |
| 66 | + } else { |
| 67 | + // method option existed ,push method body |
| 68 | + methodsCollections.get(0) |
| 69 | + .node |
| 70 | + .value |
| 71 | + .properties |
| 72 | + .push(methodBodyNode) |
| 73 | + } |
| 74 | + |
| 75 | + // replace all this.emit(eventName , prop) with ${methodName}(prop) |
| 76 | + const callMethods = root.find(j.CallExpression, { |
| 77 | + callee: { |
| 78 | + type: 'MemberExpression', |
| 79 | + object: { |
| 80 | + type: 'ThisExpression' |
| 81 | + }, |
| 82 | + property: { |
| 83 | + type: 'Identifier', |
| 84 | + name: '$emit' |
| 85 | + } |
| 86 | + } |
| 87 | + }).filter(nodePath => { |
| 88 | + const methodArgs = nodePath.node.arguments |
| 89 | + return methodArgs.length === 2 |
| 90 | + && methodArgs[0].type === 'StringLiteral' |
| 91 | + && methodArgs[0].value === propEvent |
| 92 | + && methodArgs[1].type === 'Identifier' |
| 93 | + }) |
| 94 | + |
| 95 | + if (callMethods.length) { |
| 96 | + // get the second param name |
| 97 | + callMethods.forEach(nodePath => { |
| 98 | + // @ts-ignore |
| 99 | + const paramName = nodePath.node.arguments[1].name |
| 100 | + nodePath.parentPath.replace(j.expressionStatement(j.callExpression(j.identifier(methodName), [j.identifier(paramName)]))) |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +export default wrap(transformAST) |
| 106 | +export const parser = 'babylon' |
0 commit comments