1
+ import { isString } from '@vue/shared'
2
+ import { walkIdentifiers } from 'ast-kit'
1
3
import {
2
4
advancePositionWithClone ,
5
+ buildCodeFragment ,
6
+ isConstantExpression ,
3
7
isStaticProperty ,
4
8
NewlineType ,
5
9
TS_NODE_TYPES ,
10
+ type CodeFragment ,
6
11
type SimpleExpressionNode ,
7
- type SourceLocation ,
8
- } from '@vue/compiler-dom'
9
- import { isString } from '@vue/shared'
10
- import { walkIdentifiers } from 'ast-kit'
11
- import { isConstantExpression } from '../utils'
12
+ } from '../utils'
12
13
import type { CodegenContext } from '../generate'
13
- import { buildCodeFragment , type CodeFragment } from './utils'
14
- import type { Identifier , Node } from '@babel/types'
14
+ import type { Identifier , Node , SourceLocation } from '@babel/types'
15
15
16
16
export function genExpression (
17
17
node : SimpleExpressionNode ,
18
18
context : CodegenContext ,
19
19
assignment ?: string ,
20
+ needWrap = false ,
20
21
) : CodeFragment [ ] {
21
- const { content, ast, isStatic, loc } = node
22
+ let { content, ast, isStatic, loc } = node
23
+ if ( needWrap ) {
24
+ content = `() => (${ content } )`
25
+ }
22
26
23
27
if ( isStatic ) {
24
28
return [ [ JSON . stringify ( content ) , NewlineType . None , loc ] ]
@@ -55,8 +59,7 @@ export function genExpression(
55
59
if ( ids . length ) {
56
60
const [ frag , push ] = buildCodeFragment ( )
57
61
const isTSNode = ast && TS_NODE_TYPES . includes ( ast . type )
58
- const offset =
59
- ( ast ?. start ? ast . start - 1 : 0 ) - ( ( ast as any ) . _offset || 0 )
62
+ const offset = ( ast ?. start ? ast . start - 1 : 0 ) - ( needWrap ? 7 : 0 )
60
63
ids
61
64
. sort ( ( a , b ) => a . start ! - b . start ! )
62
65
. forEach ( ( id , i ) => {
@@ -87,9 +90,10 @@ export function genExpression(
87
90
source ,
88
91
context ,
89
92
{
90
- start : advancePositionWithClone ( node . loc . start , source , start ) ,
91
- end : advancePositionWithClone ( node . loc . start , source , end ) ,
92
- source,
93
+ start : advancePositionWithClone ( node . loc ! . start , source , start ) ,
94
+ end : advancePositionWithClone ( node . loc ! . start , source , end ) ,
95
+ filename : '' ,
96
+ identifierName : undefined ,
93
97
} ,
94
98
hasMemberExpression ? undefined : assignment ,
95
99
parent ,
@@ -113,7 +117,7 @@ export function genExpression(
113
117
function genIdentifier (
114
118
raw : string ,
115
119
context : CodegenContext ,
116
- loc ?: SourceLocation ,
120
+ loc ?: SourceLocation | null ,
117
121
assignment ?: string ,
118
122
parent ?: Node ,
119
123
) : CodeFragment [ ] {
0 commit comments