@@ -8,7 +8,7 @@ use oxc_formatter::{FormatOptions, Formatter, enable_jsx_source_type, get_parse_
88use oxc_parser:: Parser ;
99use oxc_span:: SourceType ;
1010
11- use super :: FormatFileSource ;
11+ use super :: FormatFileStrategy ;
1212
1313pub enum FormatResult {
1414 Success { is_changed : bool , code : String } ,
@@ -49,32 +49,22 @@ impl SourceFormatter {
4949 }
5050
5151 /// Format a file based on its source type.
52- pub fn format ( & self , entry : & FormatFileSource , source_text : & str ) -> FormatResult {
52+ pub fn format ( & self , entry : & FormatFileStrategy , source_text : & str ) -> FormatResult {
5353 let result = match entry {
54- FormatFileSource :: OxcFormatter { path, source_type } => {
54+ FormatFileStrategy :: OxcFormatter { path, source_type } => {
5555 self . format_by_oxc_formatter ( source_text, path, * source_type)
5656 }
5757 #[ cfg( feature = "napi" ) ]
58- FormatFileSource :: ExternalFormatter { path, parser_name } => {
59- let text_to_format: Cow < ' _ , str > =
60- if self . is_sort_package_json && entry. is_package_json ( ) {
61- match sort_package_json:: sort_package_json ( source_text) {
62- Ok ( sorted) => Cow :: Owned ( sorted) ,
63- Err ( err) => {
64- return FormatResult :: Error ( vec ! [ OxcDiagnostic :: error( format!(
65- "Failed to sort package.json: {}\n {err}" ,
66- path. display( )
67- ) ) ] ) ;
68- }
69- }
70- } else {
71- Cow :: Borrowed ( source_text)
72- } ;
73-
74- self . format_by_external_formatter ( & text_to_format, path, parser_name)
58+ FormatFileStrategy :: ExternalFormatter { path, parser_name } => {
59+ self . format_by_external_formatter ( source_text, path, parser_name)
60+ }
61+ #[ cfg( feature = "napi" ) ]
62+ FormatFileStrategy :: ExternalFormatterPackageJson { path, parser_name } => {
63+ self . format_by_external_formatter_package_json ( source_text, path, parser_name)
7564 }
7665 #[ cfg( not( feature = "napi" ) ) ]
77- FormatFileSource :: ExternalFormatter { .. } => {
66+ FormatFileStrategy :: ExternalFormatter { .. }
67+ | FormatFileStrategy :: ExternalFormatterPackageJson { .. } => {
7868 unreachable ! ( "If `napi` feature is disabled, this should not be passed here" )
7969 }
8070 } ;
@@ -170,4 +160,26 @@ impl SourceFormatter {
170160 ) )
171161 } )
172162 }
163+
164+ /// Format `package.json`: optionally sort then format by external formatter.
165+ #[ cfg( feature = "napi" ) ]
166+ fn format_by_external_formatter_package_json (
167+ & self ,
168+ source_text : & str ,
169+ path : & Path ,
170+ parser_name : & str ,
171+ ) -> Result < String , OxcDiagnostic > {
172+ let source_text: Cow < ' _ , str > = if self . is_sort_package_json {
173+ Cow :: Owned ( sort_package_json:: sort_package_json ( source_text) . map_err ( |err| {
174+ OxcDiagnostic :: error ( format ! (
175+ "Failed to sort package.json: {}\n {err}" ,
176+ path. display( )
177+ ) )
178+ } ) ?)
179+ } else {
180+ Cow :: Borrowed ( source_text)
181+ } ;
182+
183+ self . format_by_external_formatter ( & source_text, path, parser_name)
184+ }
173185}
0 commit comments