puppet-lint-uncuddled_else-check 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +61 -0
- data/lib/puppet-lint/plugins/check_uncuddled_else.rb +19 -0
- data/spec/puppet-lint/plugins/check_uncuddled_else_spec.rb +99 -0
- data/spec/spec_helper.rb +3 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a3c9358c21f974ca626106734ba001ebca25142288f2c07b5339a7d682d919d7
|
4
|
+
data.tar.gz: 9d1e69cf348a673320290c427096d18d384f5f7084a68954b4cfce94304f1b87
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b30fa6f47b2174ceabdb49a2492c3d66f4b8520d316e3221cb1255d184b437b0d5d1fd325ee5c7e5afe930ac1fbddfa925583565a6f9d826fda6aa605d671a92
|
7
|
+
data.tar.gz: 8168f465e2a4ee39a4349674244fd9fa0f4c4305dad16c32291420768fb17148739a4c9d81b91b98934f8c4d0eec08450fab878ddfd4f480ec25753ab9d51b21
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) [2019] [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.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
puppet-lint-uncuddled_else-check
|
2
|
+
================================
|
3
|
+
|
4
|
+
A puppet-lint plugin to check for [uncuddled else blocks](http://wiki.c2.com/?CuddledElseBlocks).
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
### From the command line
|
9
|
+
|
10
|
+
```shell
|
11
|
+
$ gem install puppet-lint-uncuddled_else-check
|
12
|
+
```
|
13
|
+
|
14
|
+
## Checks
|
15
|
+
|
16
|
+
### Uncuddled else/elsif
|
17
|
+
|
18
|
+
There are two different styles regarding the placement of `else` and `elsif` keywords. It can be placed on the same line as the closing curly of the preceding if block (`cuddled`) or on a separate line below that closing curly (`uncuddled`). There are pros and cons for both styles -- cmp. http://wiki.c2.com/?CuddledElseBlocks.
|
19
|
+
|
20
|
+
This check helps to enforce a consistent style by warning about `uncuddled` else/elsif keywords.
|
21
|
+
|
22
|
+
#### What you have done
|
23
|
+
|
24
|
+
The `else` (or `elsif`) keyword is on a new line below the closing curly brace of the preceding if block.
|
25
|
+
|
26
|
+
```puppet
|
27
|
+
if $foo == 'bar' {
|
28
|
+
## some stuff
|
29
|
+
}
|
30
|
+
else {
|
31
|
+
## other stuff
|
32
|
+
}
|
33
|
+
```
|
34
|
+
|
35
|
+
#### What you should have done
|
36
|
+
|
37
|
+
The `else` (or `elsif`) keyword is on the same line as the closing curly brace of the preceding if block.
|
38
|
+
|
39
|
+
```puppet
|
40
|
+
if $foo == 'bar' {
|
41
|
+
## some stuff
|
42
|
+
} else {
|
43
|
+
## other stuff
|
44
|
+
}
|
45
|
+
```
|
46
|
+
|
47
|
+
#### Disabling the check
|
48
|
+
|
49
|
+
To disable this check, you can add `--no-uncuddled_else-check' to your puppet-lint command line.
|
50
|
+
|
51
|
+
```
|
52
|
+
$ puppet-lint --no-uncuddled_else-check /path/to/file.pp
|
53
|
+
```
|
54
|
+
|
55
|
+
## See also
|
56
|
+
|
57
|
+
There is a corresponding plugin `puppet-lint-cuddled_else-check` that warns for cuddled else blocks. Most probably you only want one of both checks. ;)
|
58
|
+
|
59
|
+
## Acknowledgments
|
60
|
+
|
61
|
+
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,19 @@
|
|
1
|
+
PuppetLint.new_check(:uncuddled_else) do
|
2
|
+
def check
|
3
|
+
tokens.each do |token|
|
4
|
+
next unless token.type == :ELSE || token.type == :ELSIF
|
5
|
+
|
6
|
+
## 'else/elsif' should follow closing curly and whitespaces
|
7
|
+
prev1 = token.prev_token
|
8
|
+
prev2 = prev1.prev_token
|
9
|
+
next if prev1.type == :WHITESPACE && prev2.type == :RBRACE
|
10
|
+
|
11
|
+
## otherwise it's not a cuddled else/elsif
|
12
|
+
notify :warning, {
|
13
|
+
:message => "found uncuddled #{token.type.to_s.downcase}, should be on same line as closing curly",
|
14
|
+
:line => token.line,
|
15
|
+
:column => token.column,
|
16
|
+
}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# rubocop:disable Metrics/BlockLength, Metrics/LineLength
|
4
|
+
describe 'uncuddled_else' do
|
5
|
+
## uncuddled else
|
6
|
+
context 'with fix disabled' do
|
7
|
+
let(:msg) { 'found uncuddled else, should be on same line as closing curly' }
|
8
|
+
|
9
|
+
## no indentation
|
10
|
+
context 'else on same line as closing curly of preceding if block' do
|
11
|
+
let(:code) { "if $foo == 'bar' {\n ## some stuff\n} else {\n ## other stuff\n}\n" }
|
12
|
+
|
13
|
+
it 'should not detect any problems' do
|
14
|
+
expect(problems).to have(0).problems
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'else on next line after closing curly of preceding if block' do
|
19
|
+
let(:code) { "if $foo == 'bar' {\n ## some stuff\n}\nelse {\n ## other stuff\n}\n" }
|
20
|
+
|
21
|
+
it 'should detect a single problems' do
|
22
|
+
expect(problems).to have(1).problems
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should create a warning' do
|
26
|
+
expect(problems).to contain_warning(msg).on_line(4).in_column(1)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
## with indentation
|
31
|
+
context 'else on same line as indented closing curly of preceding if block' do
|
32
|
+
let(:code) { " if $foo == 'bar' {\n ## some stuff\n } else {\n ## other stuff\n }\n" }
|
33
|
+
|
34
|
+
it 'should not detect any problems' do
|
35
|
+
expect(problems).to have(0).problems
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'indented else on next line after closing curly of preceding if block' do
|
40
|
+
let(:code) { " if $foo == 'bar' {\n ## some stuff\n }\n else {\n ## other stuff\n }\n" }
|
41
|
+
|
42
|
+
it 'should detect a single problems' do
|
43
|
+
expect(problems).to have(1).problems
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should create a warning' do
|
47
|
+
expect(problems).to contain_warning(msg).on_line(4).in_column(3)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
## uncuddled elsif
|
53
|
+
context 'with fix disabled' do
|
54
|
+
let(:msg) { 'found uncuddled elsif, should be on same line as closing curly' }
|
55
|
+
|
56
|
+
## no identation
|
57
|
+
context 'elsif on same line as closing curly of preceding if block' do
|
58
|
+
let(:code) { "if $foo == 'bar' {\n ## some stuff\n} elsif $foo == 'baz' {\n ## other stuff\n}\n" }
|
59
|
+
|
60
|
+
it 'should not detect any problems' do
|
61
|
+
expect(problems).to have(0).problems
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'elsif on next line after closing curly of preceding if block' do
|
66
|
+
let(:code) { "if $foo == 'bar' {\n ## some stuff\n}\nelsif $foo == 'baz' {\n ## other stuff\n}\n" }
|
67
|
+
|
68
|
+
it 'should detect a single problems' 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(4).in_column(1)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
## with identation
|
78
|
+
context 'indented elsif on same line as closing curly of preceding if block' do
|
79
|
+
let(:code) { " if $foo == 'bar' {\n ## some stuff\n } elsif $foo == 'baz' {\n ## other stuff\n }\n" }
|
80
|
+
|
81
|
+
it 'should not detect any problems' do
|
82
|
+
expect(problems).to have(0).problems
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'elsif on next line after indented closing curly of preceding if block' do
|
87
|
+
let(:code) { " if $foo == 'bar' {\n ## some stuff\n }\n elsif $foo == 'baz' {\n ## other stuff\n }\n" }
|
88
|
+
|
89
|
+
it 'should detect a single problems' do
|
90
|
+
expect(problems).to have(1).problems
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'should create a warning' do
|
94
|
+
expect(problems).to contain_warning(msg).on_line(4).in_column(3)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
# rubocop:enable Metrics/BlockLength, Metrics/LineLength
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: puppet-lint-uncuddled_else-check
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Christian Bartolomäus
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-06 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-its
|
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-collection_matchers
|
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 that manifest files do not have uncuddled
|
112
|
+
else 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_uncuddled_else.rb
|
121
|
+
- spec/puppet-lint/plugins/check_uncuddled_else_spec.rb
|
122
|
+
- spec/spec_helper.rb
|
123
|
+
homepage: https://github.com/optivo-org/puppet-lint-uncuddled_else-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.1
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: A puppet-lint plugin to check for uncuddled else blocks.
|
146
|
+
test_files:
|
147
|
+
- spec/spec_helper.rb
|
148
|
+
- spec/puppet-lint/plugins/check_uncuddled_else_spec.rb
|