puppet-lint-class_alignment-check 0.3.1 → 0.3.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e32258dfb55db44ee2f3f0a2057958463c3c5ea2bad429c9bc5d179b772d6a71
4
- data.tar.gz: 40ddced7d67f62decc65e9e9b396d2e60b19508aecf40039524c7c635d38fd0f
3
+ metadata.gz: d0263490e2b13cd13ec0f9cb4e3d569d4a004aa11b6caa63b51c4c0b4cb2a405
4
+ data.tar.gz: aadc7259538098f01145ff841be78496c2ed04d9c799eb1a8d6d3a25d7df9ada
5
5
  SHA512:
6
- metadata.gz: 3e6caecc7f1607637a851670b86f258e2e4da20f9c39d900aeb971216ffdade15d5d3e35d3b31094376dae4e57c3b6e5c6aa97da875d340f4dc0a9d63b795229
7
- data.tar.gz: 4fc0dd184ce132b65f032a55bd2146cdc11ee8abb7b9457ef5d8dc0e6cd4d242dc88440e0c293da1c726d61bf122a646ffbb3ad5ab478d035a6e37358d8cbfa8
6
+ metadata.gz: eb63b8f785be3ee853cab31c262c169acaeafd21100833e467c1ee8ae7cb4921aa8b877e42f8fe4a242dbca0b104c51637bbd9d62cdec9a2c1fc85d190451200
7
+ data.tar.gz: 49b6920703283013416902bfe3d28c0f57746127d2f20f07d21cd57aa29adc3bda5c6c12486c4639ab9a0997d0010007453e663ccf6fa4fd712eb3dda0e7e696
@@ -8,8 +8,29 @@ 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
+ # first var in the class
15
+ if token&.prev_token_of(:CLASS)&.next_token_of(:LPAREN)&.next_token_of(:VARIABLE) == token
16
+ return true
17
+ elsif token&.prev_token_of(:DEFINE)&.next_token_of(:LPAREN)&.next_token_of(:VARIABLE) == token
18
+ return true
19
+ end
20
+
21
+ count = 0
22
+ while token&.prev_token
23
+ token = token.prev_token
24
+ return false if token.type == :EQUALS
25
+
26
+ if %i[RPAREN RBRACK RBRACE].include?(token.type)
27
+ count += 1
28
+ elsif %i[LPAREN LBRACK LBRACE].include?(token.type)
29
+ count -= 1
30
+ end
31
+
32
+ return true if count.zero? && token.type == :COMMA
33
+ end
13
34
  end
14
35
  end
15
36
 
@@ -10,22 +10,18 @@ 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
23
  if token == tokens[-1]
24
- rparen = token
25
- while rparen&.next_token
26
- rparen = rparen.next_token
27
- break if rparen.type == :RPAREN
28
- end
24
+ rparen = token.next_token_of(:RPAREN)
29
25
 
30
26
  last_code_token = token
31
27
  while last_code_token&.prev_token
@@ -45,7 +41,6 @@ PuppetLint.new_check(:class_params_newline) do
45
41
  newline: true,
46
42
  newline_indent: item[:tokens][0].column - 1
47
43
  )
48
- break
49
44
  end
50
45
 
51
46
  next unless a_param?(token)
@@ -56,6 +51,7 @@ PuppetLint.new_check(:class_params_newline) do
56
51
  next
57
52
  end
58
53
 
54
+ # binding.break
59
55
  notify(
60
56
  :warning,
61
57
  message: "`#{token.to_manifest}` should be in a new line (expected in line #{token.line + 1}, but found it in line #{token.line})",
@@ -71,26 +67,37 @@ PuppetLint.new_check(:class_params_newline) do
71
67
 
72
68
  def fix(problem)
73
69
  token = problem[:token]
74
- case token&.prev_code_token&.type
75
- when :TYPE
76
- token = token.prev_code_token
77
- when :RBRACK
78
- count = 0
79
- while token&.prev_code_token
70
+ if token.type == :VARIABLE
71
+ # Integer $db_port
72
+ if token&.prev_code_token&.type == :TYPE
80
73
  token = token.prev_code_token
81
- case token.type
82
- when :RBRACK
83
- count += 1
84
- when :LBRACK
85
- count -= 1
86
- end
87
74
 
88
- 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
89
90
  end
90
- token = token.prev_code_token
91
91
  end
92
92
 
93
- 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
94
101
  tokens.insert(index, PuppetLint::Lexer::Token.new(:NEWLINE, "\n", 0, 0))
95
102
 
96
103
  # When there's no space at the beginning of the param
@@ -95,40 +95,136 @@ describe 'class_params_newline' do
95
95
  let(:code) do
96
96
  <<-END
97
97
  class foo ($foo = 1, $bar = $a) {}
98
+ class foo ($foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a) {}
98
99
 
99
100
  class bar ($foo = 1, $bar = $a,) {}
101
+ class bar ($foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a,) {}
100
102
 
101
103
  class aaa ( $foo = 1, $bar = $a,) {}
104
+ class aaa ( $foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a,) {}
102
105
 
103
106
  class bbb ( $foo = 1, $bar = $a, ) {}
107
+ class bbb ( $foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a, ) {}
104
108
 
105
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
+
127
+ define asdf ($prefix, $pattern, $expire, $port, $prefix, $pattern=$a and $b, $epe=-$foo, $port=!$foo) { }
106
128
  END
107
129
  end
108
130
 
109
131
  let(:fixed) do
110
132
  <<-END
133
+ class foo ($foo = 1, $bar = $a) {}
111
134
  class foo (
135
+ $foo = 1,
136
+ $bar = $a,
137
+ $foo = 1,
138
+ $bar = $a,
112
139
  $foo = 1,
113
140
  $bar = $a
114
141
  ) {}
115
142
 
143
+ class bar ($foo = 1, $bar = $a,) {}
116
144
  class bar (
117
145
  $foo = 1,
118
146
  $bar = $a,
147
+ $foo = 1,
148
+ $bar = $a,
149
+ $foo = 1,
150
+ $bar = $a,
119
151
  ) {}
120
152
 
153
+ class aaa ( $foo = 1, $bar = $a,) {}
121
154
  class aaa (
122
155
  $foo = 1,
123
156
  $bar = $a,
157
+ $foo = 1,
158
+ $bar = $a,
159
+ $foo = 1,
160
+ $bar = $a,
124
161
  ) {}
125
162
 
163
+ class bbb ( $foo = 1, $bar = $a, ) {}
126
164
  class bbb (
127
165
  $foo = 1,
128
166
  $bar = $a,
167
+ $foo = 1,
168
+ $bar = $a,
169
+ $foo = 1,
170
+ $bar = $a,
129
171
  ) {}
130
172
 
131
173
  class ccc ($foo = 1) {}
174
+
175
+ class ddd {}
176
+
177
+ class eee (
178
+ $foo = 1,
179
+ $workers = max($::processorcount, 1),
180
+ $database_path = $aaa,
181
+ ) inherits sap_zabbix::params { }
182
+ { }
183
+
184
+ class fff (
185
+ $foo,
186
+ $bar=[],
187
+ $foo,
188
+ $bar=[],
189
+ $foo,
190
+ $bar=[],
191
+ $listen_ips = [$::ipaddress]
192
+ ) {}
193
+
194
+ define ggg ($foo, $bar=[]) {}
195
+
196
+ define long_ggg (
197
+ $foo,
198
+ $bar=[],
199
+ $foo,
200
+ $bar=[],
201
+ $foo,
202
+ $bar=[],
203
+ $foo,
204
+ $bar=[]
205
+ ) {}
206
+
207
+ define asdf (
208
+ $prefix,
209
+ $pattern,
210
+ $expire,
211
+ $port,
212
+ $prefix,
213
+ $pattern,
214
+ $expire,
215
+ $port
216
+ ) { }
217
+
218
+ define asdf (
219
+ $prefix,
220
+ $pattern,
221
+ $expire,
222
+ $port,
223
+ $prefix,
224
+ $pattern=$a and $b,
225
+ $epe=-$foo,
226
+ $port=!$foo
227
+ ) { }
132
228
  END
133
229
  end
134
230
 
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.3.1
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anh Pham