|
| 1 | +/** |
| 2 | + * @param {Object} context |
| 3 | + * @param {import('jscodeshift').JSCodeshift} context.j |
| 4 | + * @param {ReturnType<import('jscodeshift').Core>} context.root |
| 5 | + */ |
| 6 | +module.exports = function createAppMount(context) { |
| 7 | + const { j, root } = context |
| 8 | + |
| 9 | + // find const appName = createApp(...) |
| 10 | + const appDeclare = root.find(j.VariableDeclarator, { |
| 11 | + id: { type: 'Identifier' }, |
| 12 | + init: { |
| 13 | + type: 'CallExpression', |
| 14 | + callee: { |
| 15 | + type: 'Identifier', |
| 16 | + name: 'createApp' |
| 17 | + } |
| 18 | + } |
| 19 | + }) |
| 20 | + if (!appDeclare.length) { |
| 21 | + return |
| 22 | + } |
| 23 | + const appName = appDeclare.at(0).get().node.id.name |
| 24 | + |
| 25 | + // Vue.filter('filterName', function(value) {}) => |
| 26 | + // app.config.globalProperties.$filters = { filterName(value) {} } |
| 27 | + const filters = root.find(j.ExpressionStatement, { |
| 28 | + expression: { |
| 29 | + type: 'CallExpression', |
| 30 | + callee: { |
| 31 | + type: 'MemberExpression', |
| 32 | + object: { type: 'Identifier', name: 'Vue' }, |
| 33 | + property: { type: 'Identifier', name: 'filter' } |
| 34 | + } |
| 35 | + } |
| 36 | + }) |
| 37 | + if (!filters.length) { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + const methods = [] |
| 42 | + for (let i = 0; i < filters.length; i++) { |
| 43 | + const filter = filters.at(i) |
| 44 | + const args = filter.get().node.expression.arguments |
| 45 | + |
| 46 | + methods.push( |
| 47 | + j.objectMethod( |
| 48 | + 'method', |
| 49 | + j.identifier(args[0].value), |
| 50 | + args[1].params, |
| 51 | + args[1].body |
| 52 | + ) |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + filters |
| 57 | + .at(0) |
| 58 | + .insertBefore( |
| 59 | + j.expressionStatement( |
| 60 | + j.assignmentExpression( |
| 61 | + '=', |
| 62 | + j.memberExpression( |
| 63 | + j.identifier(appName), |
| 64 | + j.identifier('config.globalProperties.$filters'), |
| 65 | + false |
| 66 | + ), |
| 67 | + j.objectExpression(methods) |
| 68 | + ) |
| 69 | + ) |
| 70 | + ) |
| 71 | + |
| 72 | + for (let i = 0; i < filters.length; i++) { |
| 73 | + filters.at(i).remove() |
| 74 | + } |
| 75 | +} |
0 commit comments