11use oxc:: {
22 allocator:: Allocator ,
3- parser:: { ParseOptions , Parser } ,
3+ parser:: { ParseOptions , Parser , ParserReturn } ,
44} ;
55use oxc_formatter:: { FormatOptions , Formatter } ;
66
@@ -23,25 +23,47 @@ impl Case for FormatterRunner {
2323
2424 fn idempotency_test ( & self , source : & Source ) -> Result < String , Vec < Diagnostic > > {
2525 let Source { path, source_type, source_text } = source;
26+
2627 let allocator = Allocator :: new ( ) ;
27- let options = ParseOptions { preserve_parens : false , ..ParseOptions :: default ( ) } ;
28- let program = Parser :: new ( & allocator, source_text, * source_type)
29- . with_options ( options)
30- . parse ( )
31- . program ;
32- let source_text2 = Formatter :: new ( & allocator, FormatOptions :: default ( ) ) . build ( & program) ;
33- let program = Parser :: new ( & allocator, & source_text2, * source_type)
34- . with_options ( options)
35- . parse ( )
36- . program ;
37- let source_text3 = Formatter :: new ( & allocator, FormatOptions :: default ( ) ) . build ( & program) ;
28+ let options = ParseOptions {
29+ preserve_parens : false ,
30+ allow_return_outside_function : true ,
31+ allow_v8_intrinsics : true ,
32+ parse_regular_expression : false ,
33+ } ;
34+
35+ let ParserReturn { program : program1, errors : errors1, .. } =
36+ Parser :: new ( & allocator, source_text, * source_type) . with_options ( options) . parse ( ) ;
37+ if !errors1. is_empty ( ) {
38+ return Err ( vec ! [ Diagnostic {
39+ case: self . name( ) ,
40+ path: path. clone( ) ,
41+ message: format!( "Parse error in original source: {errors1:?}" ) ,
42+ } ] ) ;
43+ }
44+
45+ let source_text2 = Formatter :: new ( & allocator, FormatOptions :: default ( ) ) . build ( & program1) ;
46+
47+ let ParserReturn { program : program2, errors : errors2, .. } =
48+ Parser :: new ( & allocator, & source_text2, * source_type) . with_options ( options) . parse ( ) ;
49+ if !errors2. is_empty ( ) {
50+ return Err ( vec ! [ Diagnostic {
51+ case: self . name( ) ,
52+ path: path. clone( ) ,
53+ message: format!( "Parse error after formatting: {errors2:?}" ) ,
54+ } ] ) ;
55+ }
56+
57+ let source_text3 = Formatter :: new ( & allocator, FormatOptions :: default ( ) ) . build ( & program2) ;
58+
3859 if source_text2 != source_text3 {
3960 return Err ( vec ! [ Diagnostic {
4061 case: self . name( ) ,
4162 path: path. clone( ) ,
4263 message: NodeModulesRunner :: get_diff( & source_text2, & source_text3, false ) ,
4364 } ] ) ;
4465 }
66+
4567 Ok ( source_text3)
4668 }
4769}
0 commit comments