Skip to content

Commit 2250d41

Browse files
authored
fix(compiler-vapor): remove types for expressions (#13395)
1 parent 6f6ab1a commit 2250d41

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

packages/compiler-vapor/__tests__/transforms/__snapshots__/vOn.spec.ts.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ export function render(_ctx, $props, $emit, $attrs, $slots) {
123123
}"
124124
`;
125125
126+
exports[`v-on > expression with type 1`] = `
127+
"import { delegateEvents as _delegateEvents, template as _template } from 'vue';
128+
const t0 = _template("<div></div>", true)
129+
_delegateEvents("click")
130+
131+
export function render(_ctx, $props, $emit, $attrs, $slots) {
132+
const n0 = t0()
133+
n0.$evtclick = e => _ctx.handleClick(e)
134+
return n0
135+
}"
136+
`;
137+
126138
exports[`v-on > function expression w/ prefixIdentifiers: true 1`] = `
127139
"import { delegateEvents as _delegateEvents, template as _template } from 'vue';
128140
const t0 = _template("<div></div>", true)

packages/compiler-vapor/__tests__/transforms/vOn.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,4 +682,17 @@ describe('v-on', () => {
682682
'_delegate(n0, "click", _withModifiers(e => _ctx.test(e), ["stop"]))',
683683
)
684684
})
685+
686+
test('expression with type', () => {
687+
const { code } = compileWithVOn(
688+
`<div @click="(<number>handleClick as any)"></div>`,
689+
{
690+
bindingMetadata: {
691+
handleClick: BindingTypes.SETUP_CONST,
692+
},
693+
},
694+
)
695+
expect(code).matchSnapshot()
696+
expect(code).include('n0.$evtclick = e => _ctx.handleClick(e)')
697+
})
685698
})

packages/compiler-vapor/src/generators/expression.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
NewlineType,
1111
type SimpleExpressionNode,
1212
type SourceLocation,
13+
TS_NODE_TYPES,
1314
advancePositionWithClone,
1415
createSimpleExpression,
1516
isInDestructureAssignment,
@@ -63,6 +64,7 @@ export function genExpression(
6364
let hasMemberExpression = false
6465
if (ids.length) {
6566
const [frag, push] = buildCodeFragment()
67+
const isTSNode = ast && TS_NODE_TYPES.includes(ast.type)
6668
ids
6769
.sort((a, b) => a.start! - b.start!)
6870
.forEach((id, i) => {
@@ -71,8 +73,10 @@ export function genExpression(
7173
const end = id.end! - 1
7274
const last = ids[i - 1]
7375

74-
const leadingText = content.slice(last ? last.end! - 1 : 0, start)
75-
if (leadingText.length) push([leadingText, NewlineType.Unknown])
76+
if (!(isTSNode && i === 0)) {
77+
const leadingText = content.slice(last ? last.end! - 1 : 0, start)
78+
if (leadingText.length) push([leadingText, NewlineType.Unknown])
79+
}
7680

7781
const source = content.slice(start, end)
7882
const parentStack = parentStackMap.get(id)!
@@ -99,7 +103,7 @@ export function genExpression(
99103
),
100104
)
101105

102-
if (i === ids.length - 1 && end < content.length) {
106+
if (i === ids.length - 1 && end < content.length && !isTSNode) {
103107
push([content.slice(end), NewlineType.Unknown])
104108
}
105109
})

0 commit comments

Comments
 (0)