@@ -8,16 +8,24 @@ use crate::configuration::Configuration;
8
8
use crate :: generation:: generate;
9
9
10
10
pub fn format_text ( _file_path : & Path , text : & str , config : & Configuration ) -> Result < Option < String > > {
11
- let node = parse_node ( text) ?;
12
-
13
- let result = dprint_core:: formatting:: format ( || generate ( & node, text, config) , config_to_print_options ( text, config) ) ;
11
+ let result = format_inner ( text, config) ?;
14
12
if result == text {
15
13
Ok ( None )
16
14
} else {
17
15
Ok ( Some ( result) )
18
16
}
19
17
}
20
18
19
+ fn format_inner ( text : & str , config : & Configuration ) -> Result < String > {
20
+ let text = strip_bom ( text) ;
21
+ let node = parse_node ( text) ?;
22
+
23
+ Ok ( dprint_core:: formatting:: format (
24
+ || generate ( & node, text, config) ,
25
+ config_to_print_options ( text, config) ,
26
+ ) )
27
+ }
28
+
21
29
#[ cfg( feature = "tracing" ) ]
22
30
pub fn trace_file ( _file_path : & Path , text : & str , config : & Configuration ) -> dprint_core:: formatting:: TracingResult {
23
31
let node = parse_node ( text) . unwrap ( ) ;
@@ -29,6 +37,10 @@ fn parse_node(text: &str) -> Result<Dockerfile> {
29
37
Ok ( Dockerfile :: parse ( text) ?)
30
38
}
31
39
40
+ fn strip_bom ( text : & str ) -> & str {
41
+ text. strip_prefix ( "\u{FEFF} " ) . unwrap_or ( text)
42
+ }
43
+
32
44
fn config_to_print_options ( text : & str , config : & Configuration ) -> PrintOptions {
33
45
PrintOptions {
34
46
indent_width : 1 ,
@@ -37,3 +49,22 @@ fn config_to_print_options(text: &str, config: &Configuration) -> PrintOptions {
37
49
new_line_text : resolve_new_line_kind ( text, config. new_line_kind ) ,
38
50
}
39
51
}
52
+
53
+ #[ cfg( test) ]
54
+ mod test {
55
+ use super :: * ;
56
+
57
+ #[ test]
58
+ fn strips_bom ( ) {
59
+ for input_text in [ "\u{FEFF} FROM example:12.16.1\n " , "\u{FEFF} FROM example:12.16.1\n " ] {
60
+ let text = format_text (
61
+ & std:: path:: PathBuf :: from ( "test.dockerfile" ) ,
62
+ input_text,
63
+ & crate :: configuration:: ConfigurationBuilder :: new ( ) . build ( ) ,
64
+ )
65
+ . unwrap ( )
66
+ . unwrap ( ) ;
67
+ assert_eq ! ( text, "FROM example:12.16.1\n " ) ;
68
+ }
69
+ }
70
+ }
0 commit comments