Skip to content

Commit f1ad5fe

Browse files
CST: Improve serialization of table expressions (#250)
This PR fixes some bugs in the serialization of table expressions, and adds some regression tests for that.
1 parent 5a8b250 commit f1ad5fe

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

luau/src/luau.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,15 @@ struct AstSerialize : public Luau::AstVisitor
349349
void serialize(Luau::AstExprTable::Item& item, Luau::CstExprTable::Item* cstNode)
350350
{
351351
lua_rawcheckstack(L, 2);
352-
lua_createtable(L, 0, 6);
352+
lua_createtable(L, 0, 7);
353353

354354
if (item.kind == Luau::AstExprTable::Item::List)
355355
{
356356
lua_pushstring(L, "list");
357357
lua_setfield(L, -2, "kind");
358+
359+
visit(item.value);
360+
lua_setfield(L, -2, "value");
358361
}
359362
else if (item.kind == Luau::AstExprTable::Item::Record)
360363
{
@@ -376,7 +379,7 @@ struct AstSerialize : public Luau::AstVisitor
376379
}
377380
else if (item.kind == Luau::AstExprTable::Item::General)
378381
{
379-
lua_pushstring(L, "record");
382+
lua_pushstring(L, "general");
380383
lua_setfield(L, -2, "kind");
381384

382385
if (cstNode && cstNode->indexerOpenPosition)
@@ -390,17 +393,17 @@ struct AstSerialize : public Luau::AstVisitor
390393

391394
if (cstNode)
392395
{
393-
if (cstNode->equalsPosition)
394-
{
395-
serializeToken(cstNode->equalsPosition.value(), "=");
396-
lua_setfield(L, -2, "equals");
397-
}
398-
399396
if (cstNode->indexerClosePosition)
400397
{
401398
serializeToken(cstNode->indexerClosePosition.value(), "]");
402399
lua_setfield(L, -2, "indexerClose");
403400
}
401+
402+
if (cstNode->equalsPosition)
403+
{
404+
serializeToken(cstNode->equalsPosition.value(), "=");
405+
lua_setfield(L, -2, "equals");
406+
}
404407
}
405408

406409
visit(item.value);

tests/astSerializerTests/table-1.luau

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local x = { 1, 2, 3 }

tests/astSerializerTests/table-2.luau

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
local x = {
2+
["test"] = true,
3+
}

tests/testAstSerializer.spec.luau

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ local function test_roundtrippableAst()
147147
"tests/astSerializerTests/interpolated-string-2.luau",
148148
"tests/astSerializerTests/local-function-declaration-1.luau",
149149
"tests/astSerializerTests/numeric-for-loop-1.luau",
150+
"tests/astSerializerTests/table-1.luau",
151+
"tests/astSerializerTests/table-2.luau",
150152
"tests/astSerializerTests/type-assertion-1.luau",
151153
"tests/astSerializerTests/type-singletons-1.luau",
152154
"tests/astSerializerTests/while-1.luau",

0 commit comments

Comments
 (0)