Skip to content

Commit ad1ce11

Browse files
committed
feat: implement global-filter transformation
1 parent 45c4a22 commit ad1ce11

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { defineInlineTest } from 'jscodeshift/src/testUtils'
2+
const transform = require('../global-filter')
3+
4+
defineInlineTest(
5+
transform,
6+
{},
7+
`const app = Vue.createApp(App)
8+
Vue.filter('capitalize', function(value) {
9+
return value
10+
})`,
11+
`const app = Vue.createApp(App)
12+
app.config.globalProperties.$filters = {
13+
capitalize(value) {
14+
return value
15+
}
16+
};`,
17+
'transform global filter'
18+
)
19+
20+
defineInlineTest(
21+
transform,
22+
{},
23+
`const app = new Vue(App)
24+
Vue.filter('capitalize', function(value) {
25+
return value
26+
})`,
27+
`const app = new Vue(App)
28+
Vue.filter('capitalize', function(value) {
29+
return value
30+
})
31+
`,
32+
'transform global filter(no effect and will warn)'
33+
)
34+
35+
36+
37+

transformations/global-filter.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import wrap from '../src/wrapAstTransformation'
2+
import type { ASTTransformation } from '../src/wrapAstTransformation'
3+
4+
export const transformAST: ASTTransformation = ({ root, j }) => {
5+
// find the createApp()
6+
const appDeclare = root.find(j.VariableDeclarator, {
7+
id: { type: 'Identifier' },
8+
init: {
9+
type: 'CallExpression',
10+
callee: {
11+
object: {
12+
name: 'Vue'
13+
},
14+
property: {
15+
name: 'createApp'
16+
}
17+
}
18+
}
19+
})
20+
21+
if (!appDeclare.length) {
22+
//dont transform new Vue(...) => Vue.createApp(...)?
23+
const newVue = root.find(j.NewExpression, {
24+
callee: {
25+
type: 'Identifier',
26+
name: 'Vue'
27+
}
28+
})
29+
30+
// need to transform global-filter first
31+
if (newVue.length) {
32+
console.warn('please transform new-global-api before transform global-filter!')
33+
}
34+
return
35+
}
36+
const appName = appDeclare.at(0).get().node.id.name
37+
38+
// Vue.filter('filterName', function(value) {}) =>
39+
// app.config.globalProperties.$filters = { filterName(value) {} }
40+
const filters = root.find(j.ExpressionStatement, {
41+
expression: {
42+
type: 'CallExpression',
43+
callee: {
44+
type: 'MemberExpression',
45+
object: { type: 'Identifier', name: 'Vue' },
46+
property: { type: 'Identifier', name: 'filter' }
47+
}
48+
}
49+
})
50+
if (!filters.length) {
51+
return
52+
}
53+
54+
const methods = []
55+
for (let i = 0; i < filters.length; i++) {
56+
const filter = filters.at(i)
57+
const args = filter.get().node.expression.arguments
58+
59+
methods.push(
60+
j.objectMethod(
61+
'method',
62+
j.identifier(args[0].value),
63+
args[1].params,
64+
args[1].body
65+
)
66+
)
67+
}
68+
69+
filters
70+
.at(0)
71+
.insertBefore(
72+
j.expressionStatement(
73+
j.assignmentExpression(
74+
'=',
75+
j.memberExpression(
76+
j.identifier(appName),
77+
j.identifier('config.globalProperties.$filters'),
78+
false
79+
),
80+
j.objectExpression(methods)
81+
)
82+
)
83+
)
84+
85+
for (let i = 0; i < filters.length; i++) {
86+
filters.at(i).remove()
87+
}
88+
}
89+
90+
export default wrap(transformAST)
91+
export const parser = 'babylon'

transformations/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const transformationMap: {
1919
'remove-vue-set-and-delete': require('./remove-vue-set-and-delete'),
2020
'rename-lifecycle': require('./rename-lifecycle'),
2121
'add-emit-declaration': require('./add-emit-declaration'),
22+
'global-filter': require('./global-filter'),
2223

2324
// atomic ones
2425
'remove-contextual-h-from-render': require('./remove-contextual-h-from-render'),

0 commit comments

Comments
 (0)