Skip to content

Commit e6544ac

Browse files
authored
fix(compiler-core): correctly handle ts type assertions in expressions (#13397)
similar to #13395
1 parent 75d44c7 commit e6544ac

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

packages/compiler-core/__tests__/transforms/__snapshots__/transformExpressions.spec.ts.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ return function render(_ctx, _cache, $props, $setup, $data, $options) {
1414
}"
1515
`;
1616

17+
exports[`compiler: expression transform > expression with type 1`] = `
18+
"const { openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
19+
20+
return function render(_ctx, _cache) {
21+
return (_openBlock(), _createElementBlock("div", {
22+
onClick: _ctx.handleClick
23+
}, null, 8 /* PROPS */, ["onClick"]))
24+
}"
25+
`;
26+
1727
exports[`compiler: expression transform > should allow leak of var declarations in for loop 1`] = `
1828
"const { openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
1929

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,4 +754,12 @@ describe('compiler: expression transform', () => {
754754
expect(code).toMatch(`_ctx.bar`)
755755
})
756756
})
757+
758+
test('expression with type', () => {
759+
const { code } = compile(
760+
`<div @click="(<number>handleClick as any)"></div>`,
761+
)
762+
expect(code).toMatch(`onClick: _ctx.handleClick`)
763+
expect(code).toMatchSnapshot()
764+
})
757765
})

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
createSimpleExpression,
1919
} from '../ast'
2020
import {
21+
TS_NODE_TYPES,
2122
isInDestructureAssignment,
2223
isInNewExpression,
2324
isStaticProperty,
@@ -347,15 +348,18 @@ export function processExpression(
347348
// an ExpressionNode has the `.children` property, it will be used instead of
348349
// `.content`.
349350
const children: CompoundExpressionNode['children'] = []
351+
const isTSNode = TS_NODE_TYPES.includes(ast.type)
350352
ids.sort((a, b) => a.start - b.start)
351353
ids.forEach((id, i) => {
352354
// range is offset by -1 due to the wrapping parens when parsed
353355
const start = id.start - 1
354356
const end = id.end - 1
355357
const last = ids[i - 1]
356-
const leadingText = rawExp.slice(last ? last.end - 1 : 0, start)
357-
if (leadingText.length || id.prefix) {
358-
children.push(leadingText + (id.prefix || ``))
358+
if (!(isTSNode && i === 0)) {
359+
const leadingText = rawExp.slice(last ? last.end - 1 : 0, start)
360+
if (leadingText.length || id.prefix) {
361+
children.push(leadingText + (id.prefix || ``))
362+
}
359363
}
360364
const source = rawExp.slice(start, end)
361365
children.push(
@@ -372,7 +376,7 @@ export function processExpression(
372376
: ConstantTypes.NOT_CONSTANT,
373377
),
374378
)
375-
if (i === ids.length - 1 && end < rawExp.length) {
379+
if (i === ids.length - 1 && end < rawExp.length && !isTSNode) {
376380
children.push(rawExp.slice(end))
377381
}
378382
})

0 commit comments

Comments
 (0)