- Notifications
You must be signed in to change notification settings - Fork54
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:mainChoose a base branch fromoriginjs:global-filter
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletionstransformations/__tests__/global-filter.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.