Skip to content

Commit

Permalink
Look for .luau before .lua in REPL & Analyze (#97) (#124)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Roni N. (Kittenz) committed Nov 6, 2021
1 parent 6342913 commit 1e1d1f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
18 changes: 14 additions & 4 deletions CLI/Analyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}};
}
Expand Down Expand Up @@ -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
Expand All @@ -256,5 +268,3 @@ int main(int argc, char** argv)

return (format == ReportFormat::Luacheck) ? 0 : failed;
}


10 changes: 6 additions & 4 deletions CLI/Repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -511,5 +515,3 @@ int main(int argc, char** argv)
return failed;
}
}


0 comments on commit 1e1d1f5

Please sign in to comment.