Skip to content

Commit 49af33f

Browse files
committed
feat: adapt global filters
1 parent 267d977 commit 49af33f

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

Diff for: generator/codemods/global-api/global-filter.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}

Diff for: generator/codemods/global-api/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = function(fileInfo, api) {
1414
require('./next-tick')(context)
1515
require('./observable')(context)
1616
require('./version')(context)
17+
require('./global-filter')(context)
1718

1819
// remove extraneous imports
1920
const removeExtraneousImport = require('../utils/remove-extraneous-import')

0 commit comments

Comments
 (0)