Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions luaparser/astnodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
Expand Down Expand Up @@ -581,13 +583,15 @@ class TrueExpr(Expression):

def __init__(self, **kwargs):
super(TrueExpr, self).__init__("True", **kwargs)
self.value = True


class FalseExpr(Expression):
"""Define the Lua false expression."""

def __init__(self, **kwargs):
super(FalseExpr, self).__init__("False", **kwargs)
self.value = False


NumberType = int or float
Expand Down
63 changes: 41 additions & 22 deletions luaparser/tests/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}"""
)
Expand Down
73 changes: 73 additions & 0 deletions luaparser/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading