puppet-lint-class_alignment-check 0.2.2 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a097f4f616f220ca1061819f771b0e9553f89d91f2de912b271026d6a1be2640
|
4
|
+
data.tar.gz: 0db8265872341ec18239a51144d2ec7fd288f71488f1b5bf2174726c17bf279d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5d927d414770af9512912b5ee24eeedfafbfb1987984f0ad5806e6950c1f9efc56ed542114bb1933eb9920f4fbc9367ef9f9233c2ec6c20ed170857e204010b
|
7
|
+
data.tar.gz: 2dfa0a335795a1d5b21a35b9e68a01fcc19a2ef77efaa50a91c2460e12a7cae7e3fde92d772aa4fbc7f285bafbf5ceb98b62a7c146eb6cd140d9a748cdc02b97
|
@@ -0,0 +1,195 @@
|
|
1
|
+
# Parameters to classes or defined types must be uniformly indented in two
|
2
|
+
# spaces from the title. The equals sign should be aligned.
|
3
|
+
#
|
4
|
+
# https://puppet.com/docs/puppet/7/style_guide.html#style_guide_classes-param-indentation-alignment
|
5
|
+
|
6
|
+
def a_param?(token)
|
7
|
+
if token&.prev_code_token&.type == :EQUALS
|
8
|
+
false
|
9
|
+
elsif token&.prev_code_token&.type == :FARROW
|
10
|
+
false
|
11
|
+
elsif token&.type == :VARIABLE && !%i[DQPRE DQMID].include?(token.prev_code_token.type)
|
12
|
+
true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def first_on_the_line?(token, type)
|
17
|
+
origin = token
|
18
|
+
while token&.prev_token
|
19
|
+
token = token.prev_token
|
20
|
+
|
21
|
+
break if token.type == :NEWLINE
|
22
|
+
end
|
23
|
+
|
24
|
+
while token&.next_token
|
25
|
+
token = token.next_token
|
26
|
+
|
27
|
+
break if token.type == type
|
28
|
+
end
|
29
|
+
|
30
|
+
origin == token
|
31
|
+
end
|
32
|
+
|
33
|
+
def the_one?(token, character)
|
34
|
+
case character
|
35
|
+
when '='
|
36
|
+
true if token.type == :EQUALS && first_on_the_line?(token, :EQUALS)
|
37
|
+
when '$'
|
38
|
+
true if a_param?(token) && first_on_the_line?(token, :VARIABLE)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def get_the_first_param(token)
|
43
|
+
while token&.prev_code_token
|
44
|
+
token = token.prev_code_token
|
45
|
+
break if token.type == :CLASS
|
46
|
+
end
|
47
|
+
|
48
|
+
while token&.next_code_token
|
49
|
+
token = token.next_code_token
|
50
|
+
return token if token.type == :VARIABLE
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_prev_code_token(token, character)
|
55
|
+
case character
|
56
|
+
when '='
|
57
|
+
token.prev_code_token
|
58
|
+
when '$'
|
59
|
+
if token.prev_code_token
|
60
|
+
if %i[CLASSREF RBRACK].include?(token.prev_code_token.type)
|
61
|
+
token.prev_code_token
|
62
|
+
elsif token.prev_code_token.type == :LPAREN
|
63
|
+
token
|
64
|
+
elsif token.prev_code_token.type == :COMMA
|
65
|
+
get_the_first_param(token)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# This function is copied & modified from puppet-lint arrow_alignment check
|
72
|
+
# https://github.com/puppetlabs/puppet-lint/blob/020143b705b023946739eb44e7c7d99fcd087527/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb#L8
|
73
|
+
def check_for(character)
|
74
|
+
# I intentionally didn't rename `arrow` to another name, to keep the code as
|
75
|
+
# similar as the original one, to easier to update in the future.
|
76
|
+
(class_indexes + defined_type_indexes).each do |res_idx|
|
77
|
+
arrow_column = [0]
|
78
|
+
level_idx = 0
|
79
|
+
level_tokens = []
|
80
|
+
param_column = [nil]
|
81
|
+
resource_tokens = res_idx[:param_tokens]
|
82
|
+
next if resource_tokens.nil?
|
83
|
+
|
84
|
+
resource_tokens.reject! do |token|
|
85
|
+
COMMENT_TYPES.include?(token.type)
|
86
|
+
end
|
87
|
+
|
88
|
+
# If this is a single line resource, skip it
|
89
|
+
first_arrow = resource_tokens.index { |r| the_one?(r, character) }
|
90
|
+
last_arrow = resource_tokens.rindex { |r| the_one?(r, character) }
|
91
|
+
next if first_arrow.nil?
|
92
|
+
next if last_arrow.nil?
|
93
|
+
next if resource_tokens[first_arrow].line == resource_tokens[last_arrow].line
|
94
|
+
|
95
|
+
resource_tokens.each do |token|
|
96
|
+
if the_one?(token, character)
|
97
|
+
param_token = get_prev_code_token(token, character)
|
98
|
+
param_token = token if param_token.nil?
|
99
|
+
|
100
|
+
param_length = param_token.to_manifest.length
|
101
|
+
|
102
|
+
param_column[level_idx] = param_token.column if param_column[level_idx].nil?
|
103
|
+
|
104
|
+
if (level_tokens[level_idx] ||= []).any? { |t| t.line == token.line }
|
105
|
+
this_arrow_column = param_column[level_idx] + param_length + 1
|
106
|
+
elsif character == '$' && param_token.type == :VARIABLE
|
107
|
+
this_arrow_column = param_token.column
|
108
|
+
else
|
109
|
+
this_arrow_column = param_token.column + param_token.to_manifest.length
|
110
|
+
this_arrow_column += 1 if param_token.type != :INDENT
|
111
|
+
end
|
112
|
+
|
113
|
+
arrow_column[level_idx] = this_arrow_column if arrow_column[level_idx] < this_arrow_column
|
114
|
+
|
115
|
+
(level_tokens[level_idx] ||= []) << token
|
116
|
+
elsif token == resource_tokens[0]
|
117
|
+
level_idx += 1
|
118
|
+
arrow_column << 0
|
119
|
+
level_tokens[level_idx] ||= []
|
120
|
+
param_column << nil
|
121
|
+
elsif token == resource_tokens[-1]
|
122
|
+
if (level_tokens[level_idx] ||= []).map(&:line).uniq.length > 1
|
123
|
+
level_tokens[level_idx].each do |arrow_tok|
|
124
|
+
next if arrow_tok.column == arrow_column[level_idx] || level_tokens[level_idx].size == 1
|
125
|
+
|
126
|
+
arrows_on_line = level_tokens[level_idx].select { |t| t.line == arrow_tok.line }
|
127
|
+
notify(
|
128
|
+
:warning,
|
129
|
+
message: "indentation of #{character} is not properly aligned (expected in column #{arrow_column[level_idx]}, but found it in column #{arrow_tok.column})",
|
130
|
+
line: arrow_tok.line,
|
131
|
+
column: arrow_tok.column,
|
132
|
+
token: arrow_tok,
|
133
|
+
arrow_column: arrow_column[level_idx],
|
134
|
+
newline: arrows_on_line.index(arrow_tok) != 0,
|
135
|
+
newline_indent: param_column[level_idx] - 1
|
136
|
+
)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
arrow_column[level_idx] = 0
|
140
|
+
level_tokens[level_idx].clear
|
141
|
+
param_column[level_idx] = nil
|
142
|
+
level_idx -= 1
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
# This function is copied & modified from puppet-lint arrow_alignment fix
|
149
|
+
# https://github.com/puppetlabs/puppet-lint/blob/020143b705b023946739eb44e7c7d99fcd087527/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb#L94
|
150
|
+
def fix_for(problem)
|
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)
|
159
|
+
|
160
|
+
if new_ws_len.negative?
|
161
|
+
raise PuppetLint::NoFix if problem[:token].prev_token.type != :INDENT
|
162
|
+
|
163
|
+
new_ws = problem[:token].prev_token.to_manifest[0...new_ws_len]
|
164
|
+
problem[:token].prev_token.value = new_ws
|
165
|
+
else
|
166
|
+
new_ws = ' ' * new_ws_len
|
167
|
+
|
168
|
+
if problem[:token].prev_token.type == :WHITESPACE
|
169
|
+
problem[:token].prev_token.value = new_ws
|
170
|
+
else
|
171
|
+
index = tokens.index(problem[:token].prev_token)
|
172
|
+
tokens.insert(index + 1, PuppetLint::Lexer::Token.new(:WHITESPACE, new_ws, 0, 0))
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
PuppetLint.new_check(:class_params_alignment) do
|
178
|
+
def check
|
179
|
+
check_for('$')
|
180
|
+
end
|
181
|
+
|
182
|
+
def fix(problem)
|
183
|
+
fix_for(problem)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
PuppetLint.new_check(:class_equals_alignment) do
|
188
|
+
def check
|
189
|
+
check_for('=')
|
190
|
+
end
|
191
|
+
|
192
|
+
def fix(problem)
|
193
|
+
fix_for(problem)
|
194
|
+
end
|
195
|
+
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
|
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.2.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anh Pham
|
@@ -122,6 +122,7 @@ extra_rdoc_files: []
|
|
122
122
|
files:
|
123
123
|
- LICENSE
|
124
124
|
- README.md
|
125
|
+
- lib/puppet-lint/plugins/check_class_alignment.rb
|
125
126
|
- spec/puppet-lint/plugins/check_class_equals_alignment_spec.rb
|
126
127
|
- spec/puppet-lint/plugins/check_class_params_alignment_spec.rb
|
127
128
|
- spec/spec_helper.rb
|