From af0047eb9b21921013fe7b3b578851ec963e24cc Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 18 Feb 2026 22:56:19 +0800 Subject: Fix TableLit AST string formatting for comments and empty lines --- src/yuescript/yue_ast.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/yuescript/yue_ast.cpp b/src/yuescript/yue_ast.cpp index 3953f97..8c19df8 100644 --- a/src/yuescript/yue_ast.cpp +++ b/src/yuescript/yue_ast.cpp @@ -1151,18 +1151,32 @@ std::string TableLit_t::to_string(void* ud) const { return "{ }"s; } bool hasInBlockExp = false; + bool hasCommentOrEmpty = false; + ast_node* lastValueNode = nullptr; for (auto value : values.objects()) { + if (ast_is(value)) { + hasCommentOrEmpty = true; + continue; + } + lastValueNode = value; if (isInBlockExp(value, value == values.back())) { hasInBlockExp = true; - break; } } - if (hasInBlockExp) { + if (hasInBlockExp || hasCommentOrEmpty) { str_list temp; temp.emplace_back("{"s); info->pushScope(); for (auto value : values.objects()) { - temp.emplace_back(info->ind() + value->to_string(ud)); + if (ast_is(value)) { + temp.emplace_back(""s); + continue; + } + auto valueStr = value->to_string(ud); + if (!ast_is(value) && value != lastValueNode) { + valueStr += ','; + } + temp.emplace_back(info->ind() + valueStr); } info->popScope(); temp.emplace_back(info->ind() + '}'); @@ -1175,6 +1189,7 @@ std::string TableLit_t::to_string(void* ud) const { return '{' + join(temp, ", "sv) + '}'; } } + std::string TableBlock_t::to_string(void* ud) const { auto info = reinterpret_cast(ud); str_list temp; -- cgit v1.2.3-55-g6feb