Repository: denoland/deno
File: cli/tools/fmt.rs (Rust)

Code before:
    Some(formatted_str)
  })
}

/// A function for formatting embedded code blocks in JavaScript and TypeScript.
fn create_external_formatter_for_typescript(
  unstable_options: &UnstableFmtOptions,
) -> impl Fn(
  MediaType,
  String,
  &dprint_plugin_typescript::configuration::Configuration,
) -> Option<String> {
  let unstable_sql = unstable_options.sql;
  move |media_type, text, config| {
    match media_type {
      MediaType::Css => format_embedded_css(&text, config),
      MediaType::Html => format_embedded_html(&text, config),
      MediaType::Sql => {
        if unstable_sql {
          format_embedded_sql(&text, config)
        } else {
          None
        }
      }
      // dprint-plugin-typescript only detects css, html, and sql for now
      _ => unreachable!(),
    }
  }
}

/// Formats embedded CSS code blocks in JavaScript and TypeScript.
///
/// This function supports properties only CSS expressions, like:
/// ```css
/// margin: 10px;
/// padding: 10px;
/// ```
///
/// To support this scenario, this function first wraps the text with `a { ... }`,
/// and then strips it off after formatting with malva.
fn format_embedded_css(
  text: &str,
  config: &dprint_plugin_typescript::configuration::Configuration,
) -> Option<String> {
  use malva::config;
  let options = config::FormatOptions {
    layout: config::LayoutOptions {
      indent_width: config.indent_width as usize,
      use_tabs: config.use_tabs,
      ..Default::default()
    },

Review comment (suggestion):
Maybe just return `None`? dprint-plugin-typescript could change in the future and that would break this code.

Code after:
    Some(formatted_str)
  })
}

/// A function for formatting embedded code blocks in JavaScript and TypeScript.
fn create_external_formatter_for_typescript(
  unstable_options: &UnstableFmtOptions,
) -> impl Fn(
  MediaType,
  String,
  &dprint_plugin_typescript::configuration::Configuration,
) -> Option<String> {
  let unstable_sql = unstable_options.sql;
  move |media_type, text, config| match media_type {
    MediaType::Css => format_embedded_css(&text, config),
    MediaType::Html => format_embedded_html(&text, config),
    MediaType::Sql => {
      if unstable_sql {
        format_embedded_sql(&text, config)
      } else {
        None
      }
    }
    _ => None,
  }
}

/// Formats embedded CSS code blocks in JavaScript and TypeScript.
///
/// This function supports properties only CSS expressions, like:
/// ```css
/// margin: 10px;
/// padding: 10px;
/// ```
///
/// To support this scenario, this function first wraps the text with `a { ... }`,
/// and then strips it off after formatting with malva.
fn format_embedded_css(
  text: &str,
  config: &dprint_plugin_typescript::configuration::Configuration,
) -> Option<String> {
  use malva::config;
  let options = config::FormatOptions {
    layout: config::LayoutOptions {
      indent_width: config.indent_width as usize,
      use_tabs: config.use_tabs,
      print_width: config.line_width as usize,
      line_break: match config.new_line_kind {
        dprint_core::configuration::NewLineKind::LineFeed => {
          config::LineBreak::Lf
        }