From 410882fd7a1aa0d8ace29c9f3f71be9a669eaba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Tue, 10 Feb 2015 14:13:19 +0100 Subject: [PATCH] Fix hashes, too --- .../plugins/check_trailing_comma.rb | 33 ++++++++++++ .../check_trailing_comma_spec.rb | 52 +++++++++++++++++-- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/lib/puppet-lint/plugins/check_trailing_comma.rb b/lib/puppet-lint/plugins/check_trailing_comma.rb index 26ffbed..3228652 100644 --- a/lib/puppet-lint/plugins/check_trailing_comma.rb +++ b/lib/puppet-lint/plugins/check_trailing_comma.rb @@ -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 = [] @@ -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) diff --git a/spec/puppet-lint/plugins/check_trailing_comma/check_trailing_comma_spec.rb b/spec/puppet-lint/plugins/check_trailing_comma/check_trailing_comma_spec.rb index c51622a..4308e8d 100644 --- a/spec/puppet-lint/plugins/check_trailing_comma/check_trailing_comma_spec.rb +++ b/spec/puppet-lint/plugins/check_trailing_comma/check_trailing_comma_spec.rb @@ -41,6 +41,14 @@ class { '::apache': owner => root, group => '0', } + + $foo = { + 'bar' => { + baz => 'abc', + bazz => 'def', + }, + 'qux' => 'ghi', + } EOS } @@ -86,11 +94,19 @@ 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 @@ -98,6 +114,8 @@ class { '::apache': 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 @@ -148,6 +166,14 @@ class { '::apache': owner => root, group => '0', } + + $foo = { + 'bar' => { + baz => 'abc', + bazz => 'def', + }, + 'qux' => 'ghi', + } EOS } @@ -197,11 +223,19 @@ 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 @@ -209,6 +243,8 @@ class { '::apache': 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 @@ -248,6 +284,14 @@ class { '::apache': owner => root, group => '0', } + + $foo = { + 'bar' => { + baz => 'abc', + bazz => 'def', + }, + 'qux' => 'ghi', + } EOS ) end