puppet-lint-param_comment-check 0.1.2 → 0.1.5

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: 57edfb5e9c1716c1bfb583619d1a36bf57f7f4492af64f33a828dba5f4b1eec2
4
- data.tar.gz: 3350c35a78495a1d4de4f25567af5da91cbed2601c25e3fe8e13e273e31e8f09
3
+ metadata.gz: 637cfc1953d7f93cd5a8d160e90d7abeaf93efd0a1eafbf651dca61259fb6668
4
+ data.tar.gz: 7d74686774590de42d926b2e1a952330a1613e2b3724a8aac531d5003d3916a8
5
5
  SHA512:
6
- metadata.gz: 0e58accb1fe706bb94f24ccbe8ed80f50aee634f942360f4b29c203a206c84a7196fc0bda9c882561f7d2b9420919d703fb7444a41ac47da9d3c4a998d54ae48
7
- data.tar.gz: 10e7eef3b38f933c9f871e727d912c88e1fc76193158dd909c6c99e9d084dd585dce554dc5573d7eb206170b2d07c7fbe530a888903e3ca15446d9fbb29fbdd7
6
+ metadata.gz: d67851be5623ec31c85ff025c13420e8535915d7f9df8d6e8c153df4e7f51c3874253a5dcdf87b30ccab4841a31501f91ab98989d40e30717ede4317dd55da18
7
+ data.tar.gz: f47b461b2b56d68e9792be6c8da580f4f4222960364ef165d72fe867007e0a2335adcffe5898b978c386141c0b374c10228f9a7257971fbd11374b765666f2f5
@@ -18,12 +18,14 @@ EMPTY_PARAM = {
18
18
  # @return The head comments
19
19
  def get_comments(tokens, token_start)
20
20
  comments = []
21
- token_pointer = token_start
21
+ token_pointer = token_start - 1
22
22
  while token_pointer >= 0
23
- comments.append(tokens[token_pointer]) if tokens[token_pointer].type == :COMMENT
23
+ break unless %i[COMMENT NEWLINE].include? tokens[token_pointer].type
24
+
25
+ comments.append(tokens[token_pointer])
24
26
  token_pointer -= 1
25
27
  end
26
- comments.reverse
28
+ comments.reject { |comment| comment.type == :NEWLINE }.reverse
27
29
  end
28
30
 
29
31
  # Analyze a parameter token
@@ -50,11 +52,11 @@ end
50
52
  def analyze_params(param_tokens) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
51
53
  params = []
52
54
  current_param = EMPTY_PARAM.dup
53
- in_brackets = false
54
- param_tokens.reject { |token| %i[WHITESPACE NEWLINE].include? token.type }.each do |token|
55
- in_brackets = true if token.type == :LBRACK
56
- in_brackets = false if token.type == :RBRACK
57
- next if in_brackets
55
+ brackets = 0
56
+ param_tokens.reject { |token| %i[WHITESPACE NEWLINE INDENT].include? token.type }.each do |token|
57
+ brackets += 1 if %i[LBRACK LBRACE].include? token.type
58
+ brackets -= 1 if %i[RBRACK RBRACE].include? token.type
59
+ next unless brackets.zero?
58
60
 
59
61
  current_param = analyze_param_token(token, current_param) unless token.type == :COMMA
60
62
  if token.type == :COMMA
@@ -62,7 +64,7 @@ def analyze_params(param_tokens) # rubocop:disable Metrics/AbcSize,Metrics/Cyclo
62
64
  current_param = EMPTY_PARAM.dup
63
65
  end
64
66
  end
65
- params.append(current_param) unless current_param == EMPTY_PARAM
67
+ params.append(current_param) unless current_param[:name] == ''
66
68
  params
67
69
  end
68
70
 
@@ -135,9 +137,9 @@ PuppetLint.new_check(:param_comment) do # rubocop:disable Metrics/BlockLength
135
137
  def check_comment_order(params)
136
138
  param_comments = @comment_engine.params
137
139
  param_comments.each_with_index do |param, index|
138
- return false unless param[:name] == params[index][:name]
140
+ return param[:line] unless param[:name] == params[index][:name]
139
141
  end
140
- true
142
+ -1
141
143
  end
142
144
 
143
145
  # Check class or defined type indexes
@@ -147,7 +149,9 @@ PuppetLint.new_check(:param_comment) do # rubocop:disable Metrics/BlockLength
147
149
  params = analyze_params(index[:param_tokens])
148
150
  return false unless check_comments(comments)
149
151
  return false unless check_parameters_count(params)
150
- return warn('Parameters sorted wrong') unless check_comment_order(params)
152
+
153
+ comment_order_line = check_comment_order(params)
154
+ return warn('Parameters sorted wrong', comment_order_line) unless comment_order_line == -1
151
155
  end
152
156
  true
153
157
  end
@@ -4,28 +4,30 @@
4
4
  EMPTY_PARAM_COMMENT = {
5
5
  name: '',
6
6
  description: '',
7
- options: []
7
+ options: [],
8
+ line: -1
8
9
  }.freeze
9
10
 
10
11
  # The empty data of a hash option
11
12
  EMPTY_OPTION_COMMENT = {
12
13
  name: '',
13
14
  type: '',
14
- description: ''
15
+ description: '',
16
+ line: -1
15
17
  }.freeze
16
18
 
17
19
  # A regular expression describing a parameter header
18
20
  REGEXP_PARAM_HEADER = /^@param (?<name>[^ ]+)$/.freeze
19
21
 
20
22
  # A regular expression describing a hash option header
21
- REGEXP_OPTION_HEADER = /^@option (?<hash_name>[^ ]+) \[(?<type>[^\]]+)\] :(?<name>[^ ]+)$/.freeze
23
+ REGEXP_OPTION_HEADER = /^@option (?<hash_name>[^ ]+) \[(?<type>.+)\] :(?<name>[^ ]+)$/.freeze
22
24
 
23
25
  # Comment received in an invalid state
24
26
  class InvalidCommentForState < StandardError
25
27
  def initialize(comment, state)
26
28
  @comment = comment
27
29
  @state = state
28
- super "Invalid state #{@state} for comment #{@comment.value.strip}"
30
+ super "Can not process the comment '#{@comment.value.strip}' in the state #{@state}"
29
31
  end
30
32
 
31
33
  attr_reader :comment
@@ -56,17 +58,23 @@ class ParamComments
56
58
  def initialize
57
59
  @workflow = ParamWorkflow.new(self)
58
60
 
61
+ reset
62
+ end
63
+
64
+ def reset
59
65
  @current_param = nil
60
66
  @current_option = nil
61
67
  @in_option = false
62
68
  @params_have_started = false
63
69
  @params = []
70
+ @workflow.restore!(:start)
64
71
  end
65
72
 
66
73
  # Walk through every comment and transition the workflow fsm accordingly
67
74
  #
68
75
  # @param comments A list of Comment tokens appearing before the class/defined type header
69
- def process(comments) # rubocop:disable Metrics/MethodLength
76
+ def process(comments) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
77
+ reset
70
78
  @current_comment = PuppetLint::Lexer::Token.new(:COMMENT, '', 1, 1)
71
79
  comments.each do |comment|
72
80
  @current_comment = comment
@@ -87,7 +95,7 @@ class ParamComments
87
95
  end
88
96
 
89
97
  # Called before the got_header event. Interpret the parameter header comment
90
- def got_header_trigger(_, comment)
98
+ def got_header_trigger(_, comment) # rubocop:disable Metrics/AbcSize
91
99
  @params_have_started = true
92
100
  @current_param[:options].append(@current_option) if @in_option && !@current_option.nil?
93
101
  @params.append(@current_param) unless @current_param.nil?
@@ -96,6 +104,7 @@ class ParamComments
96
104
  @in_option = false
97
105
  comment.value.strip.match(REGEXP_PARAM_HEADER) do |match|
98
106
  @current_param[:name] = match.named_captures['name'].strip
107
+ @current_param[:line] = comment.line
99
108
  end
100
109
  end
101
110
 
@@ -120,6 +129,7 @@ class ParamComments
120
129
 
121
130
  @current_option[:name] = match.named_captures['name']
122
131
  @current_option[:type] = match.named_captures['type']
132
+ @current_param[:line] = comment.line
123
133
  end
124
134
  end
125
135
 
@@ -53,6 +53,106 @@ describe 'param_comment' do
53
53
  end
54
54
  end
55
55
 
56
+ context 'valid code with complex type definition' do
57
+ let(:code) do
58
+ <<~CODE
59
+ # @summary
60
+ # some class
61
+ #
62
+ # @param optional
63
+ # Complicated
64
+ class my_class (
65
+ Optional[Hash[
66
+ String,
67
+ Struct[{
68
+ test => Optional[Boolean]
69
+ }]
70
+ ]] $optional = undef
71
+ ) {}
72
+ CODE
73
+ end
74
+
75
+ it 'should not detect any problems' do
76
+ expect(problems).to have(0).problems
77
+ end
78
+ end
79
+
80
+ context 'valid code with hash default' do
81
+ let(:code) do
82
+ <<~CODE
83
+ # @summary
84
+ # some class
85
+ #
86
+ # @param hashparam
87
+ # A hash
88
+ class my_class (
89
+ Hash $hashparam = {
90
+ somekey => "value"
91
+ }
92
+ ) {}
93
+ CODE
94
+ end
95
+
96
+ it 'should not detect any problems' do
97
+ expect(problems).to have(0).problems
98
+ end
99
+ end
100
+
101
+ context 'valid code with hash default with an enclosed hash' do
102
+ let(:code) do
103
+ <<~CODE
104
+ # @summary
105
+ # some class
106
+ #
107
+ # @param hashparam
108
+ # A hash
109
+ class my_class (
110
+ Hash $hashparam = {
111
+ 'anotherhash' => {
112
+ somekey => "value"
113
+ },
114
+ },
115
+ ) {}
116
+ CODE
117
+ end
118
+
119
+ it 'should not detect any problems' do
120
+ expect(problems).to have(0).problems
121
+ end
122
+ end
123
+
124
+ context 'valid code with multiple classes' do
125
+ let(:code) do
126
+ <<~CODE
127
+ # @summary
128
+ # some class
129
+ #
130
+ # @param mandatory
131
+ # A mandatory parameter
132
+ # with two lines
133
+ class my_class (
134
+ String $mandatory,
135
+ ) {}
136
+
137
+ # @summary
138
+ # some other class
139
+ #
140
+ # @see something
141
+ #
142
+ # @param mandatory
143
+ # A mandatory parameter
144
+ # with two lines
145
+ class my_other_class (
146
+ String $mandatory,
147
+ ) {}
148
+ CODE
149
+ end
150
+
151
+ it 'should not detect any problems' do
152
+ expect(problems).to have(0).problems
153
+ end
154
+ end
155
+
56
156
  context 'code with missing parameter comment' do
57
157
  let(:code) do
58
158
  <<~CODE
@@ -106,15 +206,14 @@ describe 'param_comment' do
106
206
  context 'code with wrongly sorted parameter comments' do
107
207
  let(:code) do
108
208
  <<~CODE
109
- # @param withdefault
110
- # A parameter with a default value
111
- #
112
209
  # @param mandatory
113
210
  # A mandatory parameter
114
211
  #
115
212
  # @param optional
116
213
  # An optional parameter
117
-
214
+ #
215
+ # @param withdefault
216
+ # A parameter with a default value
118
217
  class my_class (
119
218
  String $mandatory,
120
219
  Boolean $withdefault = false,
@@ -128,7 +227,7 @@ describe 'param_comment' do
128
227
  end
129
228
 
130
229
  it 'should create a warning' do
131
- expect(problems).to contain_warning('Parameters sorted wrong').on_line(1).in_column(1)
230
+ expect(problems).to contain_warning('Parameters sorted wrong').on_line(4).in_column(1)
132
231
  end
133
232
  end
134
233
 
@@ -156,7 +255,7 @@ describe 'param_comment' do
156
255
  end
157
256
 
158
257
  it 'should create a warning' do
159
- expect(problems).to contain_warning('Invalid state awaiting_separator for comment @param withdefault')
258
+ expect(problems).to contain_warning('Can not process the comment \'@param withdefault\' in the state awaiting_separator')
160
259
  .on_line(3)
161
260
  .in_column(1)
162
261
  end
@@ -177,7 +276,7 @@ describe 'param_comment' do
177
276
  end
178
277
 
179
278
  it 'should create a warning' do
180
- expect(problems).to contain_warning('Invalid param or hash option header')
279
+ expect(problems).to contain_warning('Invalid param or hash option header: @param mandatory A mandatory parameter')
181
280
  .on_line(1)
182
281
  .in_column(1)
183
282
  end
@@ -248,7 +347,7 @@ describe 'param_comment' do
248
347
 
249
348
  it 'should create a warning' do
250
349
  expect(problems).to contain_warning(
251
- 'Invalid state awaiting_header for comment @option mandatory [Boolean] :some_option'
350
+ 'Can not process the comment \'@option mandatory [Boolean] :some_option\' in the state awaiting_header'
252
351
  )
253
352
  .on_line(4)
254
353
  .in_column(1)
@@ -273,7 +372,7 @@ describe 'param_comment' do
273
372
 
274
373
  it 'should create a warning' do
275
374
  expect(problems).to contain_warning(
276
- 'Invalid param or hash option header'
375
+ 'Invalid param or hash option header: @option mandatory [Boolean] :some_option An option'
277
376
  )
278
377
  .on_line(3)
279
378
  .in_column(1)
@@ -302,10 +401,28 @@ describe 'param_comment' do
302
401
 
303
402
  it 'should create a warning' do
304
403
  expect(problems).to contain_warning(
305
- 'Invalid state awaiting_separator for comment @param second'
404
+ 'Can not process the comment \'@param second\' in the state awaiting_separator'
306
405
  )
307
406
  .on_line(5)
308
407
  .in_column(1)
309
408
  end
310
409
  end
410
+
411
+ context 'valid code with complex hash option type' do
412
+ let(:code) do
413
+ <<~CODE
414
+ # @param mandatory
415
+ # A mandatory parameter
416
+ # @option mandatory [Optional[String]] :some_option
417
+ # An option
418
+ class my_class (
419
+ Hash $mandatory,
420
+ ) {}
421
+ CODE
422
+ end
423
+
424
+ it 'should detect exactly one problem' do
425
+ expect(problems).to have(0).problems
426
+ end
427
+ end
311
428
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-lint-param_comment-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Ploeger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-31 00:00:00.000000000 Z
11
+ date: 2022-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: finite_machine