From f7d2d7cbb01e2d43227e3968d5163ce45cbf9266 Mon Sep 17 00:00:00 2001 From: daimond113 <72147841+daimond113@users.noreply.github.com> Date: Tue, 3 Dec 2024 21:29:39 +0100 Subject: [PATCH] fix: strip .luau extension from require paths --- CHANGELOG.md | 1 + src/linking/generator.rs | 43 ++++++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30ba44b..7492a2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Link dependencies before type extraction to support more use cases by @daimond113 +- Strip `.luau` extension from linker modules' require paths to comply with Luau by @daimond113 ## [0.5.0-rc.14] - 2024-11-30 ### Fixed diff --git a/src/linking/generator.rs b/src/linking/generator.rs index a675059..ed2add3 100644 --- a/src/linking/generator.rs +++ b/src/linking/generator.rs @@ -69,10 +69,29 @@ pub fn generate_lib_linking_module, S: AsRef>( fn luau_style_path(path: &Path) -> String { let path = path .components() - .filter_map(|ct| match ct { + .zip( + path.components() + .skip(1) + .map(Some) + .chain(std::iter::repeat(None)), + ) + .filter_map(|(ct, next_ct)| match ct { Component::CurDir => Some(".".to_string()), Component::ParentDir => Some("..".to_string()), - Component::Normal(part) => Some(format!("{}", part.to_string_lossy())), + Component::Normal(part) => { + let str = part.to_string_lossy(); + + Some( + (if next_ct.is_some() { + &str + } else { + str.strip_suffix(".luau") + .or_else(|| str.strip_suffix(".lua")) + .unwrap_or(&str) + }) + .to_string(), + ) + } _ => None, }) .collect::>() @@ -126,14 +145,26 @@ pub fn get_lib_require_path( let path = path .components() - .filter_map(|component| match component { + .zip( + path.components() + .skip(1) + .map(Some) + .chain(std::iter::repeat(None)), + ) + .filter_map(|(component, next_comp)| match component { Component::ParentDir => Some(".Parent".to_string()), Component::Normal(part) if part != "init.lua" && part != "init.luau" => { + let str = part.to_string_lossy(); + Some(format!( "[{:?}]", - part.to_string_lossy() - .trim_end_matches(".lua") - .trim_end_matches(".luau") + if next_comp.is_some() { + &str + } else { + str.strip_suffix(".luau") + .or_else(|| str.strip_suffix(".lua")) + .unwrap_or(&str) + } )) } _ => None,