Skip to content

Commit 5803b5e

Browse files
committed
fix(macros): return DefineComponent type for defineComponent
1 parent 4fb5d61 commit 5803b5e

File tree

4 files changed

+50
-35
lines changed

4 files changed

+50
-35
lines changed
Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { TransformOptions } from '.'
2+
import type { Code } from 'ts-macro'
23

34
export function transformDefineComponent(
45
node: import('typescript').CallExpression,
@@ -9,17 +10,35 @@ export function transformDefineComponent(
910

1011
codes.replaceRange(node.arguments[0].end, node.end - 1)
1112

12-
const componentOptions = node.arguments[1]
1313
codes.replaceRange(
1414
node.getStart(ast),
15-
node.expression.end + 1,
15+
node.expression.end,
1616
ts.isExpressionStatement(parent) ? ';' : '',
17-
'(',
18-
[node.expression.getText(ast), node.getStart(ast)],
19-
'(() => ({}) as any,',
20-
componentOptions
21-
? [componentOptions.getText(ast), componentOptions.getStart(ast)]
22-
: '',
23-
'), ',
17+
`(() => {
18+
const __setup = `,
19+
)
20+
21+
const compOptions = node.arguments[1]
22+
codes.replaceRange(
23+
node.end,
24+
node.end,
25+
'\n return ',
26+
[node.expression.getText(ast), node.expression.getStart(ast)],
27+
`({
28+
__typeProps: {} as Parameters<typeof __setup>[0],
29+
setup:() => {},
30+
...{} as Parameters<typeof __setup>[1] extends { slots?: infer S, expose?: infer E } | undefined ? {
31+
setup: E extends (exposed: infer Exposed) => any ? () => Exposed : never,
32+
slots: S extends Record<string, any> ? import('vue').SlotsType<S> : never
33+
} : {},`,
34+
...(compOptions
35+
? ([
36+
'\n ...',
37+
[compOptions.getText(ast), compOptions.getStart(ast)],
38+
] as Code[])
39+
: []),
40+
`
41+
})
42+
})()`,
2443
)
2544
}

packages/macros/src/volar/global-types.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { HELPER_PREFIX } from '@vue-macros/common'
21
import type { RootMap, TransformOptions } from '.'
32

43
export function getGlobalTypes(
@@ -10,10 +9,10 @@ export function getGlobalTypes(
109
defineStyle = options.defineStyle.alias
1110
.map(
1211
(alias) =>
13-
`declare const ${alias}: { <T>(...args: ${HELPER_PREFIX}StyleArgs): T; scss: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; sass: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; stylus: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; less: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; postcss: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T };`,
12+
`declare const ${alias}: { <T>(...args: __StyleArgs): T; scss: <T>(...args: __StyleArgs)=> T; sass: <T>(...args: __StyleArgs)=> T; stylus: <T>(...args: __StyleArgs)=> T; less: <T>(...args: __StyleArgs)=> T; postcss: <T>(...args: __StyleArgs)=> T };`,
1413
)
1514
.join('\n')
16-
defineStyle += `\ntype ${HELPER_PREFIX}StyleArgs = [style: string, options?: { scoped?: boolean }];`
15+
defineStyle += `\ntype __StyleArgs = [style: string, options?: { scoped?: boolean }];`
1716
}
1817
if (!rootMap.size) {
1918
return `\n${defineStyle}`
@@ -47,7 +46,5 @@ ${VueMacros ? `declare const { ${VueMacros} }: typeof import('vue');` : ''}
4746
${defineSlots}
4847
${defineExpose}
4948
${defineStyle}
50-
// @ts-ignore
51-
type __VLS_IsAny<T> = 0 extends 1 & T ? true : false; type __VLS_PickNotAny<A, B> = __VLS_IsAny<A> extends true ? B : A;
5249
`
5350
}

packages/macros/src/volar/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { HELPER_PREFIX, type Overwrite } from '@vue-macros/common'
21
import { getText, type TsmVirtualCode } from 'ts-macro'
32
import type { OptionsResolved } from '../options'
43
import { transformDefineComponent } from './define-component'
4+
import type { Overwrite } from '@vue-macros/common'
55

66
export { transformJsxMacros } from './transform'
77
export { getGlobalTypes } from './global-types'
@@ -178,7 +178,7 @@ export function getRootMap(options: TransformOptions): RootMap {
178178
)
179179
}
180180

181-
const id = toValidAssetId(modelName, `${HELPER_PREFIX}model`)
181+
const id = toValidAssetId(modelName, `__model`)
182182
const typeString = `import('vue').UnwrapRef<typeof ${id}>`
183183
const defineModel = (rootMap.get(root)!.defineModel ??= [])
184184
defineModel.push(
@@ -200,17 +200,16 @@ export function getRootMap(options: TransformOptions): RootMap {
200200
codes.replaceRange(
201201
expression.getStart(ast),
202202
expression.getStart(ast),
203-
`// @ts-ignore\n${HELPER_PREFIX}slots;\nconst ${HELPER_PREFIX}slots = `,
203+
`// @ts-ignore\n__slots;\nconst __slots = `,
204204
)
205-
rootMap.get(root)!.defineSlots =
206-
`Partial<typeof ${HELPER_PREFIX}slots>`
205+
rootMap.get(root)!.defineSlots = `Partial<typeof __slots>`
207206
} else if (options.defineExpose.alias.includes(macroName)) {
208207
codes.replaceRange(
209208
expression.getStart(ast),
210209
expression.getStart(ast),
211-
`// @ts-ignore\n${HELPER_PREFIX}exposed;\nconst ${HELPER_PREFIX}exposed = `,
210+
`// @ts-ignore\n__exposed;\nconst __exposed = `,
212211
)
213-
rootMap.get(root)!.defineExpose = `typeof ${HELPER_PREFIX}exposed`
212+
rootMap.get(root)!.defineExpose = `typeof __exposed`
214213
}
215214
}
216215
}

packages/macros/src/volar/transform.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { HELPER_PREFIX } from '@vue-macros/common'
21
import { transformDefineStyle } from './define-style'
32
import type { RootMap, TransformOptions } from '.'
43

@@ -22,7 +21,7 @@ export function transformJsxMacros(
2221
)
2322
if (asyncModifier && macros.defineComponent)
2423
codes.replaceRange(asyncModifier.pos, asyncModifier.end)
25-
const result = `({}) as __VLS_PickNotAny<typeof ${HELPER_PREFIX}ctx.render, {}> & { __ctx: typeof ${HELPER_PREFIX}ctx }`
24+
const result = `__ctx.render`
2625

2726
const propsType = root.parameters[0]?.type
2827
? root.parameters[0].type.getText(ast)
@@ -31,12 +30,10 @@ export function transformJsxMacros(
3130
root.parameters.pos,
3231
root.parameters.pos,
3332
ts.isArrowFunction(root) && root.parameters.pos === root.pos ? '(' : '',
34-
`${HELPER_PREFIX}props: typeof ${HELPER_PREFIX}ctx.props & ${propsType}, `,
35-
`${HELPER_PREFIX}placeholder?: {}, `,
36-
`${HELPER_PREFIX}ctx = {} as Awaited<ReturnType<typeof ${
37-
HELPER_PREFIX
38-
}setup>>, `,
39-
`${HELPER_PREFIX}setup = (${asyncModifier ? 'async' : ''}(`,
33+
`__props: typeof __ctx.props & ${propsType}, `,
34+
`__context?: typeof __ctx.context, `,
35+
`__ctx = {} as Awaited<ReturnType<typeof __fn>>, `,
36+
`__fn = (${asyncModifier ? 'async' : ''}(`,
4037
)
4138
if (ts.isArrowFunction(root)) {
4239
codes.replaceRange(
@@ -84,19 +81,22 @@ export function transformJsxMacros(
8481
codes.replaceRange(
8582
node.getStart(ast),
8683
node.expression.getStart(ast),
87-
`const ${HELPER_PREFIX}render = `,
84+
`const __render = `,
8885
shouldWrapByCall ? '(' : '',
8986
)
9087
codes.replaceRange(
9188
node.expression.end,
9289
node.expression.end,
9390
shouldWrapByCall ? ')()' : '',
9491
`
95-
return {
96-
props: {} as {${props.join(', ')}},
97-
slots: {} as ${macros.defineSlots ?? '{}'},
98-
expose: (exposed: import('vue').ShallowUnwrapRef<${macros.defineExpose ?? '{}'}>) => {},
99-
render: ${HELPER_PREFIX}render,
92+
return {} as {
93+
props: {${props.join(', ')}},
94+
context: {
95+
slots?: ${macros.defineSlots ?? '{}'},
96+
expose?: (exposed: import('vue').ShallowUnwrapRef<${macros.defineExpose ?? '{}'}>) => void,
97+
attrs?: Record<string, any>
98+
},
99+
render: typeof __render
100100
}`,
101101
)
102102
}

0 commit comments

Comments
 (0)