Skip to content

Commit 339133f

Browse files
ematipicoBoshen
andauthored
feat: functions to add more options using builder pattern (#81)
--------- Co-authored-by: Boshen <[email protected]>
1 parent c1ab462 commit 339133f

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,6 @@ jobs:
217217
- uses: actions/checkout@v4
218218
- uses: ./.github/actions/pnpm
219219
- uses: ./.github/actions/rustup
220-
- run: cargo test --quiet
220+
- run: |
221+
cargo test --quiet
222+
cargo test --doc --quiet

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//!
1818
//! ## Example
1919
//!
20-
//! ```rust
20+
//! ```rust,ignore
2121
#![doc = include_str!("../examples/resolver.rs")]
2222
//! ```
2323

src/options.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::path::Path;
12
use std::{fmt, path::PathBuf};
23

34
/// Module Resolution Options
@@ -130,6 +131,53 @@ pub struct ResolveOptions {
130131
pub builtin_modules: bool,
131132
}
132133

134+
impl ResolveOptions {
135+
/// ## Examples
136+
///
137+
/// ```
138+
/// use oxc_resolver::ResolveOptions;
139+
///
140+
/// let options = ResolveOptions::default().with_condition_names(&["bar"]);
141+
/// assert_eq!(options.condition_names, vec!["bar".to_string()])
142+
/// ```
143+
#[must_use]
144+
pub fn with_condition_names(mut self, names: &[&str]) -> Self {
145+
self.condition_names = names.iter().map(ToString::to_string).collect::<Vec<String>>();
146+
self
147+
}
148+
149+
/// ## Examples
150+
///
151+
/// ```
152+
/// use oxc_resolver::ResolveOptions;
153+
///
154+
/// let options = ResolveOptions::default().with_builtin_modules(false);
155+
/// assert_eq!(options.builtin_modules, false)
156+
/// ```
157+
#[must_use]
158+
pub fn with_builtin_modules(mut self, flag: bool) -> Self {
159+
self.builtin_modules = flag;
160+
self
161+
}
162+
163+
/// Adds a single root to the options
164+
///
165+
/// ## Examples
166+
///
167+
/// ```
168+
/// use oxc_resolver::ResolveOptions;
169+
/// use std::path::{Path, PathBuf};
170+
///
171+
/// let options = ResolveOptions::default().with_root("foo");
172+
/// assert_eq!(options.roots, vec![PathBuf::from("foo")])
173+
/// ```
174+
#[must_use]
175+
pub fn with_root<P: AsRef<Path>>(mut self, root: P) -> Self {
176+
self.roots.push(root.as_ref().to_path_buf());
177+
self
178+
}
179+
}
180+
133181
/// Value for [ResolveOptions::enforce_extension]
134182
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
135183
pub enum EnforceExtension {

0 commit comments

Comments
 (0)