From 65f2319a647a9a69324b7d8c39731a9c741af98f Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Wed, 28 Jun 2023 10:58:02 +0200 Subject: [PATCH] Add support for init files --- CHANGELOG.md | 7 +++++++ packages/lib/src/importer/require.rs | 19 ++++++++++++++----- packages/lib/src/tests.rs | 1 + tests/require/modules/init.luau | 4 ++++ tests/require/tests/directories.luau | 15 +++++++++++++++ tests/require/tests/modules/init.luau | 4 ++++ tests/require/tests/modules/modules/init.luau | 4 ++++ 7 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 tests/require/modules/init.luau create mode 100644 tests/require/tests/directories.luau create mode 100644 tests/require/tests/modules/init.luau create mode 100644 tests/require/tests/modules/modules/init.luau diff --git a/CHANGELOG.md b/CHANGELOG.md index 22ba480..003eb63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Added + +- Added support for `init` files in directories, similar to Rojo, or `index.js` or `mod.rs` in JavaScript / Rust.
+ This means that placing a file named `init.luau` or `init.luau` in a directory will now let you `require` that directory. + ## `0.7.1` - June 17th, 2023 ### Added diff --git a/packages/lib/src/importer/require.rs b/packages/lib/src/importer/require.rs index 516e39c..492c4ad 100644 --- a/packages/lib/src/importer/require.rs +++ b/packages/lib/src/importer/require.rs @@ -133,11 +133,20 @@ impl<'lua> RequireContext<'lua> { ) { (Ok(luau), _) => luau, (_, Ok(lua)) => lua, - _ => { - return Err(LuaError::RuntimeError(format!( - "File does not exist at path '{require_path}'" - ))) - } + // If we did not find a luau/lua file at the wanted path, + // we should also look for "init" files in directories + _ => match ( + canonicalize(path_relative_to_pwd.join("init").with_extension("luau")), + canonicalize(path_relative_to_pwd.join("init").with_extension("lua")), + ) { + (Ok(luau), _) => luau, + (_, Ok(lua)) => lua, + _ => { + return Err(LuaError::RuntimeError(format!( + "File does not exist at path '{require_path}'" + ))) + } + }, }; let absolute = file_path.to_string_lossy().to_string(); let relative = absolute.trim_start_matches(&self.pwd).to_string(); diff --git a/packages/lib/src/tests.rs b/packages/lib/src/tests.rs index e398f27..6c32104 100644 --- a/packages/lib/src/tests.rs +++ b/packages/lib/src/tests.rs @@ -70,6 +70,7 @@ create_tests! { require_async_sequential: "require/tests/async_sequential", require_builtins: "require/tests/builtins", require_children: "require/tests/children", + require_directories: "require/tests/directories", require_invalid: "require/tests/invalid", require_nested: "require/tests/nested", require_parents: "require/tests/parents", diff --git a/tests/require/modules/init.luau b/tests/require/modules/init.luau new file mode 100644 index 0000000..cb3159b --- /dev/null +++ b/tests/require/modules/init.luau @@ -0,0 +1,4 @@ +return { + Foo = "Bar", + Hello = "World", +} diff --git a/tests/require/tests/directories.luau b/tests/require/tests/directories.luau new file mode 100644 index 0000000..c01df2a --- /dev/null +++ b/tests/require/tests/directories.luau @@ -0,0 +1,15 @@ +local module = require("./modules") + +assert(type(module) == "table", "Required module did not return a table") +assert(module.Foo == "Bar", "Required module did not contain correct values") +assert(module.Hello == "World", "Required module did not contain correct values") + +module = require("modules") +assert(module.Foo == "Bar", "Required module did not contain correct values") +assert(module.Hello == "World", "Required module did not contain correct values") + +module = require("modules/modules") +assert(module.Foo == "Bar", "Required module did not contain correct values") +assert(module.Hello == "World", "Required module did not contain correct values") + +return true diff --git a/tests/require/tests/modules/init.luau b/tests/require/tests/modules/init.luau new file mode 100644 index 0000000..cb3159b --- /dev/null +++ b/tests/require/tests/modules/init.luau @@ -0,0 +1,4 @@ +return { + Foo = "Bar", + Hello = "World", +} diff --git a/tests/require/tests/modules/modules/init.luau b/tests/require/tests/modules/modules/init.luau new file mode 100644 index 0000000..cb3159b --- /dev/null +++ b/tests/require/tests/modules/modules/init.luau @@ -0,0 +1,4 @@ +return { + Foo = "Bar", + Hello = "World", +}