puppet-lint-param_comment-check 0.1.3 → 0.1.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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 760c2ecbb41952244ece53d7fddafcc91f322300b512a65dfc4d5ab7b5622636
|
4
|
+
data.tar.gz: 187d476c9e3418c03935d00ddf84fd9c9618cd8ac4a14a391e0046f68f92221f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8396f55b48c7cd1e82f66855eb950c2c1fd4e908423625a262e6ba11496d2cd3b9c90931c66deb97efba5d706502416e71ef4ae47f7d459b5c0fcf24d7b86ef4
|
7
|
+
data.tar.gz: '08c1d478a70c0a204805844aa0ad1724ee7a371852c3f7059596204d3c62f89d9044fa2cf44b26b6df2731331f371765b3cdf86de148ecf5cab88216336dea93'
|
@@ -51,9 +51,9 @@ def analyze_params(param_tokens) # rubocop:disable Metrics/AbcSize,Metrics/Cyclo
|
|
51
51
|
params = []
|
52
52
|
current_param = EMPTY_PARAM.dup
|
53
53
|
brackets = 0
|
54
|
-
param_tokens.reject { |token| %i[WHITESPACE NEWLINE].include? token.type }.each do |token|
|
55
|
-
brackets += 1 if token.type
|
56
|
-
brackets -= 1 if token.type
|
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
57
|
next unless brackets.zero?
|
58
58
|
|
59
59
|
current_param = analyze_param_token(token, current_param) unless token.type == :COMMA
|
@@ -62,7 +62,7 @@ def analyze_params(param_tokens) # rubocop:disable Metrics/AbcSize,Metrics/Cyclo
|
|
62
62
|
current_param = EMPTY_PARAM.dup
|
63
63
|
end
|
64
64
|
end
|
65
|
-
params.append(current_param) unless current_param ==
|
65
|
+
params.append(current_param) unless current_param[:name] == ''
|
66
66
|
params
|
67
67
|
end
|
68
68
|
|
@@ -135,9 +135,9 @@ PuppetLint.new_check(:param_comment) do # rubocop:disable Metrics/BlockLength
|
|
135
135
|
def check_comment_order(params)
|
136
136
|
param_comments = @comment_engine.params
|
137
137
|
param_comments.each_with_index do |param, index|
|
138
|
-
return
|
138
|
+
return param[:line] unless param[:name] == params[index][:name]
|
139
139
|
end
|
140
|
-
|
140
|
+
-1
|
141
141
|
end
|
142
142
|
|
143
143
|
# Check class or defined type indexes
|
@@ -147,7 +147,9 @@ PuppetLint.new_check(:param_comment) do # rubocop:disable Metrics/BlockLength
|
|
147
147
|
params = analyze_params(index[:param_tokens])
|
148
148
|
return false unless check_comments(comments)
|
149
149
|
return false unless check_parameters_count(params)
|
150
|
-
|
150
|
+
|
151
|
+
comment_order_line = check_comment_order(params)
|
152
|
+
return warn('Parameters sorted wrong', comment_order_line) unless comment_order_line == -1
|
151
153
|
end
|
152
154
|
true
|
153
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
|
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 "
|
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
|
|
@@ -77,6 +77,50 @@ describe 'param_comment' do
|
|
77
77
|
end
|
78
78
|
end
|
79
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
|
+
|
80
124
|
context 'code with missing parameter comment' do
|
81
125
|
let(:code) do
|
82
126
|
<<~CODE
|
@@ -130,15 +174,14 @@ describe 'param_comment' do
|
|
130
174
|
context 'code with wrongly sorted parameter comments' do
|
131
175
|
let(:code) do
|
132
176
|
<<~CODE
|
133
|
-
# @param withdefault
|
134
|
-
# A parameter with a default value
|
135
|
-
#
|
136
177
|
# @param mandatory
|
137
178
|
# A mandatory parameter
|
138
179
|
#
|
139
180
|
# @param optional
|
140
181
|
# An optional parameter
|
141
|
-
|
182
|
+
#
|
183
|
+
# @param withdefault
|
184
|
+
# A parameter with a default value
|
142
185
|
class my_class (
|
143
186
|
String $mandatory,
|
144
187
|
Boolean $withdefault = false,
|
@@ -152,7 +195,7 @@ describe 'param_comment' do
|
|
152
195
|
end
|
153
196
|
|
154
197
|
it 'should create a warning' do
|
155
|
-
expect(problems).to contain_warning('Parameters sorted wrong').on_line(
|
198
|
+
expect(problems).to contain_warning('Parameters sorted wrong').on_line(4).in_column(1)
|
156
199
|
end
|
157
200
|
end
|
158
201
|
|
@@ -180,7 +223,7 @@ describe 'param_comment' do
|
|
180
223
|
end
|
181
224
|
|
182
225
|
it 'should create a warning' do
|
183
|
-
expect(problems).to contain_warning('
|
226
|
+
expect(problems).to contain_warning('Can not process the comment \'@param withdefault\' in the state awaiting_separator')
|
184
227
|
.on_line(3)
|
185
228
|
.in_column(1)
|
186
229
|
end
|
@@ -272,7 +315,7 @@ describe 'param_comment' do
|
|
272
315
|
|
273
316
|
it 'should create a warning' do
|
274
317
|
expect(problems).to contain_warning(
|
275
|
-
'
|
318
|
+
'Can not process the comment \'@option mandatory [Boolean] :some_option\' in the state awaiting_header'
|
276
319
|
)
|
277
320
|
.on_line(4)
|
278
321
|
.in_column(1)
|
@@ -326,10 +369,28 @@ describe 'param_comment' do
|
|
326
369
|
|
327
370
|
it 'should create a warning' do
|
328
371
|
expect(problems).to contain_warning(
|
329
|
-
'
|
372
|
+
'Can not process the comment \'@param second\' in the state awaiting_separator'
|
330
373
|
)
|
331
374
|
.on_line(5)
|
332
375
|
.in_column(1)
|
333
376
|
end
|
334
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
|
335
396
|
end
|