@@ -31,27 +31,49 @@ function getFileComment(fileContent: string): string {
3131 return '' ;
3232}
3333
34- export async function optimizeImports ( filePath : string ) : Promise < string > {
34+ export async function organizeImportsForFile ( filePath : string ) : Promise < string > {
3535 // staged files might also include deleted files, we need to verify they exist.
3636 if ( ! / \. t s x ? $ / . test ( filePath ) || ! existsSync ( filePath ) ) {
3737 return actions . none ;
3838 }
3939
4040 let fileContent = readFileSync ( filePath ) . toString ( ) ;
41- const lineEnding = detectLineEnding ( fileContent ) ;
41+ if ( / \/ [ / * ] \s * i m p o r t - c o n d u c t o r - s k i p / . test ( fileContent ) ) {
42+ log ( 'gray' , 'skipped (via comment)' , filePath ) ;
43+ return actions . skipped ;
44+ }
4245 const { staged, autoAdd, dryRun } = getConfig ( ) ;
46+ const fileWithOrganizedImports = await organizeImports ( fileContent ) ;
47+ const fileHasChanged = fileWithOrganizedImports !== fileContent ;
48+
49+ if ( fileHasChanged ) {
50+ ! dryRun && writeFileSync ( filePath , fileWithOrganizedImports ) ;
51+ let msg = 'imports reordered' ;
52+ if ( staged && autoAdd ) {
53+ await git . add ( filePath ) ;
54+ msg += ', added to git' ;
55+ }
56+ log ( 'green' , msg , filePath ) ;
57+ } else {
58+ log ( 'gray' , 'no change needed' , filePath ) ;
59+ }
60+
61+ return fileHasChanged ? actions . reordered : actions . none ;
62+ }
63+
64+ export async function organizeImports ( fileContent : string ) : Promise < string > {
65+ const lineEnding = detectLineEnding ( fileContent ) ;
4366 if ( / \/ [ / * ] \s * i m p o r t - c o n d u c t o r - s k i p / . test ( fileContent ) ) {
44- log ( 'gray' , filePath , ' skipped (via comment)') ;
67+ log ( 'gray' , 'Format skipped (via comment)') ;
4568 return actions . skipped ;
4669 }
4770
4871 let fileComment = getFileComment ( fileContent ) ;
49- // Remove the comment from the file content.
5072 if ( fileComment ) {
5173 fileContent = fileContent . replace ( fileComment , '' ) ;
5274 }
5375
54- const rootNode = ts . createSourceFile ( filePath , fileContent , ts . ScriptTarget . Latest , true ) ;
76+ const rootNode = ts . createSourceFile ( 'temp' , fileContent , ts . ScriptTarget . Latest , true ) ;
5577 const importNodes = collectImportNodes ( rootNode ) ;
5678 const importStatementMap = getImportStatementMap ( importNodes ) ;
5779 if ( importStatementMap . size === 0 ) {
@@ -65,7 +87,6 @@ export async function optimizeImports(filePath: string): Promise<string> {
6587 const lastImport = importNodes . pop ( ) ;
6688 const contentWithoutImportStatements = fileContent . slice ( lastImport . end ) ;
6789
68- // Add back code blocks that were between the import statements
6990 const nonImportNodes = collectNonImportNodes ( rootNode , lastImport ) ;
7091 if ( nonImportNodes ) {
7192 updatedContent += nonImportNodes . map ( ( n ) => n . getFullText ( ) ) . join ( '' ) ;
@@ -74,23 +95,7 @@ export async function optimizeImports(filePath: string): Promise<string> {
7495 updatedContent += contentWithoutImportStatements ;
7596
7697 if ( fileComment ) {
77- // Add the comment back to the file content.
78- fileContent = `${ fileComment } ${ fileContent } ` ;
7998 updatedContent = `${ fileComment } \n` + updatedContent ;
8099 }
81-
82- const fileHasChanged = updatedContent !== fileContent ;
83- if ( fileHasChanged ) {
84- ! dryRun && writeFileSync ( filePath , updatedContent ) ;
85- let msg = 'imports reordered' ;
86- if ( staged && autoAdd ) {
87- await git . add ( filePath ) ;
88- msg += ', added to git' ;
89- }
90- log ( 'green' , filePath , msg ) ;
91- } else {
92- log ( 'gray' , filePath , 'no change needed' ) ;
93- }
94-
95- return fileHasChanged ? actions . reordered : actions . none ;
100+ return updatedContent ;
96101}
0 commit comments