Skip to content

Commit 1a2c4b7

Browse files
committed
(PUP-11981) WIP
1 parent 775592c commit 1a2c4b7

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

Diff for: lib/puppet/pops/evaluator/literal_evaluator.rb

+12-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ module Evaluator
1111
# QualifiedName
1212
# Default (produced :default)
1313
# Regular Expression (produces ruby regular expression)
14-
#
15-
# Not considered literal
16-
# QualifiedReference # i.e. File, FooBar
14+
# QualifiedReference
15+
# AccessExpresion
1716
#
1817
class LiteralEvaluator
1918

@@ -40,6 +39,7 @@ def literal_Program(o)
4039
end
4140

4241
def literal_LiteralString(o)
42+
# require 'byebug'; byebug if $DEBUG
4343
o.value
4444
end
4545

@@ -67,6 +67,15 @@ def literal_LiteralRegularExpression(o)
6767
o.value
6868
end
6969

70+
def literal_QualifiedReference(o)
71+
o.value
72+
end
73+
74+
def literal_AccessExpression(o)
75+
throw :not_literal if o.keys.size == 1 && o.keys[0].is_a?(Model::LiteralList)
76+
o.keys.map {|v| literal(v) }
77+
end
78+
7079
def literal_ConcatenatedString(o)
7180
# use double quoted string value if there is no interpolation
7281
throw :not_literal unless o.segments.size == 1 && o.segments[0].is_a?(Model::LiteralString)

Diff for: lib/puppet/pops/issues.rb

+4
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ def self.hard_issue(issue_code, *args, &block)
207207
_("The numeric parameter name '$%{name}' cannot be used (clashes with numeric match result variables)") % { name: name }
208208
end
209209

210+
ILLEGAL_NONLITERAL_PARAMETER_TYPE = issue :ILLEGAL_NONLITERAL_PARAMETER_TYPE, :name, :type_class do
211+
_("The parameter '$%{name}' must be a literal type, not %{type_class}") % { name: name, type_class: label.a_an(type_class) }
212+
end
213+
210214
# In certain versions of Puppet it may be allowed to assign to a not already assigned key
211215
# in an array or a hash. This is an optional validation that may be turned on to prevent accidental
212216
# mutation.

Diff for: lib/puppet/pops/validation/checker4_0.rb

+12
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ def check_HostClassDefinition(o)
471471
internal_check_parameter_name_uniqueness(o)
472472
internal_check_reserved_params(o)
473473
internal_check_no_idem_last(o)
474+
internal_check_parameter_type_literal(o)
474475
end
475476

476477
def check_ResourceTypeDefinition(o)
@@ -481,6 +482,17 @@ def check_ResourceTypeDefinition(o)
481482
internal_check_no_idem_last(o)
482483
end
483484

485+
def internal_check_parameter_type_literal(o)
486+
type = nil
487+
o.parameters.each do |p|
488+
next unless p.type_expr
489+
catch :not_literal do
490+
type = literal(p.type_expr)
491+
end
492+
acceptor.accept(Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE, p, {name: p.name, type_class: p.type_expr.class}) if type.nil?
493+
end
494+
end
495+
484496
def internal_check_return_type(o)
485497
r = o.return_type
486498
internal_check_type_ref(o, r) unless r.nil?

Diff for: spec/unit/info_service_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class Borked($Herp+$Derp) {}
313313
CODE
314314
'json_unsafe.pp' => <<-CODE,
315315
class json_unsafe($arg1 = /.*/, $arg2 = default, $arg3 = {1 => 1}) {}
316-
CODE
316+
CODE
317317
})
318318
end
319319

Diff for: spec/unit/pops/evaluator/literal_evaluator_spec.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
'"a"' => 'a',
1717
'a' => 'a',
1818
'a::b' => 'a::b',
19+
'Integer[1]' => [1],
20+
'File' => "file",
1921

2022
# special values
2123
'default' => :default,
@@ -35,9 +37,9 @@
3537
expect(leval.literal(parser.parse_string('undef'))).to be_nil
3638
end
3739

38-
['1+1', 'File', '[1,2, 1+2]', '{a=>1+1}', 'Integer[1]', '"x$y"', '"x${y}z"'].each do |source|
40+
['1+1', '[1,2, 1+2]', '{a=>1+1}', '"x$y"', '"x${y}z"', 'Integer[1-3]', 'Optional[[String]]'].each do |source|
3941
it "throws :not_literal for non literal expression '#{source}'" do
40-
expect{leval.literal(parser.parse_string('1+1'))}.to throw_symbol(:not_literal)
42+
expect{leval.literal(parser.parse_string(source))}.to throw_symbol(:not_literal)
4143
end
4244
end
4345
end

0 commit comments

Comments
 (0)