Skip to content

Commit 86d0bb8

Browse files
authored
Merge pull request #44 from msalway/double_indent
Only indent once for doubled brackets
2 parents 894d55f + c837f30 commit 86d0bb8

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

lib/puppet-lint/plugins/check_strict_indent.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,20 @@ def check
4949
end.each do |token|
5050
temp_indent = 0
5151

52+
matched_open_lines = []
5253
# indent for open groups in the previous line
5354
open_groups = 0
5455
prev_token = token.prev_token
5556
while !prev_token.nil? and prev_token.type != :NEWLINE
5657
temp_indent += 1 if prev_token.type == :HEREDOC_OPEN
57-
if %i[LBRACE LBRACK
58-
LPAREN].include?(prev_token.type) && (matches[prev_token].nil? or matches[prev_token].line > prev_token.line)
59-
# left braces not matched in the same line increase indent
60-
open_groups += 1
58+
if %i[LBRACE LBRACK LPAREN].include?(prev_token.type)
59+
if matches[prev_token].nil?
60+
open_groups += 1
61+
elsif matches[prev_token].line > prev_token.line and !matched_open_lines.include?(matches[prev_token].line)
62+
# increase indent for left braces not matched in the same line or in a line we have already matched a brace to
63+
open_groups += 1
64+
matched_open_lines << matches[prev_token].line
65+
end
6166
end
6267
prev_token = prev_token.prev_token
6368
end
@@ -96,11 +101,15 @@ def check
96101

97102
# unindent for closing brackets in the current line
98103
next_token = token.next_token
104+
matched_close_lines = []
99105
while !next_token.nil? and next_token.type != :NEWLINE
100106
if %i[RBRACE RBRACK RPAREN].include?(next_token.type)
101-
if !matches[next_token].nil? and matches[next_token].line < next_token.line
102-
# right braces matched in a previous line decrease indent
107+
if !matches[next_token].nil? and
108+
matches[next_token].line < next_token.line and
109+
!matched_close_lines.include?(matches[next_token].line)
110+
# right braces matched in a previous line decrease indent unless we already matched a brace in the same line
103111
indent -= 1
112+
matched_close_lines << matches[next_token].line
104113
end
105114
if next_token.type == :RBRACE and !colon_indent.nil? && (!matches[next_token].nil? and matches[next_token].line < colon_indent)
106115
# unindent at the end of resources if needed

spec/fixtures/pass/doubles.pp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
$content = epp('file.epp',{
2+
'options' => '124',
3+
})
4+
5+
$content = epp(
6+
'file.epp',{
7+
'options' => '124',
8+
})
9+
10+
$content = epp('file.epp',{
11+
'options' => '124',
12+
}
13+
)

0 commit comments

Comments
 (0)