Skip to content

Commit cb54032

Browse files
committed
fix(language-core): variable used in key appears as unused in v-for template tag
close #329, close #3421
1 parent b359aa9 commit cb54032

File tree

2 files changed

+30
-4
lines changed
  • packages/language-core/lib/codegen/template
  • test-workspace/tsc/vue3/#329

2 files changed

+30
-4
lines changed

packages/language-core/lib/codegen/template/vFor.ts

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as CompilerDOM from '@vue/compiler-dom';
22
import type { Code } from '../../types';
3-
import { collectVars, createTsAst, newLine } from '../common';
3+
import { collectVars, createTsAst, endOfLine, newLine } from '../common';
44
import type { TemplateCodegenContext } from './context';
55
import type { TemplateCodegenOptions } from './index';
66
import { isFragment } from './index';
@@ -48,12 +48,33 @@ export function* generateVFor(
4848
yield `{} as any`;
4949
}
5050
yield `) {${newLine}`;
51-
if (isFragment(node)) {
52-
yield* ctx.resetDirectiveComments('end of v-for start');
53-
}
5451
for (const varName of forBlockVars) {
5552
ctx.addLocalVariable(varName);
5653
}
54+
for (const argument of node.codegenNode?.children.arguments ?? []) {
55+
if (
56+
argument.type === CompilerDOM.NodeTypes.JS_FUNCTION_EXPRESSION
57+
&& argument.returns.type === CompilerDOM.NodeTypes.VNODE_CALL
58+
&& argument.returns.props?.type === CompilerDOM.NodeTypes.JS_OBJECT_EXPRESSION
59+
) {
60+
for (const prop of argument.returns.props.properties) {
61+
yield* generateInterpolation(
62+
options,
63+
ctx,
64+
prop.value.loc.source,
65+
prop.value.loc,
66+
prop.value.loc.start.offset,
67+
ctx.codeFeatures.all,
68+
'(',
69+
')',
70+
);
71+
yield endOfLine;
72+
}
73+
}
74+
}
75+
if (isFragment(node)) {
76+
yield* ctx.resetDirectiveComments('end of v-for start');
77+
}
5778
let prev: CompilerDOM.TemplateChildNode | undefined;
5879
for (const childNode of node.children) {
5980
yield* generateTemplateChild(options, ctx, childNode, currentComponent, prev, componentCtxVar);

test-workspace/tsc/vue3/#329/main.vue

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<template v-for="(i, part) of ['a']" :key="i">
3+
{{ part }}
4+
</template>
5+
</template>

0 commit comments

Comments
 (0)