Skip to content

Commit

Permalink
fix: panic when ? is passed in (#70)
Browse files Browse the repository at this point in the history
closes #68
  • Loading branch information
Boshen authored Feb 2, 2024
1 parent b65dca0 commit 7cfa4d0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 101,8 @@ impl ResolveError {
/// Error for [ResolveError::Specifier]
#[derive(Debug, Clone, Eq, PartialEq, Error)]
pub enum SpecifierError {
#[error("The specifiers must be a non-empty string. Received ''")]
Empty,
#[error("The specifiers must be a non-empty string. Received \"{0}\"")]
Empty(String),
}

/// JSON error from [serde_json::Error]
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 966,11 @@ impl<Fs: FileSystem Default> ResolverGeneric<Fs> {
let mut extended_tsconfig_paths = vec![];
for tsconfig_extend_specifier in &tsconfig.extends {
let extended_tsconfig_path = match tsconfig_extend_specifier.as_bytes().first() {
None => return Err(ResolveError::Specifier(SpecifierError::Empty)),
None => {
return Err(ResolveError::Specifier(SpecifierError::Empty(
tsconfig_extend_specifier.to_string(),
)))
}
Some(b'/') => PathBuf::from(tsconfig_extend_specifier),
Some(b'.') => tsconfig.directory().normalize_with(tsconfig_extend_specifier),
_ => self
Expand Down
14 changes: 11 additions & 3 deletions src/specifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 15,16 @@ impl<'a> Specifier<'a> {

pub fn parse(specifier: &'a str) -> Result<Specifier<'a>, SpecifierError> {
if specifier.is_empty() {
return Err(SpecifierError::Empty);
return Err(SpecifierError::Empty(specifier.to_string()));
}
let offset = match specifier.as_bytes()[0] {
b'/' | b'.' | b'#' => 1,
_ => 0,
};
let (path, query, fragment) = Self::parse_query_framgment(specifier, offset);
if path.is_empty() {
return Err(SpecifierError::Empty(specifier.to_string()));
}
Ok(Self { path, query, fragment })
}

Expand Down Expand Up @@ -81,8 84,13 @@ mod tests {

#[test]
fn empty() {
let specifier = "";
assert_eq!(Specifier::parse(specifier), Err(SpecifierError::Empty));
let specifiers = ["", "?"];
for specifier in specifiers {
assert_eq!(
Specifier::parse(specifier),
Err(SpecifierError::Empty(specifier.to_string()))
);
}
}

#[test]
Expand Down

0 comments on commit 7cfa4d0

Please sign in to comment.