From 8628f3f60161e2408141e83b3ee9209c054d26a4 Mon Sep 17 00:00:00 2001 From: johnsoncodehk Date: Thu, 1 Jul 2021 05:13:01 +0800 Subject: [PATCH] fix: can't assign expression to no args event close #270 --- .../src/generators/template.ts | 94 +++++++++---------- .../src/virtuals/global.ts | 1 + 2 files changed, 44 insertions(+), 51 deletions(-) diff --git a/packages/vscode-vue-languageservice/src/generators/template.ts b/packages/vscode-vue-languageservice/src/generators/template.ts index e25773a206..b8f16d6628 100644 --- a/packages/vscode-vue-languageservice/src/generators/template.ts +++ b/packages/vscode-vue-languageservice/src/generators/template.ts @@ -1003,7 +1003,7 @@ export function generate( tsCodeGen.addText(`let ${var_on}!: { `); tsCodeGen.addText(validTsVar.test(key_1) ? key_1 : `'${key_1}'`); - tsCodeGen.addText(`: __VLS_FirstFunction<\n`); + tsCodeGen.addText(`: __VLS_FillingEventArg<__VLS_FirstFunction<\n`); if (key_1 !== key_3) { tsCodeGen.addText(`__VLS_FirstFunction<\n`); tsCodeGen.addText(`__VLS_EmitEvent,\n`); @@ -1025,7 +1025,7 @@ export function generate( capabilities: capabilitiesSet.attr, }, ); - tsCodeGen.addText(`]> };\n`); + tsCodeGen.addText(`]>>\n};\n`); const transformResult = transformOn(prop, node, transformContext); for (const prop_2 of transformResult.props) { @@ -1043,63 +1043,55 @@ export function generate( }, ); tsCodeGen.addText(`: `); + appendExpressionNode(prop); + tsCodeGen.addText(`\n};\n`); - if (prop.exp?.type === NodeTypes.SIMPLE_EXPRESSION) { - if (value.type === NodeTypes.SIMPLE_EXPRESSION) { - if (value.content === prop.exp.content) { - writeCode( - value.content, - { - start: prop.exp.loc.start.offset, - end: prop.exp.loc.end.offset, - }, - SourceMaps.Mode.Offset, - { - vueTag: 'template', - capabilities: capabilitiesSet.all, - }, - formatBrackets.empty, - ); + function appendExpressionNode(prop: vueDom.DirectiveNode) { + if (prop.exp?.type === NodeTypes.SIMPLE_EXPRESSION) { + if (value.type === NodeTypes.SIMPLE_EXPRESSION) { + appendSimpleExpressionNode(value, prop.exp); } - else { - tsCodeGen.addText(value.content); + else if (value.type === NodeTypes.COMPOUND_EXPRESSION) { + appendCompoundExpressionNode(value, prop.exp); } } - else if (value.type === NodeTypes.COMPOUND_EXPRESSION) { - for (const child of value.children) { - if (typeof child === 'string') { - tsCodeGen.addText(child); - } - else if (typeof child === 'symbol') { - // ignore - } - else if (child.type === NodeTypes.SIMPLE_EXPRESSION) { - if (child.content === prop.exp.content) { - writeCode( - child.content, - { - start: prop.exp.loc.start.offset, - end: prop.exp.loc.end.offset, - }, - SourceMaps.Mode.Offset, - { - vueTag: 'template', - capabilities: capabilitiesSet.all, - }, - formatBrackets.empty, - ); - } - else { - tsCodeGen.addText(child.content); - } - } + else { + tsCodeGen.addText(`undefined`); + } + } + function appendCompoundExpressionNode(node: vueDom.CompoundExpressionNode, exp: vueDom.SimpleExpressionNode) { + for (const child of node.children) { + if (typeof child === 'string') { + tsCodeGen.addText(child); + } + else if (typeof child === 'symbol') { + // ignore + } + else if (child.type === NodeTypes.SIMPLE_EXPRESSION) { + appendSimpleExpressionNode(child, exp); } } } - else { - tsCodeGen.addText(`undefined`); + function appendSimpleExpressionNode(node: vueDom.SimpleExpressionNode, exp: vueDom.SimpleExpressionNode) { + if (node.content === exp.content) { + writeCode( + node.content, + { + start: exp.loc.start.offset, + end: exp.loc.end.offset, + }, + SourceMaps.Mode.Offset, + { + vueTag: 'template', + capabilities: capabilitiesSet.all, + }, + formatBrackets.empty, + ); + } + else { + tsCodeGen.addText(node.content); + } } - tsCodeGen.addText(`\n};\n`); } } } diff --git a/packages/vscode-vue-languageservice/src/virtuals/global.ts b/packages/vscode-vue-languageservice/src/virtuals/global.ts index c996a2f4eb..2a57d18c72 100644 --- a/packages/vscode-vue-languageservice/src/virtuals/global.ts +++ b/packages/vscode-vue-languageservice/src/virtuals/global.ts @@ -61,6 +61,7 @@ declare global { function __VLS_getVforIndexType(source: T): T extends AnyArray ? undefined : number; function __VLS_getNameOption(t?: T): T extends { name: infer N } ? N : undefined; function __VLS_pickForItem(source: S, forOfItem: T1, forInItem: T2): S extends { [Symbol.iterator](): infer _ } ? T1 : T2; + type __VLS_FillingEventArg = E extends () => infer R ? ($event?: undefined) => R : E; type __VLS_PickNotAny = PickNotAny; type __VLS_ExtractComponentProps = C extends new (...args: any) => { $props: infer P1 } ? P1 : C extends FunctionalComponent ? P2 : C extends { props: infer P3 } ? P3 : C; type __VLS_ExtractRawComponents = { [K in keyof T]: __VLS_ExtractRawComponent };