Skip to content

do not add trailing comma to object elements during dict -> hcl2 reconstruction #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025
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
3 changes: 2 additions & 1 deletion hcl2/hcl2.lark
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ DOUBLE_PIPE : "||"
PLUS : "+"
LPAR : "("
RPAR : ")"
COMMA : ","

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

tuple : "[" (new_line_or_comment* expression new_line_or_comment* ",")* (new_line_or_comment* expression)? new_line_or_comment* "]"
object : "{" new_line_or_comment? (new_line_or_comment* (object_elem | (object_elem ",")) new_line_or_comment*)* "}"
object : "{" new_line_or_comment? (new_line_or_comment* (object_elem | (object_elem COMMA)) new_line_or_comment*)* "}"
object_elem : object_elem_key ( EQ | ":") expression
object_elem_key : float_lit | int_lit | identifier | STRING_LIT

Expand Down
6 changes: 6 additions & 0 deletions hcl2/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections import namedtuple
from typing import List, Dict, Any

from lark import Token
from lark.tree import Meta
from lark.visitors import Transformer, Discard, _DiscardType, v_args

Expand Down Expand Up @@ -111,6 +112,11 @@ def object(self, args: List) -> Dict:
args = self.strip_new_line_tokens(args)
result: Dict[str, Any] = {}
for arg in args:
if (
isinstance(arg, Token) and arg.type == "COMMA"
): # skip optional comma at the end of object element
continue

result.update(arg)
return result

Expand Down
6 changes: 3 additions & 3 deletions test/helpers/terraform-config/locals_embedded_condition.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
locals {
terraform = {
channels = (local.running_in_ci ? local.ci_channels : local.local_channels),
authentication = [],
foo = null,
channels = (local.running_in_ci ? local.ci_channels : local.local_channels)
authentication = []
foo = null
}
}