Skip to content

Commit

Permalink
feat: support RFC 436 as experimental (#1964)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk authored Oct 9, 2022
1 parent 8ea5136 commit 6aab5c4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
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

0 comments on commit 6aab5c4

Please sign in to comment.