Skip to content

Commit

Permalink
feat(volar/jsx-directive): support generic component (#673)
Browse files Browse the repository at this point in the history
* feat(volar/jsx-directive): support generic component

* chore: add changeset
  • Loading branch information
zhiyuanzmj committed May 4, 2024
1 parent b868726 commit 3227507
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-clouds-check.md
@@ -0,0 +1,5 @@
---
"@vue-macros/volar": patch
---

support generic component for jsx-direcitve
16 changes: 12 additions & 4 deletions packages/volar/src/jsx-directive/index.ts
Expand Up @@ -188,7 +188,7 @@ function transformCtx(
index: number,
options: TransformOptions,
) {
const { ts, codes } = options
const { ts, codes, sfc } = options

const tag = ts.isJsxSelfClosingElement(node)
? node
Expand Down Expand Up @@ -219,8 +219,16 @@ function transformCtx(
`,
)
}
codes.push(
`const ${ctxName} = __VLS_getFunctionalComponentCtx(${tagName}, __VLS_asFunctionalComponent(${tagName})({${props}}));\n`,
)

const code = `const ${ctxName} = __VLS_getFunctionalComponentCtx(${tagName}, __VLS_asFunctionalComponent(${tagName})({${props}}));\n`
if (sfc.scriptSetup?.generic) {
const index = codes.findIndex((code) =>
code.includes('__VLS_setup = (async () => {'),
)
codes.splice(index + 1, 0, code)
} else {
codes.push(code)
}

return ctxName
}
@@ -1,4 +1,4 @@
<script setup lang="tsx">
<script setup lang="tsx" generic="T">
import Child from './child.vue'
import type { FunctionalComponent } from 'vue'
Expand Down

0 comments on commit 3227507

Please sign in to comment.