Look for `.luau` before `.lua` in REPL & Analyze (#97) (#124)

As discussed in the issue, Luau has evolved from Lua to the point
where a new default extension `.luau` would be needed.

This change makes the REPL and Analyze look for `.luau`
extension first and if not found, fall back to `.lua`.
This commit is contained in:
Roni N. (Kittenz) 2021-11-06 04:11:26 +02:00 committed by GitHub
parent 6342913533
commit 1e1d1f58e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 8 deletions

View File

@ -115,7 +115,12 @@ struct CliFileResolver : Luau::FileResolver
{
if (Luau::AstExprConstantString* expr = node->as<Luau::AstExprConstantString>())
{
Luau::ModuleName name = std::string(expr->value.data, expr->value.size) + ".lua";
Luau::ModuleName name = std::string(expr->value.data, expr->value.size) + ".luau";
if (!moduleExists(name))
{
// fall back to .lua if a module with .luau doesn't exist
name = std::string(expr->value.data, expr->value.size) + ".lua";
}
return {{name}};
}
@ -236,8 +241,15 @@ int main(int argc, char** argv)
if (isDirectory(argv[i]))
{
traverseDirectory(argv[i], [&](const std::string& name) {
if (name.length() > 4 && name.rfind(".lua") == name.length() - 4)
// Look for .luau first and if absent, fall back to .lua
if (name.length() > 5 && name.rfind(".luau") == name.length() - 5)
{
failed += !analyzeFile(frontend, name.c_str(), format, annotate);
}
else if (name.length() > 4 && name.rfind(".lua") == name.length() - 4)
{
failed += !analyzeFile(frontend, name.c_str(), format, annotate);
}
});
}
else
@ -256,5 +268,3 @@ int main(int argc, char** argv)
return (format == ReportFormat::Luacheck) ? 0 : failed;
}

View File

@ -51,9 +51,13 @@ static int lua_require(lua_State* L)
return finishrequire(L);
lua_pop(L, 1);
std::optional<std::string> source = readFile(name + ".lua");
std::optional<std::string> source = readFile(name + ".luau");
if (!source)
luaL_argerrorL(L, 1, ("error loading " + name).c_str());
{
source = readFile(name + ".lua"); // try .lua if .luau doesn't exist
if (!source)
luaL_argerrorL(L, 1, ("error loading " + name).c_str()); // if neither .luau nor .lua exist, we have an error
}
// module needs to run in a new thread, isolated from the rest
lua_State* GL = lua_mainthread(L);
@ -511,5 +515,3 @@ int main(int argc, char** argv)
return failed;
}
}