Skip to content

Commit 6654f0e

Browse files
authored
Merge pull request #45 from alexjfisher/issue44
Fix bracket matching of nested arrays
2 parents 73ef080 + 0122e4a commit 6654f0e

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

lib/puppet-lint/plugins/check_trailing_comma.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ def array_indexes
55
tokens.each_with_index do |token, token_idx|
66
next unless token.type == :LBRACK
77

8+
level = 0
89
real_idx = 0
910
tokens[token_idx + 1..-1].each_with_index do |cur_token, cur_token_idx|
1011
real_idx = token_idx + 1 + cur_token_idx
11-
break if cur_token.type == :RBRACK
12+
13+
level += 1 if cur_token.type == :LBRACK
14+
level -= 1 if cur_token.type == :RBRACK
15+
break if level < 0
1216
end
1317

1418
# Ignore resource references

spec/puppet-lint/plugins/check_trailing_comma/check_trailing_comma_spec.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,31 @@ class { '::apache':
167167
expect(problems).to contain_warning(msg).on_line(46).in_column(25)
168168
expect(problems).to contain_warning(msg).on_line(58).in_column(14)
169169
end
170+
171+
context 'with nested arrays' do
172+
let(:code) do
173+
<<~EOS
174+
class { 'myclass':
175+
directives => [
176+
{
177+
directives => [
178+
'if net = 0.0.0.0/0 then reject',
179+
'else accept'
180+
],
181+
},
182+
],
183+
}
184+
EOS
185+
end
186+
187+
it 'detects 1 problem' do
188+
expect(problems).to have(1).problems
189+
end
190+
191+
it 'creates a warning' do
192+
expect(problems).to contain_warning(msg).on_line(6).in_column(24)
193+
end
194+
end
170195
end
171196

172197
context 'with heredoc' do
@@ -458,6 +483,44 @@ class { '::apache':
458483
EOS
459484
)
460485
end
486+
487+
context 'with nested arrays' do
488+
let(:code) do
489+
<<~EOS
490+
class { 'myclass':
491+
directives => [
492+
{
493+
directives => [
494+
'if net = 0.0.0.0/0 then reject',
495+
'else accept'
496+
],
497+
},
498+
],
499+
}
500+
EOS
501+
end
502+
503+
it 'detects 1 problem' do
504+
expect(problems).to have(1).problems
505+
end
506+
507+
it 'adds single trailing comma' do
508+
expect(manifest).to eq(
509+
<<~EOS,
510+
class { 'myclass':
511+
directives => [
512+
{
513+
directives => [
514+
'if net = 0.0.0.0/0 then reject',
515+
'else accept',
516+
],
517+
},
518+
],
519+
}
520+
EOS
521+
)
522+
end
523+
end
461524
end
462525

463526
context 'with heredoc' do

0 commit comments

Comments
 (0)