From cb540323d4f0e80feed4feb4fea301f7d532f0c5 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Mon, 13 May 2024 18:52:54 +0800 Subject: [PATCH] fix(language-core): variable used in `key` appears as unused in v-for template tag close #329, close #3421 --- .../lib/codegen/template/vFor.ts | 29 ++++++++++++++++--- test-workspace/tsc/vue3/#329/main.vue | 5 ++++ 2 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 test-workspace/tsc/vue3/#329/main.vue diff --git a/packages/language-core/lib/codegen/template/vFor.ts b/packages/language-core/lib/codegen/template/vFor.ts index fc5e7ea163..51a7d51b73 100644 --- a/packages/language-core/lib/codegen/template/vFor.ts +++ b/packages/language-core/lib/codegen/template/vFor.ts @@ -1,6 +1,6 @@ import * as CompilerDOM from '@vue/compiler-dom'; import type { Code } from '../../types'; -import { collectVars, createTsAst, newLine } from '../common'; +import { collectVars, createTsAst, endOfLine, newLine } from '../common'; import type { TemplateCodegenContext } from './context'; import type { TemplateCodegenOptions } from './index'; import { isFragment } from './index'; @@ -48,12 +48,33 @@ export function* generateVFor( yield `{} as any`; } yield `) {${newLine}`; - if (isFragment(node)) { - yield* ctx.resetDirectiveComments('end of v-for start'); - } for (const varName of forBlockVars) { ctx.addLocalVariable(varName); } + for (const argument of node.codegenNode?.children.arguments ?? []) { + if ( + argument.type === CompilerDOM.NodeTypes.JS_FUNCTION_EXPRESSION + && argument.returns.type === CompilerDOM.NodeTypes.VNODE_CALL + && argument.returns.props?.type === CompilerDOM.NodeTypes.JS_OBJECT_EXPRESSION + ) { + for (const prop of argument.returns.props.properties) { + yield* generateInterpolation( + options, + ctx, + prop.value.loc.source, + prop.value.loc, + prop.value.loc.start.offset, + ctx.codeFeatures.all, + '(', + ')', + ); + yield endOfLine; + } + } + } + if (isFragment(node)) { + yield* ctx.resetDirectiveComments('end of v-for start'); + } let prev: CompilerDOM.TemplateChildNode | undefined; for (const childNode of node.children) { yield* generateTemplateChild(options, ctx, childNode, currentComponent, prev, componentCtxVar); diff --git a/test-workspace/tsc/vue3/#329/main.vue b/test-workspace/tsc/vue3/#329/main.vue new file mode 100644 index 0000000000..fb5859759e --- /dev/null +++ b/test-workspace/tsc/vue3/#329/main.vue @@ -0,0 +1,5 @@ +