Skip to content

Commit 0a5b315

Browse files
committed
fix(macros/volar): use __MACROS_ctx to infer type
1 parent bebec2b commit 0a5b315

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

packages/macros/src/volar/transform.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ export function transformJsxMacros(
1717
const asyncModifier = root.modifiers?.find(
1818
(modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword,
1919
)
20-
let setup = `typeof ${HELPER_PREFIX}setup`
21-
setup = asyncModifier ? `Awaited<${setup}>` : setup
2220
if (asyncModifier && map.defineComponent)
2321
replaceRange(codes, asyncModifier.pos, asyncModifier.end)
24-
const result = `({}) as __VLS_PickNotAny<${setup}['render'], {}> & { __ctx: ${setup} }`
22+
const result = `({}) as __VLS_PickNotAny<typeof ${HELPER_PREFIX}ctx.render, {}> & { __ctx: typeof ${HELPER_PREFIX}ctx }`
2523

2624
const propsType = root.parameters[0]?.type
2725
? String(root.parameters[0].type.getText(ast))
@@ -31,16 +29,19 @@ export function transformJsxMacros(
3129
root.parameters.pos,
3230
root.parameters.pos,
3331
ts.isArrowFunction(root) && root.parameters.pos === root.pos ? '(' : '',
34-
`${HELPER_PREFIX}props: ${setup}['props'] & ${propsType}, `,
32+
`${HELPER_PREFIX}props: typeof ${HELPER_PREFIX}ctx.props & ${propsType}, `,
3533
`${HELPER_PREFIX}placeholder?: {}, `,
34+
`${HELPER_PREFIX}ctx = {} as Awaited<ReturnType<typeof ${
35+
HELPER_PREFIX
36+
}setup>>, `,
3637
`${HELPER_PREFIX}setup = (${asyncModifier ? 'async' : ''}(`,
3738
)
3839
if (ts.isArrowFunction(root)) {
3940
replaceRange(
4041
codes,
4142
root.end,
4243
root.end,
43-
`)())${root.pos === root.parameters.pos ? ')' : ''} => `,
44+
`))${root.pos === root.parameters.pos ? ')' : ''} => `,
4445
result,
4546
)
4647
} else {
@@ -50,7 +51,7 @@ export function transformJsxMacros(
5051
root.body.getStart(ast),
5152
'=>',
5253
)
53-
replaceRange(codes, root.end, root.end, `)()){ return `, result, '}')
54+
replaceRange(codes, root.end, root.end, `)){ return `, result, '}')
5455
}
5556

5657
ts.forEachChild(root.body, (node) => {

0 commit comments

Comments
 (0)