puppet-lint-trailing_comma-check 0.1.3 → 0.2.0

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
  SHA1:
3
- metadata.gz: 7d67395151a03a6376cb6ac21c9858adf686dd1d
4
- data.tar.gz: 890f46cdc36f8aa114432dce26631230e4a24c19
3
+ metadata.gz: 7d68ac60631084104f565a115f14a8492d2e41d8
4
+ data.tar.gz: cfa1c09748146e3cd158cd08f58e705c685e07a8
5
5
  SHA512:
6
- metadata.gz: c9b0dcb07ff05ec38da689d4beb941c86d988744732df2126f7a3019469c00a38b6e38341d1064912d757959131fb0ff535d741a8cab2ec7c7c171ae7df23abb
7
- data.tar.gz: 423bb368b8afbf50eef5bca8f6562ad1c627c6c5044d3e4e61ec29c481859212c0382037409b877a206d98ef8a0e0c7940f43076d727d7b6b4266c74167f7422
6
+ metadata.gz: 23415bfa426889069025182ef393b769a0eb9926f0115466d6aebae0ea7e81ce244f517bea03e9557d4066343017855291064f5ba2babed3bf06c36236d0d782
7
+ data.tar.gz: ae3f3b49374e58d5a5b4453f8b47db53e795c6f2c307f54aa2aaedf49119dd30c425461487e28f6b9892c2635de9cfe18d4f4aa78578fba0ea43580ec4d644a0
@@ -1,19 +1,75 @@
1
1
  PuppetLint.new_check(:trailing_comma) do
2
+ def array_indexes
3
+ results = []
4
+ tokens.each_with_index do |token, token_idx|
5
+ if token.type == :LBRACK
6
+ real_idx = 0
7
+ tokens[token_idx+1..-1].each_with_index do |cur_token, cur_token_idx|
8
+ real_idx = token_idx + 1 + cur_token_idx
9
+ break if cur_token.type == :RBRACK
10
+ end
11
+
12
+ results << {
13
+ :start => token_idx,
14
+ :end => real_idx,
15
+ :tokens => tokens[token_idx..real_idx],
16
+ }
17
+ end
18
+ end
19
+ results
20
+ end
21
+
22
+ def defaults_indexes
23
+ results = []
24
+ tokens.each_with_index do |token, token_idx|
25
+ if token.type == :CLASSREF && token.next_code_token && \
26
+ token.next_code_token.type == :LBRACE
27
+ real_idx = 0
28
+
29
+ tokens[token_idx+1..-1].each_with_index do |cur_token, cur_token_idx|
30
+ real_idx = token_idx + 1 + cur_token_idx
31
+ break if cur_token.type == :RBRACE
32
+ end
33
+
34
+ results << {
35
+ :start => token_idx,
36
+ :end => real_idx,
37
+ :tokens => tokens[token_idx..real_idx],
38
+ }
39
+ end
40
+ end
41
+ results
42
+ end
43
+
44
+ def check_elem(elem)
45
+ lbo_token = elem[:tokens][-1].prev_code_token
46
+ if lbo_token && lbo_token.type != :COLON && \
47
+ elem[:tokens][-1].type != :SEMIC && \
48
+ lbo_token.type != :COMMA && \
49
+ lbo_token.next_token.type == :NEWLINE
50
+ notify :warning, {
51
+ :message => 'missing trailing comma after last element',
52
+ :line => lbo_token.next_token.line,
53
+ :column => lbo_token.next_token.column,
54
+ :token => lbo_token.next_token,
55
+ }
56
+ end
57
+ end
58
+
2
59
  def check
3
60
  # Resource and class declarations
4
61
  resource_indexes.each do |resource|
5
- lbo_token = resource[:tokens][-1].prev_code_token
6
- if lbo_token && lbo_token.type != :COLON && \
7
- resource[:tokens][-1].type != :SEMIC && \
8
- lbo_token.type != :COMMA && \
9
- lbo_token.next_token.type == :NEWLINE
10
- notify :warning, {
11
- :message => 'missing trailing comma after last parameter',
12
- :line => lbo_token.next_token.line,
13
- :column => lbo_token.next_token.column,
14
- :token => lbo_token.next_token,
15
- }
16
- end
62
+ check_elem(resource)
63
+ end
64
+
65
+ # Arrays
66
+ array_indexes.each do |array|
67
+ check_elem(array)
68
+ end
69
+
70
+ # Defaults
71
+ defaults_indexes.each do |defaults|
72
+ check_elem(defaults)
17
73
  end
18
74
  end
19
75
 
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'trailing_comma' do
4
- let (:msg) { 'missing trailing comma after last parameter' }
4
+ let (:msg) { 'missing trailing comma after last element' }
5
5
 
6
6
  context 'with fix disabled' do
7
7
  context 'trailing comma present' do
@@ -26,6 +26,19 @@ describe 'trailing_comma' do
26
26
  }
27
27
 
28
28
  resources { 'sshkey': purge => true }
29
+
30
+ user { 'elmo':
31
+ groups => [
32
+ 'foo',
33
+ 'bar',
34
+ ],
35
+ groupss => ['baz', 'qux'],
36
+ }
37
+
38
+ File {
39
+ owner => root,
40
+ group => '0',
41
+ }
29
42
  EOS
30
43
  }
31
44
 
@@ -56,16 +69,31 @@ describe 'trailing_comma' do
56
69
  }
57
70
 
58
71
  resources { 'sshkey': purge => true }
72
+
73
+ user { 'elmo':
74
+ groups => [
75
+ 'foo',
76
+ 'bar'
77
+ ],
78
+ groupss => ['baz', 'qux'],
79
+ }
80
+
81
+ File {
82
+ owner => root,
83
+ group => '0'
84
+ }
59
85
  EOS
60
86
  }
61
87
 
62
- it 'should detect a single problem' do
63
- expect(problems).to have(2).problems
88
+ it 'should detect 4 problems' do
89
+ expect(problems).to have(4).problems
64
90
  end
65
91
 
66
- it 'should create a warning' do
92
+ it 'should create warnings' do
67
93
  expect(problems).to contain_warning(msg).on_line(3).in_column(32)
68
94
  expect(problems).to contain_warning(msg).on_line(10).in_column(27)
95
+ expect(problems).to contain_warning(msg).on_line(24).in_column(18)
96
+ expect(problems).to contain_warning(msg).on_line(31).in_column(23)
69
97
  end
70
98
  end
71
99
  end
@@ -101,6 +129,19 @@ describe 'trailing_comma' do
101
129
  }
102
130
 
103
131
  resources { 'sshkey': purge => true }
132
+
133
+ user { 'elmo':
134
+ groups => [
135
+ 'foo',
136
+ 'bar',
137
+ ],
138
+ groupss => ['baz', 'qux'],
139
+ }
140
+
141
+ File {
142
+ owner => root,
143
+ group => '0',
144
+ }
104
145
  EOS
105
146
  }
106
147
 
@@ -135,16 +176,31 @@ describe 'trailing_comma' do
135
176
  }
136
177
 
137
178
  resources { 'sshkey': purge => true }
179
+
180
+ user { 'elmo':
181
+ groups => [
182
+ 'foo',
183
+ 'bar'
184
+ ],
185
+ groupss => ['baz', 'qux'],
186
+ }
187
+
188
+ File {
189
+ owner => root,
190
+ group => '0'
191
+ }
138
192
  EOS
139
193
  }
140
194
 
141
- it 'should detect a single problem' do
142
- expect(problems).to have(2).problems
195
+ it 'should detect 4 problems' do
196
+ expect(problems).to have(4).problems
143
197
  end
144
198
 
145
199
  it 'should create a warning' do
146
200
  expect(problems).to contain_fixed(msg).on_line(3).in_column(32)
147
201
  expect(problems).to contain_fixed(msg).on_line(10).in_column(27)
202
+ expect(problems).to contain_fixed(msg).on_line(24).in_column(18)
203
+ expect(problems).to contain_fixed(msg).on_line(31).in_column(23)
148
204
  end
149
205
 
150
206
  it 'should add trailing commas' do
@@ -169,6 +225,19 @@ describe 'trailing_comma' do
169
225
  }
170
226
 
171
227
  resources { 'sshkey': purge => true }
228
+
229
+ user { 'elmo':
230
+ groups => [
231
+ 'foo',
232
+ 'bar',
233
+ ],
234
+ groupss => ['baz', 'qux'],
235
+ }
236
+
237
+ File {
238
+ owner => root,
239
+ group => '0',
240
+ }
172
241
  EOS
173
242
  )
174
243
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-lint-trailing_comma-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Camptocamp