Skip to content

Commit a5c0c85

Browse files
committed
fix(comiler): unwrap type for expressions
1 parent 02e7575 commit a5c0c85

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

packages/compiler/src/utils.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
createSimpleExpression,
1717
isLiteralWhitelisted,
1818
NodeTypes,
19+
unwrapTSNode,
1920
walkIdentifiers,
2021
type SimpleExpressionNode,
2122
type TextNode,
@@ -130,14 +131,16 @@ export function resolveExpression(
130131
context: TransformContext,
131132
effect = false,
132133
): SimpleExpressionNode {
133-
node = node?.type === 'JSXExpressionContainer' ? node.expression : node
134+
if (!node) return createSimpleExpression('', true)
135+
node = unwrapTSNode(
136+
node.type === 'JSXExpressionContainer' ? node.expression : node,
137+
)
134138
const isStatic =
135-
!!node &&
136-
(node.type === 'StringLiteral' ||
137-
node.type === 'JSXText' ||
138-
node.type === 'JSXIdentifier')
139+
node.type === 'StringLiteral' ||
140+
node.type === 'JSXText' ||
141+
node.type === 'JSXIdentifier'
139142
let source =
140-
!node || node.type === 'JSXEmptyExpression'
143+
node.type === 'JSXEmptyExpression'
141144
? ''
142145
: node.type === 'JSXIdentifier'
143146
? node.name
@@ -148,16 +151,16 @@ export function resolveExpression(
148151
: node.type === 'Identifier'
149152
? node.name
150153
: context.ir.source.slice(node.start!, node.end!)
151-
const location = node ? node.loc : null
152-
const isResolved = node && resolvedExpressions.has(node)
154+
const location = node.loc
155+
const isResolved = resolvedExpressions.has(node)
153156
if (source && !isStatic && effect && !isConstant(node)) {
154157
source = `() => (${source})`
155158
if (location && node && !isResolved) {
156159
location.start.column -= 7
157160
node.start! -= 7
158161
}
159162
}
160-
if (node && !isResolved) {
163+
if (!isResolved) {
161164
const offset = node.start! - 1
162165
walkIdentifiers(
163166
node,

packages/compiler/test/transforms/__snapshots__/expression.spec.ts.snap

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ exports[`compiler: expression > conditional expression with v-once 1`] = `
6767
"
6868
`;
6969

70+
exports[`compiler: expression > expression with type 1`] = `
71+
"
72+
const n5 = t2()
73+
_setInsertionState(n5)
74+
const n0 = _createIf(() => (ok), () => {
75+
const n2 = t0()
76+
const x2 = _child(n2)
77+
_setNodes(x2, () => (msg))
78+
return n2
79+
}, () => {
80+
const n4 = t1()
81+
return n4
82+
})
83+
return n5
84+
"
85+
`;
86+
7087
exports[`compiler: expression > logical expression 1`] = `
7188
"
7289
const n0 = _createIf(() => (ok), () => {

packages/compiler/test/transforms/vOn.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,16 @@ describe('v-on', () => {
329329
'_delegate(n0, "click", _withModifiers(e => _ctx.test(e), ["stop"]))',
330330
)
331331
})
332+
333+
test('expression with type', () => {
334+
const { code } = compileWithVOn(`<div onClick={handleClick as any} />`)
335+
expect(code).toMatchInlineSnapshot(`
336+
"
337+
const n0 = t0()
338+
n0.$evtclick = e => handleClick(e)
339+
return n0
340+
"
341+
`)
342+
expect(code).contains('n0.$evtclick = e => handleClick(e)')
343+
})
332344
})

0 commit comments

Comments
 (0)