Skip to content

Commit

Permalink
Add, support require alias (.luaurc)
Browse files Browse the repository at this point in the history
  • Loading branch information
noonomyen committed Jan 26, 2025
1 parent 44480f7 commit 82fa810
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 28 deletions.
5 changes: 3 additions & 2 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"${workspaceFolder}/submodules/luau/Analysis/include",
"${workspaceFolder}/submodules/luau/Ast/include",
"${workspaceFolder}/submodules/luau/CLI/include",
"${workspaceFolder}/submodules/luau/Common/include"
"${workspaceFolder}/submodules/luau/Common/include",
"${workspaceFolder}/submodules/luau/Config/include"
],
"defines": [],
"compilerPath": "/usr/bin/g++",
Expand All @@ -19,4 +20,4 @@
}
],
"version": 4
}
}
49 changes: 48 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
{
"luau-lsp.sourcemap.enabled": false
"luau-lsp.sourcemap.enabled": false,
"files.associations": {
"*.luau": "luau",
"iostream": "cpp",
"fstream": "cpp",
"filesystem": "cpp",
"iosfwd": "cpp",
"array": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory_resource": "cpp",
"optional": "cpp",
"random": "cpp",
"string": "cpp",
"string_view": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"initializer_list": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"ranges": "cpp",
"span": "cpp",
"stdexcept": "cpp",
"typeinfo": "cpp",
"variant": "cpp",
"deque": "cpp",
"list": "cpp",
"unordered_set": "cpp",
"format": "cpp",
"text_encoding": "cpp",
"bitset": "cpp"
}
}
9 changes: 9 additions & 0 deletions example/.luaurc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"languageMode": "strict",
"lint": { "*": true, "LocalUnused": true },
"lintErrors": true,
"typeErrors": true,
"aliases": {
"print": "./lib/print"
}
}
2 changes: 0 additions & 2 deletions example/a.luau
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ print_counter("Counter A", counter_a)
print_counter("Counter B", counter_b)
print_counter("Counter C", counter_c)

print("A LOADED")

return {
counter_a = counter_a,
counter_b = counter_b,
Expand Down
2 changes: 0 additions & 2 deletions example/b.luau
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ counter_c:increment(10)
print_counter("Counter B", counter_b)
print_counter("Counter C", counter_c)

print("B LOADED")

return {
counter_b = counter_b,
counter_c = counter_c
Expand Down
2 changes: 0 additions & 2 deletions example/c.luau
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ print("----- C (C+15)")
counter_c:increment(15)
print_counter("Counter C", counter_c)

print("C LOADED")

return {
counter_c = counter_c
}
10 changes: 10 additions & 0 deletions example/hello/.luaurc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"languageMode": "strict",
"lint": { "*": true, "LocalUnused": true },
"lintErrors": true,
"typeErrors": true,
"aliases": {
"w": "./world",
"s": "./space"
}
}
6 changes: 1 addition & 5 deletions example/hello/init.luau
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
print("HELLO/INIT LOADED")

return {
hello = function()
print("Hello")
end
text = ("Hello" .. require("@s").text)
}
3 changes: 3 additions & 0 deletions example/hello/space/init.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
return {
text = (" " .. require("@w").text)
}
3 changes: 3 additions & 0 deletions example/hello/world.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
return {
text = "World!"
}
2 changes: 0 additions & 2 deletions example/lib/count.luau
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ function Counter:increment(amount: number?)
self.count += amount or 1
end

print("LIB/COUNT LOADED")

return Counter
2 changes: 0 additions & 2 deletions example/lib/pi.luau
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
print("LIB/PI LOADED")

return {
value = 3.141592653589793238462643383279502884197
}
2 changes: 0 additions & 2 deletions example/lib/print.luau
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ function print_counter(name: string, counter: Counter)
print(name .. " [" .. tostring(counter) .. "]: " .. counter:current())
end

print("LIB/PRINT LOADED")

return print_counter
7 changes: 2 additions & 5 deletions example/main.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local print_counter = require("./lib/print")
local print_counter = require("@print")
local counters = require("./a")
local hello = require("./hello").hello

print("----- MAIN")

Expand All @@ -19,6 +18,4 @@ print("-----")

print("Try to print Counter without require: " .. tostring(Counter))

hello()

print("MAIN LOADED")
print(require("./hello").text)
52 changes: 51 additions & 1 deletion src/luaumb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "Luau/ToString.h"
#include "Luau/FileUtils.h"
#include "Luau/Config.h"

#include "localizer.h"
#include "bundle.h"
Expand All @@ -31,6 +32,7 @@ int main(int argc, char** argv) {
const char* out_name = argv[2];

std::vector<Luau::ParseError> errors;
std::map<std::string, Luau::Config> configs;
RelativePathModule main_path(in_name);
LuauModuleBundle lmb(main_path);
std::queue<RelativePathModule> q;
Expand All @@ -49,11 +51,59 @@ int main(int argc, char** argv) {
return 1;
}

const std::string config_file_relative_path = module_path.relative.parent_path().string();

if (configs.find(config_file_relative_path) == configs.end()) {
Luau::Config config;

std::filesystem::path config_file_path = module_path.path;
config_file_path.replace_filename(Luau::kConfigName);

std::filesystem::path relative = module_path.relative.parent_path().parent_path();
while (relative.string() != "/") {
const auto it = configs.find(relative.string());
if (it != configs.end()) {
config = Luau::Config(it->second);
break;
} else {
relative = relative.parent_path();
}
}

if (std::filesystem::exists(config_file_path.string())) {
std::optional<std::string> config_file = readFile(config_file_path);

if (!config_file) {
std::cout << "Couldn't read config " << module_path.path << std::endl;
return 1;
}

Luau::ConfigOptions config_option = {false, std::optional<Luau::ConfigOptions::AliasOptions>({config_file_relative_path, true})};
std::optional<std::string> error = Luau::parseConfig(*config_file, config, config_option);

if (error) throw std::runtime_error("Error parsing config: " + *error + "\n File: " + module_path.path.string());
}

configs[config_file_relative_path] = config;
}

const Luau::Config& config = configs[config_file_relative_path];

RequireFunctionLocalizerResult result = require_function_localizer(*file);
errors.insert(errors.end(), result.parse_errors.begin(), result.parse_errors.end());

for (ExprCallRequire& require : result.list) {
RelativePathModule next = relative_path_module(module_path, require.path);
std::filesystem::path path;
if (require.path.rfind("./", 0) == 0 || require.path.rfind("../", 0) == 0) {
path = require.path;
} else if (require.path.rfind("@", 0) == 0) {
const auto alias_info = config.aliases.find(require.path.substr(1));
if (alias_info == NULL) throw std::runtime_error("Error, is not a valid alias: " + require.path);
path = alias_info->configLocation;
path = path.lexically_relative(module_path.relative.parent_path()).relative_path() / std::filesystem::path(alias_info->value);
}

RelativePathModule next = relative_path_module(module_path, path);
require.name = next.relative.string();
lmb.add_dependency(module_path.relative.string(), next.relative.string());
q.push(next);
Expand Down
6 changes: 4 additions & 2 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ const std::array<const string, 4> module_suffixes = {".luau", ".lua", "/init.lua

RelativePathModule::RelativePathModule(const fs::path& root, const fs::path& relative, const fs::path& path) {
this->root = root;
this->relative = relative;
this->relative = relative.is_absolute() ? relative : fs::path("/") / relative;
this->path = path;
}

RelativePathModule::RelativePathModule(const fs::path& main_path) {
this->path = main_path.lexically_normal();
this->relative = main_path.filename();
fs::path root_relative("/");
root_relative += main_path.filename();
this->relative = root_relative;
this->root = main_path.parent_path();

if (!fs::exists(this->path)) {
Expand Down

0 comments on commit 82fa810

Please sign in to comment.