Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: implement transform global filter#20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ygj6 wants to merge1 commit intovuejs:main
base:main
Choose a base branch
Loading
fromoriginjs:global-filter
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletionstransformations/__tests__/global-filter.spec.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
import { defineInlineTest } from 'jscodeshift/src/testUtils'
const transform = require('../global-filter')

defineInlineTest(
transform,
{},
`const app = Vue.createApp(App)
Vue.filter('capitalize', function(value) {
return value
})`,
`const app = Vue.createApp(App)
app.config.globalProperties.$filters = {
capitalize(value) {
return value
}
};`,
'transform global filter'
)

defineInlineTest(
transform,
{},
`const app = new Vue(App)
Vue.filter('capitalize', function(value) {
return value
})`,
`const app = new Vue(App)
Vue.filter('capitalize', function(value) {
return value
})
`,
'transform global filter(no effect and will warn)'
)




91 changes: 91 additions & 0 deletionstransformations/global-filter.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
import wrap from '../src/wrapAstTransformation'
import type { ASTTransformation } from '../src/wrapAstTransformation'

export const transformAST: ASTTransformation = ({ root, j }) => {
// find the createApp()
const appDeclare = root.find(j.VariableDeclarator, {
id: { type: 'Identifier' },
init: {
type: 'CallExpression',
callee: {
object: {
name: 'Vue'
},
property: {
name: 'createApp'
}
}
}
})

if (!appDeclare.length) {
//dont transform new Vue(...) => Vue.createApp(...)?
const newVue = root.find(j.NewExpression, {
callee: {
type: 'Identifier',
name: 'Vue'
}
})

// need to transform global-filter first
if (newVue.length) {
console.warn('please transform new-global-api before transform global-filter!')
}
return
}
const appName = appDeclare.at(0).get().node.id.name

// Vue.filter('filterName', function(value) {}) =>
// app.config.globalProperties.$filters = { filterName(value) {} }
const filters = root.find(j.ExpressionStatement, {
expression: {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: { type: 'Identifier', name: 'Vue' },
property: { type: 'Identifier', name: 'filter' }
}
}
})
if (!filters.length) {
return
}

const methods = []
for (let i = 0; i < filters.length; i++) {
const filter = filters.at(i)
const args = filter.get().node.expression.arguments

methods.push(
j.objectMethod(
'method',
j.identifier(args[0].value),
args[1].params,
args[1].body
)
)
}

filters
.at(0)
.insertBefore(
j.expressionStatement(
j.assignmentExpression(
'=',
j.memberExpression(
j.identifier(appName),
j.identifier('config.globalProperties.$filters'),
false
),
j.objectExpression(methods)
)
)
)

for (let i = 0; i < filters.length; i++) {
filters.at(i).remove()
}
}

export default wrap(transformAST)
export const parser = 'babylon'
1 change: 1 addition & 0 deletionstransformations/index.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -17,6 +17,7 @@ const transformationMap: {
'scoped-slots-to-slots': require('./scoped-slots-to-slots'),
'new-directive-api': require('./new-directive-api'),
'remove-vue-set-and-delete': require('./remove-vue-set-and-delete'),
'global-filter': require('./global-filter'),

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

[8]ページ先頭

©2009-2025 Movatter.jp