Skip to content

Commit

Permalink
feat: 🎸 get builtin module original specifier
Browse files Browse the repository at this point in the history
  • Loading branch information
IWANABETHATGUY committed Oct 21, 2024
1 parent 2d03390 commit 201f70c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ pub enum ResolveError {
///
/// This is an error due to not being a Node.js runtime.
/// The `alias` option can be used to resolve a builtin module to a polyfill.
#[error("Builtin module {0}")]
Builtin(String),
/// User could get specifier before adding `node:` by `prefixed_with_node_colon`, using
/// boolean to avoid allocation
#[error("Builtin module {resolved}")]
Builtin { resolved: String, prefixed_with_node_colon: bool },

/// All of the aliased extension are not found
#[error("All of the aliased extension are not found")]
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,10 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
if !starts_with_node {
specifier = format!("node:{specifier}");
}
return Err(ResolveError::Builtin(specifier));
return Err(ResolveError::Builtin {
resolved: specifier,
prefixed_with_node_colon: !starts_with_node,
});
}
}
Ok(())
Expand Down
12 changes: 10 additions & 2 deletions src/tests/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ fn builtins() {
for request in pass {
let prefixed_request = format!("node:{request}");
for request in [prefixed_request.clone(), request.to_string()] {
let starts_with_node = request.starts_with("node:");
let resolved_path = resolver.resolve(f, &request).map(|r| r.full_path());
let err = ResolveError::Builtin(prefixed_request.clone());
let err = ResolveError::Builtin {
resolved: prefixed_request.clone(),
prefixed_with_node_colon: !starts_with_node,
};
assert_eq!(resolved_path, Err(err), "{request}");
}
}
Expand All @@ -114,8 +118,12 @@ fn imports() {
});

for request in ["#fs", "#http"] {
let prefixed_with_node_colon = request == "#fs";
let resolved_path = resolver.resolve(f.clone(), request).map(|r| r.full_path());
let err = ResolveError::Builtin(format!("node:{}", request.trim_start_matches('#')));
let err = ResolveError::Builtin {
resolved: (format!("node:{}", request.trim_start_matches('#'))),
prefixed_with_node_colon,
};
assert_eq!(resolved_path, Err(err));
}
}

0 comments on commit 201f70c

Please sign in to comment.