1+ import { isString } from '@vue/shared'
2+ import { walkIdentifiers } from 'ast-kit'
13import {
24 advancePositionWithClone ,
5+ buildCodeFragment ,
6+ isConstantExpression ,
37 isStaticProperty ,
48 NewlineType ,
59 TS_NODE_TYPES ,
10+ type CodeFragment ,
611 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'
1213import 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'
1515
1616export function genExpression (
1717 node : SimpleExpressionNode ,
1818 context : CodegenContext ,
1919 assignment ?: string ,
20+ needWrap = false ,
2021) : CodeFragment [ ] {
21- const { content, ast, isStatic, loc } = node
22+ let { content, ast, isStatic, loc } = node
23+ if ( needWrap ) {
24+ content = `() => (${ content } )`
25+ }
2226
2327 if ( isStatic ) {
2428 return [ [ JSON . stringify ( content ) , NewlineType . None , loc ] ]
@@ -55,8 +59,7 @@ export function genExpression(
5559 if ( ids . length ) {
5660 const [ frag , push ] = buildCodeFragment ( )
5761 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 )
6063 ids
6164 . sort ( ( a , b ) => a . start ! - b . start ! )
6265 . forEach ( ( id , i ) => {
@@ -87,9 +90,10 @@ export function genExpression(
8790 source ,
8891 context ,
8992 {
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 ,
9397 } ,
9498 hasMemberExpression ? undefined : assignment ,
9599 parent ,
@@ -113,7 +117,7 @@ export function genExpression(
113117function genIdentifier (
114118 raw : string ,
115119 context : CodegenContext ,
116- loc ?: SourceLocation ,
120+ loc ?: SourceLocation | null ,
117121 assignment ?: string ,
118122 parent ?: Node ,
119123) : CodeFragment [ ] {
0 commit comments