Skip to content

feat(language-core): auto infer $el type #4805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: try to use __typeEl
  • Loading branch information
KazariEX committed Sep 4, 2024
commit 7162c28f28d995877013dd7ad21726402d1581d6
9 changes: 0 additions & 9 deletions packages/language-core/lib/codegen/localTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ type __VLS_WithTemplateSlots<T, S> = T & {
${vueCompilerOptions.jsxSlots ? `$props: ${PropsChildren.name}<S>;` : ''}
}
};
`.trimStart()
);
// TODO:
const WithTemplateRootEl = defineHelper(
`__VLS_WithTemplateRootEl`,
() => `
type __VLS_WithTemplateRootEl<T, E> = T;
`.trimStart()
);
const PropsChildren = defineHelper(
Expand Down Expand Up @@ -92,7 +85,6 @@ type __VLS_TypePropsToOption<T> = {
[OmitKeepDiscriminatedUnion.name]: OmitKeepDiscriminatedUnion,
[WithDefaults.name]: WithDefaults,
[WithTemplateSlots.name]: WithTemplateSlots,
[WithTemplateRootEl.name]: WithTemplateRootEl,
[PropsChildren.name]: PropsChildren,
[TypePropsToOption.name]: TypePropsToOption,
[OmitIndexSignature.name]: OmitIndexSignature,
Expand All @@ -108,7 +100,6 @@ type __VLS_TypePropsToOption<T> = {
get OmitKeepDiscriminatedUnion() { return OmitKeepDiscriminatedUnion.name; },
get WithDefaults() { return WithDefaults.name; },
get WithTemplateSlots() { return WithTemplateSlots.name; },
get WithTemplateRootEl() { return WithTemplateRootEl.name; },
get PropsChildren() { return PropsChildren.name; },
get TypePropsToOption() { return TypePropsToOption.name; },
get OmitIndexSignature() { return OmitIndexSignature.name; },
Expand Down
3 changes: 3 additions & 0 deletions packages/language-core/lib/codegen/script/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export function* generateComponent(
if (options.vueCompilerOptions.target >= 3.5 && scriptSetupRanges.templateRefs.length) {
yield `__typeRefs: {} as __VLS_TemplateResult['refs'],${newLine}`;
}
if (options.vueCompilerOptions.target >= 3.5 && options.templateCodegen?.singleRootEl) {
yield `__typeEl: {} as __VLS_TemplateResult['rootEl'],${newLine}`;
}
yield `})`;
}

Expand Down
26 changes: 11 additions & 15 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,22 +294,18 @@ function* generateSetupFunction(
yield `type __VLS_TemplateResult = ReturnType<typeof __VLS_template>${endOfLine}`;

if (syntax) {
yield `const __VLS_component = `;
yield* generateComponent(options, ctx, scriptSetup, scriptSetupRanges);
yield endOfLine;
yield `${syntax} {} as `;

let exp = 'typeof __VLS_component';
if (!options.vueCompilerOptions.skipTemplateCodegen) {
if (options.templateCodegen?.hasSlot || scriptSetupRanges?.slots.define) {
exp = `${ctx.localTypes.WithTemplateSlots}<${exp}, __VLS_TemplateResult['slots']>`;
}
if (options.templateCodegen?.singleRootEl) {
exp = `${ctx.localTypes.WithTemplateRootEl}<${exp}, __VLS_TemplateResult['rootEl']>`;
}
if (!options.vueCompilerOptions.skipTemplateCodegen && (options.templateCodegen?.hasSlot || scriptSetupRanges?.slots.define)) {
yield `const __VLS_component = `;
yield* generateComponent(options, ctx, scriptSetup, scriptSetupRanges);
yield endOfLine;
yield `${syntax} `;
yield `{} as ${ctx.localTypes.WithTemplateSlots}<typeof __VLS_component, __VLS_TemplateResult['slots']>${endOfLine}`;
}
else {
yield `${syntax} `;
yield* generateComponent(options, ctx, scriptSetup, scriptSetupRanges);
yield endOfLine;
}
yield exp;
yield endOfLine;
}
}

Expand Down
4 changes: 2 additions & 2 deletions test-workspace/tsc/passedFixtures/vue3/rootEl/child.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script setup lang="ts">
import { exactType } from '../../shared';
import Base from './base.vue';
import Base from './temp';
</script>

<template>
<Base>
{{ exactType($el, {} as HTMLAnchorElement | undefined) }}
{{ exactType($el, {} as HTMLAnchorElement) }}
</Base>
</template>
2 changes: 1 addition & 1 deletion test-workspace/tsc/passedFixtures/vue3/rootEl/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import Child from './child.vue';

<template>
<Child>
{{ exactType($el, {} as HTMLAnchorElement | undefined) }}
{{ exactType($el, {} as HTMLAnchorElement) }}
</Child>
</template>
Loading