Skip to content

Commit 3be0636

Browse files
authored
chore: add precautionary measures to top level require methods (#94)
closes #93
1 parent d59a4a6 commit 3be0636

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/lib.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,6 @@ impl<Fs: FileSystem + Default> ResolverGeneric<Fs> {
260260
self.require_hash(cached_path, specifier, ctx)
261261
}
262262
_ => {
263-
debug_assert!(Path::new(specifier)
264-
.components()
265-
.next()
266-
.is_some_and(|c| matches!(c, Component::Normal(_))));
267263
// 1. If X is a core module,
268264
// a. return the core module
269265
// b. STOP
@@ -292,6 +288,11 @@ impl<Fs: FileSystem + Default> ResolverGeneric<Fs> {
292288
specifier: &str,
293289
ctx: &mut Ctx,
294290
) -> Result<CachedPath, ResolveError> {
291+
// Make sure only path prefixes gets called
292+
debug_assert!(Path::new(specifier)
293+
.components()
294+
.next()
295+
.is_some_and(|c| matches!(c, Component::RootDir | Component::Prefix(_))));
295296
if !self.options.prefer_relative && self.options.prefer_absolute {
296297
if let Ok(path) = self.load_package_self_or_node_modules(cached_path, specifier, ctx) {
297298
return Ok(path);
@@ -327,6 +328,11 @@ impl<Fs: FileSystem + Default> ResolverGeneric<Fs> {
327328
specifier: &str,
328329
ctx: &mut Ctx,
329330
) -> Result<CachedPath, ResolveError> {
331+
// Make sure only relative or normal paths gets called
332+
debug_assert!(Path::new(specifier).components().next().is_some_and(|c| matches!(
333+
c,
334+
Component::CurDir | Component::ParentDir | Component::Normal(_)
335+
)));
330336
let path = cached_path.path().normalize_with(specifier);
331337
let cached_path = self.cache.value(&path);
332338
// a. LOAD_AS_FILE(Y + X)
@@ -344,6 +350,7 @@ impl<Fs: FileSystem + Default> ResolverGeneric<Fs> {
344350
specifier: &str,
345351
ctx: &mut Ctx,
346352
) -> Result<CachedPath, ResolveError> {
353+
debug_assert_eq!(specifier.chars().next(), Some('#'));
347354
// a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))
348355
if let Some(path) = self.load_package_imports(cached_path, specifier, ctx)? {
349356
return Ok(path);
@@ -357,6 +364,11 @@ impl<Fs: FileSystem + Default> ResolverGeneric<Fs> {
357364
specifier: &str,
358365
ctx: &mut Ctx,
359366
) -> Result<CachedPath, ResolveError> {
367+
// Make sure no other path prefixes gets called
368+
debug_assert!(Path::new(specifier)
369+
.components()
370+
.next()
371+
.is_some_and(|c| matches!(c, Component::Normal(_))));
360372
if self.options.prefer_relative {
361373
if let Ok(path) = self.require_relative(cached_path, specifier, ctx) {
362374
return Ok(path);

0 commit comments

Comments
 (0)