puppet-lint-class_alignment-check 0.2.2 → 0.2.3

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: 353b9e1ee8377e4b5c9c123cb085c1938d8bc0aaac7e1530d1d4196335536bb9
4
- data.tar.gz: fe8828f2dbf5c3564d9ac0f85c19600fb50f7ed4c97c4758c8464ca16e579524
3
+ metadata.gz: 472da99d1199ebc852ca972b0df75171b5421ec006e03fa02af41a1eefcbbff7
4
+ data.tar.gz: eb86074f1126e1f27749f33f6d4572892efe8dfd490208e35cd1a5ea359f9357
5
5
  SHA512:
6
- metadata.gz: ee8c5781dd514939d0ee515e392fa7e697332b556cf4898ec99fe6eb71b5347c85a8f1de3b270688d821ebbf9ffe621a65da8c14958a89d19b5a828c4a0dc7b0
7
- data.tar.gz: a27fcde6ff8fc088fa94bdacb8ac0e40c5b322039f31d7e797e50548c3714679793f3bd6d01df398973277f5a95373b59e0973fa3170d4716a0a08fff7af389d
6
+ metadata.gz: b21647377367ee30cd6eaae3280e4baf6359ffb9b1b62ed27791d5ff84649b0b47f4e209d6bd6e45bd5468f719877690892c79835f12be1641a515e7abd929fa
7
+ data.tar.gz: 9175489f85c7e46de2c76d29a9ec3d0d36d9287892a88f2add6df9c5f9fc7ca6841c8b0e0d2601ee7f78d2192d218999bca97e064a9fcae99c9852a1abe2241f
@@ -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.prev_token.type == :LPAREN
117
+ level_idx += 1
118
+ arrow_column << 0
119
+ level_tokens[level_idx] ||= []
120
+ param_column << nil
121
+ elsif token.next_token.type == :RPAREN
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
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.2
4
+ version: 0.2.3
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