puppet-lint-empty_lines_around_body-check 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a3918100de439e4488287e6e8ceceb008c8551552f002a352283a066e0f92136
4
+ data.tar.gz: c92f887b662c319c004a9f503dc630b2c030a9185e6e4c8953504ba21c81b9f5
5
+ SHA512:
6
+ metadata.gz: 9e4acde0a48ba50818efa1d1cdc761b6d8e48cc713df7dd99098a6729fb1ff29ad1c8ca5457a07cefa1815ee03515549d97db11eb2ade5cef78ce6f5f120629e
7
+ data.tar.gz: 125fa606bb796cca0421de836af6769a1083cd1ad70f771df1c5c48ca160d514feae4df1d27f6ae99a74dfed82c7e39870f351df1a132b11227354653e09c0f9
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) [2020] [Christian Bartolomäus]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,80 @@
1
+ puppet-lint-empty_lines_around_body-check
2
+ =========================================
3
+
4
+ A puppet-lint plugin to check for empty lines around bodies of
5
+ * classes and defined types
6
+ * conditional blocks (if/elsif/else, unless, case, selectors, ...)
7
+
8
+ ## Installation
9
+
10
+ ### From the command line
11
+
12
+ ```shell
13
+ $ gem install puppet-lint-empty_lines_around_body-check
14
+ ```
15
+
16
+ ## Checks
17
+
18
+ #### What you have done
19
+
20
+ There is an empty line either after the opening curly brace that starts a block or before the closing curly brace that ends a block.
21
+
22
+ ```puppet
23
+ if $foo == 'bar' {
24
+
25
+ ## some stuff
26
+
27
+ }
28
+ ```
29
+
30
+ ```puppet
31
+ class foo::config {
32
+
33
+ ## some stuff
34
+
35
+ }
36
+ ```
37
+
38
+ ```puppet
39
+ case $facts['os']['name'] {
40
+
41
+ 'Solaris': { include role::solaris }
42
+ default: { include role::generic }
43
+
44
+ }
45
+ ```
46
+
47
+ #### What you should have done
48
+
49
+ The indentation suffices to signal the start end end of a block, so blocks shouldn't have leading or trailing empty lines.
50
+
51
+ ```puppet
52
+ if $foo == 'bar' {
53
+ ## some stuff
54
+ }
55
+ ```
56
+
57
+ ```puppet
58
+ class foo::config {
59
+ ## some stuff
60
+ }
61
+ ```
62
+
63
+ ```puppet
64
+ case $facts['os']['name'] {
65
+ 'Solaris': { include role::solaris }
66
+ default: { include role::generic }
67
+ }
68
+ ```
69
+
70
+ #### Disabling the check
71
+
72
+ To disable this check, you can add `--no-empty_lines_around_block-check' to your puppet-lint command line.
73
+
74
+ ```
75
+ $ puppet-lint --no-empty_lines_around_block-check /path/to/file.pp
76
+ ```
77
+
78
+ ## Acknowledgments
79
+
80
+ This plugin for `puppet-lint` has been written owing to the very nice [tutorial for writing puppet-lint checks](http://puppet-lint.com/developer/tutorial/).
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Metrics/BlockLength
4
+ PuppetLint.new_check(:empty_lines_around_body) do
5
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
6
+ def check
7
+ checked_block_tokens = %i[IF ELSIF ELSE UNLESS CASE CLASS DEFINE NODE]
8
+ checked_blocks = {}
9
+
10
+ depth = 0
11
+ tokens.each_index do |token_idx|
12
+ if checked_block_tokens.include?(tokens[token_idx].type)
13
+ checked_blocks[depth] = { :start => token_idx }
14
+ end
15
+
16
+ if tokens[token_idx].type == :LBRACE
17
+ depth += 1
18
+ unless checked_blocks[depth - 1].nil?
19
+ idx_with_problem = idx_2nd_nl(token_idx)
20
+ unless idx_with_problem == -1
21
+ block_type = tokens[checked_blocks[depth - 1][:start]].type.downcase
22
+ notify(
23
+ :warning,
24
+ :message => "empty line at start of #{block_type} block",
25
+ :line => tokens[idx_with_problem].line,
26
+ :column => tokens[idx_with_problem].column
27
+ )
28
+ end
29
+ end
30
+ elsif tokens[token_idx].type == :RBRACE
31
+ unless checked_blocks[depth - 1].nil?
32
+ idx_with_problem = idx_prec_2nd_nl(token_idx)
33
+ unless idx_with_problem == -1
34
+ block_type = tokens[checked_blocks[depth - 1][:start]].type.downcase
35
+ notify(
36
+ :warning,
37
+ :message => "empty line at end of #{block_type} block",
38
+ :line => tokens[idx_with_problem].line,
39
+ :column => tokens[idx_with_problem].column
40
+ )
41
+ end
42
+ checked_blocks.delete(depth)
43
+ end
44
+ depth -= 1
45
+ end
46
+ end
47
+ end
48
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
49
+
50
+ # rubocop:disable Metrics/MethodLength
51
+ def idx_2nd_nl(start_idx)
52
+ seen_newlines = 0
53
+ tokens[(start_idx + 1)..-1].each_index do |idx_in_block|
54
+ idx_abs = idx_in_block + start_idx + 1
55
+ if tokens[idx_abs].type == :NEWLINE
56
+ seen_newlines += 1
57
+ return idx_abs if seen_newlines > 1
58
+ elsif %i[WHITESPACE INDENT].include? tokens[idx_abs].type
59
+ next
60
+ else
61
+ break
62
+ end
63
+ end
64
+ -1
65
+ end
66
+ # rubocop:enable Metrics/MethodLength
67
+
68
+ # rubocop:disable Metrics/MethodLength
69
+ def idx_prec_2nd_nl(start_idx)
70
+ seen_newlines = 0
71
+ idx_last_nl = -1
72
+ (start_idx - 1).downto(0).each do |idx_abs|
73
+ if tokens[idx_abs].type == :NEWLINE
74
+ seen_newlines += 1
75
+ return idx_last_nl if seen_newlines > 1
76
+
77
+ idx_last_nl = idx_abs
78
+ elsif %i[WHITESPACE INDENT].include? tokens[idx_abs].type
79
+ next
80
+ else
81
+ break
82
+ end
83
+ end
84
+ -1
85
+ end
86
+ # rubocop:enable Metrics/MethodLength
87
+ end
88
+ # rubocop:enable Metrics/BlockLength
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ # rubocop:disable Metrics/BlockLength
6
+ describe 'empty_lines_around_body' do
7
+ ## if block
8
+ context 'with fix disabled' do
9
+ let(:msg) { 'empty line at start of if block' }
10
+
11
+ ## neither leading nor trailing empty line
12
+ context 'neither leading nor trailing empty line in if block' do
13
+ let(:code) { "if $foo == 'bar' {\n include role::foo\n}\n" }
14
+
15
+ it 'should not detect any problems' do
16
+ expect(problems).to have(0).problems
17
+ end
18
+ end
19
+
20
+ ## leading empty line
21
+ context 'empty line after opening curly of if block' do
22
+ let(:code) { "if $foo == 'bar' {\n \n include role::foo\n}\n" }
23
+
24
+ it 'should detect a single problem' do
25
+ expect(problems).to have(1).problems
26
+ end
27
+
28
+ it 'should create a warning' do
29
+ expect(problems).to contain_warning(msg).on_line(2).in_column(3)
30
+ end
31
+ end
32
+ end
33
+
34
+ context 'with fix disabled' do
35
+ let(:msg) { 'empty line at end of if block' }
36
+
37
+ ## trailing empty line
38
+ context 'empty line before closing curly of if block' do
39
+ let(:code) { "if $foo == 'bar' {\n include role::foo\n \n}\n" }
40
+
41
+ it 'should detect a single problem' do
42
+ expect(problems).to have(1).problems
43
+ end
44
+
45
+ it 'should create a warning' do
46
+ expect(problems).to contain_warning(msg).on_line(3).in_column(3)
47
+ end
48
+ end
49
+ end
50
+
51
+ ## class block
52
+ context 'with fix disabled' do
53
+ let(:msg) { 'empty line at start of class block' }
54
+
55
+ ## neither leading nor trailing empty line
56
+ context 'neither leading nor trailing empty line in class block' do
57
+ let(:code) { "class foo {\n include role::bar\n}\n" }
58
+
59
+ it 'should not detect any problems' do
60
+ expect(problems).to have(0).problems
61
+ end
62
+ end
63
+
64
+ ## leading empty line
65
+ context 'empty line after opening curly of if block' do
66
+ let(:code) { "class foo {\n \n include role::bar\n}\n" }
67
+
68
+ it 'should detect a single problem' do
69
+ expect(problems).to have(1).problems
70
+ end
71
+
72
+ it 'should create a warning' do
73
+ expect(problems).to contain_warning(msg).on_line(2).in_column(3)
74
+ end
75
+ end
76
+ end
77
+
78
+ context 'with fix disabled' do
79
+ let(:msg) { 'empty line at end of class block' }
80
+
81
+ ## trailing empty line
82
+ context 'empty line before closing curly of class block' do
83
+ let(:code) { "class foo {\n include role::bar\n \n}\n" }
84
+
85
+ it 'should detect a single problem' do
86
+ expect(problems).to have(1).problems
87
+ end
88
+
89
+ it 'should create a warning' do
90
+ expect(problems).to contain_warning(msg).on_line(3).in_column(3)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ # rubocop:enable Metrics/BlockLength
@@ -0,0 +1,3 @@
1
+ require 'puppet-lint'
2
+
3
+ PuppetLint::Plugins.load_spec_helper
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppet-lint-empty_lines_around_body-check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Christian Bartolomäus
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: puppet-lint
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-collection_matchers
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-its
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-json_expectations
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: " A puppet-lint plugin to check for empty lines around the body of
112
+ blocks.\n"
113
+ email: use_v6@aglaz.de
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - LICENSE
119
+ - README.md
120
+ - lib/puppet-lint/plugins/check_empty_lines_around_body.rb
121
+ - spec/puppet-lint/plugins/check_empty_lines_around_body_spec.rb
122
+ - spec/spec_helper.rb
123
+ homepage: https://github.com/usev6/puppet-lint-empty_lines_around_body-check
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubygems_version: 3.0.3
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: A puppet-lint plugin to check for empty lines around the body of blocks.
146
+ test_files:
147
+ - spec/spec_helper.rb
148
+ - spec/puppet-lint/plugins/check_empty_lines_around_body_spec.rb