@@ -12,19 +12,24 @@ use super::configuration::Configuration;
12
12
use super :: generation:: generate;
13
13
14
14
pub fn format_text ( path : & Path , text : & str , config : & Configuration ) -> Result < Option < String > > {
15
- let parse_result = parse ( text) ?;
16
- let is_jsonc = is_jsonc_file ( path, config) ;
17
- let result = dprint_core:: formatting:: format (
18
- || generate ( parse_result, text, config, is_jsonc) ,
19
- config_to_print_options ( text, config) ,
20
- ) ;
15
+ let result = format_text_inner ( path, text, config) ?;
21
16
if result == text {
22
17
Ok ( None )
23
18
} else {
24
19
Ok ( Some ( result) )
25
20
}
26
21
}
27
22
23
+ fn format_text_inner ( path : & Path , text : & str , config : & Configuration ) -> Result < String > {
24
+ let text = strip_bom ( text) ;
25
+ let parse_result = parse ( text) ?;
26
+ let is_jsonc = is_jsonc_file ( path, config) ;
27
+ Ok ( dprint_core:: formatting:: format (
28
+ || generate ( parse_result, text, config, is_jsonc) ,
29
+ config_to_print_options ( text, config) ,
30
+ ) )
31
+ }
32
+
28
33
#[ cfg( feature = "tracing" ) ]
29
34
pub fn trace_file ( text : & str , config : & Configuration ) -> dprint_core:: formatting:: TracingResult {
30
35
let parse_result = parse ( text) . unwrap ( ) ;
@@ -35,6 +40,10 @@ pub fn trace_file(text: &str, config: &Configuration) -> dprint_core::formatting
35
40
)
36
41
}
37
42
43
+ fn strip_bom ( text : & str ) -> & str {
44
+ text. strip_prefix ( "\u{FEFF} " ) . unwrap_or ( text)
45
+ }
46
+
38
47
fn parse ( text : & str ) -> Result < ParseResult < ' _ > > {
39
48
let parse_result = parse_to_ast (
40
49
text,
@@ -150,4 +159,14 @@ mod tests {
150
159
assert ! ( is_jsonc_file( & PathBuf :: from( "test\\ .vscode\\ settings.json" ) , & config) ) ;
151
160
}
152
161
}
162
+
163
+ #[ test]
164
+ fn should_strip_bom ( ) {
165
+ for input_text in [ "\u{FEFF} {}" , "\u{FEFF} { }" ] {
166
+ let global_config = GlobalConfiguration :: default ( ) ;
167
+ let config = resolve_config ( ConfigKeyMap :: new ( ) , & global_config) . config ;
168
+ let output_text = format_text ( Path :: new ( "." ) , input_text, & config) . unwrap ( ) . unwrap ( ) ;
169
+ assert_eq ! ( output_text, "{}\n " ) ;
170
+ }
171
+ }
153
172
}
0 commit comments