Skip to content

feat: Support RFC 436 (Experimental) #1964

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 1 commit into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
"never"
],
"markdownDescription": "https://github.com/johnsoncodehk/volar/issues/1038, https://github.com/johnsoncodehk/volar/issues/1121"
},
"experimentalRfc436": {
"type": "boolean",
"default": false,
"markdownDescription": "https://github.com/vuejs/rfcs/discussions/436"
}
}
}
Expand Down
25 changes: 21 additions & 4 deletions vue-language-tools/vue-language-core/src/generators/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ export function generate(
0,
{ diagnostic: true },
]);
codeGen.push('export default await (async () => {\n');
codeGen.push('export default await (async ');
if (vueCompilerOptions.experimentalRfc436 && sfc.scriptSetup.generic) {
codeGen.push(`<${sfc.scriptSetup.generic}>`);
}
codeGen.push('() => {\n');
}

codeGen.push('const __VLS_setup = async () => {\n');
Expand Down Expand Up @@ -377,12 +381,25 @@ export function generate(

writeTemplate();

codeGen.push(`return {} as typeof __VLS_Component`);
if (htmlGen?.slotsNum) {
codeGen.push(`return {} as typeof __VLS_Component & (new () => { ${getSlotsPropertyName(vueVersion)}: ReturnType<typeof __VLS_template> });\n`);
codeGen.push(` & (new () => { ${getSlotsPropertyName(vueVersion)}: ReturnType<typeof __VLS_template> })`);
}
else {
codeGen.push(`return {} as typeof __VLS_Component;\n`);
if (vueCompilerOptions.experimentalRfc436 && sfc.scriptSetup.generic) {
codeGen.push(` & (new <${sfc.scriptSetup.generic}>() => {\n`);
if (scriptSetupRanges.propsTypeArg) {
codeGen.push(`$props: `);
addVirtualCode('scriptSetup', scriptSetupRanges.propsTypeArg.start, scriptSetupRanges.propsTypeArg.end);
codeGen.push(`,\n`);
}
if (scriptSetupRanges.emitsTypeArg) {
codeGen.push(`$emit: `);
addVirtualCode('scriptSetup', scriptSetupRanges.emitsTypeArg.start, scriptSetupRanges.emitsTypeArg.end);
codeGen.push(`,\n`);
}
codeGen.push(`})`);
}
codeGen.push(`;\n`);

codeGen.push(`};\n`);
codeGen.push(`return await __VLS_setup();\n`);
Expand Down
1 change: 1 addition & 0 deletions vue-language-tools/vue-language-core/src/sourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ export class VueSourceFile implements SourceFile {
endTagStart: block.loc.end.offset,
content: block.content,
lang: block.lang ?? 'js',
generic: typeof block.attrs.generic === 'string' ? block.attrs.generic : undefined,
} : null;

if (self.sfc.scriptSetup && newData) {
Expand Down
6 changes: 5 additions & 1 deletion vue-language-tools/vue-language-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface ResolvedVueCompilerOptions {
experimentalTemplateCompilerOptions: any;
experimentalTemplateCompilerOptionsRequirePath: string | undefined;
experimentalResolveStyleCssClasses: 'scoped' | 'always' | 'never';
experimentalRfc436: boolean;
}

export type VueLanguagePlugin = (ctx: {
Expand Down Expand Up @@ -66,7 +67,10 @@ export interface Sfc {
script: (SfcBlock & {
src: string | undefined;
}) | null;
scriptSetup: SfcBlock | null;
scriptSetup: SfcBlock & {
// https://github.com/vuejs/rfcs/discussions/436
generic: string | undefined;
} | null;
styles: (SfcBlock & {
module: string | undefined;
scoped: boolean;
Expand Down
1 change: 1 addition & 0 deletions vue-language-tools/vue-language-core/src/utils/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export function resolveVueCompilerOptions(vueOptions: VueCompilerOptions): Resol
experimentalTemplateCompilerOptions: vueOptions.experimentalTemplateCompilerOptions ?? {},
experimentalTemplateCompilerOptionsRequirePath: vueOptions.experimentalTemplateCompilerOptionsRequirePath ?? undefined,
experimentalResolveStyleCssClasses: vueOptions.experimentalResolveStyleCssClasses ?? 'scoped',
experimentalRfc436: vueOptions.experimentalRfc436 ?? false,
};
}

Expand Down