Skip to main content

dprint_plugin_dockerfile/
format_text.rs

1use dprint_core::configuration::resolve_new_line_kind;
2use dprint_core::formatting::PrintOptions;
3use std::path::Path;
4
5use crate::ast::Dockerfile;
6use crate::configuration::Configuration;
7use crate::error::FormatError;
8use crate::generation::generate;
9
10pub fn format_text(_file_path: &Path, text: &str, config: &Configuration) -> Result<Option<String>, FormatError> {
11  let result = format_inner(text, config)?;
12  if result == text { Ok(None) } else { Ok(Some(result)) }
13}
14
15fn format_inner(text: &str, config: &Configuration) -> Result<String, FormatError> {
16  let text = strip_bom(text);
17  let node = parse_node(text)?;
18
19  Ok(dprint_core::formatting::format(
20    || generate(&node, text, config),
21    config_to_print_options(text, config),
22  ))
23}
24
25#[cfg(feature = "tracing")]
26pub fn trace_file(_file_path: &Path, text: &str, config: &Configuration) -> dprint_core::formatting::TracingResult {
27  let node = parse_node(text).unwrap();
28
29  dprint_core::formatting::trace_printing(|| generate(&node, text, config), config_to_print_options(text, config))
30}
31
32fn parse_node(text: &str) -> Result<Dockerfile, FormatError> {
33  Ok(Dockerfile::parse(text)?)
34}
35
36fn strip_bom(text: &str) -> &str {
37  text.strip_prefix("\u{FEFF}").unwrap_or(text)
38}
39
40fn config_to_print_options(text: &str, config: &Configuration) -> PrintOptions {
41  PrintOptions {
42    indent_width: 1,
43    max_width: config.line_width,
44    use_tabs: false,
45    new_line_text: resolve_new_line_kind(text, config.new_line_kind),
46  }
47}
48
49#[cfg(test)]
50mod test {
51  use super::*;
52
53  #[test]
54  fn strips_bom() {
55    for input_text in ["\u{FEFF}FROM example:12.16.1\n", "\u{FEFF}FROM    example:12.16.1\n"] {
56      let text = format_text(
57        &std::path::PathBuf::from("test.dockerfile"),
58        input_text,
59        &crate::configuration::ConfigurationBuilder::new().build(),
60      )
61      .unwrap()
62      .unwrap();
63      assert_eq!(text, "FROM example:12.16.1\n");
64    }
65  }
66
67  #[test]
68  fn comment_with_interior_tab_does_not_panic() {
69    // a tab inside a comment must be emitted as a tab signal, not a raw tab
70    // that the printer rejects
71    let result = format_text(
72      &std::path::PathBuf::from("Dockerfile"),
73      "# a\tb\nFROM x\n",
74      &crate::configuration::ConfigurationBuilder::new().build(),
75    );
76    assert!(result.is_ok());
77  }
78}