Skip to content

Commit

Permalink
refactor: don't generate additional virtual code for defineSlots (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk authored Sep 21, 2023
1 parent 6b2fcd5 commit 8006c98
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 43 deletions.
37 changes: 16 additions & 21 deletions packages/vue-language-core/src/generators/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ export function generate(
leadingCommentEndOffset: 0,
importSectionEndOffset: 0,
defineProps: undefined,
defineSlots: undefined,
slotsAssignName: undefined,
propsAssignName: undefined,
propsRuntimeArg: undefined,
propsTypeArg: undefined,
slotsTypeArg: undefined,
withDefaultsArg: undefined,
defineProp: [],
};
Expand Down Expand Up @@ -383,11 +384,9 @@ export function generate(
}
codes.push(`}`);
}
if (scriptSetupRanges.slotsTypeArg && vueCompilerOptions.jsxSlots) {
if (scriptSetupRanges.defineSlots && vueCompilerOptions.jsxSlots) {
usedHelperTypes.PropsChildren = true;
codes.push(` & __VLS_PropsChildren<`);
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.slotsTypeArg.start, scriptSetupRanges.slotsTypeArg.end);
codes.push('>');
codes.push(` & __VLS_PropsChildren<typeof __VLS_slots>`);
}
codes.push(`;\n`);
//#endregion
Expand Down Expand Up @@ -490,7 +489,14 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:

const scriptSetupGeneratedOffset = muggle.getLength(codes) - scriptSetupRanges.importSectionEndOffset;

addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset);
if (scriptSetupRanges.defineSlots && !scriptSetupRanges.slotsAssignName) {
addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.defineSlots.start);
codes.push(`const __VLS_slots = `);
addVirtualCode('scriptSetup', scriptSetupRanges.defineSlots.start);
}
else {
addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset);
}

if (scriptSetupRanges.propsTypeArg && scriptSetupRanges.withDefaultsArg) {
// fix https://github.com/vuejs/language-tools/issues/1187
Expand Down Expand Up @@ -655,7 +661,7 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
codes.push('export default ');
}
if (mode === 'return' || mode === 'export') {
if (!vueCompilerOptions.skipTemplateCodegen && (htmlGen?.hasSlot || scriptSetupRanges?.slotsTypeArg)) {
if (!vueCompilerOptions.skipTemplateCodegen && (htmlGen?.hasSlot || scriptSetupRanges?.defineSlots)) {
usedHelperTypes.WithTemplateSlots = true;
codes.push(`{} as __VLS_WithTemplateSlots<typeof __VLS_publicComponent, ReturnType<typeof __VLS_template>>;`);
}
Expand All @@ -678,12 +684,6 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
generateExportOptions();
generateConstNameOption();

if (scriptSetupRanges?.slotsTypeArg && sfc.scriptSetup) {
codes.push(`var __VLS_slots!: `);
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.slotsTypeArg.start, scriptSetupRanges.slotsTypeArg.end);
codes.push(';\n');
};

codes.push(`function __VLS_template() {\n`);

const templateGened = generateTemplateContext();
Expand Down Expand Up @@ -837,7 +837,7 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
/* Components */
codes.push('/* Components */\n');
codes.push(`let __VLS_otherComponents!: NonNullable<typeof __VLS_internalComponent extends { components: infer C } ? C : {}> & typeof __VLS_componentsOption;\n`);
codes.push(`let __VLS_own!: __VLS_SelfComponent<typeof __VLS_name, typeof __VLS_internalComponent & typeof __VLS_publicComponent & (new () => { ${getSlotsPropertyName(vueCompilerOptions.target)}: typeof __VLS_slots })>;\n`);
codes.push(`let __VLS_own!: __VLS_SelfComponent<typeof __VLS_name, typeof __VLS_internalComponent & typeof __VLS_publicComponent & (new () => { ${getSlotsPropertyName(vueCompilerOptions.target)}: typeof ${scriptSetupRanges?.slotsAssignName ?? '__VLS_slots'} })>;\n`);
codes.push(`let __VLS_localComponents!: typeof __VLS_otherComponents & Omit<typeof __VLS_own, keyof typeof __VLS_otherComponents>;\n`);
codes.push(`let __VLS_components!: typeof __VLS_localComponents & __VLS_GlobalComponents & typeof __VLS_ctx;\n`); // for html completion, TS references...

Expand Down Expand Up @@ -878,17 +878,12 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:

if (!htmlGen) {
codes.push(`// no template\n`);
if (scriptSetupRanges?.slotsTypeArg && sfc.scriptSetup) {
codes.push(`let __VLS_slots!: `);
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.slotsTypeArg.start, scriptSetupRanges.slotsTypeArg.end);
codes.push(`;\n`);
}
else {
if (!scriptSetupRanges?.defineSlots) {
codes.push(`const __VLS_slots = {};\n`);
}
}

codes.push(`return __VLS_slots;\n`);
codes.push(`return ${scriptSetupRanges?.slotsAssignName ?? '__VLS_slots'};\n`);

return { cssIds };

Expand Down
3 changes: 2 additions & 1 deletion packages/vue-language-core/src/generators/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export function generate(
sourceLang: string,
sfc: Sfc,
hasScriptSetupSlots: boolean,
slotsAssignName: string | undefined,
codegenStack: boolean,
) {

Expand Down Expand Up @@ -1644,7 +1645,7 @@ export function generate(
codes.push(
'__VLS_normalizeSlot(',
['', 'template', node.loc.start.offset, capabilitiesPresets.diagnosticOnly],
'__VLS_slots[',
`${slotsAssignName ?? '__VLS_slots'}[`,
['', 'template', node.loc.start.offset, capabilitiesPresets.diagnosticOnly],
slotNameExpNode?.content ?? `('${getSlotName()}' as const)`,
['', 'template', node.loc.end.offset, capabilitiesPresets.diagnosticOnly],
Expand Down
17 changes: 11 additions & 6 deletions packages/vue-language-core/src/parsers/scriptSetupRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export function parseScriptSetupRanges(
let defineProps: TextRange | undefined;
let propsRuntimeArg: TextRange | undefined;
let propsTypeArg: TextRange | undefined;
let slotsTypeArg: TextRange | undefined;
let defineSlots: TextRange | undefined;
let slotsAssignName: string | undefined;
let emitsAssignName: string | undefined;
let emitsRuntimeArg: TextRange | undefined;
let emitsTypeArg: TextRange | undefined;
Expand Down Expand Up @@ -65,10 +66,11 @@ export function parseScriptSetupRanges(
bindings,
withDefaultsArg,
defineProps,
defineSlots,
propsAssignName,
propsRuntimeArg,
propsTypeArg,
slotsTypeArg,
slotsAssignName,
emitsAssignName,
emitsRuntimeArg,
emitsTypeArg,
Expand Down Expand Up @@ -170,6 +172,12 @@ export function parseScriptSetupRanges(
if (vueCompilerOptions.macros.defineProps.includes(callText)) {
defineProps = _getStartEnd(node);
}
if (vueCompilerOptions.macros.defineSlots.includes(callText)) {
defineSlots = _getStartEnd(node);
if (ts.isVariableDeclaration(parent)) {
slotsAssignName = parent.name.getText(ast);
}
}
if (node.arguments.length) {
const runtimeArg = node.arguments[0];
if (vueCompilerOptions.macros.defineProps.includes(callText)) {
Expand All @@ -196,10 +204,7 @@ export function parseScriptSetupRanges(
propsAssignName = parent.name.getText(ast);
}
}
if (vueCompilerOptions.macros.defineSlots.includes(callText)) {
slotsTypeArg = _getStartEnd(typeArg);
}
else if (vueCompilerOptions.macros.defineEmits.includes(callText)) {
if (vueCompilerOptions.macros.defineEmits.includes(callText)) {
emitsTypeArg = _getStartEnd(typeArg);
if (ts.isTypeLiteralNode(typeArg)) {
emitsTypeNums = typeArg.members.length;
Expand Down
11 changes: 9 additions & 2 deletions packages/vue-language-core/src/plugins/vue-tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,19 @@ function createTsx(fileName: string, _sfc: Sfc, { vueCompilerOptions, compilerOp
_sfc.template?.lang ?? 'html',
_sfc,
hasScriptSetupSlots.value,
slotsAssignName.value,
codegenStack,
);
});
const hasScriptSetupSlots = ref(false); // remove when https://github.com/vuejs/core/pull/5912 merged

//#region remove when https://github.com/vuejs/core/pull/5912 merged
const hasScriptSetupSlots = ref(false);
const slotsAssignName = ref<string>();
//#endregion

const tsxGen = computed(() => {
hasScriptSetupSlots.value = !!scriptSetupRanges.value?.slotsTypeArg;
hasScriptSetupSlots.value = !!scriptSetupRanges.value?.defineSlots;
slotsAssignName.value = scriptSetupRanges.value?.slotsAssignName;
return genScript(
ts,
fileName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const ScriptSetupExact = defineComponent({
// https://vuejs.org/api/sfc-script-setup.html#defineexpose
const ScriptSetupExposeExact = defineComponent({
setup() {
const a = 1
const b = ref(2)
const a = 1;
const b = ref(2);
return {
a,
b
Expand Down Expand Up @@ -67,13 +67,13 @@ declare const ScriptSetupGenericExact: <T, >(
_ctx?: Pick<NonNullable<Awaited<typeof _setup>>, 'attrs' | 'emit' | 'slots'>,
_expose?: NonNullable<Awaited<typeof _setup>>['expose'],
_setup?: Promise<{
props: { foo: T } & { [K in keyof JSX.ElementChildrenAttribute]?: { default?(data: T): any } },
props: { foo: T; } & { [K in keyof JSX.ElementChildrenAttribute]?: Readonly<{ default?(data: T): any; }> },
attrs: any,
slots: { default?(data: T): any },
emit: { (e: 'bar', data: T): void },
expose(_exposed: { baz: T }): void,
slots: Readonly<{ default?(data: T): any; }>,
emit: { (e: 'bar', data: T): void; },
expose(_exposed: { baz: T; }): void,
}>
) => import('vue').VNode & { __ctx?: Awaited<typeof _setup> };
) => import('vue').VNode & { __ctx?: Awaited<typeof _setup>; };
exactType(ScriptSetup, ScriptSetupExact);
exactType(ScriptSetupExpose, ScriptSetupExposeExact);
Expand Down
12 changes: 6 additions & 6 deletions packages/vue-tsc/tests/__snapshots__/dts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ exports[`vue-tsc-dts > Input: components/script-setup-generic.vue, Output: compo
baz: T;
}): void;
attrs: any;
slots: {
slots: Readonly<{
default?(data: T): any;
};
}>;
emit: (e: 'bar', data: T) => void;
}, \\"slots\\" | \\"attrs\\" | \\"emit\\">, __VLS_expose?: (exposed: {
baz: T;
Expand All @@ -78,9 +78,9 @@ exports[`vue-tsc-dts > Input: components/script-setup-generic.vue, Output: compo
baz: T;
}): void;
attrs: any;
slots: {
slots: Readonly<{
default?(data: T): any;
};
}>;
emit: (e: 'bar', data: T) => void;
}>) => import(\\"vue\\").VNode<import(\\"vue\\").RendererNode, import(\\"vue\\").RendererElement, {
[key: string]: any;
Expand All @@ -93,9 +93,9 @@ exports[`vue-tsc-dts > Input: components/script-setup-generic.vue, Output: compo
baz: T;
}): void;
attrs: any;
slots: {
slots: Readonly<{
default?(data: T): any;
};
}>;
emit: (e: 'bar', data: T) => void;
};
};
Expand Down

0 comments on commit 8006c98

Please sign in to comment.