Skip to content

Commit cb57772

Browse files
do not add trailing comma to object elements during dict -> hcl2 reconstruction
1 parent 849c260 commit cb57772

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

hcl2/hcl2.lark

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ DOUBLE_PIPE : "||"
3737
PLUS : "+"
3838
LPAR : "("
3939
RPAR : ")"
40+
COMMA : ","
4041

4142
expr_term : LPAR new_line_or_comment? expression new_line_or_comment? RPAR
4243
| float_lit
@@ -71,7 +72,7 @@ EXP_MARK : ("e" | "E") ("+" | "-")? DECIMAL+
7172
EQ : /[ \t]*=(?!=|>)/
7273

7374
tuple : "[" (new_line_or_comment* expression new_line_or_comment* ",")* (new_line_or_comment* expression)? new_line_or_comment* "]"
74-
object : "{" new_line_or_comment? (new_line_or_comment* (object_elem | (object_elem ",")) new_line_or_comment*)* "}"
75+
object : "{" new_line_or_comment? (new_line_or_comment* (object_elem | (object_elem COMMA)) new_line_or_comment*)* "}"
7576
object_elem : object_elem_key ( EQ | ":") expression
7677
object_elem_key : float_lit | int_lit | identifier | STRING_LIT
7778

hcl2/transformer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from collections import namedtuple
66
from typing import List, Dict, Any
77

8+
from lark import Token
89
from lark.tree import Meta
910
from lark.visitors import Transformer, Discard, _DiscardType, v_args
1011

@@ -108,9 +109,15 @@ def object_elem(self, args: List) -> Dict:
108109
return {key: value}
109110

110111
def object(self, args: List) -> Dict:
112+
print(args)
111113
args = self.strip_new_line_tokens(args)
112114
result: Dict[str, Any] = {}
113115
for arg in args:
116+
if (
117+
isinstance(arg, Token) and arg.type == "COMMA"
118+
): # skip optional comma at the end of object element
119+
continue
120+
114121
result.update(arg)
115122
return result
116123

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
locals {
22
terraform = {
3-
channels = (local.running_in_ci ? local.ci_channels : local.local_channels),
4-
authentication = [],
5-
foo = null,
3+
channels = (local.running_in_ci ? local.ci_channels : local.local_channels)
4+
authentication = []
5+
foo = null
66
}
77
}

0 commit comments

Comments
 (0)