diff --git a/lib/puppet-lint/plugins/check_strict_indent.rb b/lib/puppet-lint/plugins/check_strict_indent.rb index 61d3eee..f09ec4a 100644 --- a/lib/puppet-lint/plugins/check_strict_indent.rb +++ b/lib/puppet-lint/plugins/check_strict_indent.rb @@ -116,6 +116,12 @@ def check indent -= 1 colon_indent = nil end + elsif next_token.type == :SEMIC and !colon_indent.nil? and + %i[INDENT NEWLINE].include?(next_token.prev_token.type) and + (next_token.next_token.nil? or next_token.next_token.type == :NEWLINE) + # For a lone semicolon within a block decrement immediately. Use temp_indent because + # indent will be decremented in the next line by the prev_token logic above. + temp_indent -= 1 end next_token = next_token.next_token end diff --git a/spec/fixtures/pass/semicolon.pp b/spec/fixtures/pass/semicolon.pp new file mode 100644 index 0000000..e987b61 --- /dev/null +++ b/spec/fixtures/pass/semicolon.pp @@ -0,0 +1,15 @@ +file { + default: + ensure => file, + owner => 'root', + group => 'wheel', + mode => '0600', + ; + ['ssh_host_dsa_key', 'ssh_host_key', 'ssh_host_rsa_key']: + # use all defaults + ; + ['ssh_config', 'ssh_host_dsa_key.pub', 'ssh_host_key.pub', 'ssh_host_rsa_key.pub', 'sshd_config']: + # override mode + mode => '0644', + ; +} diff --git a/spec/puppet-lint/plugins/check_strict_indent_spec.rb b/spec/puppet-lint/plugins/check_strict_indent_spec.rb index 759c3c7..1da82ad 100644 --- a/spec/puppet-lint/plugins/check_strict_indent_spec.rb +++ b/spec/puppet-lint/plugins/check_strict_indent_spec.rb @@ -1,4 +1,7 @@ require 'spec_helper' +# rubocop:disable Lint/RedundantRequireStatement +require 'pp' +# rubocop:enable Lint/RedundantRequireStatement describe 'strict_indent' do before do @@ -90,4 +93,74 @@ class () {} expect(problems).to have(0).problems end end + + context 'misplaced resource semicolons' do + before do + PuppetLint.configuration.fix = true + end + + after do + PuppetLint.configuration.fix = false + end + + let(:code) do + <<~EOF + file { + default: + ensure => file, + owner => 'root', + group => 'wheel', + mode => '0600', + ; + ['ssh_host_dsa_key', 'ssh_host_key', 'ssh_host_rsa_key']: + # use all defaults + ; + ['ssh_config', 'ssh_host_dsa_key.pub', 'ssh_host_key.pub', 'ssh_host_rsa_key.pub', 'sshd_config']: + # override mode + mode => '0644', + ; + } + EOF + end + + let(:fixed) do + <<~EOF + file { + default: + ensure => file, + owner => 'root', + group => 'wheel', + mode => '0600', + ; + ['ssh_host_dsa_key', 'ssh_host_key', 'ssh_host_rsa_key']: + # use all defaults + ; + ['ssh_config', 'ssh_host_dsa_key.pub', 'ssh_host_key.pub', 'ssh_host_rsa_key.pub', 'sshd_config']: + # override mode + mode => '0644', + ; + } + EOF + end + + it 'detects three problems' do + expect(problems).to have(3).problem + end + + it 'fixes the first problem' do + expect(problems).to contain_fixed('indent should be 2 chars and is 4').on_line(7).in_column(1) + end + + it 'fixes the second problem' do + expect(problems).to contain_fixed('indent should be 2 chars and is 4').on_line(10).in_column(1) + end + + it 'fixes the third problem' do + expect(problems).to contain_fixed('indent should be 2 chars and is 0').on_line(14).in_column(1) + end + + it 'moves the semicolons' do + expect(manifest).to eq fixed + end + end end