mirror of https://github.com/vuejs/core.git
fix(compiler-core): correctly handle ts type assertions in expressions (#13397)
similar to #13395
This commit is contained in:
parent
75d44c7189
commit
e6544ac292
|
|
@ -14,6 +14,16 @@ return function render(_ctx, _cache, $props, $setup, $data, $options) {
|
||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`compiler: expression transform > expression with type 1`] = `
|
||||||
|
"const { openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
|
||||||
|
|
||||||
|
return function render(_ctx, _cache) {
|
||||||
|
return (_openBlock(), _createElementBlock("div", {
|
||||||
|
onClick: _ctx.handleClick
|
||||||
|
}, null, 8 /* PROPS */, ["onClick"]))
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`compiler: expression transform > should allow leak of var declarations in for loop 1`] = `
|
exports[`compiler: expression transform > should allow leak of var declarations in for loop 1`] = `
|
||||||
"const { openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
|
"const { openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -754,4 +754,12 @@ describe('compiler: expression transform', () => {
|
||||||
expect(code).toMatch(`_ctx.bar`)
|
expect(code).toMatch(`_ctx.bar`)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('expression with type', () => {
|
||||||
|
const { code } = compile(
|
||||||
|
`<div @click="(<number>handleClick as any)"></div>`,
|
||||||
|
)
|
||||||
|
expect(code).toMatch(`onClick: _ctx.handleClick`)
|
||||||
|
expect(code).toMatchSnapshot()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ import {
|
||||||
createSimpleExpression,
|
createSimpleExpression,
|
||||||
} from '../ast'
|
} from '../ast'
|
||||||
import {
|
import {
|
||||||
|
TS_NODE_TYPES,
|
||||||
isInDestructureAssignment,
|
isInDestructureAssignment,
|
||||||
isInNewExpression,
|
isInNewExpression,
|
||||||
isStaticProperty,
|
isStaticProperty,
|
||||||
|
|
@ -347,15 +348,18 @@ export function processExpression(
|
||||||
// an ExpressionNode has the `.children` property, it will be used instead of
|
// an ExpressionNode has the `.children` property, it will be used instead of
|
||||||
// `.content`.
|
// `.content`.
|
||||||
const children: CompoundExpressionNode['children'] = []
|
const children: CompoundExpressionNode['children'] = []
|
||||||
|
const isTSNode = TS_NODE_TYPES.includes(ast.type)
|
||||||
ids.sort((a, b) => a.start - b.start)
|
ids.sort((a, b) => a.start - b.start)
|
||||||
ids.forEach((id, i) => {
|
ids.forEach((id, i) => {
|
||||||
// range is offset by -1 due to the wrapping parens when parsed
|
// range is offset by -1 due to the wrapping parens when parsed
|
||||||
const start = id.start - 1
|
const start = id.start - 1
|
||||||
const end = id.end - 1
|
const end = id.end - 1
|
||||||
const last = ids[i - 1]
|
const last = ids[i - 1]
|
||||||
const leadingText = rawExp.slice(last ? last.end - 1 : 0, start)
|
if (!(isTSNode && i === 0)) {
|
||||||
if (leadingText.length || id.prefix) {
|
const leadingText = rawExp.slice(last ? last.end - 1 : 0, start)
|
||||||
children.push(leadingText + (id.prefix || ``))
|
if (leadingText.length || id.prefix) {
|
||||||
|
children.push(leadingText + (id.prefix || ``))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const source = rawExp.slice(start, end)
|
const source = rawExp.slice(start, end)
|
||||||
children.push(
|
children.push(
|
||||||
|
|
@ -372,7 +376,7 @@ export function processExpression(
|
||||||
: ConstantTypes.NOT_CONSTANT,
|
: ConstantTypes.NOT_CONSTANT,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
if (i === ids.length - 1 && end < rawExp.length) {
|
if (i === ids.length - 1 && end < rawExp.length && !isTSNode) {
|
||||||
children.push(rawExp.slice(end))
|
children.push(rawExp.slice(end))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue