Skip to content

Commit 826c2f1

Browse files
do not add trailing comma to object elements during dict -> hcl2 reconstruction (#204)
1 parent 849c260 commit 826c2f1

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

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: 6 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

@@ -111,6 +112,11 @@ def object(self, args: List) -> Dict:
111112
args = self.strip_new_line_tokens(args)
112113
result: Dict[str, Any] = {}
113114
for arg in args:
115+
if (
116+
isinstance(arg, Token) and arg.type == "COMMA"
117+
): # skip optional comma at the end of object element
118+
continue
119+
114120
result.update(arg)
115121
return result
116122

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)