File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -122,10 +122,12 @@ const logFmtResult = (
122122 matchedFileCount : number ,
123123 durationSeconds : number ,
124124) : void => {
125+ let writtenCount = 0 ;
125126 let differentCount = 0 ;
126127
127128 for ( const file of result . files ) {
128129 if ( file . status === 'written' ) {
130+ writtenCount ++ ;
129131 continue ;
130132 }
131133
@@ -139,18 +141,17 @@ const logFmtResult = (
139141 }
140142
141143 if ( mode === 'write' ) {
142- if ( result . exitCode !== 0 ) {
144+ if ( writtenCount === 0 && result . exitCode !== 0 ) {
143145 return ;
144146 }
145147
146- const writtenCount = result . files . length ;
147148 const matchedFiles = formatFileCount ( matchedFileCount ) ;
148149 const time = prettyTime ( durationSeconds ) ;
149- if ( writtenCount > 0 ) {
150- logger . success ( `Formatted ${ formatCount ( writtenCount ) } of ${ matchedFiles } in ${ time } .` ) ;
151- } else {
152- logger . success ( `Checked ${ matchedFiles } in ${ time } . No changes needed.` ) ;
153- }
150+ const message =
151+ writtenCount > 0
152+ ? `Formatted ${ formatCount ( writtenCount ) } of ${ matchedFiles } in ${ time } .`
153+ : `Checked ${ matchedFiles } in ${ time } . No changes needed.` ;
154+ logger [ result . exitCode === 0 ? 'success' : 'info' ] ( message ) ;
154155 return ;
155156 }
156157
Original file line number Diff line number Diff line change @@ -370,6 +370,19 @@ test('returns exit code 2 for formatting errors', () => {
370370 expect ( result . stderr ) . toContain ( 'error index.ts: SyntaxError:' ) ;
371371} ) ;
372372
373+ test ( 'reports partial writes when formatting fails' , ( ) => {
374+ writeProjectFile ( 'valid.ts' , 'const value=true' ) ;
375+ writeProjectFile ( 'invalid.ts' , 'const invalid = ;' ) ;
376+
377+ const result = runFmt ( [ 'valid.ts' , 'invalid.ts' ] ) ;
378+
379+ expect ( result . status ) . toBe ( 2 ) ;
380+ expect ( normalizeDuration ( result . stdout ) ) . toBe ( 'info Formatted 1 of 2 files in <duration>.\n' ) ;
381+ expect ( result . stderr ) . toContain ( 'error invalid.ts: SyntaxError:' ) ;
382+ expect ( readProjectFile ( 'valid.ts' ) ) . toBe ( 'const value = true;\n' ) ;
383+ expect ( readProjectFile ( 'invalid.ts' ) ) . toBe ( 'const invalid = ;' ) ;
384+ } ) ;
385+
373386test ( 'reports when no files match' , ( ) => {
374387 const writeResult = runFmt ( [ 'missing/**/*.ts' ] ) ;
375388
You can’t perform that action at this time.
0 commit comments