puppet-lint-class_alignment-check 0.3.0 → 0.3.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: fe4a55df13a00eb32e3dab657bc0d1dc1e2982851d3992ec1329210b2642ccc5
4
- data.tar.gz: 0d227e2da3f0dfa710ee46a11c3a9bd0e6e4fbf05728e96b553ae28a018c305e
3
+ metadata.gz: 3dc0a012b0d80156eaad90609fb62d8f32d580d6acbc5f28e6981d71137fc301
4
+ data.tar.gz: 72378622329307ce0c0631caebec4fef365fe137b89581bfd01bae1e8bad9537
5
5
  SHA512:
6
- metadata.gz: 4c33eb7910db84735f6baa86e020b604cdc272c18d726c7c53bb34b3995b6ca874fc5da985756df1f34f985cc80385f2477fe571756ff0c8da374ca0e1001de8
7
- data.tar.gz: 9d3122d562637f655b4fdb251e59256c3a29a42b6affecf78d26d9618cacacd81d3ae698541d5fda6b0e1aa1350d71807bfbd87b5f55213d671bb7ba0f010a41
6
+ metadata.gz: f7750b39ff5ef57ec5fca519a77e1aa0a1b0def9d906814c4c6b2cf096229a8cde51bc75d796e174c4f351e3fe17f4a5baeb79b3649d468e255f88433d3691a2
7
+ data.tar.gz: 79e3897e36b000f944ebe5d90bbdd671215e2fe94d4a6956a3ddd258fbced0ec4ecd5c8bee7e94b9a69fbc1df88fff411b87872f74babce420f870fc3ac84339
@@ -8,8 +8,21 @@ def a_param?(token)
8
8
  false
9
9
  elsif token&.prev_code_token&.type == :FARROW
10
10
  false
11
- elsif token&.type == :VARIABLE && !%i[DQPRE DQMID].include?(token.prev_code_token.type)
12
- true
11
+ elsif %i[DQPRE DQMID].include?(token&.prev_code_token&.type)
12
+ false
13
+ elsif token&.type == :VARIABLE
14
+ count = 0
15
+ while token&.prev_token
16
+ token = token.prev_token
17
+ if %i[LPAREN LBRACK LBRACE].include?(token.type)
18
+ count += 1
19
+ elsif %i[RPAREN RBRACK RBRACE].include?(token.type)
20
+ count -= 1
21
+ elsif %i[DEFINE CLASS].include?(token.type)
22
+ break
23
+ end
24
+ end
25
+ true if count == 1
13
26
  end
14
27
  end
15
28
 
@@ -10,27 +10,51 @@ PuppetLint.new_check(:class_params_newline) do
10
10
  def check
11
11
  (class_indexes + defined_type_indexes).each do |item|
12
12
  tokens = item[:param_tokens]
13
+ next if tokens.nil?
13
14
 
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]
15
+ # Skip if line length < 80 chars
16
+ first_paren = tokens[0]&.prev_token_of(:LPAREN)
17
+ last_paren = tokens[-1]&.next_token_of(:RPAREN)
18
+ next if first_paren.nil?
19
+ next if last_paren.nil?
20
+ next if first_paren.line == last_paren.line && last_paren.column < 80
21
21
 
22
22
  tokens.each do |token|
23
+ if token == tokens[-1]
24
+ rparen = token.next_token_of(:RPAREN)
25
+
26
+ last_code_token = token
27
+ while last_code_token&.prev_token
28
+ break unless %i[WHITESPACE INDENT NEWLINE].include?(last_code_token.type)
29
+
30
+ last_code_token = last_code_token.prev_token
31
+ end
32
+
33
+ next if rparen.line != last_code_token.line
34
+
35
+ notify(
36
+ :warning,
37
+ message: "`)` should be in a new line (expected in line #{token.line + 1}, but found it in line #{token.line})",
38
+ line: rparen.line,
39
+ column: rparen.column,
40
+ token: rparen,
41
+ newline: true,
42
+ newline_indent: item[:tokens][0].column - 1
43
+ )
44
+ end
45
+
23
46
  next unless a_param?(token)
24
47
 
25
- if token.prev_code_token == :LPAREN
48
+ if token.prev_code_token.type == :LPAREN
26
49
  next if token.line != token.prev_code_token.line
27
50
  elsif token.line != prev_param_token(token).line
28
51
  next
29
52
  end
30
53
 
54
+ # binding.break
31
55
  notify(
32
56
  :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})",
57
+ message: "`#{token.to_manifest}` should be in a new line (expected in line #{token.line + 1}, but found it in line #{token.line})",
34
58
  line: token.line,
35
59
  column: token.column,
36
60
  token: token,
@@ -43,28 +67,46 @@ PuppetLint.new_check(:class_params_newline) do
43
67
 
44
68
  def fix(problem)
45
69
  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
70
+ if token.type == :VARIABLE
71
+ # Integer $db_port
72
+ if token&.prev_code_token&.type == :TYPE
52
73
  token = token.prev_code_token
53
- case token.type
54
- when :RBRACK
55
- count += 1
56
- when :LBRACK
57
- count -= 1
58
- end
59
74
 
60
- break if count.zero?
75
+ # Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port
76
+ elsif token&.prev_code_token&.type == :RBRACK
77
+ count = 0
78
+ while token&.prev_code_token
79
+ token = token.prev_code_token
80
+ case token.type
81
+ when :RBRACK
82
+ count += 1
83
+ when :LBRACK
84
+ count -= 1
85
+ end
86
+
87
+ break if count.zero?
88
+ end
89
+ token = token.prev_code_token
61
90
  end
62
- token = token.prev_code_token
63
91
  end
64
92
 
65
- index = tokens.index(token.prev_code_token.next_token)
93
+ last_non_whitespace_token = token.prev_token
94
+ while last_non_whitespace_token&.prev_token
95
+ break unless %i[WHITESPACE INDENT NEWLINE].include?(last_non_whitespace_token.type)
96
+
97
+ last_non_whitespace_token = last_non_whitespace_token.prev_token
98
+ end
99
+
100
+ index = tokens.index(last_non_whitespace_token) + 1
66
101
  tokens.insert(index, PuppetLint::Lexer::Token.new(:NEWLINE, "\n", 0, 0))
67
102
 
68
- token.prev_token.value = ' ' * problem[:newline_indent] if %i[WHITESPACE INDENT].include?(token.prev_token.type)
103
+ # When there's no space at the beginning of the param
104
+ # e.g. class foo($bar="aaa") {}
105
+ if token.prev_code_token.next_token == token
106
+ tokens.insert(index + 1, PuppetLint::Lexer::Token.new(:INDENT, ' ' * problem[:newline_indent], 0, 0))
107
+
108
+ elsif %i[WHITESPACE INDENT].include?(token.prev_token.type)
109
+ token.prev_token.value = ' ' * problem[:newline_indent]
110
+ end
69
111
  end
70
112
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
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)' }
4
+ let(:msg) { '`%s` should be in a new line (expected in line %d, but found it in line %d)' }
5
5
 
6
6
  context 'with fix disabled' do
7
7
  context 'tidy code' do
@@ -90,5 +90,134 @@ describe 'class_params_newline' do
90
90
  expect(manifest).to eq(fixed)
91
91
  end
92
92
  end
93
+
94
+ context 'in a single line' do
95
+ let(:code) do
96
+ <<-END
97
+ class foo ($foo = 1, $bar = $a) {}
98
+ class foo ($foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a) {}
99
+
100
+ class bar ($foo = 1, $bar = $a,) {}
101
+ class bar ($foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a,) {}
102
+
103
+ class aaa ( $foo = 1, $bar = $a,) {}
104
+ class aaa ( $foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a,) {}
105
+
106
+ class bbb ( $foo = 1, $bar = $a, ) {}
107
+ class bbb ( $foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a, ) {}
108
+
109
+ class ccc ($foo = 1) {}
110
+
111
+ class ddd {}
112
+
113
+ class eee (
114
+ $foo = 1,
115
+ $workers = max($::processorcount, 1),
116
+ $database_path = $aaa,) inherits sap_zabbix::params { }
117
+ { }
118
+
119
+ class fff ($foo, $bar=[], $foo, $bar=[], $foo, $bar=[], $listen_ips = [$::ipaddress]) {}
120
+
121
+ define ggg ($foo, $bar=[]) {}
122
+
123
+ define long_ggg ($foo, $bar=[], $foo, $bar=[], $foo, $bar=[], $foo, $bar=[]) {}
124
+
125
+ define asdf ($prefix, $pattern, $expire, $port, $prefix, $pattern, $expire, $port) { }
126
+ END
127
+ end
128
+
129
+ let(:fixed) do
130
+ <<-END
131
+ class foo ($foo = 1, $bar = $a) {}
132
+ class foo (
133
+ $foo = 1,
134
+ $bar = $a,
135
+ $foo = 1,
136
+ $bar = $a,
137
+ $foo = 1,
138
+ $bar = $a
139
+ ) {}
140
+
141
+ class bar ($foo = 1, $bar = $a,) {}
142
+ class bar (
143
+ $foo = 1,
144
+ $bar = $a,
145
+ $foo = 1,
146
+ $bar = $a,
147
+ $foo = 1,
148
+ $bar = $a,
149
+ ) {}
150
+
151
+ class aaa ( $foo = 1, $bar = $a,) {}
152
+ class aaa (
153
+ $foo = 1,
154
+ $bar = $a,
155
+ $foo = 1,
156
+ $bar = $a,
157
+ $foo = 1,
158
+ $bar = $a,
159
+ ) {}
160
+
161
+ class bbb ( $foo = 1, $bar = $a, ) {}
162
+ class bbb (
163
+ $foo = 1,
164
+ $bar = $a,
165
+ $foo = 1,
166
+ $bar = $a,
167
+ $foo = 1,
168
+ $bar = $a,
169
+ ) {}
170
+
171
+ class ccc ($foo = 1) {}
172
+
173
+ class ddd {}
174
+
175
+ class eee (
176
+ $foo = 1,
177
+ $workers = max($::processorcount, 1),
178
+ $database_path = $aaa,
179
+ ) inherits sap_zabbix::params { }
180
+ { }
181
+
182
+ class fff (
183
+ $foo,
184
+ $bar=[],
185
+ $foo,
186
+ $bar=[],
187
+ $foo,
188
+ $bar=[],
189
+ $listen_ips = [$::ipaddress]
190
+ ) {}
191
+
192
+ define ggg ($foo, $bar=[]) {}
193
+
194
+ define long_ggg (
195
+ $foo,
196
+ $bar=[],
197
+ $foo,
198
+ $bar=[],
199
+ $foo,
200
+ $bar=[],
201
+ $foo,
202
+ $bar=[]
203
+ ) {}
204
+
205
+ define asdf (
206
+ $prefix,
207
+ $pattern,
208
+ $expire,
209
+ $port,
210
+ $prefix,
211
+ $pattern,
212
+ $expire,
213
+ $port
214
+ ) { }
215
+ END
216
+ end
217
+
218
+ it 'should fix the problems' do
219
+ expect(manifest).to eq(fixed)
220
+ end
221
+ end
93
222
  end
94
223
  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.3.0
4
+ version: 0.3.3
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-08 00:00:00.000000000 Z
11
+ date: 2022-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puppet-lint