Skip to content

Commit f684966

Browse files
committed
Accept UnaryMinusExpression as class parameter type
Previously, class parameters of the form `Integer[-1] $param` would fail compilation, because the value `-1` was lexed as a UnaryMinusExpression containing a LiteralInteger. And since the LiteralEvaluator didn't implement the `literal_UnaryMinusExpression` method, the visitor called `literal_XXX` for each ancestor class, until reaching `literal_Object`, which always raises. This adds the `literal_UnaryMinusExpression` method and returns -1 times the expression it wraps. This is similar to how the TypeParser interprets UnaryMinusExpressions[1]. [1] https://github.com/puppetlabs/puppet/blob/8.5.0/lib/puppet/pops/types/type_parser.rb#L161
1 parent 57a00cc commit f684966

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

lib/puppet/pops/evaluator/literal_evaluator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ def literal_QualifiedReference(o)
7070
o.value
7171
end
7272

73+
def literal_UnaryMinusExpression(o)
74+
-literal(o.expr)
75+
end
76+
7377
def literal_AccessExpression(o)
7478
# to prevent parameters with [[]] like Optional[[String]]
7579
throw :not_literal if o.keys.size == 1 && o.keys[0].is_a?(Model::LiteralList)

spec/unit/pops/evaluator/literal_evaluator_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'a' => 'a',
1818
'a::b' => 'a::b',
1919
'Integer[1]' => [1],
20+
'Integer[-1]' => [-1],
2021
'File' => "file",
2122

2223
# special values
@@ -37,7 +38,7 @@
3738
expect(leval.literal(parser.parse_string('undef'))).to be_nil
3839
end
3940

40-
['1+1', '[1,2, 1+2]', '{a=>1+1}', '"x$y"', '"x${y}z"', 'Integer[1-3]', 'Optional[[String]]'].each do |source|
41+
['1+1', '[1,2, 1+2]', '{a=>1+1}', '"x$y"', '"x${y}z"', 'Integer[1-3]', 'Intger[-1-3]', 'Optional[[String]]'].each do |source|
4142
it "throws :not_literal for non literal expression '#{source}'" do
4243
expect{leval.literal(parser.parse_string(source))}.to throw_symbol(:not_literal)
4344
end

0 commit comments

Comments
 (0)