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
17 changes: 16 additions & 1 deletion hcl2/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
TupleRule,
)
from hcl2.rules.expressions import ExprTermRule
from hcl2.rules.functions import FunctionCallRule
from hcl2.rules.for_expressions import (
ForTupleExprRule,
ForObjectExprRule,
Expand All @@ -33,7 +34,7 @@ class FormatterOptions:

indent_length: int = 2
open_empty_blocks: bool = True
open_empty_objects: bool = True
open_empty_objects: bool = False
open_empty_tuples: bool = False

vertically_align_attributes: bool = True
Expand Down Expand Up @@ -125,6 +126,10 @@ def format_tuple_rule(self, rule: TupleRule, indent_level: int = 0):
if isinstance(child, (COMMA, LSQB)): # type: ignore[misc]
new_children.append(self._build_newline(indent_level))

# If no trailing comma, add newline before closing bracket
if not isinstance(new_children[-2], NewLineOrCommentRule):
new_children.insert(-1, self._build_newline(indent_level))

self._deindent_last_line()
self._set_children(rule, new_children)

Expand Down Expand Up @@ -175,9 +180,19 @@ def format_expression(self, rule: ExprTermRule, indent_level: int = 0):
elif isinstance(rule.expression, ForObjectExprRule):
self.format_forobjectexpr(rule.expression, indent_level)

elif isinstance(rule.expression, FunctionCallRule):
self.format_function_call(rule.expression, indent_level)

elif isinstance(rule.expression, ExprTermRule):
self.format_expression(rule.expression, indent_level)

def format_function_call(self, rule: FunctionCallRule, indent_level: int = 0):
"""Format a function call by recursively formatting its arguments."""
if rule.arguments is not None:
for arg in rule.arguments.arguments:
if isinstance(arg, ExprTermRule):
self.format_expression(arg, indent_level)

def format_fortupleexpr(self, expression: ForTupleExprRule, indent_level: int = 0):
"""Format a for-tuple expression with newlines around clauses."""
for child in expression.children:
Expand Down
22 changes: 22 additions & 0 deletions test/integration/hcl2_original/function_objects.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
variable "object" {
type = object({
key = string
value = string
})
}

variable "nested" {
type = map(object({
name = string
enabled = bool
}))
}

variable "multi_arg" {
default = merge({
a = 1
b = 2
}, {
c = 3
})
}
24 changes: 24 additions & 0 deletions test/integration/hcl2_reconstructed/function_objects.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
variable "object" {
type = object({
key = string,
value = string
})
}


variable "nested" {
type = map(object({
name = string,
enabled = bool
}))
}


variable "multi_arg" {
default = merge({
a = 1,
b = 2
}, {
c = 3
})
}
22 changes: 22 additions & 0 deletions test/integration/json_reserialized/function_objects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"variable": [
{
"\"object\"": {
"type": "${object({key = string, value = string})}",
"__is_block__": true
}
},
{
"\"nested\"": {
"type": "${map(object({name = string, enabled = bool}))}",
"__is_block__": true
}
},
{
"\"multi_arg\"": {
"default": "${merge({a = 1, b = 2}, {c = 3})}",
"__is_block__": true
}
}
]
}
22 changes: 22 additions & 0 deletions test/integration/json_serialized/function_objects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"variable": [
{
"\"object\"": {
"type": "${object({key = string, value = string})}",
"__is_block__": true
}
},
{
"\"nested\"": {
"type": "${map(object({name = string, enabled = bool}))}",
"__is_block__": true
}
},
{
"\"multi_arg\"": {
"default": "${merge({a = 1, b = 2}, {c = 3})}",
"__is_block__": true
}
}
]
}
2 changes: 1 addition & 1 deletion test/unit/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_defaults(self):
opts = FormatterOptions()
self.assertEqual(opts.indent_length, 2)
self.assertTrue(opts.open_empty_blocks)
self.assertTrue(opts.open_empty_objects)
self.assertFalse(opts.open_empty_objects)
self.assertFalse(opts.open_empty_tuples)
self.assertTrue(opts.vertically_align_attributes)
self.assertTrue(opts.vertically_align_object_elements)
Expand Down