Skip to content

Commit bdea212

Browse files
BaseFormatter - fixes to complex function args formatting; (#266)
`FormatterOptions` - dont open empty objects by default
1 parent 989b9f0 commit bdea212

File tree

6 files changed

+107
-2
lines changed

6 files changed

+107
-2
lines changed

hcl2/formatter.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
TupleRule,
1818
)
1919
from hcl2.rules.expressions import ExprTermRule
20+
from hcl2.rules.functions import FunctionCallRule
2021
from hcl2.rules.for_expressions import (
2122
ForTupleExprRule,
2223
ForObjectExprRule,
@@ -33,7 +34,7 @@ class FormatterOptions:
3334

3435
indent_length: int = 2
3536
open_empty_blocks: bool = True
36-
open_empty_objects: bool = True
37+
open_empty_objects: bool = False
3738
open_empty_tuples: bool = False
3839

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

129+
# If no trailing comma, add newline before closing bracket
130+
if not isinstance(new_children[-2], NewLineOrCommentRule):
131+
new_children.insert(-1, self._build_newline(indent_level))
132+
128133
self._deindent_last_line()
129134
self._set_children(rule, new_children)
130135

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

183+
elif isinstance(rule.expression, FunctionCallRule):
184+
self.format_function_call(rule.expression, indent_level)
185+
178186
elif isinstance(rule.expression, ExprTermRule):
179187
self.format_expression(rule.expression, indent_level)
180188

189+
def format_function_call(self, rule: FunctionCallRule, indent_level: int = 0):
190+
"""Format a function call by recursively formatting its arguments."""
191+
if rule.arguments is not None:
192+
for arg in rule.arguments.arguments:
193+
if isinstance(arg, ExprTermRule):
194+
self.format_expression(arg, indent_level)
195+
181196
def format_fortupleexpr(self, expression: ForTupleExprRule, indent_level: int = 0):
182197
"""Format a for-tuple expression with newlines around clauses."""
183198
for child in expression.children:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
variable "object" {
2+
type = object({
3+
key = string
4+
value = string
5+
})
6+
}
7+
8+
variable "nested" {
9+
type = map(object({
10+
name = string
11+
enabled = bool
12+
}))
13+
}
14+
15+
variable "multi_arg" {
16+
default = merge({
17+
a = 1
18+
b = 2
19+
}, {
20+
c = 3
21+
})
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
variable "object" {
2+
type = object({
3+
key = string,
4+
value = string
5+
})
6+
}
7+
8+
9+
variable "nested" {
10+
type = map(object({
11+
name = string,
12+
enabled = bool
13+
}))
14+
}
15+
16+
17+
variable "multi_arg" {
18+
default = merge({
19+
a = 1,
20+
b = 2
21+
}, {
22+
c = 3
23+
})
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"variable": [
3+
{
4+
"\"object\"": {
5+
"type": "${object({key = string, value = string})}",
6+
"__is_block__": true
7+
}
8+
},
9+
{
10+
"\"nested\"": {
11+
"type": "${map(object({name = string, enabled = bool}))}",
12+
"__is_block__": true
13+
}
14+
},
15+
{
16+
"\"multi_arg\"": {
17+
"default": "${merge({a = 1, b = 2}, {c = 3})}",
18+
"__is_block__": true
19+
}
20+
}
21+
]
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"variable": [
3+
{
4+
"\"object\"": {
5+
"type": "${object({key = string, value = string})}",
6+
"__is_block__": true
7+
}
8+
},
9+
{
10+
"\"nested\"": {
11+
"type": "${map(object({name = string, enabled = bool}))}",
12+
"__is_block__": true
13+
}
14+
},
15+
{
16+
"\"multi_arg\"": {
17+
"default": "${merge({a = 1, b = 2}, {c = 3})}",
18+
"__is_block__": true
19+
}
20+
}
21+
]
22+
}

test/unit/test_formatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_defaults(self):
113113
opts = FormatterOptions()
114114
self.assertEqual(opts.indent_length, 2)
115115
self.assertTrue(opts.open_empty_blocks)
116-
self.assertTrue(opts.open_empty_objects)
116+
self.assertFalse(opts.open_empty_objects)
117117
self.assertFalse(opts.open_empty_tuples)
118118
self.assertTrue(opts.vertically_align_attributes)
119119
self.assertTrue(opts.vertically_align_object_elements)

0 commit comments

Comments
 (0)