Skip to content

Commit

Permalink
Merge pull request #7 from ninech/fix/allow_function_definition
Browse files Browse the repository at this point in the history
fix: allow definition of functions in global scope
  • Loading branch information
shieldwed authored Oct 5, 2023
2 parents 1417981 + 516d93c commit 2da4ed7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/puppet-lint/plugins/check_global_definition.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module PuppetLintGlobalDefinionCheck
private

def check_for_global_token(type, value = nil)
global_tokens.each_with_index do |token, i|
def check_for_global_token(type)
global_tokens.each do |token|
next unless token.type == type
next unless value.nil? || token.value == value

message = value.nil? ? token.value : "#{token.value} #{token.next_code_token.value}"
message = yield(token)
next unless message

notify :error,
message: "definition #{message} in global space",
Expand Down Expand Up @@ -37,7 +37,9 @@ def secure_ranges

def check
check_for_global_resources
check_for_global_token(:NAME, "include")
check_for_global_token(:NAME) do |token|
"#{token.value} #{token.next_code_token.value}" if token.value == "include"
end
end

def check_for_global_resources
Expand All @@ -56,6 +58,8 @@ def check_for_global_resources
include PuppetLintGlobalDefinionCheck

def check
check_for_global_token(:FUNCTION_NAME)
check_for_global_token(:FUNCTION_NAME) do |token|
"#{token.value} #{token.next_code_token.value}" unless !token.prev_code_token.nil? && token.prev_code_token.type == :FUNCTION
end
end
end
22 changes: 22 additions & 0 deletions spec/puppet-lint/plugins/check_global_function_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,26 @@ class test {
expect(problems).to have(1).problems
end
end

context "module function definition" do
let(:code) do
<<-EOS
# @summary function to clean hash of undef and empty values
function nine_networkinterfaces::delete_empty_values(
Hash $hash,
) >> Hash {
$hash.filter |$key, $value| {
case $value {
Collection: { $value =~ NotUndef and !$value.empty }
default: { $value =~ NotUndef }
}
}
}
EOS
end

it "should not detect any problems" do
expect(problems).to have(0).problems
end
end
end

0 comments on commit 2da4ed7

Please sign in to comment.