From 1bc4cb7c0548483980e27483022d6abf0888891d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Mon, 26 Feb 2024 16:01:32 +0900 Subject: [PATCH] fix(es/modules): Do not call `Path::parent()` for `FileName::Anon` (#8662) **Related issue:** - Closes #8660 --- crates/swc_ecma_transforms_module/src/path.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/swc_ecma_transforms_module/src/path.rs b/crates/swc_ecma_transforms_module/src/path.rs index 2166dd994c6..3df6d962f8b 100644 --- a/crates/swc_ecma_transforms_module/src/path.rs +++ b/crates/swc_ecma_transforms_module/src/path.rs @@ -7,7 +7,7 @@ use std::{ sync::Arc, }; -use anyhow::{Context, Error}; +use anyhow::{anyhow, Context, Error}; use path_clean::PathClean; use pathdiff::diff_paths; use swc_atoms::JsWord; @@ -276,7 +276,10 @@ where } }; let mut base = match base { - FileName::Real(v) => Cow::Borrowed(v), + FileName::Real(v) => Cow::Borrowed( + v.parent() + .ok_or_else(|| anyhow!("failed to get parent of {:?}", v))?, + ), FileName::Anon => { if cfg!(target_arch = "wasm32") { panic!("Please specify `filename`") @@ -303,13 +306,7 @@ where target.display() ); - let rel_path = diff_paths( - &target, - match base.parent() { - Some(v) => v, - None => &base, - }, - ); + let rel_path = diff_paths(&target, &*base); let rel_path = match rel_path { Some(v) => v,