Skip to content
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

fix(language-core): prevent the generation of generics in JS #4836

Merged
merged 12 commits into from
Oct 24, 2024
1 change: 1 addition & 0 deletions packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface ScriptCodegenOptions {
vueCompilerOptions: VueCompilerOptions;
sfc: Sfc;
lang: string;
isTs: boolean;
scriptRanges: ScriptRanges | undefined;
scriptSetupRanges: ScriptSetupRanges | undefined;
templateCodegen: TemplateCodegenContext & { codes: Code[]; } | undefined;
Expand Down
26 changes: 13 additions & 13 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function* generateSetupFunction(
]);
}
}
if (scriptSetupRanges.cssModules.length) {
if (options.isTs) {
for (const { exp, arg } of scriptSetupRanges.cssModules) {
if (arg) {
setupCodeModifies.push([
Expand All @@ -242,18 +242,18 @@ function* generateSetupFunction(
]);
}
}
}
for (const { define } of scriptSetupRanges.templateRefs) {
if (define?.arg) {
setupCodeModifies.push([
[
`<__VLS_TemplateResult['refs'][`,
generateSfcBlockSection(scriptSetup, define.arg.start, define.arg.end, codeFeatures.navigation),
`], keyof __VLS_TemplateResult['refs']>`
],
define.arg.start - 1,
define.arg.start - 1
]);
for (const { define } of scriptSetupRanges.templateRefs) {
if (define?.arg) {
setupCodeModifies.push([
[
`<__VLS_TemplateResult['refs'][`,
generateSfcBlockSection(scriptSetup, define.arg.start, define.arg.end, codeFeatures.navigation),
`], keyof __VLS_TemplateResult['refs']>`
],
define.arg.start - 1,
define.arg.start - 1
]);
}
}
}
setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]);
Expand Down
4 changes: 4 additions & 0 deletions packages/language-core/lib/plugins/vue-tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ function createTsx(
: _sfc.script && _sfc.script.lang !== 'js' ? _sfc.script.lang
: 'js';
});
const isTs = computed(() => {
return lang() !== 'js' && lang() !== 'jsx';
});
const scriptRanges = computed(() =>
_sfc.script
? parseScriptRanges(ts, _sfc.script.ast, !!_sfc.scriptSetup, false)
Expand Down Expand Up @@ -183,6 +186,7 @@ function createTsx(
fileBaseName: path.basename(fileName),
sfc: _sfc,
lang: lang(),
isTs: isTs(),
scriptRanges: scriptRanges(),
scriptSetupRanges: scriptSetupRanges(),
templateCodegen: generatedTemplate(),
Expand Down
1 change: 1 addition & 0 deletions test-workspace/tsc/passedFixtures/vue2/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"../vue3/#4649",
"../vue3/#4777",
"../vue3/#4820",
"../vue3/#4836",
"../vue3/components",
"../vue3/defineEmits",
"../vue3/defineModel",
Expand Down
1 change: 1 addition & 0 deletions test-workspace/tsc/passedFixtures/vue3.4/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"../vue3/#3820",
"../vue3/#4777",
"../vue3/#4820",
"../vue3/#4836",
"../vue3/rootEl",
"../vue3/templateRef",
"../vue3/templateRef_native",
Expand Down
10 changes: 10 additions & 0 deletions test-workspace/tsc/passedFixtures/vue3/#4836/child.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script setup>
import { useTemplateRef } from 'vue';

const a = useTemplateRef('a');
defineExpose({ a });
</script>

<template>
<a ref="a" />
</template>
12 changes: 12 additions & 0 deletions test-workspace/tsc/passedFixtures/vue3/#4836/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script setup lang="ts">
import { exactType } from 'tsc/passedFixtures/shared';
import Child from './child.vue';
import { useTemplateRef } from 'vue';

const child = useTemplateRef('child');
exactType(child.value?.a, {} as unknown);
</script>

<template>
<Child ref="child" />
</template>