Skip to content

Commit

Permalink
fix(language-core): variable used in key appears as unused in v-for…
Browse files Browse the repository at this point in the history
… template tag

close #329, close #3421
  • Loading branch information
johnsoncodehk committed May 13, 2024
1 parent b359aa9 commit cb54032
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
29 changes: 25 additions & 4 deletions packages/language-core/lib/codegen/template/vFor.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions test-workspace/tsc/vue3/#329/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<template v-for="(i, part) of ['a']" :key="i">
{{ part }}
</template>
</template>

0 comments on commit cb54032

Please sign in to comment.