11import ts from 'typescript' ;
2- import { evaluate , hasModifier } from './utils' ;
2+ import { evaluate , getModifier } from './utils' ;
33
44export default function ( program : ts . Program , pluginOptions ?: unknown ) {
55 return ( ctx : ts . TransformationContext ) => {
@@ -12,18 +12,16 @@ export default function(program: ts.Program, pluginOptions?: unknown) {
1212 if ( ! ts . isEnumDeclaration ( node ) ) {
1313 return ts . visitEachChild ( node , visitor , ctx ) ;
1414 }
15- if (
16- ! hasModifier ( node , ts . SyntaxKind . ExportKeyword )
17- || ! hasModifier ( node , ts . SyntaxKind . ConstKeyword )
18- ) {
19- return node ;
20- }
15+ const exportModifier = getModifier ( node , ts . SyntaxKind . ExportKeyword ) ;
16+ if ( ! exportModifier ) return node ;
17+ const constModifier = getModifier ( node , ts . SyntaxKind . ConstKeyword ) ;
18+ if ( ! constModifier ) return node ;
2119
2220 if ( ambient ) {
2321 return ts . visitEachChild ( node , stripConstKeyword , ctx ) ;
2422 }
2523
26- return transformEnum ( node ) ;
24+ return transformEnum ( node , [ exportModifier , constModifier ] ) ;
2725 }
2826 } ;
2927 } ;
@@ -32,10 +30,10 @@ export default function(program: ts.Program, pluginOptions?: unknown) {
3230 return node . kind === ts . SyntaxKind . ConstKeyword ? undefined : node ;
3331 }
3432
35- function transformEnum ( node : ts . EnumDeclaration ) {
33+ function transformEnum ( node : ts . EnumDeclaration , modifiers : ts . Modifier [ ] ) {
3634 const members = node . members ;
3735 const known = new Map < string , number | string > ( ) ;
38- const ast = [ ] ;
36+ const properties : ts . PropertyAssignment [ ] = [ ] ;
3937 let lastValue : string | number = - 1 ;
4038
4139 for ( const member of members ) {
@@ -44,19 +42,18 @@ export default function(program: ts.Program, pluginOptions?: unknown) {
4442 }
4543 const name = member . name . getText ( ) ;
4644 let value : string | number ;
45+ let valueExpr : ts . Expression ;
4746
4847 if ( member . initializer ) {
4948 value = evaluate ( member . initializer , known ) ;
50- let valueExpr : ts . Expression ;
49+
5150 if ( typeof value === 'number' ) {
5251 valueExpr = ts . factory . createNumericLiteral ( value ) ;
5352 lastValue = value ;
5453 } else if ( typeof value === 'string' ) {
5554 valueExpr = ts . factory . createStringLiteral ( value ) ;
5655 lastValue = value ;
5756 } else throw new Error ( 'Unsupported member value' ) ;
58-
59- ast . push ( ts . factory . createPropertyAssignment ( name , valueExpr ) ) ;
6057 } else {
6158 if ( typeof lastValue === 'string' ) {
6259 throw new Error ( 'Invalid enum' ) ;
@@ -72,29 +69,24 @@ export default function(program: ts.Program, pluginOptions?: unknown) {
7269 * }
7370 */
7471 value = ++ lastValue ;
75- ast . push (
76- ts . factory . createPropertyAssignment (
77- name ,
78- ts . factory . createNumericLiteral ( value ) ,
79- ) ,
80- ) ;
72+ valueExpr = ts . factory . createNumericLiteral ( value ) ;
8173 }
74+ const assignment = ts . factory . createPropertyAssignment ( name , valueExpr ) ;
75+ ts . setSourceMapRange ( assignment , ts . getSourceMapRange ( member ) ) ;
76+ properties . push ( assignment ) ;
8277 known . set ( name , value ) ;
8378 }
8479
85- return ts . factory . createVariableStatement (
86- [
87- ts . factory . createModifier ( ts . SyntaxKind . ExportKeyword ) ,
88- ts . factory . createModifier ( ts . SyntaxKind . ConstKeyword ) ,
89- ] ,
80+ const result = ts . factory . createVariableStatement (
81+ modifiers ,
9082 ts . factory . createVariableDeclarationList (
9183 [
9284 ts . factory . createVariableDeclaration (
9385 node . name ,
9486 undefined ,
9587 undefined ,
9688 ts . factory . createAsExpression (
97- ts . factory . createObjectLiteralExpression ( ast , true ) ,
89+ ts . factory . createObjectLiteralExpression ( properties , true ) ,
9890 ts . factory . createTypeReferenceNode (
9991 ts . factory . createIdentifier ( 'const' ) ,
10092 undefined ,
@@ -105,5 +97,8 @@ export default function(program: ts.Program, pluginOptions?: unknown) {
10597 ts . NodeFlags . Const ,
10698 ) ,
10799 ) ;
100+
101+ ts . setSourceMapRange ( result , ts . getSourceMapRange ( node ) ) ;
102+ return result ;
108103 }
109104}
0 commit comments