Add support for init files

This commit is contained in:
Filip Tibell 2023-06-28 10:58:02 +02:00
parent 594e773236
commit 65f2319a64
No known key found for this signature in database
7 changed files with 49 additions and 5 deletions

View file

@ -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. <br/>
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

View file

@ -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();

View file

@ -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",

View file

@ -0,0 +1,4 @@
return {
Foo = "Bar",
Hello = "World",
}

View file

@ -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

View file

@ -0,0 +1,4 @@
return {
Foo = "Bar",
Hello = "World",
}

View file

@ -0,0 +1,4 @@
return {
Foo = "Bar",
Hello = "World",
}