diff --git a/luaparser/astnodes.py b/luaparser/astnodes.py index b8cfa66..c74df08 100644 --- a/luaparser/astnodes.py +++ b/luaparser/astnodes.py @@ -118,12 +118,14 @@ def to_json(self) -> any: **{ k: v for k, v in self.__dict__.items() - if not k.startswith("_") and v + if not k.startswith("_") + and v is not None + and v != [] }, **{ "start_char": self.start_char, "stop_char": self.stop_char, - "line": self.line, + "line": self.line if hasattr(self, "_tokens") else None, }, } } @@ -581,6 +583,7 @@ class TrueExpr(Expression): def __init__(self, **kwargs): super(TrueExpr, self).__init__("True", **kwargs) + self.value = True class FalseExpr(Expression): @@ -588,6 +591,7 @@ class FalseExpr(Expression): def __init__(self, **kwargs): super(FalseExpr, self).__init__("False", **kwargs) + self.value = False NumberType = int or float diff --git a/luaparser/tests/test_ast.py b/luaparser/tests/test_ast.py index ebdd588..7c9a3d3 100644 --- a/luaparser/tests/test_ast.py +++ b/luaparser/tests/test_ast.py @@ -109,32 +109,51 @@ def test_to_pretty_json(self): exp = textwrap.dedent( """\ { - "comments": [], - "body": { - "comments": [], - "body": [ - { - "comments": [], - "wrapped": false, - "targets": [ + "Chunk": { + "body": { + "Block": { + "body": [ { - "comments": [], - "wrapped": false, - "id": "a", - "attribute": null + "LocalAssign": { + "wrapped": false, + "targets": [ + { + "Name": { + "wrapped": false, + "id": "a", + "start_char": null, + "stop_char": null, + "line": null + } + } + ], + "values": [ + { + "String": { + "wrapped": false, + "s": "foo", + "raw": "foo", + "delimiter": {}, + "start_char": 10, + "stop_char": 14, + "line": null + } + } + ], + "start_char": 0, + "stop_char": 14, + "line": null + } } ], - "values": [ - { - "comments": [], - "wrapped": false, - "s": "foo", - "raw": "foo", - "delimiter": {} - } - ] + "start_char": 0, + "stop_char": 14, + "line": null } - ] + }, + "start_char": 0, + "stop_char": 14, + "line": null } }""" ) diff --git a/luaparser/tests/test_integration.py b/luaparser/tests/test_integration.py index 1d7fa2a..0aaf427 100644 --- a/luaparser/tests/test_integration.py +++ b/luaparser/tests/test_integration.py @@ -386,3 +386,76 @@ def test_cont_int_14(self): ) self.assertEqual(source, ast.to_lua_source(ast.parse(source))) self.assertEqual(exp, tree) + + # Boolean values serialize with value field #76 + def test_cont_int_15(self): + source = "local a, b = true, false" + tree = ast.parse(source) + exp = textwrap.dedent( + """\ + { + "Chunk": { + "body": { + "Block": { + "body": [ + { + "LocalAssign": { + "wrapped": false, + "targets": [ + { + "Name": { + "wrapped": false, + "id": "a", + "start_char": null, + "stop_char": null, + "line": null + } + }, + { + "Name": { + "wrapped": false, + "id": "b", + "start_char": null, + "stop_char": null, + "line": null + } + } + ], + "values": [ + { + "True": { + "wrapped": false, + "value": true, + "start_char": 13, + "stop_char": 16, + "line": null + } + }, + { + "False": { + "wrapped": false, + "value": false, + "start_char": 19, + "stop_char": 23, + "line": null + } + } + ], + "start_char": 0, + "stop_char": 23, + "line": null + } + } + ], + "start_char": 0, + "stop_char": 23, + "line": null + } + }, + "start_char": 0, + "stop_char": 23, + "line": null + } + }""" + ) + self.assertEqual(ast.to_pretty_json(tree), exp)