Skip to content

Commit 182f9c2

Browse files
authored
refactor: Use explicit detect_code_removal api (#125)
Blocked by oxc-project/oxc#15619
1 parent a60a2c5 commit 182f9c2

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/formatter_dcr.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use oxc::{
22
allocator::Allocator,
33
parser::{Parser, ParserReturn},
44
};
5-
use oxc_formatter::{FormatOptions, Formatter, get_parse_options};
5+
use oxc_formatter::{FormatOptions, Formatter, detect_code_removal, get_parse_options};
66

77
use crate::{Case, Diagnostic, Driver, Source};
88

99
// Another `FormatterRunner` for detecting code removal.
1010
//
11-
// Detection is enabled by the feature flag "detect_code_removal".
11+
// Detection api is enabled by the feature flag "detect_code_removal".
1212
// While the main FormatterRunner also has this capability,
1313
// there are currently many reported idempotency issues.
1414
// Therefore, for clarity, we separate this test to focus only on detecting code removal.
@@ -29,7 +29,7 @@ impl Case for FormatterDCRRunner {
2929
}
3030

3131
fn test(&self, source: &Source) -> Result<(), Vec<Diagnostic>> {
32-
let Source { source_type, source_text, .. } = source;
32+
let Source { path, source_type, source_text, .. } = source;
3333

3434
let allocator = Allocator::new();
3535
let options = get_parse_options();
@@ -41,8 +41,15 @@ impl Case for FormatterDCRRunner {
4141
return Ok(());
4242
}
4343

44-
// NOTE: This will panic if code removal is detected
45-
let _ = Formatter::new(&allocator, FormatOptions::default()).format(&program);
44+
let source_text2 = Formatter::new(&allocator, FormatOptions::default()).build(&program);
45+
46+
if let Some(diff) = detect_code_removal(source_text, &source_text2, *source_type) {
47+
return Err(vec![Diagnostic {
48+
case: self.name(),
49+
path: path.clone(),
50+
message: format!("Code removal detected:\n{diff}"),
51+
}]);
52+
}
4653

4754
Ok(())
4855
}

0 commit comments

Comments
 (0)