11import ts from 'typescript' ;
2- import { evaluate , getModifier } from './utils' ;
2+ import { evaluate , getExportedNamesOfSource , hasModifier } from './utils' ;
33
44export default function ( program : ts . Program , pluginOptions ?: unknown ) {
55 return ( ctx : ts . TransformationContext ) => {
66 return ( sourceFile : ts . SourceFile ) => {
7- const ambient = sourceFile . isDeclarationFile ;
8-
97 return ts . visitEachChild ( sourceFile , visitor , ctx ) ;
108
119 function visitor ( node : ts . Node ) : ts . Node {
1210 if ( ! ts . isEnumDeclaration ( node ) ) {
1311 return ts . visitEachChild ( node , visitor , ctx ) ;
1412 }
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 ;
1913
20- if ( ambient ) {
14+ if ( ! hasModifier ( node , ts . SyntaxKind . ConstKeyword ) ) {
15+ return node ;
16+ }
17+
18+ if ( ! hasModifier ( node , ts . SyntaxKind . ExportKeyword ) ) {
19+ if ( ! getExportedNamesOfSource ( program , sourceFile ) . includes ( node . name . text ) ) {
20+ return node ;
21+ }
22+ }
23+
24+ if ( sourceFile . isDeclarationFile ) {
2125 return ts . visitEachChild ( node , stripConstKeyword , ctx ) ;
2226 }
2327
24- return transformEnum ( node , [ exportModifier , constModifier ] ) ;
28+ return transformEnum ( node ) ;
2529 }
2630 } ;
2731 } ;
@@ -30,7 +34,7 @@ export default function(program: ts.Program, pluginOptions?: unknown) {
3034 return node . kind === ts . SyntaxKind . ConstKeyword ? undefined : node ;
3135 }
3236
33- function transformEnum ( node : ts . EnumDeclaration , modifiers : ts . Modifier [ ] ) {
37+ function transformEnum ( node : ts . EnumDeclaration ) {
3438 const members = node . members ;
3539 const known = new Map < string , number | string > ( ) ;
3640 const properties : ts . PropertyAssignment [ ] = [ ] ;
@@ -78,7 +82,7 @@ export default function(program: ts.Program, pluginOptions?: unknown) {
7882 }
7983
8084 const result = ts . factory . createVariableStatement (
81- modifiers ,
85+ node . modifiers ,
8286 ts . factory . createVariableDeclarationList (
8387 [
8488 ts . factory . createVariableDeclaration (
0 commit comments