Skip to content
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

fix(compiler-core): avoid using component name as parameter name #11731

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions packages/compiler-core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export enum ErrorCodes {
X_V_FOR_NO_EXPRESSION,
X_V_FOR_MALFORMED_EXPRESSION,
X_V_FOR_TEMPLATE_KEY_PLACEMENT,
X_V_FOR_PARAMS,
X_V_BIND_NO_EXPRESSION,
X_V_ON_NO_EXPRESSION,
X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET,
Expand Down Expand Up @@ -159,6 +160,7 @@ export const errorMessages: Record<ErrorCodes, string> = {
[ErrorCodes.X_V_FOR_NO_EXPRESSION]: `v-for is missing expression.`,
[ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION]: `v-for has invalid expression.`,
[ErrorCodes.X_V_FOR_TEMPLATE_KEY_PLACEMENT]: `<template v-for> key should be placed on the <template> tag.`,
[ErrorCodes.X_V_FOR_PARAMS]: `<template v-for> the parameter name cannot be the same as the current component name.`,
[ErrorCodes.X_V_BIND_NO_EXPRESSION]: `v-bind is missing expression.`,
[ErrorCodes.X_V_BIND_INVALID_SAME_NAME_ARGUMENT]: `v-bind with same-name shorthand only allows static argument.`,
[ErrorCodes.X_V_ON_NO_EXPRESSION]: `v-on is missing expression.`,
Expand Down
35 changes: 35 additions & 0 deletions packages/compiler-core/src/transforms/vFor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
createStructuralDirectiveTransform,
} from '../transform'
import {
type BaseElementNode,
type BlockCodegenNode,
type CompoundExpressionNode,
ConstantTypes,
type DirectiveNode,
type ElementNode,
Expand Down Expand Up @@ -206,7 +208,40 @@ export const transformFor: NodeTransform = createStructuralDirectiveTransform(
helper(getVNodeHelper(context.inSSR, childBlock.isComponent))
}
}
if (__DEV__ || !__BROWSER__) {
let currentTag = ''
if (forNode.children[0]) {
currentTag = (forNode.children[0] as BaseElementNode).tag
}
if (
currentTag &&
forNode.parseResult.value &&
(forNode.parseResult.value as SimpleExpressionNode).content ===
currentTag
) {
context.onError(
createCompilerError(ErrorCodes.X_V_FOR_PARAMS, childBlock.loc),
)
}
let identifiers: CompoundExpressionNode['identifiers'] = []
if (
forNode.parseResult.value &&
(forNode.parseResult.value as CompoundExpressionNode).identifiers
) {
identifiers = (forNode.parseResult.value as CompoundExpressionNode)
.identifiers
}

if (
currentTag &&
identifiers &&
identifiers.some(i => i === currentTag)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is not correct. Consider the following scenario:

  <div v-for="Comp of list">
    <Comp>{{Comp}}</Comp>
  </div>

Playground

) {
context.onError(
createCompilerError(ErrorCodes.X_V_FOR_PARAMS, childBlock.loc),
)
}
}
if (memo) {
const loop = createFunctionExpression(
createForLoopParams(forNode.parseResult, [
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-dom/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function createDOMCompilerError(
}

export enum DOMErrorCodes {
X_V_HTML_NO_EXPRESSION = 53 /* ErrorCodes.__EXTEND_POINT__ */,
X_V_HTML_NO_EXPRESSION = 54 /* ErrorCodes.__EXTEND_POINT__ */,
X_V_HTML_WITH_CHILDREN,
X_V_TEXT_NO_EXPRESSION,
X_V_TEXT_WITH_CHILDREN,
Expand Down