Skip to content

fix(language-core): generate codes for :key prop on v-for and v-if node #4544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 92 additions & 27 deletions packages/language-core/lib/codegen/template/vFor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as CompilerDOM from '@vue/compiler-dom';
import type { Code } from '../../types';
import { collectVars, createTsAst, endOfLine, newLine } from '../common';
import { collectVars, createTsAst, newLine } from '../common';
import type { TemplateCodegenContext } from './context';
import type { TemplateCodegenOptions } from './index';
import { generateInterpolation } from './interpolation';
import { generateTemplateChild } from './templateChild';
import { generateElement } from './element';

export function* generateVFor(
options: TemplateCodegenOptions,
Expand Down Expand Up @@ -51,32 +52,96 @@ export function* generateVFor(
ctx.addLocalVariable(varName);
}
let isFragment = true;
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
) {
if (argument.returns.tag !== CompilerDOM.FRAGMENT) {
isFragment = false;
continue;
}
for (const prop of argument.returns.props.properties) {
if (
prop.value.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
&& !prop.value.isStatic
) {
yield* generateInterpolation(
options,
ctx,
prop.value.content,
prop.value.loc,
prop.value.loc.start.offset,
ctx.codeFeatures.all,
'(',
')'
);
yield endOfLine;
if (node.codegenNode) {
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
) {
if (argument.returns.tag !== CompilerDOM.FRAGMENT) {
isFragment = false;
continue;
}
// #4539, #329
for (const prop of argument.returns.props.properties) {
if (
prop.key.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
&& prop.value.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
) {
const codeBeforeKeyOffset = prop.value.loc.start.offset - node.codegenNode.loc.start.offset;
const codeBeforeKey = node.codegenNode.loc.source.slice(0, codeBeforeKeyOffset);
const isShorthand = codeBeforeKey[codeBeforeKey.length - 1] === ':';
const lastKeyStartOffset = isShorthand ? codeBeforeKeyOffset : codeBeforeKey.lastIndexOf('key');
let fakeProp: CompilerDOM.AttributeNode | CompilerDOM.DirectiveNode;
if (prop.value.isStatic) {
fakeProp = {
type: CompilerDOM.NodeTypes.ATTRIBUTE,
name: prop.key.content,
nameLoc: {} as any,
value: {
type: CompilerDOM.NodeTypes.TEXT,
content: prop.value.content,
loc: prop.value.loc,
},
loc: {
...prop.loc,
start: {
...prop.loc.start,
offset: node.codegenNode.loc.start.offset + lastKeyStartOffset,
},
end: {
...prop.loc.end,
offset: node.codegenNode.loc.start.offset + lastKeyStartOffset + 3,
}
},
};
}
else {
fakeProp = {
type: CompilerDOM.NodeTypes.DIRECTIVE,
name: 'bind',
rawName: '',
arg: {
...prop.key,
loc: {
...prop.key.loc,
start: {
...prop.key.loc.start,
offset: node.codegenNode.loc.start.offset + lastKeyStartOffset,
},
end: {
...prop.key.loc.end,
offset: node.codegenNode.loc.start.offset + lastKeyStartOffset + 3,
}
}
},
exp: prop.value,
loc: {
...prop.key.loc,
start: {
...prop.key.loc.start,
offset: node.codegenNode.loc.start.offset + lastKeyStartOffset,
},
end: {
...prop.key.loc.end,
offset: node.codegenNode.loc.start.offset + lastKeyStartOffset + 3,
}
},
modifiers: [],
};
}
yield* generateElement(options, ctx, {
type: CompilerDOM.NodeTypes.ELEMENT,
tag: 'template',
tagType: CompilerDOM.ElementTypes.TEMPLATE,
ns: 0,
children: node.children,
loc: node.codegenNode.loc,
props: [fakeProp],
codegenNode: undefined,
}, currentComponent, componentCtxVar);
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions packages/language-core/lib/codegen/template/vIf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { TemplateCodegenContext } from './context';
import type { TemplateCodegenOptions } from './index';
import { generateInterpolation } from './interpolation';
import { generateTemplateChild } from './templateChild';
import { generateElement } from './element';

export function* generateVIf(
options: TemplateCodegenOptions,
Expand Down Expand Up @@ -58,6 +59,23 @@ export function* generateVIf(
if (isFragment(node)) {
yield* ctx.resetDirectiveComments('end of v-if start');
}
// #4539
if (
node.codegenNode
&& node.codegenNode.type === CompilerDOM.NodeTypes.JS_CONDITIONAL_EXPRESSION
&& node.codegenNode.consequent.type === CompilerDOM.NodeTypes.VNODE_CALL
&& node.codegenNode.consequent.tag === CompilerDOM.FRAGMENT
) {
yield* generateElement(options, ctx, {
...branch,
type: CompilerDOM.NodeTypes.ELEMENT,
tag: "template",
tagType: CompilerDOM.ElementTypes.TEMPLATE,
props: branch.userKey ? [branch.userKey] : [],
ns: 0,
codegenNode: undefined
}, currentComponent, componentCtxVar);
}
let prev: CompilerDOM.TemplateChildNode | undefined;
for (const childNode of branch.children) {
yield* generateTemplateChild(options, ctx, childNode, currentComponent, prev, componentCtxVar);
Expand Down
1 change: 1 addition & 0 deletions packages/tsc/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const shouldErrorDirs = [
'should-error',
'should-error-2',
'should-error-3',
'should-error-#4539',
];

function prettyPath(path: string, isRoot: boolean) {
Expand Down
11 changes: 11 additions & 0 deletions test-workspace/tsc/should-error-#4539/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script setup lang="ts">
const key = {};
</script>

<template>
<template v-for="_i in 1" :key></template>
<template v-for="_i in 1" :key="{}"></template>
<template v-if="true" :key></template>
<template v-if="true" :key="{}"></template>
<div v-if="true" :key="{}"></div>
</template>
4 changes: 4 additions & 0 deletions test-workspace/tsc/should-error-#4539/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": [ "**/*" ]
}
Loading