From b668ffb8c8d126f9e9b16d4fcb92b8d08411b4b2 Mon Sep 17 00:00:00 2001 From: karl-police Date: Tue, 22 Jul 2025 19:52:24 +0200 Subject: [PATCH] Add "AstStatDeclareGlobal" to the Transpiler, so you can use it with Luau::toString (#1889) --- Analysis/src/Transpiler.cpp | 9 +++++++++ tests/Transpiler.test.cpp | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/Analysis/src/Transpiler.cpp b/Analysis/src/Transpiler.cpp index 813771c6..548c154c 100644 --- a/Analysis/src/Transpiler.cpp +++ b/Analysis/src/Transpiler.cpp @@ -1252,6 +1252,15 @@ struct Printer writer.symbol(")"); } + else if (const auto& a = program.as()) + { + writer.keyword("declare"); + writer.advance(a->nameLocation.begin); + writer.identifier(a->name.value); + + writer.symbol(":"); + visualizeTypeAnnotation(*a->type); + } else { LUAU_ASSERT(!"Unknown AstStat"); diff --git a/tests/Transpiler.test.cpp b/tests/Transpiler.test.cpp index d57b1473..cd47e8ff 100644 --- a/tests/Transpiler.test.cpp +++ b/tests/Transpiler.test.cpp @@ -1568,6 +1568,22 @@ TEST_CASE_FIXTURE(Fixture, "transpile_parse_error") CHECK_EQ("Expected identifier when parsing expression, got ", 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'";