Skip to content

Commit

Permalink
feat: functions to add more options using builder pattern (#81)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Boshen <boshenc@gmail.com>
  • Loading branch information
ematipico and Boshen authored Feb 8, 2024
1 parent c1ab462 commit 339133f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,6 @@ jobs:
- uses: actions/checkout@v4
- uses: ./.github/actions/pnpm
- uses: ./.github/actions/rustup
- run: cargo test --quiet
- run: |
cargo test --quiet
cargo test --doc --quiet
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//!
//! ## Example
//!
//! ```rust
//! ```rust,ignore
#![doc = include_str!("../examples/resolver.rs")]
//! ```
Expand Down
48 changes: 48 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::path::Path;
use std::{fmt, path::PathBuf};

/// Module Resolution Options
Expand Down Expand Up @@ -130,6 +131,53 @@ pub struct ResolveOptions {
pub builtin_modules: bool,
}

impl ResolveOptions {
/// ## Examples
///
/// ```
/// use oxc_resolver::ResolveOptions;
///
/// let options = ResolveOptions::default().with_condition_names(&["bar"]);
/// assert_eq!(options.condition_names, vec!["bar".to_string()])
/// ```
#[must_use]
pub fn with_condition_names(mut self, names: &[&str]) -> Self {
self.condition_names = names.iter().map(ToString::to_string).collect::<Vec<String>>();
self
}

/// ## Examples
///
/// ```
/// use oxc_resolver::ResolveOptions;
///
/// let options = ResolveOptions::default().with_builtin_modules(false);
/// assert_eq!(options.builtin_modules, false)
/// ```
#[must_use]
pub fn with_builtin_modules(mut self, flag: bool) -> Self {
self.builtin_modules = flag;
self
}

/// Adds a single root to the options
///
/// ## Examples
///
/// ```
/// use oxc_resolver::ResolveOptions;
/// use std::path::{Path, PathBuf};
///
/// let options = ResolveOptions::default().with_root("foo");
/// assert_eq!(options.roots, vec![PathBuf::from("foo")])
/// ```
#[must_use]
pub fn with_root<P: AsRef<Path>>(mut self, root: P) -> Self {
self.roots.push(root.as_ref().to_path_buf());
self
}
}

/// Value for [ResolveOptions::enforce_extension]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EnforceExtension {
Expand Down

0 comments on commit 339133f

Please # to comment.