Repository: denoland/deno
File: resolvers/node/resolution.rs (Rust)

Code before:
use crate::PathClean;

pub static IMPORT_CONDITIONS: &[Cow<'static, str>] = &[
  Cow::Borrowed("deno"),
  Cow::Borrowed("node"),
  Cow::Borrowed("import"),
];
pub static REQUIRE_CONDITIONS: &[Cow<'static, str>] =
  &[Cow::Borrowed("require"), Cow::Borrowed("node")];
static TYPES_ONLY_CONDITIONS: &[Cow<'static, str>] = &[Cow::Borrowed("types")];

#[derive(Debug, Default, Clone)]
pub struct ConditionOptions {
  pub conditions: Vec<Cow<'static, str>>,
  /// Provide a value to override the default import conditions.
  ///
  /// Defaults to `["deno", "node", "import"]`
  pub import_conditions_override: Option<Vec<Cow<'static, str>>>,
  /// Provide a value to override the default require conditions.
  ///
  /// Defaults to `["require", "node"]`
  pub require_conditions_override: Option<Vec<Cow<'static, str>>>,
}

#[derive(Debug, Clone)]
struct ConditionResolver {
  import_conditions: Cow<'static, [Cow<'static, str>]>,
  require_conditions: Cow<'static, [Cow<'static, str>]>,
}

impl ConditionResolver {
  pub fn new(options: ConditionOptions) -> Self {
    fn combine_conditions(
      user_conditions: Cow<'_, [Cow<'static, str>]>,
      override_default: Option<Vec<Cow<'static, str>>>,
      default_conditions: &'static [Cow<'static, str>],
    ) -> Cow<'static, [Cow<'static, str>]> {
      if user_conditions.is_empty() {
        Cow::Borrowed(default_conditions)
      } else {
        let default_conditions = override_default
          .map(Cow::Owned)
          .unwrap_or(Cow::Borrowed(default_conditions));
        let mut new =
          Vec::with_capacity(user_conditions.len() + default_conditions.len());
        let mut append =
          |conditions: Cow<'_, [Cow<'static, str>]>| match conditions {
            Cow::Borrowed(conditions) => new.extend(conditions.iter().cloned()),
            Cow::Owned(conditions) => new.extend(conditions),
          };
        append(user_conditions);

Review comment (suggestion):
I noticed we don't need to expose this and only need to expose the options. Saves having to import one extra type in the caller.

Code after:
use crate::PathClean;

pub static IMPORT_CONDITIONS: &[Cow<'static, str>] = &[
  Cow::Borrowed("deno"),
  Cow::Borrowed("node"),
  Cow::Borrowed("import"),
];
pub static REQUIRE_CONDITIONS: &[Cow<'static, str>] =
  &[Cow::Borrowed("require"), Cow::Borrowed("node")];
static TYPES_ONLY_CONDITIONS: &[Cow<'static, str>] = &[Cow::Borrowed("types")];

#[derive(Debug, Default, Clone)]
pub struct NodeConditionOptions {
  pub conditions: Vec<Cow<'static, str>>,
  /// Provide a value to override the default import conditions.
  ///
  /// Defaults to `["deno", "node", "import"]`
  pub import_conditions_override: Option<Vec<Cow<'static, str>>>,
  /// Provide a value to override the default require conditions.
  ///
  /// Defaults to `["require", "node"]`
  pub require_conditions_override: Option<Vec<Cow<'static, str>>>,
}

#[derive(Debug, Clone)]
struct ConditionResolver {
  import_conditions: Cow<'static, [Cow<'static, str>]>,
  require_conditions: Cow<'static, [Cow<'static, str>]>,
}

impl ConditionResolver {
  pub fn new(options: NodeConditionOptions) -> Self {
    fn combine_conditions(
      user_conditions: Cow<'_, [Cow<'static, str>]>,
      override_default: Option<Vec<Cow<'static, str>>>,
      default_conditions: &'static [Cow<'static, str>],
    ) -> Cow<'static, [Cow<'static, str>]> {
      if user_conditions.is_empty() {
        Cow::Borrowed(default_conditions)
      } else {
        let default_conditions = override_default
          .map(Cow::Owned)
          .unwrap_or(Cow::Borrowed(default_conditions));
        let mut new =
          Vec::with_capacity(user_conditions.len() + default_conditions.len());
        let mut append =
          |conditions: Cow<'_, [Cow<'static, str>]>| match conditions {
            Cow::Borrowed(conditions) => new.extend(conditions.iter().cloned()),
            Cow::Owned(conditions) => new.extend(conditions),
          };
        append(user_conditions);