Add "AstStatDeclareGlobal" to the Transpiler, so you can use it with Luau::toString (#1889)

This commit is contained in:
karl-police 2025-07-22 19:52:24 +02:00 committed by GitHub
parent 0ce993fe6c
commit b668ffb8c8
Signed by: DevComp
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 0 deletions

View file

@ -1252,6 +1252,15 @@ struct Printer
writer.symbol(")");
}
else if (const auto& a = program.as<AstStatDeclareGlobal>())
{
writer.keyword("declare");
writer.advance(a->nameLocation.begin);
writer.identifier(a->name.value);
writer.symbol(":");
visualizeTypeAnnotation(*a->type);
}
else
{
LUAU_ASSERT(!"Unknown AstStat");

View file

@ -1568,6 +1568,22 @@ TEST_CASE_FIXTURE(Fixture, "transpile_parse_error")
CHECK_EQ("Expected identifier when parsing expression, got <eof>", result.parseError);
}
TEST_CASE_FIXTURE(Fixture, "transpile_declare_global_stat")
{
std::string code = "declare _G: any";
ParseOptions options;
options.allowDeclarationSyntax = true;
auto allocator = Allocator{};
auto names = AstNameTable{allocator};
ParseResult parseResult = Parser::parse(code.data(), code.size(), names, allocator, options);
auto result = transpileWithTypes(*parseResult.root);
CHECK_EQ(result, code);
}
TEST_CASE_FIXTURE(Fixture, "transpile_to_string")
{
std::string code = "local a: string = 'hello'";