Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement render to resolveComponent #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions transformations/__tests__/render-to-resolveComponent.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineInlineTest } from 'jscodeshift/src/testUtils'
const transform = require('../render-to-resolveComponent')

defineInlineTest(
transform,
{},
`export default {
render(h){
return h('button-counter')
}
}`,
`
import { resolveComponent } from "vue";
export default {
render() {
const buttonCounter = resolveComponent('button-counter')
return buttonCounter;
}
}`,
'transform render-to-resolveComponent'
)
1 change: 1 addition & 0 deletions transformations/index.ts
Original file line number Diff line number Diff 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'),
'render-to-resolveComponent': require('./render-to-resolveComponent'),

// atomic ones
'remove-contextual-h-from-render': require('./remove-contextual-h-from-render'),
Expand Down
70 changes: 70 additions & 0 deletions transformations/render-to-resolveComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import wrap from '../src/wrapAstTransformation'
import type { ASTTransformation } from '../src/wrapAstTransformation'
import { transformAST as addImport } from './add-import'

export const transformAST: ASTTransformation = (context) => {

const { root, j } = context
// find render function
const renderCollections = root.find(j.ObjectMethod, node => {
return node.key.name === 'render'
&& node.params.length === 1
})
.filter(nodePath => nodePath.parent.parent.node.type === 'ExportDefaultDeclaration')
if (!renderCollections) return

// add import
addImport(context, {
specifier: { type: 'named', imported: 'resolveComponent' },
source: 'vue'
})

renderCollections.forEach(({ node }) => {
// @ts-ignore
const paramName = node.params[0].name
// remove render function param
node.params = []
const callExpressionCollection = j(node).find(j.CallExpression, node => {
return node.callee.name === paramName
&& node.arguments.length === 1
})

if (!callExpressionCollection.length) return
// find the component name
const componentName = callExpressionCollection.get(0).node.arguments[0].value
// remove non-letter for complying variable name rules
const componentVariableName = removeNonLetter(componentName)
callExpressionCollection.get(0).parent.insertBefore(j(`const ${componentVariableName} = resolveComponent('${componentName}')`).find(j.VariableDeclaration).get().node)
// replace h('xxx') with resolveComponent('xxx')
// @ts-ignore
callExpressionCollection.replaceWith(nodePath => nodePath.node.callee.name = componentVariableName)
})
}

/**
* remove non-letter and uppercase the first letter after non-letter
* button-component => buttonComponent
* @param str
*/
function removeNonLetter(str: string): string | undefined {

if (str) {
let returnValue: string = ''
for (let i = 0; i < str.length; i++) {
// letter
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
returnValue += str[i]
} else {
// non-letter , remove and uppercase the first letter after non-letter
i++
if (str[i] >= 'a' && str[i] <= 'z') {
returnValue += String.fromCharCode(str[i].charCodeAt(0) - 32)
}
}
}
return returnValue
}
}

export default wrap(transformAST)
export const parser = 'babylon'