@@ -4,7 +4,7 @@ mod tags;
4
4
mod tsconfig;
5
5
6
6
use std:: {
7
- collections:: { HashMap , HashSet } ,
7
+ collections:: { BTreeMap , HashMap , HashSet } ,
8
8
fs:: OpenOptions ,
9
9
io:: Write ,
10
10
sync:: { Arc , LazyLock , Mutex } ,
@@ -13,6 +13,7 @@ use std::{
13
13
pub use config:: { BoundariesConfig , Permissions , Rule } ;
14
14
use git2:: Repository ;
15
15
use globwalk:: Settings ;
16
+ use indicatif:: ProgressIterator ;
16
17
use miette:: { Diagnostic , NamedSource , Report , SourceSpan } ;
17
18
use regex:: Regex ;
18
19
use swc_common:: {
@@ -235,7 +236,8 @@ impl Run {
235
236
let repo = Repository :: discover ( self . repo_root ( ) ) . ok ( ) . map ( Mutex :: new) ;
236
237
let mut result = BoundariesResult :: default ( ) ;
237
238
let global_implicit_dependencies = self . get_implicit_dependencies ( & PackageName :: Root ) ;
238
- for ( package_name, package_info) in packages {
239
+ println ! ( "Checking packages..." ) ;
240
+ for ( package_name, package_info) in packages. into_iter ( ) . progress ( ) {
239
241
if !self . filtered_pkgs ( ) . contains ( package_name)
240
242
|| matches ! ( package_name, PackageName :: Root )
241
243
{
@@ -475,43 +477,56 @@ impl Run {
475
477
Ok ( ( ) )
476
478
}
477
479
478
- pub fn add_ignore (
480
+ pub fn patch_file (
479
481
& self ,
480
482
file_path : & AbsoluteSystemPath ,
481
- span : SourceSpan ,
482
- reason : String ,
483
+ file_patches : Vec < ( SourceSpan , String ) > ,
483
484
) -> Result < ( ) , Error > {
485
+ // Deduplicate and sort by offset
486
+ let file_patches = file_patches
487
+ . into_iter ( )
488
+ . map ( |( span, patch) | ( span. offset ( ) , patch. into ( ) ) )
489
+ . collect :: < BTreeMap < usize , String > > ( ) ;
490
+
484
491
let contents = file_path
485
492
. read_to_string ( )
486
493
. map_err ( |_| Error :: FileNotFound ( file_path. to_owned ( ) ) ) ?;
487
- let contents_before_span = & contents[ ..span. offset ( ) ] ;
488
494
489
- // Find the last newline before the span
490
- let newline_idx = contents_before_span. rfind ( '\n' ) ;
491
495
let mut options = OpenOptions :: new ( ) ;
492
496
options. read ( true ) . write ( true ) . truncate ( true ) ;
493
497
let mut file = file_path
494
498
. open_with_options ( options)
495
499
. map_err ( |_| Error :: FileNotFound ( file_path. to_owned ( ) ) ) ?;
496
500
497
- if let Some ( newline_idx) = newline_idx {
498
- file. write_all ( contents[ ..newline_idx] . as_bytes ( ) )
499
- . map_err ( |_| Error :: FileWriteError ( file_path. to_owned ( ) ) ) ?;
500
- file. write_all ( b"\n " )
501
- . map_err ( |_| Error :: FileWriteError ( file_path. to_owned ( ) ) ) ?;
501
+ let mut last_idx = 0 ;
502
+ for ( idx, reason) in file_patches {
503
+ let contents_before_span = & contents[ last_idx..idx] ;
504
+
505
+ // Find the last newline before the span (note this is the index into the slice,
506
+ // not the full file)
507
+ let newline_idx = contents_before_span. rfind ( '\n' ) ;
508
+
509
+ // If newline exists, we write all the contents before newline
510
+ if let Some ( newline_idx) = newline_idx {
511
+ file. write_all ( contents[ last_idx..( last_idx + newline_idx) ] . as_bytes ( ) )
512
+ . map_err ( |_| Error :: FileWriteError ( file_path. to_owned ( ) ) ) ?;
513
+ file. write_all ( b"\n " )
514
+ . map_err ( |_| Error :: FileWriteError ( file_path. to_owned ( ) ) ) ?;
515
+ }
516
+
502
517
file. write_all ( b"// @boundaries-ignore " )
503
518
. map_err ( |_| Error :: FileWriteError ( file_path. to_owned ( ) ) ) ?;
504
519
file. write_all ( reason. as_bytes ( ) )
505
520
. map_err ( |_| Error :: FileWriteError ( file_path. to_owned ( ) ) ) ?;
506
- file. write_all ( contents[ newline_idx..] . as_bytes ( ) )
507
- . map_err ( |_| Error :: FileWriteError ( file_path. to_owned ( ) ) ) ?;
508
- } else {
509
- file. write_all ( b"// @boundaries-ignore\n " )
510
- . map_err ( |_| Error :: FileWriteError ( file_path. to_owned ( ) ) ) ?;
511
- file. write_all ( contents. as_bytes ( ) )
521
+ file. write_all ( b"\n " )
512
522
. map_err ( |_| Error :: FileWriteError ( file_path. to_owned ( ) ) ) ?;
523
+
524
+ last_idx = idx;
513
525
}
514
526
527
+ file. write_all ( contents[ last_idx..] . as_bytes ( ) )
528
+ . map_err ( |_| Error :: FileWriteError ( file_path. to_owned ( ) ) ) ?;
529
+
515
530
Ok ( ( ) )
516
531
}
517
532
}
0 commit comments