puppet-lint-class_alignment-check 0.2.1 → 0.2.4
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: ebf0e96bee2765a17143585f49c445c388612b1563ac6ec54513ee39989013ef
|
4
|
+
data.tar.gz: 95d3539f4c25fd584610774c695394fb43b17ebb3881e0e73976f0a337eb6a57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 374133ecbc4fa790491148cc2f62bbe630584891a10df2d3cffec9ce289b2b63ab8bdd6fcd43477735f25fe52e246535017af0f59d27b75f60bd9bf3714bff3c
|
7
|
+
data.tar.gz: d98666d602b2d846c5ad815d6f06b1dd15fa29ef7c896f6b507217d14e54f5536202aaef80127fc6b900b4ecd05b5183214641e55e45ae27a45dd7f28b0ba856
|
data/README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
[![License](https://img.shields.io/github/license/anhpt379/puppet-lint-class_alignment-check.svg)](https://github.com/anhpt379/puppet-lint-class_alignment-check/blob/master/LICENSE)
|
4
4
|
[![Test](https://github.com/anhpt379/puppet-lint-class_alignment-check/actions/workflows/test.yml/badge.svg)](https://github.com/anhpt379/puppet-lint-class_alignment-check/actions/workflows/test.yml)
|
5
|
+
[![Release](https://github.com/anhpt379/puppet-lint-class_alignment-check/actions/workflows/release.yml/badge.svg)](https://github.com/anhpt379/puppet-lint-class_alignment-check/actions/workflows/release.yml)
|
5
6
|
[![codecov](https://codecov.io/gh/anhpt379/puppet-lint-class_alignment-check/branch/master/graph/badge.svg?token=2DI8JYJ8AZ)](https://codecov.io/gh/anhpt379/puppet-lint-class_alignment-check)
|
6
7
|
[![RubyGem Version](https://img.shields.io/gem/v/puppet-lint-class_alignment-check.svg)](https://rubygems.org/gems/puppet-lint-class_alignment-check)
|
7
8
|
[![RubyGem Downloads](https://img.shields.io/gem/dt/puppet-lint-class_alignment-check.svg)](https://rubygems.org/gems/puppet-lint-class_alignment-check)
|
@@ -0,0 +1,211 @@
|
|
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_param_on_the_line?(token)
|
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 == :VARIABLE
|
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
|
37
|
+
when '$'
|
38
|
+
true if token.type == :VARIABLE && a_param?(token) && first_param_on_the_line?(token)
|
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
|
+
if problem[:newline]
|
152
|
+
index = tokens.index(problem[:token].prev_code_token.prev_token)
|
153
|
+
|
154
|
+
# insert newline
|
155
|
+
tokens.insert(index, PuppetLint::Lexer::Token.new(:NEWLINE, "\n", 0, 0))
|
156
|
+
|
157
|
+
# indent the parameter to the correct depth
|
158
|
+
problem[:token].prev_code_token.prev_token.type = :INDENT
|
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
|
175
|
+
|
176
|
+
if new_ws_len.negative?
|
177
|
+
raise PuppetLint::NoFix if problem[:token].prev_token.type != :INDENT
|
178
|
+
|
179
|
+
new_ws = problem[:token].prev_token.to_manifest[0...new_ws_len]
|
180
|
+
problem[:token].prev_token.value = new_ws
|
181
|
+
else
|
182
|
+
new_ws = ' ' * new_ws_len
|
183
|
+
|
184
|
+
if problem[:token].prev_token.type == :WHITESPACE
|
185
|
+
problem[:token].prev_token.value = new_ws
|
186
|
+
else
|
187
|
+
index = tokens.index(problem[:token].prev_token)
|
188
|
+
tokens.insert(index + 1, PuppetLint::Lexer::Token.new(:WHITESPACE, new_ws, 0, 0))
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
PuppetLint.new_check(:class_params_alignment) do
|
194
|
+
def check
|
195
|
+
check_for('$')
|
196
|
+
end
|
197
|
+
|
198
|
+
def fix(problem)
|
199
|
+
fix_for(problem)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
PuppetLint.new_check(:class_equals_alignment) do
|
204
|
+
def check
|
205
|
+
check_for('=')
|
206
|
+
end
|
207
|
+
|
208
|
+
def fix(problem)
|
209
|
+
fix_for(problem)
|
210
|
+
end
|
211
|
+
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.4
|
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
|