Skip to content

Commit 2c27556

Browse files
authored
fix(compiler): avoid namespace collisions when transforming template refs in inline mode (#6975)
fix #6964
1 parent 8a882ce commit 2c27556

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

packages/compiler-core/__tests__/transforms/transformElement.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,32 @@ describe('compiler: element transform', () => {
10631063
})
10641064
})
10651065

1066+
test('script setup inline mode template ref (binding does not exist but props with the same name exist)', () => {
1067+
const { node } = parseWithElementTransform(`<input ref="msg"/>`, {
1068+
inline: true,
1069+
bindingMetadata: {
1070+
msg: BindingTypes.PROPS,
1071+
ref: BindingTypes.SETUP_CONST
1072+
}
1073+
})
1074+
expect(node.props).toMatchObject({
1075+
type: NodeTypes.JS_OBJECT_EXPRESSION,
1076+
properties: [
1077+
{
1078+
type: NodeTypes.JS_PROPERTY,
1079+
key: {
1080+
content: 'ref',
1081+
isStatic: true
1082+
},
1083+
value: {
1084+
content: 'msg',
1085+
isStatic: true
1086+
}
1087+
}
1088+
]
1089+
})
1090+
})
1091+
10661092
test('HYDRATE_EVENTS', () => {
10671093
// ignore click events (has dedicated fast path)
10681094
const { node } = parseWithElementTransform(`<div @click="foo" />`, {

packages/compiler-core/src/transforms/transformElement.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -497,19 +497,21 @@ export function buildProps(
497497
// in inline mode there is no setupState object, so we can't use string
498498
// keys to set the ref. Instead, we need to transform it to pass the
499499
// actual ref instead.
500-
if (
501-
!__BROWSER__ &&
502-
value &&
503-
context.inline &&
504-
context.bindingMetadata[value.content]
505-
) {
506-
isStatic = false
507-
properties.push(
508-
createObjectProperty(
509-
createSimpleExpression('ref_key', true),
510-
createSimpleExpression(value.content, true, value.loc)
500+
if (!__BROWSER__ && value && context.inline) {
501+
const binding = context.bindingMetadata[value.content]
502+
if (
503+
binding === BindingTypes.SETUP_LET ||
504+
binding === BindingTypes.SETUP_REF ||
505+
binding === BindingTypes.SETUP_MAYBE_REF
506+
) {
507+
isStatic = false
508+
properties.push(
509+
createObjectProperty(
510+
createSimpleExpression('ref_key', true),
511+
createSimpleExpression(value.content, true, value.loc)
512+
)
511513
)
512-
)
514+
}
513515
}
514516
}
515517
// skip is on <component>, or is="vue:xxx"

0 commit comments

Comments
 (0)