Skip to content

Commit

Permalink
refactor: don't generate additional virtual code for defineExpose
Browse files Browse the repository at this point in the history
related to #3581
  • Loading branch information
johnsoncodehk committed Sep 25, 2023
1 parent f88bd7b commit c7b9589
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 57 deletions.
43 changes: 21 additions & 22 deletions packages/vue-language-core/src/generators/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function generate(
defineProps: undefined,
defineSlots: undefined,
defineEmits: undefined,
defineExpose: undefined,
slotsAssignName: undefined,
propsAssignName: undefined,
propsRuntimeArg: undefined,
Expand Down Expand Up @@ -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`);
Expand Down Expand Up @@ -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<typeof __VLS_template>,\n');
codes.push('emit: typeof __VLS_emit');
Expand Down Expand Up @@ -449,25 +439,36 @@ declare function defineProp<T>(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);
}
}
}
Expand Down Expand Up @@ -601,9 +602,7 @@ declare function defineProp<T>(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`);
Expand Down
59 changes: 24 additions & 35 deletions packages/vue-language-core/src/parsers/scriptSetupRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,6 +67,7 @@ export function parseScriptSetupRanges(
defineProps,
defineSlots,
defineEmits,
defineExpose,
propsAssignName,
propsRuntimeArg,
propsTypeArg,
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit c7b9589

Please sign in to comment.