diff --git a/packages/vue-language-core/src/generators/script.ts b/packages/vue-language-core/src/generators/script.ts index 425706102d..6c358875e5 100644 --- a/packages/vue-language-core/src/generators/script.ts +++ b/packages/vue-language-core/src/generators/script.ts @@ -55,6 +55,7 @@ export function generate( defineProps: undefined, defineSlots: undefined, defineEmits: undefined, + defineExpose: undefined, slotsAssignName: undefined, propsAssignName: undefined, propsRuntimeArg: undefined, @@ -299,17 +300,6 @@ export function generate( codes.push('__VLS_setup = (async () => {\n'); scriptSetupGeneratedOffset = generateSetupFunction(true, 'none', definePropMirrors); - //#region exposed - codes.push(`const __VLS_exposed = `); - if (scriptSetupRanges.exposeRuntimeArg) { - addVirtualCode('scriptSetup', scriptSetupRanges.exposeRuntimeArg.start, scriptSetupRanges.exposeRuntimeArg.end); - } - else { - codes.push(`{}`); - } - codes.push(';\n'); - //#endregion - //#region props if (scriptSetupRanges.defineProp.length) { codes.push(`const __VLS_defaults = {\n`); @@ -370,7 +360,7 @@ export function generate( codes.push('return {} as {\n'); codes.push(`props: typeof __VLS_props,\n`); - codes.push('expose(exposed: typeof __VLS_exposed): void,\n'); + codes.push(`expose(exposed: ${scriptSetupRanges.exposeRuntimeArg ? 'typeof __VLS_exposed' : '{}'}): void,\n`); codes.push('attrs: any,\n'); codes.push('slots: ReturnType,\n'); codes.push('emit: typeof __VLS_emit'); @@ -449,25 +439,36 @@ declare function defineProp(value?: T | (() => T), required?: boolean, rest?: const scriptSetupGeneratedOffset = muggle.getLength(codes) - scriptSetupRanges.importSectionEndOffset; - let setupCodeModifies: [string, number][] = []; + let setupCodeModifies: [() => void, number, number][] = []; if (scriptSetupRanges.defineSlots && !scriptSetupRanges.slotsAssignName) { - setupCodeModifies.push([`const __VLS_slots = `, scriptSetupRanges.defineSlots.start]); + setupCodeModifies.push([() => codes.push(`const __VLS_slots = `), scriptSetupRanges.defineSlots.start, scriptSetupRanges.defineSlots.start]); } if (scriptSetupRanges.defineEmits && !scriptSetupRanges.emitsAssignName) { - setupCodeModifies.push([`const __VLS_emit = `, scriptSetupRanges.defineEmits.start]); + setupCodeModifies.push([() => codes.push(`const __VLS_emit = `), scriptSetupRanges.defineEmits.start, scriptSetupRanges.defineEmits.start]); + } + if (scriptSetupRanges.defineExpose && scriptSetupRanges.exposeRuntimeArg) { + setupCodeModifies.push([() => { + codes.push(`const __VLS_exposed = `); + addVirtualCode('scriptSetup', scriptSetupRanges!.exposeRuntimeArg!.start, scriptSetupRanges!.exposeRuntimeArg!.end); + codes.push(`;`); + addVirtualCode('scriptSetup', scriptSetupRanges!.defineExpose!.start, scriptSetupRanges!.exposeRuntimeArg!.start); + codes.push(`__VLS_exposed`); + addVirtualCode('scriptSetup', scriptSetupRanges!.exposeRuntimeArg!.end, scriptSetupRanges!.defineExpose!.end); + }, scriptSetupRanges.defineExpose.start, scriptSetupRanges.defineExpose.end]); } setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]); if (setupCodeModifies.length) { addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset, setupCodeModifies[0][1]); while (setupCodeModifies.length) { - const [code, start] = setupCodeModifies.shift()!; - codes.push(code); + const [generate, _, end] = setupCodeModifies.shift()!; + generate(); if (setupCodeModifies.length) { - addVirtualCode('scriptSetup', start, setupCodeModifies[0][1]); + const nextStart = setupCodeModifies[0][1]; + addVirtualCode('scriptSetup', end, nextStart); } else { - addVirtualCode('scriptSetup', start); + addVirtualCode('scriptSetup', end); } } } @@ -601,9 +602,7 @@ declare function defineProp(value?: T | (() => T), required?: boolean, rest?: } if (scriptSetupRanges.exposeRuntimeArg) { - codes.push(`...(`); - addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.exposeRuntimeArg.start, scriptSetupRanges.exposeRuntimeArg.end); - codes.push(`),\n`); + codes.push(`...__VLS_exposed,\n`); } codes.push(`};\n`); diff --git a/packages/vue-language-core/src/parsers/scriptSetupRanges.ts b/packages/vue-language-core/src/parsers/scriptSetupRanges.ts index 500b1a2d6f..2dcd81f65b 100644 --- a/packages/vue-language-core/src/parsers/scriptSetupRanges.ts +++ b/packages/vue-language-core/src/parsers/scriptSetupRanges.ts @@ -18,6 +18,7 @@ export function parseScriptSetupRanges( let propsTypeArg: TextRange | undefined; let defineSlots: TextRange | undefined; let defineEmits: TextRange | undefined; + let defineExpose: TextRange | undefined; let slotsAssignName: string | undefined; let emitsAssignName: string | undefined; let exposeRuntimeArg: TextRange | undefined; @@ -66,6 +67,7 @@ export function parseScriptSetupRanges( defineProps, defineSlots, defineEmits, + defineExpose, propsAssignName, propsRuntimeArg, propsTypeArg, @@ -159,47 +161,34 @@ export function parseScriptSetupRanges( }); } } - if ( - vueCompilerOptions.macros.defineProps.includes(callText) - || vueCompilerOptions.macros.defineSlots.includes(callText) - || vueCompilerOptions.macros.defineEmits.includes(callText) - || vueCompilerOptions.macros.defineExpose.includes(callText) - ) { - if (vueCompilerOptions.macros.defineProps.includes(callText)) { - defineProps = _getStartEnd(node); + else if (vueCompilerOptions.macros.defineSlots.includes(callText)) { + defineSlots = _getStartEnd(node); + if (ts.isVariableDeclaration(parent)) { + slotsAssignName = parent.name.getText(ast); } - if (vueCompilerOptions.macros.defineSlots.includes(callText)) { - defineSlots = _getStartEnd(node); - if (ts.isVariableDeclaration(parent)) { - slotsAssignName = parent.name.getText(ast); - } + } + else if (vueCompilerOptions.macros.defineEmits.includes(callText)) { + defineEmits = _getStartEnd(node); + if (ts.isVariableDeclaration(parent)) { + emitsAssignName = parent.name.getText(ast); } - if (vueCompilerOptions.macros.defineEmits.includes(callText)) { - defineEmits = _getStartEnd(node); - if (ts.isVariableDeclaration(parent)) { - emitsAssignName = parent.name.getText(ast); - } + } + else if (vueCompilerOptions.macros.defineExpose.includes(callText)) { + defineExpose = _getStartEnd(node); + if (node.arguments.length) { + exposeRuntimeArg = _getStartEnd(node.arguments[0]); + } + } + else if (vueCompilerOptions.macros.defineProps.includes(callText)) { + defineProps = _getStartEnd(node); + if (ts.isVariableDeclaration(parent)) { + propsAssignName = parent.name.getText(ast); } if (node.arguments.length) { - const runtimeArg = node.arguments[0]; - if (vueCompilerOptions.macros.defineProps.includes(callText)) { - propsRuntimeArg = _getStartEnd(runtimeArg); - if (ts.isVariableDeclaration(parent)) { - propsAssignName = parent.name.getText(ast); - } - } - else if (vueCompilerOptions.macros.defineExpose.includes(callText)) { - exposeRuntimeArg = _getStartEnd(runtimeArg); - } + propsRuntimeArg = _getStartEnd(node.arguments[0]); } if (node.typeArguments?.length) { - const typeArg = node.typeArguments[0]; - if (vueCompilerOptions.macros.defineProps.includes(callText)) { - propsTypeArg = _getStartEnd(typeArg); - if (ts.isVariableDeclaration(parent)) { - propsAssignName = parent.name.getText(ast); - } - } + propsTypeArg = _getStartEnd(node.typeArguments[0]); } } else if (vueCompilerOptions.macros.withDefaults.includes(callText)) {