@@ -11,10 +11,16 @@ import {
1111} from '@vue/compiler-dom'
1212import htmlTags , { type HtmlTags } from 'html-tags'
1313import svgTags from 'svg-tags'
14+ import { type ParseResult , parseExpression } from '@babel/parser'
1415import { EMPTY_EXPRESSION } from './transforms/utils'
16+ import type { TransformContext } from './transform'
1517import type { VaporDirectiveNode } from './ir'
1618import type {
1719 BigIntLiteral ,
20+ Expression ,
21+ JSXAttribute ,
22+ JSXIdentifier ,
23+ JSXText ,
1824 Node ,
1925 NumericLiteral ,
2026 SourceLocation ,
@@ -53,16 +59,6 @@ export function isConstantExpression(exp: SimpleExpressionNode) {
5359 )
5460}
5561
56- export function resolveExpression ( exp : SimpleExpressionNode ) {
57- if ( ! exp . isStatic ) {
58- const value = getLiteralExpressionValue ( exp )
59- if ( value !== null ) {
60- return createSimpleExpression ( `${ value } ` , true , exp . loc )
61- }
62- }
63- return exp
64- }
65-
6662export function getLiteralExpressionValue (
6763 exp : SimpleExpressionNode ,
6864) : number | string | boolean | null {
@@ -83,24 +79,73 @@ export function getLiteralExpressionValue(
8379 return exp . isStatic ? exp . content : null
8480}
8581
82+ export function resolveExpression (
83+ node : JSXAttribute [ 'value' ] | JSXText | JSXIdentifier ,
84+ context : TransformContext ,
85+ ) {
86+ const isStatic =
87+ ! ! node &&
88+ ( node . type === 'StringLiteral' ||
89+ node . type === 'JSXText' ||
90+ node . type === 'JSXIdentifier' )
91+ const source = ! node
92+ ? ''
93+ : node . type === 'JSXIdentifier'
94+ ? node . name
95+ : isStatic
96+ ? node . value
97+ : node . type === 'JSXExpressionContainer'
98+ ? node . expression . type === 'Identifier'
99+ ? node . expression . name
100+ : context . ir . source . slice (
101+ node . expression . start ! ,
102+ node . expression . end ! ,
103+ )
104+ : ''
105+ const location = node ? node . loc : null
106+ let ast : false | ParseResult < Expression > = false
107+ if ( ! isStatic && context . options . prefixIdentifiers ) {
108+ ast = parseExpression ( ` ${ source } ` , {
109+ sourceType : 'module' ,
110+ plugins : context . options . expressionPlugins ,
111+ } )
112+ }
113+ return resolveSimpleExpression ( source , isStatic , location , ast )
114+ }
115+
86116export function resolveSimpleExpression (
87117 source : string ,
88118 isStatic : boolean ,
89- location : SourceLocation ,
119+ location ?: SourceLocation | null ,
120+ ast ?: false | ParseResult < Expression > ,
90121) {
91- return createSimpleExpression ( source , isStatic , {
92- start : {
93- line : location . start . line ,
94- column : location . start . column ,
95- offset : location . start . index ,
96- } ,
97- end : {
98- line : location . end . line ,
99- column : location . end . column ,
100- offset : location . end . index ,
101- } ,
122+ const result = createSimpleExpression (
123+ source ,
124+ isStatic ,
125+ resolveLocation ( location , source ) ,
126+ )
127+ result . ast = ast ?? null
128+ return result
129+ }
130+
131+ export function resolveLocation ( location ?: SourceLocation | null , source = '' ) {
132+ return {
133+ start : location
134+ ? {
135+ line : location . start . line ,
136+ column : location . start . column + 1 ,
137+ offset : location . start . index ,
138+ }
139+ : { line : 1 , column : 1 , offset : 0 } ,
140+ end : location
141+ ? {
142+ line : location . end . line ,
143+ column : location . end . column + 1 ,
144+ offset : location . end . index ,
145+ }
146+ : { line : 1 , column : 1 , offset : 0 } ,
102147 source,
103- } )
148+ }
104149}
105150
106151export function isComponent ( node : Node ) {
0 commit comments