puppet-lint-param_comment-check 0.1.1 → 0.1.4

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: b4b8ad42633483b29d912da99b108a9bcd277b7c6b1d21ebaeeedb4aa9634e12
4
- data.tar.gz: 768790358859c7375df58da9fbbd2b932a94f606647d9f13e513fc119cbd29e3
3
+ metadata.gz: 760c2ecbb41952244ece53d7fddafcc91f322300b512a65dfc4d5ab7b5622636
4
+ data.tar.gz: 187d476c9e3418c03935d00ddf84fd9c9618cd8ac4a14a391e0046f68f92221f
5
5
  SHA512:
6
- metadata.gz: eee0e509c76c55d1cc9b97ed444bdadbf1caa8a970e03e339618083d1e5019f0a18e70d627b4f691f1aa255a78a8240972db6a68d38b2a41c9f9993f3a3714e4
7
- data.tar.gz: 6af8ad1b81d6d0b9f2e4d8cbf9cee33507e6813f025691187edab23adc9f0510372e4cd88362f614066904c32243bc3bc0a550fddfe238f9e5c250bfc897f592
6
+ metadata.gz: 8396f55b48c7cd1e82f66855eb950c2c1fd4e908423625a262e6ba11496d2cd3b9c90931c66deb97efba5d706502416e71ef4ae47f7d459b5c0fcf24d7b86ef4
7
+ data.tar.gz: '08c1d478a70c0a204805844aa0ad1724ee7a371852c3f7059596204d3c62f89d9044fa2cf44b26b6df2731331f371765b3cdf86de148ecf5cab88216336dea93'
@@ -35,7 +35,7 @@ def analyze_param_token(token, current_param)
35
35
  case token.type
36
36
  when :VARIABLE
37
37
  current_param[:name] = token.value
38
- when :CLASSREF
38
+ when :CLASSREF, :TYPE
39
39
  current_param[:type] = token.value
40
40
  when :EQUALS
41
41
  current_param[:has_default] = true
@@ -47,17 +47,22 @@ end
47
47
  # Analyze the parameters of a class or a defined type
48
48
  #
49
49
  # @param param_tokens The parameter tokens to analyze
50
- def analyze_params(param_tokens)
50
+ def analyze_params(param_tokens) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
51
51
  params = []
52
52
  current_param = EMPTY_PARAM.dup
53
- param_tokens.reject { |token| %i[WHITESPACE NEWLINE].include? token.type }.each do |token|
53
+ brackets = 0
54
+ param_tokens.reject { |token| %i[WHITESPACE NEWLINE INDENT].include? token.type }.each do |token|
55
+ brackets += 1 if %i[LBRACK LBRACE].include? token.type
56
+ brackets -= 1 if %i[RBRACK RBRACE].include? token.type
57
+ next unless brackets.zero?
58
+
54
59
  current_param = analyze_param_token(token, current_param) unless token.type == :COMMA
55
60
  if token.type == :COMMA
56
61
  params.append(current_param)
57
62
  current_param = EMPTY_PARAM.dup
58
63
  end
59
64
  end
60
- params.append(current_param) unless current_param == EMPTY_PARAM
65
+ params.append(current_param) unless current_param[:name] == ''
61
66
  params
62
67
  end
63
68
 
@@ -100,7 +105,7 @@ PuppetLint.new_check(:param_comment) do # rubocop:disable Metrics/BlockLength
100
105
  next unless comment.value.match?(/@param/) || comment.value.match?(/@option/)
101
106
  next if comment.value.strip.match?(REGEXP_PARAM_HEADER) || comment.value.strip.match?(REGEXP_OPTION_HEADER)
102
107
 
103
- return warn('Invalid param or hash option header', comment.line, comment.column)
108
+ return warn("Invalid param or hash option header: #{comment.value.strip}", comment.line, comment.column)
104
109
  end
105
110
  true
106
111
  end
@@ -130,9 +135,9 @@ PuppetLint.new_check(:param_comment) do # rubocop:disable Metrics/BlockLength
130
135
  def check_comment_order(params)
131
136
  param_comments = @comment_engine.params
132
137
  param_comments.each_with_index do |param, index|
133
- return false unless param[:name] == params[index][:name]
138
+ return param[:line] unless param[:name] == params[index][:name]
134
139
  end
135
- true
140
+ -1
136
141
  end
137
142
 
138
143
  # Check class or defined type indexes
@@ -142,7 +147,9 @@ PuppetLint.new_check(:param_comment) do # rubocop:disable Metrics/BlockLength
142
147
  params = analyze_params(index[:param_tokens])
143
148
  return false unless check_comments(comments)
144
149
  return false unless check_parameters_count(params)
145
- return warn('Parameters sorted wrong') unless check_comment_order(params)
150
+
151
+ comment_order_line = check_comment_order(params)
152
+ return warn('Parameters sorted wrong', comment_order_line) unless comment_order_line == -1
146
153
  end
147
154
  true
148
155
  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
@@ -66,7 +68,7 @@ class ParamComments
66
68
  # Walk through every comment and transition the workflow fsm accordingly
67
69
  #
68
70
  # @param comments A list of Comment tokens appearing before the class/defined type header
69
- def process(comments) # rubocop:disable Metrics/MethodLength
71
+ def process(comments) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
70
72
  @current_comment = PuppetLint::Lexer::Token.new(:COMMENT, '', 1, 1)
71
73
  comments.each do |comment|
72
74
  @current_comment = comment
@@ -87,7 +89,7 @@ class ParamComments
87
89
  end
88
90
 
89
91
  # Called before the got_header event. Interpret the parameter header comment
90
- def got_header_trigger(_, comment)
92
+ def got_header_trigger(_, comment) # rubocop:disable Metrics/AbcSize
91
93
  @params_have_started = true
92
94
  @current_param[:options].append(@current_option) if @in_option && !@current_option.nil?
93
95
  @params.append(@current_param) unless @current_param.nil?
@@ -96,6 +98,7 @@ class ParamComments
96
98
  @in_option = false
97
99
  comment.value.strip.match(REGEXP_PARAM_HEADER) do |match|
98
100
  @current_param[:name] = match.named_captures['name'].strip
101
+ @current_param[:line] = comment.line
99
102
  end
100
103
  end
101
104
 
@@ -120,6 +123,7 @@ class ParamComments
120
123
 
121
124
  @current_option[:name] = match.named_captures['name']
122
125
  @current_option[:type] = match.named_captures['type']
126
+ @current_param[:line] = comment.line
123
127
  end
124
128
  end
125
129
 
@@ -34,6 +34,93 @@ describe 'param_comment' do
34
34
  end
35
35
  end
36
36
 
37
+ context 'valid code with spaces in the type' do
38
+ let(:code) do
39
+ <<~CODE
40
+ # @summary
41
+ # some class
42
+ #
43
+ # @param ensure
44
+ # Ensure it
45
+ class my_class (
46
+ Enum['present', 'absent'] $ensure = 'present'
47
+ ) {}
48
+ CODE
49
+ end
50
+
51
+ it 'should not detect any problems' do
52
+ expect(problems).to have(0).problems
53
+ end
54
+ end
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
+
37
124
  context 'code with missing parameter comment' do
38
125
  let(:code) do
39
126
  <<~CODE
@@ -87,15 +174,14 @@ describe 'param_comment' do
87
174
  context 'code with wrongly sorted parameter comments' do
88
175
  let(:code) do
89
176
  <<~CODE
90
- # @param withdefault
91
- # A parameter with a default value
92
- #
93
177
  # @param mandatory
94
178
  # A mandatory parameter
95
179
  #
96
180
  # @param optional
97
181
  # An optional parameter
98
-
182
+ #
183
+ # @param withdefault
184
+ # A parameter with a default value
99
185
  class my_class (
100
186
  String $mandatory,
101
187
  Boolean $withdefault = false,
@@ -109,7 +195,7 @@ describe 'param_comment' do
109
195
  end
110
196
 
111
197
  it 'should create a warning' do
112
- expect(problems).to contain_warning('Parameters sorted wrong').on_line(1).in_column(1)
198
+ expect(problems).to contain_warning('Parameters sorted wrong').on_line(4).in_column(1)
113
199
  end
114
200
  end
115
201
 
@@ -137,7 +223,7 @@ describe 'param_comment' do
137
223
  end
138
224
 
139
225
  it 'should create a warning' do
140
- expect(problems).to contain_warning('Invalid state awaiting_separator for comment @param withdefault')
226
+ expect(problems).to contain_warning('Can not process the comment \'@param withdefault\' in the state awaiting_separator')
141
227
  .on_line(3)
142
228
  .in_column(1)
143
229
  end
@@ -158,7 +244,7 @@ describe 'param_comment' do
158
244
  end
159
245
 
160
246
  it 'should create a warning' do
161
- expect(problems).to contain_warning('Invalid param or hash option header')
247
+ expect(problems).to contain_warning('Invalid param or hash option header: @param mandatory A mandatory parameter')
162
248
  .on_line(1)
163
249
  .in_column(1)
164
250
  end
@@ -229,7 +315,7 @@ describe 'param_comment' do
229
315
 
230
316
  it 'should create a warning' do
231
317
  expect(problems).to contain_warning(
232
- 'Invalid state awaiting_header for comment @option mandatory [Boolean] :some_option'
318
+ 'Can not process the comment \'@option mandatory [Boolean] :some_option\' in the state awaiting_header'
233
319
  )
234
320
  .on_line(4)
235
321
  .in_column(1)
@@ -254,7 +340,7 @@ describe 'param_comment' do
254
340
 
255
341
  it 'should create a warning' do
256
342
  expect(problems).to contain_warning(
257
- 'Invalid param or hash option header'
343
+ 'Invalid param or hash option header: @option mandatory [Boolean] :some_option An option'
258
344
  )
259
345
  .on_line(3)
260
346
  .in_column(1)
@@ -283,10 +369,28 @@ describe 'param_comment' do
283
369
 
284
370
  it 'should create a warning' do
285
371
  expect(problems).to contain_warning(
286
- 'Invalid state awaiting_separator for comment @param second'
372
+ 'Can not process the comment \'@param second\' in the state awaiting_separator'
287
373
  )
288
374
  .on_line(5)
289
375
  .in_column(1)
290
376
  end
291
377
  end
378
+
379
+ context 'valid code with complex hash option type' do
380
+ let(:code) do
381
+ <<~CODE
382
+ # @param mandatory
383
+ # A mandatory parameter
384
+ # @option mandatory [Optional[String]] :some_option
385
+ # An option
386
+ class my_class (
387
+ Hash $mandatory,
388
+ ) {}
389
+ CODE
390
+ end
391
+
392
+ it 'should detect exactly one problem' do
393
+ expect(problems).to have(0).problems
394
+ end
395
+ end
292
396
  end
metadata CHANGED
@@ -1,7 +1,7 @@
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.1
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Ploeger
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2022-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: finite_machine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: puppet-lint
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -25,13 +39,13 @@ dependencies:
25
39
  - !ruby/object:Gem::Version
26
40
  version: '1.0'
27
41
  - !ruby/object:Gem::Dependency
28
- name: finite_machine
42
+ name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - ">="
32
46
  - !ruby/object:Gem::Version
33
47
  version: '0'
34
- type: :runtime
48
+ type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
@@ -53,7 +67,7 @@ dependencies:
53
67
  - !ruby/object:Gem::Version
54
68
  version: '3.0'
55
69
  - !ruby/object:Gem::Dependency
56
- name: rspec-its
70
+ name: rspec-collection_matchers
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
73
  - - "~>"
@@ -67,7 +81,7 @@ dependencies:
67
81
  - !ruby/object:Gem::Version
68
82
  version: '1.0'
69
83
  - !ruby/object:Gem::Dependency
70
- name: rspec-collection_matchers
84
+ name: rspec-its
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - "~>"
@@ -80,20 +94,6 @@ dependencies:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
96
  version: '1.0'
83
- - !ruby/object:Gem::Dependency
84
- name: rake
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rubocop
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -158,8 +158,7 @@ requirements: []
158
158
  rubygems_version: 3.0.3.1
159
159
  signing_key:
160
160
  specification_version: 4
161
- summary: '["A puppet-lint plugin to check @param comments", " A puppet-lint plugin
162
- to check that manifest files contain properly formatted @param comments.\n"]'
161
+ summary: A puppet-lint plugin to check @param comments
163
162
  test_files:
164
163
  - spec/spec_helper.rb
165
164
  - spec/puppet-lint/plugins/check_param_comment_spec.rb