puppet-lint-class_alignment-check 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +14 -1
- data/lib/puppet-lint/plugins/check_class_alignment.rb +17 -33
- data/lib/puppet-lint/plugins/check_class_params_newline.rb +70 -0
- data/spec/puppet-lint/plugins/check_class_equals_alignment_spec.rb +15 -21
- data/spec/puppet-lint/plugins/check_class_params_alignment_spec.rb +2 -2
- data/spec/puppet-lint/plugins/check_class_params_newline_spec.rb +94 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe4a55df13a00eb32e3dab657bc0d1dc1e2982851d3992ec1329210b2642ccc5
|
4
|
+
data.tar.gz: 0d227e2da3f0dfa710ee46a11c3a9bd0e6e4fbf05728e96b553ae28a018c305e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c33eb7910db84735f6baa86e020b604cdc272c18d726c7c53bb34b3995b6ca874fc5da985756df1f34f985cc80385f2477fe571756ff0c8da374ca0e1001de8
|
7
|
+
data.tar.gz: 9d3122d562637f655b4fdb251e59256c3a29a42b6affecf78d26d9618cacacd81d3ae698541d5fda6b0e1aa1350d71807bfbd87b5f55213d671bb7ba0f010a41
|
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
|
|
@@ -113,12 +113,12 @@ def check_for(character)
|
|
113
113
|
arrow_column[level_idx] = this_arrow_column if arrow_column[level_idx] < this_arrow_column
|
114
114
|
|
115
115
|
(level_tokens[level_idx] ||= []) << token
|
116
|
-
elsif token
|
116
|
+
elsif token == resource_tokens[0]
|
117
117
|
level_idx += 1
|
118
118
|
arrow_column << 0
|
119
119
|
level_tokens[level_idx] ||= []
|
120
120
|
param_column << nil
|
121
|
-
elsif token
|
121
|
+
elsif token == resource_tokens[-1]
|
122
122
|
if (level_tokens[level_idx] ||= []).map(&:line).uniq.length > 1
|
123
123
|
level_tokens[level_idx].each do |arrow_tok|
|
124
124
|
next if arrow_tok.column == arrow_column[level_idx] || level_tokens[level_idx].size == 1
|
@@ -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,70 @@
|
|
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
|
+
next unless a_param?(token)
|
24
|
+
|
25
|
+
if token.prev_code_token == :LPAREN
|
26
|
+
next if token.line != token.prev_code_token.line
|
27
|
+
elsif token.line != prev_param_token(token).line
|
28
|
+
next
|
29
|
+
end
|
30
|
+
|
31
|
+
notify(
|
32
|
+
:warning,
|
33
|
+
message: "Parameter #{token.to_manifest} should have its own line (expected in line #{token.line + 1}, but found it in line #{token.line})",
|
34
|
+
line: token.line,
|
35
|
+
column: token.column,
|
36
|
+
token: token,
|
37
|
+
newline: true,
|
38
|
+
newline_indent: item[:tokens][0].column + 1
|
39
|
+
)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def fix(problem)
|
45
|
+
token = problem[:token]
|
46
|
+
case token&.prev_code_token&.type
|
47
|
+
when :TYPE
|
48
|
+
token = token.prev_code_token
|
49
|
+
when :RBRACK
|
50
|
+
count = 0
|
51
|
+
while token&.prev_code_token
|
52
|
+
token = token.prev_code_token
|
53
|
+
case token.type
|
54
|
+
when :RBRACK
|
55
|
+
count += 1
|
56
|
+
when :LBRACK
|
57
|
+
count -= 1
|
58
|
+
end
|
59
|
+
|
60
|
+
break if count.zero?
|
61
|
+
end
|
62
|
+
token = token.prev_code_token
|
63
|
+
end
|
64
|
+
|
65
|
+
index = tokens.index(token.prev_code_token.next_token)
|
66
|
+
tokens.insert(index, PuppetLint::Lexer::Token.new(:NEWLINE, "\n", 0, 0))
|
67
|
+
|
68
|
+
token.prev_token.value = ' ' * problem[:newline_indent] if %i[WHITESPACE INDENT].include?(token.prev_token.type)
|
69
|
+
end
|
70
|
+
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
|
@@ -280,7 +280,7 @@ describe 'class_params_alignment' do
|
|
280
280
|
<<-END
|
281
281
|
class name (
|
282
282
|
$aaa = function('foo', 'bar'),
|
283
|
-
|
283
|
+
$bbb = function('foo', 'bar'),
|
284
284
|
Boolean $key1 = false,
|
285
285
|
Enum['never', 'allow', 'try', 'demand'] $key2,
|
286
286
|
$materializer_version = $foo ? {
|
@@ -308,7 +308,7 @@ describe 'class_params_alignment' do
|
|
308
308
|
|
309
309
|
it 'should fix 4 problems' do
|
310
310
|
expect(problems).to contain_fixed(format(msg, 53, 13)).on_line(2).in_column(13)
|
311
|
-
expect(problems).to contain_fixed(format(msg, 53,
|
311
|
+
expect(problems).to contain_fixed(format(msg, 53, 15)).on_line(3).in_column(15)
|
312
312
|
expect(problems).to contain_fixed(format(msg, 53, 21)).on_line(4).in_column(21)
|
313
313
|
expect(problems).to contain_fixed(format(msg, 53, 11)).on_line(6).in_column(11)
|
314
314
|
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'class_params_newline' do
|
4
|
+
let(:msg) { 'Parameter %s should have its own 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
|
+
end
|
94
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anh Pham
|
@@ -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
|