@@ -19,6 +19,7 @@ import type {
19
19
VisitResult ,
20
20
Statement
21
21
} from 'typescript'
22
+ import { cloneNode , type CloneNodeOptions } from 'ts-clone-node'
22
23
23
24
import ts = require( 'typescript' )
24
25
import { join , resolve } from 'path'
@@ -104,10 +105,26 @@ function getDataViewSetMethod (defines: Record<string, any>, type: Type): string
104
105
}
105
106
}
106
107
108
+ function parseExpression ( input : string , factory ?: NodeFactory ) : Expression {
109
+ const expression : Expression = ( ts . createSourceFile ( '' , input , ts . ScriptTarget . ESNext , true , ts . ScriptKind . TS ) . statements [ 0 ] as ExpressionStatement ) . expression
110
+ const cloneOptions : Partial < CloneNodeOptions < Expression > > = {
111
+ typescript : ts ,
112
+ factory,
113
+ setOriginalNodes : true ,
114
+ setParents : true ,
115
+ preserveComments : true ,
116
+ preserveSymbols : true
117
+ }
118
+ return cloneNode ( expression , cloneOptions )
119
+ }
120
+
107
121
function byteOffsetParameter ( factory : NodeFactory , defines : Record < string , any > , param : Expression ) : NumericLiteral | Expression {
108
- if ( ts . isNumericLiteral ( param ) || ts . isStringLiteral ( param ) ) {
122
+ if ( ts . isNumericLiteral ( param ) ) {
109
123
return factory . createNumericLiteral ( param . text )
110
124
}
125
+ if ( ts . isStringLiteral ( param ) ) {
126
+ return parseExpression ( param . text , factory )
127
+ }
111
128
if ( ts . isIdentifier ( param ) ) {
112
129
if ( param . text === 'POINTER_SIZE' ) {
113
130
return factory . createNumericLiteral ( defines . MEMORY64 ? 8 : 4 )
@@ -120,7 +137,7 @@ function byteOffsetParameter (factory: NodeFactory, defines: Record<string, any>
120
137
if ( ! ts . isStringLiteral ( left ) ) throw new Error ( 'left must be string literal' )
121
138
if ( ! ts . isIdentifier ( right ) ) throw new Error ( 'right must be identifier' )
122
139
if ( right . text !== 'POINTER_SIZE' ) throw new Error ( 'right.text !== "POINTER_SIZE"' )
123
- return factory . createNumericLiteral ( `${ left . text as string } ${ defines . MEMORY64 ? '8' : '4' } ` )
140
+ return parseExpression ( `${ left . text as string } ${ defines . MEMORY64 ? '8' : '4' } ` , factory )
124
141
}
125
142
if ( param . operatorToken . kind === ts . SyntaxKind . AsteriskToken ) {
126
143
const left = param . left
@@ -544,9 +561,9 @@ class Transform {
544
561
undefined ,
545
562
[
546
563
( ( ts . isNumericLiteral ( argv1 ) || ts . isStringLiteral ( argv1 ) ) && argv1 . text === '0' )
547
- ? this . ctx . factory . createNumericLiteral ( argv0 . text )
564
+ ? parseExpression ( argv0 . text , this . ctx . factory )
548
565
: ( this . ctx . factory . createBinaryExpression (
549
- this . ctx . factory . createNumericLiteral ( argv0 . text ) ,
566
+ parseExpression ( argv0 . text , this . ctx . factory ) ,
550
567
this . ctx . factory . createToken ( ts . SyntaxKind . PlusToken ) ,
551
568
byteOffsetParameter ( this . ctx . factory , this . defines , argv1 )
552
569
) ) ,
@@ -591,19 +608,19 @@ class Transform {
591
608
undefined ,
592
609
[
593
610
( ( ts . isNumericLiteral ( argv1 ) || ts . isStringLiteral ( argv1 ) ) && argv1 . text === '0' )
594
- ? this . ctx . factory . createNumericLiteral ( argv0 . text )
611
+ ? parseExpression ( argv0 . text , this . ctx . factory )
595
612
: ( this . ctx . factory . createBinaryExpression (
596
- this . ctx . factory . createNumericLiteral ( argv0 . text ) ,
613
+ parseExpression ( argv0 . text , this . ctx . factory ) ,
597
614
this . ctx . factory . createToken ( ts . SyntaxKind . PlusToken ) ,
598
615
byteOffsetParameter ( this . ctx . factory , this . defines , argv1 )
599
616
) ) ,
600
617
methodName === 'setBigInt64' || methodName === 'setBigUint64'
601
618
? ( this . ctx . factory . createCallExpression (
602
619
this . ctx . factory . createIdentifier ( 'BigInt' ) ,
603
620
undefined ,
604
- [ this . ctx . factory . createNumericLiteral ( argv2 . text ) ]
621
+ [ parseExpression ( argv2 . text , this . ctx . factory ) ]
605
622
) )
606
- : this . ctx . factory . createNumericLiteral ( argv2 . text ) ,
623
+ : parseExpression ( argv2 . text , this . ctx . factory ) ,
607
624
this . ctx . factory . createTrue ( )
608
625
]
609
626
)
@@ -625,7 +642,7 @@ class Transform {
625
642
this . ctx . factory . createCallExpression (
626
643
this . ctx . factory . createIdentifier ( this . defines . MEMORY64 ? 'BigInt' : 'Number' ) ,
627
644
undefined ,
628
- [ this . ctx . factory . createNumericLiteral ( argv1 . text ) ]
645
+ [ parseExpression ( argv1 . text , this . ctx . factory ) ]
629
646
)
630
647
]
631
648
)
@@ -655,9 +672,9 @@ class Transform {
655
672
? ( this . ctx . factory . createCallExpression (
656
673
this . ctx . factory . createIdentifier ( 'Number' ) ,
657
674
undefined ,
658
- [ this . ctx . factory . createNumericLiteral ( argv1 . text ) ]
675
+ [ parseExpression ( argv1 . text , this . ctx . factory ) ]
659
676
) )
660
- : this . ctx . factory . createNumericLiteral ( argv1 . text )
677
+ : parseExpression ( argv1 . text , this . ctx . factory )
661
678
]
662
679
) )
663
680
}
0 commit comments