@@ -9,6 +9,8 @@ import { load, save } from './util/package-json.ts';
99import { extname , join } from './util/path.ts' ;
1010import { removeExport } from './util/remove-export.ts' ;
1111
12+ const MODULE_MARKER = / ^ [ \t ] * ( i m p o r t | e x p o r t ) \b / m;
13+
1214export const fix = async ( issues : Issues , counters : Counters , options : MainOptions ) => {
1315 const fixer = new IssueFixer ( options ) ;
1416 const touchedFiles = await fixer . fixIssues ( issues ) ;
@@ -79,12 +81,14 @@ class IssueFixer {
7981 for ( const [ filePath , fixes ] of allFixes ) {
8082 if ( fixes . length === 0 ) continue ;
8183 const absFilePath = join ( this . options . cwd , filePath ) ;
82- const sourceFileText = fixes
84+ const originalSource = await readFile ( absFilePath , 'utf-8' ) ;
85+ let sourceFileText = fixes
8386 . sort ( ( a , b ) => b [ 0 ] - a [ 0 ] )
84- . reduce (
85- ( text , [ start , end , flags ] ) => removeExport ( { text, start, end, flags } ) ,
86- await readFile ( absFilePath , 'utf-8' )
87- ) ;
87+ . reduce ( ( text , [ start , end , flags ] ) => removeExport ( { text, start, end, flags } ) , originalSource ) ;
88+
89+ if ( MODULE_MARKER . test ( originalSource ) && ! MODULE_MARKER . test ( sourceFileText ) ) {
90+ sourceFileText = `${ sourceFileText . trimEnd ( ) } \n\nexport {};\n` ;
91+ }
8892
8993 await writeFile ( absFilePath , sourceFileText ) ;
9094
0 commit comments