Skip to content

Commit bde18e7

Browse files
committed
Fix #122
1 parent cc275ca commit bde18e7

File tree

4 files changed

+75
-23
lines changed

4 files changed

+75
-23
lines changed

lib/puppet-lint/lexer/token.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Token
3030
# etc) in the manifest.
3131
attr_accessor :next_code_token
3232

33-
# Public: Gets/sets the previous code tokne (skips whitespace,
33+
# Public: Gets/sets the previous code token (skips whitespace,
3434
# comments, etc) in the manifest.
3535
attr_accessor :prev_code_token
3636

lib/puppet-lint/plugins/check_classes/parameter_order.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def check
3737
column: token.column,
3838
description: 'Test the manifest tokens for any parameterised classes or defined types that take ' \
3939
'parameters and record a warning if there are any optional parameters listed before required parameters.',
40-
help_uri: 'https://puppet.com/docs/puppet/latest/style_guide.html#display-order-of-parameters',
40+
help_uri: 'https://puppet.com/docs/puppet/latest/style_guide.html#params-display-order',
4141
)
4242
end
4343
end
@@ -48,17 +48,15 @@ def parameter?(token)
4848
return false unless token.prev_code_token
4949

5050
[
51-
:LPAREN, # First parameter, no type specification
52-
:COMMA, # Subsequent parameter, no type specification
53-
:TYPE, # Parameter with simple type specification
54-
:RBRACK, # Parameter with complex type specification
51+
:LPAREN, # First parameter, no type specification
52+
:COMMA, # Subsequent parameter, no type specification
53+
:TYPE, # Parameter with simple type specification
54+
:RBRACK, # Parameter with complex type specification
55+
:CLASSREF, # Parameter with custom type specification
5556
].include?(token.prev_code_token.type)
5657
end
5758

5859
def required_parameter?(token)
59-
data_type = token.prev_token_of(:TYPE, skip_blocks: true)
60-
return false if data_type && data_type.value == 'Optional'
61-
6260
return !(token.prev_code_token && token.prev_code_token.type == :EQUALS) if token.next_code_token.nil? || [:COMMA, :RPAREN].include?(token.next_code_token.type)
6361

6462
false

spec/fixtures/test/reports/code_climate.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
}
3434
},
3535
"fingerprint": "e34cf289e008446b633d1be7cf58120e",
36-
"content": "Test the manifest tokens for any parameterised classes or defined types that take parameters and record a warning if there are any optional parameters listed before required parameters. See [this page](https://puppet.com/docs/puppet/latest/style_guide.html#display-order-of-parameters) for more information about the `parameter_order` check."
36+
"content": "Test the manifest tokens for any parameterised classes or defined types that take parameters and record a warning if there are any optional parameters listed before required parameters. See [this page](https://puppet.com/docs/puppet/latest/style_guide.html#params-display-order) for more information about the `parameter_order` check."
3737
}
3838
]

spec/unit/puppet-lint/plugins/check_classes/parameter_order_spec.rb

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,6 @@
135135
it { expect(problems).to have(0).problem }
136136
end
137137

138-
context "#{type} parameter with Optional data type" do
139-
let(:code) do
140-
<<-END
141-
#{type} test(
142-
String $test = 'value',
143-
Optional[String] $optional,
144-
) { }
145-
END
146-
end
147-
148-
it { expect(problems).to have(0).problems }
149-
end
150-
151138
context "#{type} parameter with array operation" do
152139
let(:code) do
153140
<<-END
@@ -165,5 +152,72 @@
165152

166153
it { expect(problems).to have(0).problems }
167154
end
155+
156+
context "#{type} parameters of Optional type are just regular parameters" do
157+
let(:code) do
158+
<<-END
159+
#{type} test (
160+
String $param1 = '',
161+
Optional[Boolean] $param2,
162+
) { }
163+
END
164+
end
165+
166+
it { expect(problems).to have(1).problems }
167+
end
168+
169+
[['Custom::Type1', 'Custom::Type2'], ['Custom::Type1', 'String'], ['String', 'Custom::Type2']].each_with_index do |testtypes, i| # rubocop:disable Performance/CollectionLiteralInLoop
170+
context "#{type} parameters of custom type - no values - #{i}" do
171+
let(:code) do
172+
<<-END
173+
#{type} test (
174+
#{testtypes[0]} $param1,
175+
#{testtypes[0]} $param2,
176+
) { }
177+
END
178+
end
179+
180+
it { expect(problems).to have(0).problems }
181+
end
182+
183+
context "#{type} parameters of custom type - two values - #{i}" do
184+
let(:code) do
185+
<<-END
186+
#{type} test (
187+
#{testtypes[0]} $param1 = 'foo',
188+
#{testtypes[1]} $param2 = 'bar',
189+
) { }
190+
END
191+
end
192+
193+
it { expect(problems).to have(0).problems }
194+
end
195+
196+
context "#{type} parameters of custom type - one value, wrong order - #{i}" do
197+
let(:code) do
198+
<<-END
199+
#{type} test (
200+
#{testtypes[0]} $param1 = 'foo',
201+
#{testtypes[1]} $param2,
202+
) { }
203+
END
204+
end
205+
206+
it { expect(problems).to have(1).problems }
207+
end
208+
209+
context "#{type} parameters of custom type - one value, right order - #{i}" do
210+
let(:code) do
211+
<<-END
212+
#{type} test (
213+
#{testtypes[0]} $param1,
214+
#{testtypes[1]} $param2 = 'bar',
215+
) { }
216+
END
217+
end
218+
219+
it { expect(problems).to have(0).problems }
220+
end
221+
end
168222
end
169223
end

0 commit comments

Comments
 (0)