Skip to content

Commit

Permalink
Fix hashes, too
Browse files Browse the repository at this point in the history
  • Loading branch information
raphink committed Feb 10, 2015
1 parent 18f5503 commit 410882f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
33 changes: 33 additions & 0 deletions lib/puppet-lint/plugins/check_trailing_comma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,34 @@ def array_indexes
end.call
end

def hash_indexes
@hash_indexes ||= Proc.new do
hashes = []
tokens.each_with_index do |token, token_idx|
next unless token.prev_code_token
next unless [:EQUALS, :ISEQUAL, :FARROW, :LPAREN].include? token.prev_code_token.type
if token.type == :LBRACE
level = 0
real_idx = 0
tokens[token_idx+1..-1].each_with_index do |cur_token, cur_token_idx|
real_idx = token_idx + 1 + cur_token_idx

level += 1 if cur_token.type == :LBRACE
level -= 1 if cur_token.type == :RBRACE
break if level < 0
end

hashes << {
:start => token_idx,
:end => real_idx,
:tokens => tokens[token_idx..real_idx],
}
end
end
hashes
end.call
end

def defaults_indexes
@defaults_indexes ||= Proc.new do
defaults = []
Expand Down Expand Up @@ -71,6 +99,11 @@ def check
check_elem(array, :LBRACK)
end

# Hashes
hash_indexes.each do |hash|
check_elem(hash, :LBRACE)
end

# Defaults
defaults_indexes.each do |defaults|
check_elem(defaults, :LBRACE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ class { '::apache':
owner => root,
group => '0',
}
$foo = {
'bar' => {
baz => 'abc',
bazz => 'def',
},
'qux' => 'ghi',
}
EOS
}

Expand Down Expand Up @@ -86,18 +94,28 @@ class { '::apache':
owner => root,
group => '0'
}
$foo = {
'bar' => {
baz => 'abc',
bazz => 'def'
},
'qux' => 'ghi'
}
EOS
}

it 'should detect 4 problems' do
expect(problems).to have(4).problems
it 'should detect 6 problems' do
expect(problems).to have(6).problems
end

it 'should create warnings' do
expect(problems).to contain_warning(msg).on_line(3).in_column(32)
expect(problems).to contain_warning(msg).on_line(10).in_column(27)
expect(problems).to contain_warning(msg).on_line(24).in_column(18)
expect(problems).to contain_warning(msg).on_line(33).in_column(23)
expect(problems).to contain_warning(msg).on_line(39).in_column(26)
expect(problems).to contain_warning(msg).on_line(41).in_column(25)
end
end
end
Expand Down Expand Up @@ -148,6 +166,14 @@ class { '::apache':
owner => root,
group => '0',
}
$foo = {
'bar' => {
baz => 'abc',
bazz => 'def',
},
'qux' => 'ghi',
}
EOS
}

Expand Down Expand Up @@ -197,18 +223,28 @@ class { '::apache':
owner => root,
group => '0'
}
$foo = {
'bar' => {
baz => 'abc',
bazz => 'def'
},
'qux' => 'ghi'
}
EOS
}

it 'should detect 4 problems' do
expect(problems).to have(4).problems
it 'should detect 6 problems' do
expect(problems).to have(6).problems
end

it 'should create a warning' do
expect(problems).to contain_fixed(msg).on_line(3).in_column(32)
expect(problems).to contain_fixed(msg).on_line(10).in_column(27)
expect(problems).to contain_fixed(msg).on_line(24).in_column(18)
expect(problems).to contain_fixed(msg).on_line(33).in_column(23)
expect(problems).to contain_fixed(msg).on_line(39).in_column(26)
expect(problems).to contain_fixed(msg).on_line(41).in_column(25)
end

it 'should add trailing commas' do
Expand Down Expand Up @@ -248,6 +284,14 @@ class { '::apache':
owner => root,
group => '0',
}
$foo = {
'bar' => {
baz => 'abc',
bazz => 'def',
},
'qux' => 'ghi',
}
EOS
)
end
Expand Down

0 comments on commit 410882f

Please sign in to comment.