fix(compiler-vapor): remove types for expressions (#13395)

This commit is contained in:
zhiyuanzmj 2025-06-20 08:43:15 +08:00 committed by GitHub
parent 6f6ab1ab3c
commit 2250d415b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 3 deletions

View File

@ -123,6 +123,18 @@ export function render(_ctx, $props, $emit, $attrs, $slots) {
}" }"
`; `;
exports[`v-on > expression with type 1`] = `
"import { delegateEvents as _delegateEvents, template as _template } from 'vue';
const t0 = _template("<div></div>", true)
_delegateEvents("click")
export function render(_ctx, $props, $emit, $attrs, $slots) {
const n0 = t0()
n0.$evtclick = e => _ctx.handleClick(e)
return n0
}"
`;
exports[`v-on > function expression w/ prefixIdentifiers: true 1`] = ` exports[`v-on > function expression w/ prefixIdentifiers: true 1`] = `
"import { delegateEvents as _delegateEvents, template as _template } from 'vue'; "import { delegateEvents as _delegateEvents, template as _template } from 'vue';
const t0 = _template("<div></div>", true) const t0 = _template("<div></div>", true)

View File

@ -682,4 +682,17 @@ describe('v-on', () => {
'_delegate(n0, "click", _withModifiers(e => _ctx.test(e), ["stop"]))', '_delegate(n0, "click", _withModifiers(e => _ctx.test(e), ["stop"]))',
) )
}) })
test('expression with type', () => {
const { code } = compileWithVOn(
`<div @click="(<number>handleClick as any)"></div>`,
{
bindingMetadata: {
handleClick: BindingTypes.SETUP_CONST,
},
},
)
expect(code).matchSnapshot()
expect(code).include('n0.$evtclick = e => _ctx.handleClick(e)')
})
}) })

View File

@ -10,6 +10,7 @@ import {
NewlineType, NewlineType,
type SimpleExpressionNode, type SimpleExpressionNode,
type SourceLocation, type SourceLocation,
TS_NODE_TYPES,
advancePositionWithClone, advancePositionWithClone,
createSimpleExpression, createSimpleExpression,
isInDestructureAssignment, isInDestructureAssignment,
@ -63,6 +64,7 @@ export function genExpression(
let hasMemberExpression = false let hasMemberExpression = false
if (ids.length) { if (ids.length) {
const [frag, push] = buildCodeFragment() const [frag, push] = buildCodeFragment()
const isTSNode = ast && TS_NODE_TYPES.includes(ast.type)
ids ids
.sort((a, b) => a.start! - b.start!) .sort((a, b) => a.start! - b.start!)
.forEach((id, i) => { .forEach((id, i) => {
@ -71,8 +73,10 @@ export function genExpression(
const end = id.end! - 1 const end = id.end! - 1
const last = ids[i - 1] const last = ids[i - 1]
if (!(isTSNode && i === 0)) {
const leadingText = content.slice(last ? last.end! - 1 : 0, start) const leadingText = content.slice(last ? last.end! - 1 : 0, start)
if (leadingText.length) push([leadingText, NewlineType.Unknown]) if (leadingText.length) push([leadingText, NewlineType.Unknown])
}
const source = content.slice(start, end) const source = content.slice(start, end)
const parentStack = parentStackMap.get(id)! const parentStack = parentStackMap.get(id)!
@ -99,7 +103,7 @@ export function genExpression(
), ),
) )
if (i === ids.length - 1 && end < content.length) { if (i === ids.length - 1 && end < content.length && !isTSNode) {
push([content.slice(end), NewlineType.Unknown]) push([content.slice(end), NewlineType.Unknown])
} }
}) })