puppet-lint-class_parameter-check 0.1.4 → 0.2.0

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
  SHA1:
3
- metadata.gz: 6e75015f13ea813a8d308575f028536653bc84cf
4
- data.tar.gz: 95ed706edf7e95efb9b03e7028ebc8990aee9376
3
+ metadata.gz: 2cc6a4bf5f8538998456c3a4264a06293cca5534
4
+ data.tar.gz: 0ae21922016a83a566236f4f72812d0a435c9d4a
5
5
  SHA512:
6
- metadata.gz: 3143d719a396c96967fe5a010114c5d344cb2ba65c4ccc2b4a877a3d43c99819e17e5cdcefa9999bd9b16992ba4cc15534b1d7553b83f5b94931cfb3a5f2d5d1
7
- data.tar.gz: d3659ced8a9ddcc5621e11c134bd61a1eb14fcfd85b9e093cfa501883939232e043b8480a27da91bc7970ccda378c25165d6b30607a8d036c63cde0cbff32c0d
6
+ metadata.gz: 3eadbe8e218b908e4beea58261300069ea7ff61685b108edd6b4630658135d05513b2f8d9a34c9f2f10ebb86752fc20b61e0213b9b58d505d8b02b2dfaad1bad
7
+ data.tar.gz: 3fd10e790eea0cb463752948a9f0ff889e2e92529d1c71def67b0558576bb30003cadb8c4519c72fae16cb98d4de7f34a7d5feff08a232cd0bd2ca85a7e7caaa
@@ -0,0 +1,38 @@
1
+ require_relative 'parameter_documentation'
2
+
3
+ class DocumentationList
4
+ attr_reader :start_index, :end_index
5
+
6
+ def initialize(tokens, start_index, end_index)
7
+ @tokens, @start_index, @end_index = tokens, start_index, end_index
8
+ end
9
+
10
+ def parameter_documentation_list
11
+ return @parameter_documentation_list unless @parameter_documentation_list.nil?
12
+
13
+ parameter_documentation = ParameterDocumentation.new
14
+ harvest_tokens = false
15
+
16
+ @parameter_documentation_list = @tokens.inject([]) do |memo, token|
17
+ if token.type == :COMMENT && token.value.match(/@param \w*/)
18
+ memo << parameter_documentation if parameter_documentation.tokens.any?
19
+ parameter_documentation = ParameterDocumentation.new
20
+ harvest_tokens = true
21
+ end
22
+
23
+ if (token.type == :COMMENT && token.value.strip == "")
24
+ harvest_tokens = false
25
+ end
26
+
27
+ if harvest_tokens
28
+ parameter_documentation.tokens << token
29
+ end
30
+
31
+ memo
32
+ end
33
+
34
+ @parameter_documentation_list << parameter_documentation if parameter_documentation.tokens.any?
35
+
36
+ @parameter_documentation_list
37
+ end
38
+ end
@@ -1,4 +1,6 @@
1
- class ClassParameter
1
+ class Parameter
2
+ attr_accessor :documentation
3
+
2
4
  def initialize
3
5
  @tokens = []
4
6
  end
@@ -0,0 +1,15 @@
1
+ class ParameterDocumentation
2
+ attr_reader :tokens
3
+
4
+ def initialize
5
+ @tokens = []
6
+ end
7
+
8
+ def name
9
+ @tokens.each do |token|
10
+ if token.value.match(/@param (\w*)/)
11
+ return $1
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,6 +1,6 @@
1
- require_relative 'class_parameter'
1
+ require_relative 'parameter'
2
2
 
3
- class ClassParameterList
3
+ class ParameterList
4
4
  attr_reader :errors, :start_index, :end_index
5
5
 
6
6
  def initialize(tokens, start_index, end_index)
@@ -12,6 +12,12 @@ class ClassParameterList
12
12
  parameters.sort
13
13
  end
14
14
 
15
+ def each(&block)
16
+ parameters.each do |parameter|
17
+ yield parameter
18
+ end
19
+ end
20
+
15
21
  def optional_parameters
16
22
  parameters.select(&:is_optional?)
17
23
  end
@@ -44,7 +50,7 @@ class ClassParameterList
44
50
 
45
51
  private
46
52
  def parameters
47
- parameter = ClassParameter.new
53
+ parameter = Parameter.new
48
54
  stack = []
49
55
 
50
56
  @tokens.inject([]) do |memo, token|
@@ -60,7 +66,7 @@ class ClassParameterList
60
66
  end
61
67
 
62
68
  memo << parameter
63
- parameter = ClassParameter.new
69
+ parameter = Parameter.new
64
70
  else
65
71
  parameter.add(token)
66
72
  end
@@ -0,0 +1,73 @@
1
+ require_relative 'parameter_list'
2
+ require_relative 'documentation_list'
3
+
4
+ class PuppetClass
5
+ def initialize(tokens, index)
6
+ @tokens, @index = tokens, index
7
+ end
8
+
9
+ def name
10
+ @name ||= @index[:tokens].select do |token|
11
+ token.type == :NAME
12
+ end.first.value
13
+ end
14
+
15
+ def has_parameter_documentation?
16
+ !parameter_documentation_list.empty?
17
+ end
18
+
19
+ def parameter_list
20
+ param_tokens = @index[:param_tokens]
21
+ return if param_tokens.nil?
22
+
23
+ @parameter_list ||= ParameterList.new(param_tokens, @tokens.index(param_tokens.first), @tokens.index(param_tokens.last))
24
+ end
25
+
26
+ def documentation_list
27
+ @documentation_list ||= DocumentationList.new(documentation_tokens, @tokens.index(documentation_tokens.first), @tokens.index(documentation_tokens.last))
28
+ end
29
+
30
+ def parameter_documentation_list
31
+ documentation_list.parameter_documentation_list
32
+ end
33
+
34
+ def sorted_parameter_documentation_list
35
+ sorted_parameter_list.inject([]) do |memo, parameter|
36
+ documentation = parameter_documentation_list.select do |parameter_documentation|
37
+ parameter_documentation.name == parameter.name
38
+ end.first
39
+
40
+ memo << documentation if documentation
41
+
42
+ memo
43
+ end
44
+ end
45
+
46
+ def sorted_parameter_list
47
+ parameter_list.sort
48
+ end
49
+
50
+ def parameter_documentation_start_index
51
+ @tokens.index(parameter_documentation_list.first.tokens.first)
52
+ end
53
+
54
+ def parameter_documentation_end_index
55
+ @tokens.index(parameter_documentation_list.last.tokens.last) + 1
56
+ end
57
+
58
+ private
59
+ def documentation_tokens
60
+ return @doc_tokens unless @doc_tokens.nil? || @doc_tokens.empty?
61
+
62
+ @doc_tokens = []
63
+ prev_token = @index[:tokens].first.prev_token
64
+ while (prev_token && prev_token.type != :RBRACE)
65
+ @doc_tokens << prev_token
66
+
67
+ prev_token = prev_token.prev_token
68
+ end
69
+
70
+ @doc_tokens.reverse!
71
+ return @doc_tokens
72
+ end
73
+ end
@@ -1,22 +1,40 @@
1
- require_relative '../../model/class_parameter_list'
1
+ require_relative '../../model/puppet_class'
2
2
 
3
3
  PuppetLint.new_check(:class_parameter) do
4
+ @fixed = false
5
+
4
6
  def check
5
- class_parameter_lists.each do |class_parameter_list|
6
- unless class_parameter_list.validate
7
- class_parameter_list.errors.each { |error| notify :error, error }
7
+ class_indexes.each do |class_index|
8
+ puppet_class = PuppetClass.new(tokens, class_index)
9
+
10
+ next unless puppet_class.parameter_list
11
+
12
+ unless puppet_class.parameter_list.validate
13
+ puppet_class.parameter_list.errors.each { |error| notify :error, error }
8
14
  end
9
15
  end
10
16
  end
11
17
 
12
18
  def fix(problem)
19
+ return if @fixed
20
+
13
21
  resorted_tokens = []
14
22
  token_index = 0
15
23
 
16
- class_parameter_lists.each do |class_parameter_list|
17
- resorted_tokens += tokens[token_index..class_parameter_list.start_index]
24
+ class_indexes.each do |class_index|
25
+ puppet_class = PuppetClass.new(tokens, class_index)
18
26
 
19
- sorted_class_parameter_list = class_parameter_list.sort
27
+ if puppet_class.has_parameter_documentation?
28
+ resorted_tokens += tokens[token_index...puppet_class.parameter_documentation_start_index]
29
+ puppet_class.sorted_parameter_documentation_list.each do |parameter_documentation|
30
+ resorted_tokens += parameter_documentation.tokens
31
+ end
32
+ resorted_tokens += tokens[puppet_class.parameter_documentation_end_index..puppet_class.parameter_list.start_index]
33
+ else
34
+ resorted_tokens += tokens[token_index..puppet_class.parameter_list.start_index]
35
+ end
36
+
37
+ sorted_class_parameter_list = puppet_class.sorted_parameter_list
20
38
  sorted_class_parameter_list.each do |parameter|
21
39
  resorted_tokens += parameter.tokens
22
40
 
@@ -24,27 +42,15 @@ PuppetLint.new_check(:class_parameter) do
24
42
  resorted_tokens << PuppetLint::Lexer::Token.new(:COMMA, ",", 0,0)
25
43
  end
26
44
 
27
- resorted_tokens << PuppetLint::Lexer::Token.new(:NEWLINE, "\n", 0,0)
45
+ resorted_tokens << PuppetLint::Lexer::Token.new(:NEWLINE, "\n", 0,0) unless parameter == sorted_class_parameter_list.last
28
46
  end
29
47
 
30
- token_index = class_parameter_list.end_index
48
+ token_index = puppet_class.parameter_list.end_index
31
49
  end
32
50
 
33
51
  resorted_tokens += tokens[token_index..-1]
34
52
  tokens.replace(resorted_tokens)
35
- end
36
-
37
- private
38
- def class_parameter_lists
39
- lists = []
40
-
41
- class_indexes.each do |class_index|
42
- param_tokens = class_index[:param_tokens]
43
- next if param_tokens.nil?
44
-
45
- lists << ClassParameterList.new(param_tokens, tokens.index(param_tokens.first), tokens.index(param_tokens.last))
46
- end
47
53
 
48
- lists
54
+ @fixed = true
49
55
  end
50
56
  end
@@ -202,54 +202,100 @@ describe 'class_parameter' do
202
202
 
203
203
  context 'multiple classes not sorted alphabetically' do
204
204
  let(:code) { <<-EOF
205
- class puppet_module(
206
- String $non_alphabetical,
207
- String $alphabetical
208
- ) { }
209
-
210
- class puppet_module2(
211
- String $non_alphabetical,
212
- String $alphabetical
213
- ) { }
214
- EOF
205
+ class puppet_module(
206
+ String $non_alphabetical,
207
+ String $alphabetical
208
+ ) { }
209
+
210
+ class puppet_module2(
211
+ String $non_alphabetical,
212
+ String $alphabetical
213
+ ) { }
214
+ EOF
215
215
  }
216
216
 
217
217
  it 'fixes the problem' do
218
218
  expect(manifest).to eq(<<-EOF
219
- class puppet_module(
220
- String $alphabetical,
221
- String $non_alphabetical
222
- ) { }
223
-
224
- class puppet_module2(
225
- String $alphabetical,
226
- String $non_alphabetical
227
- ) { }
228
- EOF
219
+ class puppet_module(
220
+ String $alphabetical,
221
+ String $non_alphabetical
222
+ ) { }
223
+
224
+ class puppet_module2(
225
+ String $alphabetical,
226
+ String $non_alphabetical
227
+ ) { }
228
+ EOF
229
229
  )
230
230
  end
231
231
  end
232
232
 
233
233
  context 'not sorted in groups and not alphabetically' do
234
234
  let(:code) { <<-EOF
235
- class puppet_module(
236
- String $non_alphabetical,
237
- String $non_alphabetical_optional = $puppet_module::params::non_alphabetical_optional,
238
- String $alphabetical,
239
- String $alphabetical_optional = "default"
240
- ) inherits puppet_module::params { }
241
- EOF
235
+ class puppet_module(
236
+ String $non_alphabetical,
237
+ String $non_alphabetical_optional = $puppet_module::params::non_alphabetical_optional,
238
+ String $alphabetical,
239
+ String $alphabetical_optional = "default"
240
+ ) inherits puppet_module::params { }
241
+ EOF
242
242
  }
243
243
 
244
244
  it 'fixes the problem' do
245
245
  expect(manifest).to eq(<<-EOF
246
- class puppet_module(
247
- String $alphabetical,
248
- String $non_alphabetical,
249
- String $alphabetical_optional = "default",
250
- String $non_alphabetical_optional = $puppet_module::params::non_alphabetical_optional
251
- ) inherits puppet_module::params { }
252
- EOF
246
+ class puppet_module(
247
+ String $alphabetical,
248
+ String $non_alphabetical,
249
+ String $alphabetical_optional = "default",
250
+ String $non_alphabetical_optional = $puppet_module::params::non_alphabetical_optional
251
+ ) inherits puppet_module::params { }
252
+ EOF
253
+ )
254
+ end
255
+ end
256
+
257
+ context 'with documented parameters not sorted alphabetically' do
258
+ let(:code) { <<-EOF
259
+ #
260
+ # @param non_alphabetical The non_alphabetical parameter
261
+ # @param alphabetical The alphabetical parameter
262
+ #
263
+ class puppet_module(
264
+ String $non_alphabetical,
265
+ String $alphabetical
266
+ ) { }
267
+
268
+ #
269
+ # @param non_alphabetical The non_alphabetical parameter
270
+ # @param alphabetical The alphabetical parameter
271
+ #
272
+ class puppet_module2(
273
+ String $non_alphabetical,
274
+ String $alphabetical
275
+ ) { }
276
+ EOF
277
+ }
278
+
279
+ it 'fixes the problem' do
280
+ expect(manifest).to eq(<<-EOF
281
+ #
282
+ # @param alphabetical The alphabetical parameter
283
+ # @param non_alphabetical The non_alphabetical parameter
284
+ #
285
+ class puppet_module(
286
+ String $alphabetical,
287
+ String $non_alphabetical
288
+ ) { }
289
+
290
+ #
291
+ # @param alphabetical The alphabetical parameter
292
+ # @param non_alphabetical The non_alphabetical parameter
293
+ #
294
+ class puppet_module2(
295
+ String $alphabetical,
296
+ String $non_alphabetical
297
+ ) { }
298
+ EOF
253
299
  )
254
300
  end
255
301
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-lint-class_parameter-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roelof Reitsma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-01 00:00:00.000000000 Z
11
+ date: 2016-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puppet-lint
@@ -91,8 +91,11 @@ extra_rdoc_files: []
91
91
  files:
92
92
  - README.md
93
93
  - LICENSE
94
- - lib/model/class_parameter.rb
95
- - lib/model/class_parameter_list.rb
94
+ - lib/model/documentation_list.rb
95
+ - lib/model/parameter.rb
96
+ - lib/model/parameter_documentation.rb
97
+ - lib/model/parameter_list.rb
98
+ - lib/model/puppet_class.rb
96
99
  - lib/puppet-lint/plugins/check_class_parameter.rb
97
100
  - spec/puppet-lint/plugins/check_class_parameter_spec.rb
98
101
  - spec/spec_helper.rb