puppet-lint-class_alignment-check 0.2.4 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -1
- data/lib/puppet-lint/plugins/check_class_alignment.rb +15 -31
- data/lib/puppet-lint/plugins/check_class_params_newline.rb +105 -0
- data/spec/puppet-lint/plugins/check_class_equals_alignment_spec.rb +15 -21
- data/spec/puppet-lint/plugins/check_class_params_newline_spec.rb +140 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e32258dfb55db44ee2f3f0a2057958463c3c5ea2bad429c9bc5d179b772d6a71
|
4
|
+
data.tar.gz: 40ddced7d67f62decc65e9e9b396d2e60b19508aecf40039524c7c635d38fd0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e6caecc7f1607637a851670b86f258e2e4da20f9c39d900aeb971216ffdade15d5d3e35d3b31094376dae4e57c3b6e5c6aa97da875d340f4dc0a9d63b795229
|
7
|
+
data.tar.gz: 4fc0dd184ce132b65f032a55bd2146cdc11ee8abb7b9457ef5d8dc0e6cd4d242dc88440e0c293da1c726d61bf122a646ffbb3ad5ab478d035a6e37358d8cbfa8
|
data/README.md
CHANGED
@@ -11,13 +11,26 @@ A puppet-lint plugin to check and fix class params/equals alignment.
|
|
11
11
|
|
12
12
|
## Usage
|
13
13
|
|
14
|
-
This plugin provides
|
14
|
+
This plugin provides 3 checks for puppet-lint:
|
15
15
|
|
16
|
+
- `class_params_newline`
|
16
17
|
- `class_params_alignment`
|
17
18
|
- `class_equals_alignment`
|
18
19
|
|
19
20
|
It supports `--fix` flag.
|
20
21
|
|
22
|
+
To properly reformat the code, run `puppet-lint --fix` like this:
|
23
|
+
|
24
|
+
```bash
|
25
|
+
$ puppet-lint --only-checks class_params_newline --fix .
|
26
|
+
$ puppet-lint --only-checks class_params_alignment --fix .
|
27
|
+
$ puppet-lint --only-checks class_equals_alignment --fix .
|
28
|
+
|
29
|
+
# It's best to combine the above checks with `strict_indent` to fix all remaining issues
|
30
|
+
$ puppet-lint --only-checks strict_indent --fix .
|
31
|
+
|
32
|
+
```
|
33
|
+
|
21
34
|
> Parameters to classes or defined types must be uniformly indented in two
|
22
35
|
> spaces from the title. The equals sign should be aligned.
|
23
36
|
>
|
@@ -4,16 +4,16 @@
|
|
4
4
|
# https://puppet.com/docs/puppet/7/style_guide.html#style_guide_classes-param-indentation-alignment
|
5
5
|
|
6
6
|
def a_param?(token)
|
7
|
-
if token
|
7
|
+
if token&.prev_code_token&.type == :EQUALS
|
8
8
|
false
|
9
|
-
elsif token
|
9
|
+
elsif token&.prev_code_token&.type == :FARROW
|
10
10
|
false
|
11
|
-
elsif token
|
11
|
+
elsif token&.type == :VARIABLE && !%i[DQPRE DQMID].include?(token.prev_code_token.type)
|
12
12
|
true
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
16
|
+
def first_on_the_line?(token, type)
|
17
17
|
origin = token
|
18
18
|
while token&.prev_token
|
19
19
|
token = token.prev_token
|
@@ -24,7 +24,7 @@ def first_param_on_the_line?(token)
|
|
24
24
|
while token&.next_token
|
25
25
|
token = token.next_token
|
26
26
|
|
27
|
-
break if token.type ==
|
27
|
+
break if token.type == type
|
28
28
|
end
|
29
29
|
|
30
30
|
origin == token
|
@@ -33,9 +33,9 @@ end
|
|
33
33
|
def the_one?(token, character)
|
34
34
|
case character
|
35
35
|
when '='
|
36
|
-
true if token.type == :EQUALS
|
36
|
+
true if token.type == :EQUALS && first_on_the_line?(token, :EQUALS)
|
37
37
|
when '$'
|
38
|
-
true if
|
38
|
+
true if a_param?(token) && first_on_the_line?(token, :VARIABLE)
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -148,30 +148,14 @@ end
|
|
148
148
|
# This function is copied & modified from puppet-lint arrow_alignment fix
|
149
149
|
# https://github.com/puppetlabs/puppet-lint/blob/020143b705b023946739eb44e7c7d99fcd087527/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb#L94
|
150
150
|
def fix_for(problem)
|
151
|
-
if problem[:newline]
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
problem[:token].prev_code_token.prev_token.value = ' ' * problem[:newline_indent]
|
160
|
-
|
161
|
-
end_param_idx = tokens.index(problem[:token].prev_code_token)
|
162
|
-
start_param_idx = tokens.index(problem[:token].prev_token_of(%i[INDENT NEWLINE]))
|
163
|
-
param_length = tokens[start_param_idx..end_param_idx].map do |r|
|
164
|
-
r.to_manifest.length
|
165
|
-
end.reduce(0) { |sum, x| sum + x } + 1
|
166
|
-
new_ws_len = problem[:arrow_column] - param_length
|
167
|
-
else
|
168
|
-
new_ws_len = if problem[:token].prev_token.type == :WHITESPACE
|
169
|
-
problem[:token].prev_token.to_manifest.length
|
170
|
-
else
|
171
|
-
0
|
172
|
-
end
|
173
|
-
new_ws_len += (problem[:arrow_column] - problem[:token].column)
|
174
|
-
end
|
151
|
+
raise PuppetLint::NoFix if problem[:newline]
|
152
|
+
|
153
|
+
new_ws_len = if problem[:token].prev_token.type == :WHITESPACE
|
154
|
+
problem[:token].prev_token.to_manifest.length
|
155
|
+
else
|
156
|
+
0
|
157
|
+
end
|
158
|
+
new_ws_len += (problem[:arrow_column] - problem[:token].column)
|
175
159
|
|
176
160
|
if new_ws_len.negative?
|
177
161
|
raise PuppetLint::NoFix if problem[:token].prev_token.type != :INDENT
|
@@ -0,0 +1,105 @@
|
|
1
|
+
def prev_param_token(token)
|
2
|
+
while token&.prev_code_token
|
3
|
+
token = token.prev_code_token
|
4
|
+
break if a_param?(token)
|
5
|
+
end
|
6
|
+
token
|
7
|
+
end
|
8
|
+
|
9
|
+
PuppetLint.new_check(:class_params_newline) do
|
10
|
+
def check
|
11
|
+
(class_indexes + defined_type_indexes).each do |item|
|
12
|
+
tokens = item[:param_tokens]
|
13
|
+
|
14
|
+
first_param = tokens.index { |token| a_param?(token) }
|
15
|
+
last_param = tokens.rindex { |token| a_param?(token) }
|
16
|
+
next if first_param.nil?
|
17
|
+
next if last_param.nil?
|
18
|
+
|
19
|
+
# Skip if there's only 1 param
|
20
|
+
next if tokens[first_param] == tokens[last_param]
|
21
|
+
|
22
|
+
tokens.each do |token|
|
23
|
+
if token == tokens[-1]
|
24
|
+
rparen = token
|
25
|
+
while rparen&.next_token
|
26
|
+
rparen = rparen.next_token
|
27
|
+
break if rparen.type == :RPAREN
|
28
|
+
end
|
29
|
+
|
30
|
+
last_code_token = token
|
31
|
+
while last_code_token&.prev_token
|
32
|
+
break unless %i[WHITESPACE INDENT NEWLINE].include?(last_code_token.type)
|
33
|
+
|
34
|
+
last_code_token = last_code_token.prev_token
|
35
|
+
end
|
36
|
+
|
37
|
+
next if rparen.line != last_code_token.line
|
38
|
+
|
39
|
+
notify(
|
40
|
+
:warning,
|
41
|
+
message: "`)` should be in a new line (expected in line #{token.line + 1}, but found it in line #{token.line})",
|
42
|
+
line: rparen.line,
|
43
|
+
column: rparen.column,
|
44
|
+
token: rparen,
|
45
|
+
newline: true,
|
46
|
+
newline_indent: item[:tokens][0].column - 1
|
47
|
+
)
|
48
|
+
break
|
49
|
+
end
|
50
|
+
|
51
|
+
next unless a_param?(token)
|
52
|
+
|
53
|
+
if token.prev_code_token.type == :LPAREN
|
54
|
+
next if token.line != token.prev_code_token.line
|
55
|
+
elsif token.line != prev_param_token(token).line
|
56
|
+
next
|
57
|
+
end
|
58
|
+
|
59
|
+
notify(
|
60
|
+
:warning,
|
61
|
+
message: "`#{token.to_manifest}` should be in a new line (expected in line #{token.line + 1}, but found it in line #{token.line})",
|
62
|
+
line: token.line,
|
63
|
+
column: token.column,
|
64
|
+
token: token,
|
65
|
+
newline: true,
|
66
|
+
newline_indent: item[:tokens][0].column + 1
|
67
|
+
)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def fix(problem)
|
73
|
+
token = problem[:token]
|
74
|
+
case token&.prev_code_token&.type
|
75
|
+
when :TYPE
|
76
|
+
token = token.prev_code_token
|
77
|
+
when :RBRACK
|
78
|
+
count = 0
|
79
|
+
while token&.prev_code_token
|
80
|
+
token = token.prev_code_token
|
81
|
+
case token.type
|
82
|
+
when :RBRACK
|
83
|
+
count += 1
|
84
|
+
when :LBRACK
|
85
|
+
count -= 1
|
86
|
+
end
|
87
|
+
|
88
|
+
break if count.zero?
|
89
|
+
end
|
90
|
+
token = token.prev_code_token
|
91
|
+
end
|
92
|
+
|
93
|
+
index = tokens.index(token.prev_code_token.next_token)
|
94
|
+
tokens.insert(index, PuppetLint::Lexer::Token.new(:NEWLINE, "\n", 0, 0))
|
95
|
+
|
96
|
+
# When there's no space at the beginning of the param
|
97
|
+
# e.g. class foo($bar="aaa") {}
|
98
|
+
if token.prev_code_token.next_token == token
|
99
|
+
tokens.insert(index + 1, PuppetLint::Lexer::Token.new(:INDENT, ' ' * problem[:newline_indent], 0, 0))
|
100
|
+
|
101
|
+
elsif %i[WHITESPACE INDENT].include?(token.prev_token.type)
|
102
|
+
token.prev_token.value = ' ' * problem[:newline_indent]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -241,13 +241,12 @@ describe 'class_equals_alignment' do
|
|
241
241
|
END
|
242
242
|
end
|
243
243
|
|
244
|
-
it 'should detect
|
245
|
-
expect(problems).to have(
|
244
|
+
it 'should detect 1 problem' do
|
245
|
+
expect(problems).to have(1).problems
|
246
246
|
end
|
247
247
|
|
248
|
-
it 'should create
|
248
|
+
it 'should create 1 warning' do
|
249
249
|
expect(problems).to contain_warning(format(msg, 18, 16)).on_line(2).in_column(16)
|
250
|
-
expect(problems).to contain_warning(format(msg, 18, 29)).on_line(2).in_column(29)
|
251
250
|
end
|
252
251
|
end
|
253
252
|
end
|
@@ -424,23 +423,21 @@ describe 'class_equals_alignment' do
|
|
424
423
|
let(:fixed) do
|
425
424
|
<<-END
|
426
425
|
class test (
|
427
|
-
$a = 'foo',
|
428
|
-
$bb = 'bar',
|
426
|
+
$a = 'foo', $bb = 'bar',
|
429
427
|
$ccc = 'baz',
|
430
428
|
) {}
|
431
429
|
END
|
432
430
|
end
|
433
431
|
|
434
|
-
it 'should detect
|
435
|
-
expect(problems).to have(
|
432
|
+
it 'should detect 1 problem' do
|
433
|
+
expect(problems).to have(1).problems
|
436
434
|
end
|
437
435
|
|
438
|
-
it 'should fix
|
436
|
+
it 'should fix 1 problem' do
|
439
437
|
expect(problems).to contain_fixed(format(msg, 18, 16)).on_line(2).in_column(16)
|
440
|
-
expect(problems).to contain_fixed(format(msg, 18, 29)).on_line(2).in_column(29)
|
441
438
|
end
|
442
439
|
|
443
|
-
it 'should move the extra param onto its own line and realign' do
|
440
|
+
it 'should not move the extra param onto its own line and realign' do
|
444
441
|
expect(manifest).to eq(fixed)
|
445
442
|
end
|
446
443
|
end
|
@@ -458,24 +455,21 @@ describe 'class_equals_alignment' do
|
|
458
455
|
let(:fixed) do
|
459
456
|
<<-END
|
460
457
|
class test (
|
461
|
-
$a
|
462
|
-
$
|
463
|
-
$ccc = 'baz',
|
458
|
+
$a = 'foo', $bbccc = 'bar',
|
459
|
+
$ccc = 'baz',
|
464
460
|
) {}
|
465
461
|
END
|
466
462
|
end
|
467
463
|
|
468
|
-
it 'should detect
|
469
|
-
expect(problems).to have(
|
464
|
+
it 'should detect 1 problem' do
|
465
|
+
expect(problems).to have(1).problems
|
470
466
|
end
|
471
467
|
|
472
|
-
it 'should fix
|
473
|
-
expect(problems).to contain_fixed(format(msg,
|
474
|
-
expect(problems).to contain_fixed(format(msg, 20, 32)).on_line(2).in_column(32)
|
475
|
-
expect(problems).to contain_fixed(format(msg, 20, 18)).on_line(3).in_column(18)
|
468
|
+
it 'should fix 1 problem' do
|
469
|
+
expect(problems).to contain_fixed(format(msg, 18, 16)).on_line(2).in_column(16)
|
476
470
|
end
|
477
471
|
|
478
|
-
it 'should move the extra param onto its own line and realign' do
|
472
|
+
it 'should not move the extra param onto its own line and realign' do
|
479
473
|
expect(manifest).to eq(fixed)
|
480
474
|
end
|
481
475
|
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'class_params_newline' do
|
4
|
+
let(:msg) { '`%s` should be in a new line (expected in line %d, but found it in line %d)' }
|
5
|
+
|
6
|
+
context 'with fix disabled' do
|
7
|
+
context 'tidy code' do
|
8
|
+
let(:code) do
|
9
|
+
<<-END
|
10
|
+
class bar(
|
11
|
+
$someparam = 'somevalue',
|
12
|
+
$another,
|
13
|
+
String $nope,
|
14
|
+
$foo,
|
15
|
+
Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port,
|
16
|
+
# $aaa,
|
17
|
+
String $third = 'meh',
|
18
|
+
Array[Int, Int] $asdf = 'asdf',
|
19
|
+
) { }
|
20
|
+
END
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should detect 0 problems' do
|
24
|
+
expect(problems).to have(0).problems
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'messy code' do
|
29
|
+
let(:code) do
|
30
|
+
<<-END
|
31
|
+
class bar( $someparam = 'somevalue',
|
32
|
+
$another, String $nope, $foo,
|
33
|
+
Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port,
|
34
|
+
# $aaa,
|
35
|
+
String $third = 'meh', Array[Int, Int] $asdf = 'asdf',
|
36
|
+
) { }
|
37
|
+
END
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should detect 4 problems' do
|
41
|
+
expect(problems).to have(4).problems
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should create four warnings' do
|
45
|
+
expect(problems).to contain_warning(format(msg, '$someparam', 2, 1)).on_line(1).in_column(77)
|
46
|
+
expect(problems).to contain_warning(format(msg, '$nope', 3, 2)).on_line(2).in_column(30)
|
47
|
+
expect(problems).to contain_warning(format(msg, '$foo', 3, 2)).on_line(2).in_column(37)
|
48
|
+
expect(problems).to contain_warning(format(msg, '$asdf', 6, 5)).on_line(5).in_column(101)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'with fix enabled' do
|
54
|
+
before do
|
55
|
+
PuppetLint.configuration.fix = true
|
56
|
+
end
|
57
|
+
|
58
|
+
after do
|
59
|
+
PuppetLint.configuration.fix = false
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'messy code' do
|
63
|
+
let(:code) do
|
64
|
+
<<-END
|
65
|
+
class bar( $someparam = 'somevalue',
|
66
|
+
$another, String $nope, $foo,
|
67
|
+
Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port,
|
68
|
+
# $aaa,
|
69
|
+
String $third = 'meh', Array[Int, Int] $asdf = 'asdf',
|
70
|
+
) { }
|
71
|
+
END
|
72
|
+
end
|
73
|
+
|
74
|
+
let(:fixed) do
|
75
|
+
<<-END
|
76
|
+
class bar(
|
77
|
+
$someparam = 'somevalue',
|
78
|
+
$another,
|
79
|
+
String $nope,
|
80
|
+
$foo,
|
81
|
+
Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port,
|
82
|
+
# $aaa,
|
83
|
+
String $third = 'meh',
|
84
|
+
Array[Int, Int] $asdf = 'asdf',
|
85
|
+
) { }
|
86
|
+
END
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should fix the problems' do
|
90
|
+
expect(manifest).to eq(fixed)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'in a single line' do
|
95
|
+
let(:code) do
|
96
|
+
<<-END
|
97
|
+
class foo ($foo = 1, $bar = $a) {}
|
98
|
+
|
99
|
+
class bar ($foo = 1, $bar = $a,) {}
|
100
|
+
|
101
|
+
class aaa ( $foo = 1, $bar = $a,) {}
|
102
|
+
|
103
|
+
class bbb ( $foo = 1, $bar = $a, ) {}
|
104
|
+
|
105
|
+
class ccc ($foo = 1) {}
|
106
|
+
END
|
107
|
+
end
|
108
|
+
|
109
|
+
let(:fixed) do
|
110
|
+
<<-END
|
111
|
+
class foo (
|
112
|
+
$foo = 1,
|
113
|
+
$bar = $a
|
114
|
+
) {}
|
115
|
+
|
116
|
+
class bar (
|
117
|
+
$foo = 1,
|
118
|
+
$bar = $a,
|
119
|
+
) {}
|
120
|
+
|
121
|
+
class aaa (
|
122
|
+
$foo = 1,
|
123
|
+
$bar = $a,
|
124
|
+
) {}
|
125
|
+
|
126
|
+
class bbb (
|
127
|
+
$foo = 1,
|
128
|
+
$bar = $a,
|
129
|
+
) {}
|
130
|
+
|
131
|
+
class ccc ($foo = 1) {}
|
132
|
+
END
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'should fix the problems' do
|
136
|
+
expect(manifest).to eq(fixed)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: puppet-lint-class_alignment-check
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anh Pham
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-05-
|
11
|
+
date: 2022-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: puppet-lint
|
@@ -123,8 +123,10 @@ files:
|
|
123
123
|
- LICENSE
|
124
124
|
- README.md
|
125
125
|
- lib/puppet-lint/plugins/check_class_alignment.rb
|
126
|
+
- lib/puppet-lint/plugins/check_class_params_newline.rb
|
126
127
|
- spec/puppet-lint/plugins/check_class_equals_alignment_spec.rb
|
127
128
|
- spec/puppet-lint/plugins/check_class_params_alignment_spec.rb
|
129
|
+
- spec/puppet-lint/plugins/check_class_params_newline_spec.rb
|
128
130
|
- spec/spec_helper.rb
|
129
131
|
homepage: https://github.com/anhpt379/puppet-lint-class_alignment-check
|
130
132
|
licenses:
|
@@ -152,4 +154,5 @@ summary: A puppet-lint plugin to check & fix class params/equals alignment.
|
|
152
154
|
test_files:
|
153
155
|
- spec/puppet-lint/plugins/check_class_equals_alignment_spec.rb
|
154
156
|
- spec/puppet-lint/plugins/check_class_params_alignment_spec.rb
|
157
|
+
- spec/puppet-lint/plugins/check_class_params_newline_spec.rb
|
155
158
|
- spec/spec_helper.rb
|