Skip to content

Commit 8949edf

Browse files
committed
chore: clean up docs and remove some pub fields
1 parent 15df9b1 commit 8949edf

File tree

7 files changed

+42
-33
lines changed

7 files changed

+42
-33
lines changed

examples/resolver.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
1-
// Instruction:
2-
//
3-
// run `cargo run --example resolver -- `pwd` test.js`
4-
// or `cargo watch -x "run --example resolver" -- `pwd` test.js`
5-
//
6-
// NOTE: The first argument must be a absolute path.
7-
81
use std::{env, path::PathBuf};
92

103
use oxc_resolver::{AliasValue, ResolveOptions, Resolver};
114

125
fn main() {
13-
let path = env::args().nth(1).expect("require path");
14-
let request = env::args().nth(2).expect("require request");
6+
// Path to directory, must be in absolute path.
7+
let path = env::args().nth(1).expect("path");
8+
let specifier = env::args().nth(2).expect("specifier");
159
let path = PathBuf::from(path).canonicalize().unwrap();
1610

1711
println!("path: {path:?}");
18-
println!("request: {request}");
12+
println!("request: {specifier}");
1913

2014
let options = ResolveOptions {
2115
alias_fields: vec![vec!["browser".into()]],
22-
alias: vec![("/asdf".into(), vec![AliasValue::Path("./test.js".into())])],
16+
alias: vec![("asdf".into(), vec![AliasValue::Path("./test.js".into())])],
2317
..ResolveOptions::default()
2418
};
2519

26-
match Resolver::new(options).resolve(path, &request) {
20+
match Resolver::new(options).resolve(path, &specifier) {
2721
Err(error) => println!("Error: {error}"),
28-
Ok(resolution) => println!("Resolved: {}", resolution.full_path().to_string_lossy()),
22+
Ok(resolution) => println!("Resolved: {:?}", resolution.full_path()),
2923
}
3024
}

src/error.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::{io, path::PathBuf, sync::Arc};
22
use thiserror::Error;
33

4-
/// All resolution errors.
4+
/// All resolution errors
5+
///
6+
/// `thiserror` is used to display meaningful error messages.
57
#[derive(Debug, Clone, PartialEq, Error)]
68
pub enum ResolveError {
79
/// Ignored path
@@ -96,13 +98,14 @@ impl ResolveError {
9698
}
9799
}
98100

101+
/// Error for [ResolveError::Specifier]
99102
#[derive(Debug, Clone, Eq, PartialEq, Error)]
100103
pub enum SpecifierError {
101-
#[error("[ERR_INVALID_ARG_VALUE]: The specifiers must be a non-empty string. Received ''")]
104+
#[error("The specifiers must be a non-empty string. Received ''")]
102105
Empty,
103106
}
104107

105-
/// JSON error from [serde_json::Error].
108+
/// JSON error from [serde_json::Error]
106109
#[derive(Debug, Clone, Eq, PartialEq)]
107110
pub struct JSONError {
108111
pub path: PathBuf,

src/file_system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub trait FileSystem: Send + Sync {
5353
fn canonicalize(&self, path: &Path) -> io::Result<PathBuf>;
5454
}
5555

56-
/// Metadata information about a file.
56+
/// Metadata information about a file
5757
#[derive(Debug, Clone, Copy)]
5858
pub struct FileMetadata {
5959
pub(crate) is_file: bool,

src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22
//!
33
//! Node.js Module Resolution.
44
//!
5-
//! All configuration options are aligned with [enhanced-resolve]
5+
//! All configuration options are aligned with [enhanced-resolve].
66
//!
77
//! ## References:
88
//!
9-
//! * Tests ported from [enhanced-resolve]
109
//! * Algorithm adapted from Node.js [CommonJS Module Resolution Algorithm] and [ECMAScript Module Resolution Algorithm]
10+
//! * Tests are ported from [enhanced-resolve]
1111
//! * Some code adapted from [parcel-resolver]
1212
//!
1313
//! [enhanced-resolve]: https://github.com/webpack/enhanced-resolve
1414
//! [CommonJS Module Resolution Algorithm]: https://nodejs.org/api/modules.html#all-together
1515
//! [ECMAScript Module Resolution Algorithm]: https://nodejs.org/api/esm.html#resolution-algorithm-specification
1616
//! [parcel-resolver]: https://github.com/parcel-bundler/parcel/blob/v2/packages/utils/node-resolver-rs
17+
//!
18+
//! ## Example
19+
//!
20+
//! ```rust
21+
#![doc = include_str!("../examples/resolver.rs")]
22+
//! ```
1723
1824
mod builtins;
1925
mod cache;
@@ -63,7 +69,7 @@ pub use crate::{
6369
/// Resolver with the current operating system as the file system
6470
pub type Resolver = ResolverGeneric<FileSystemOs>;
6571

66-
/// Generic implementation of the resolver, can be configured by the [FileSystem] trait.
72+
/// Generic implementation of the resolver, can be configured by the [FileSystem] trait
6773
pub struct ResolverGeneric<Fs> {
6874
options: ResolveOptions,
6975
cache: Arc<Cache<Fs>>,
@@ -153,10 +159,12 @@ impl<Fs: FileSystem + Default> ResolverGeneric<Fs> {
153159
Self { options: options.sanitize(), cache: Arc::clone(&self.cache) }
154160
}
155161

162+
/// Returns the options.
156163
pub fn options(&self) -> &ResolveOptions {
157164
&self.options
158165
}
159166

167+
/// Clear the underlying cache.
160168
pub fn clear_cache(&self) {
161169
self.cache.clear();
162170
}

src/options.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ pub struct ResolveOptions {
130130
pub builtin_modules: bool,
131131
}
132132

133+
/// Value for [ResolveOptions::enforce_extension]
133134
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
134135
pub enum EnforceExtension {
135136
Auto,
@@ -164,13 +165,14 @@ pub enum AliasValue {
164165
Ignore,
165166
}
166167

168+
/// Value for [ResolveOptions::restrictions]
167169
#[derive(Debug, Clone)]
168170
pub enum Restriction {
169171
Path(PathBuf),
170172
RegExp(String),
171173
}
172174

173-
/// Tsconfig Options
175+
/// Tsconfig Options for [ResolveOptions::tsconfig]
174176
///
175177
/// Derived from [tsconfig-paths-webpack-plugin](https://github.com/dividab/tsconfig-paths-webpack-plugin#options)
176178
#[derive(Debug, Clone)]
@@ -185,6 +187,7 @@ pub struct TsconfigOptions {
185187
pub references: TsconfigReferences,
186188
}
187189

190+
/// Configuration for [TsconfigOptions::references]
188191
#[derive(Debug, Clone)]
189192
pub enum TsconfigReferences {
190193
Disabled,

src/package_json.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ use crate::{path::PathUtil, ResolveError, ResolveOptions};
1515

1616
type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
1717

18+
/// Deserialized package.json
1819
#[derive(Debug, Deserialize)]
1920
pub struct PackageJson {
2021
/// Path to `package.json`. Contains the `package.json` filename.
2122
#[serde(skip)]
22-
pub path: PathBuf,
23+
pub(crate) path: PathBuf,
2324

2425
/// Realpath to `package.json`. Contains the `package.json` filename.
2526
#[serde(skip)]
26-
pub realpath: PathBuf,
27+
pub(crate) realpath: PathBuf,
2728

2829
#[serde(skip)]
29-
#[serde(default)]
30-
pub raw_json: Arc<serde_json::Value>,
30+
pub(crate) raw_json: Arc<serde_json::Value>,
3131

3232
/// The "name" field defines your package's name.
3333
/// The "name" field can be used in addition to the "exports" field to self-reference a package using its name.
3434
///
3535
/// <https://nodejs.org/api/packages.html#name>
36-
pub name: Option<String>,
36+
pub(crate) name: Option<String>,
3737

3838
/// The "main" field defines the entry point of a package when imported by name via a node_modules lookup. Its value is a path.
3939
/// When a package has an "exports" field, this will take precedence over the "main" field when importing the package by name.
@@ -42,26 +42,26 @@ pub struct PackageJson {
4242
///
4343
/// <https://nodejs.org/api/packages.html#main>
4444
#[serde(skip)]
45-
pub main_fields: Vec<String>,
45+
pub(crate) main_fields: Vec<String>,
4646

4747
/// The "exports" field allows defining the entry points of a package when imported by name loaded either via a node_modules lookup or a self-reference to its own name.
4848
///
4949
/// <https://nodejs.org/api/packages.html#exports>
5050
#[serde(skip)]
51-
pub exports: Vec<ExportsField>,
51+
pub(crate) exports: Vec<ExportsField>,
5252

5353
/// In addition to the "exports" field, there is a package "imports" field to create private mappings that only apply to import specifiers from within the package itself.
5454
///
5555
/// <https://nodejs.org/api/packages.html#subpath-imports>
5656
#[serde(default)]
57-
pub imports: Box<MatchObject>,
57+
pub(crate) imports: Box<MatchObject>,
5858

5959
/// The "browser" field is provided by a module author as a hint to javascript bundlers or component tools when packaging modules for client side use.
6060
/// Multiple values are configured by [ResolveOptions::alias_fields].
6161
///
6262
/// <https://github.com/defunctzombie/package-browser-field-spec>
6363
#[serde(skip)]
64-
pub browser_fields: Vec<BrowserField>,
64+
pub(crate) browser_fields: Vec<BrowserField>,
6565
}
6666

6767
/// `matchObj` defined in `PACKAGE_IMPORTS_EXPORTS_RESOLVE`
@@ -119,7 +119,7 @@ pub enum BrowserField {
119119
impl PackageJson {
120120
/// # Panics
121121
/// # Errors
122-
pub fn parse(
122+
pub(crate) fn parse(
123123
path: PathBuf,
124124
realpath: PathBuf,
125125
json: &str,
@@ -241,7 +241,7 @@ impl PackageJson {
241241
/// # Errors
242242
///
243243
/// * Returns [ResolveError::Ignored] for `"path": false` in `browser` field.
244-
pub fn resolve_browser_field(
244+
pub(crate) fn resolve_browser_field(
245245
&self,
246246
path: &Path,
247247
request: Option<&str>,

src/resolution.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
sync::Arc,
66
};
77

8-
/// The final path resolution with optional `?query` and `#fragment`.
8+
/// The final path resolution with optional `?query` and `#fragment`
99
#[derive(Clone)]
1010
pub struct Resolution {
1111
pub(crate) path: PathBuf,
@@ -58,6 +58,7 @@ impl Resolution {
5858
self.fragment.as_deref()
5959
}
6060

61+
/// Returns serialized package_json
6162
pub fn package_json(&self) -> Option<&Arc<PackageJson>> {
6263
self.package_json.as_ref()
6364
}

0 commit comments

Comments
 (0)