Skip to content

Commit 52becf3

Browse files
committed
CONT-234 Refactor nested loops
The check iterates over an array of exec commands and then over each token in each command. Prior to this commit, these loops were nested. The goal of this commit is to refactor this to clean up the code a bit. This was achieved by extracting the logic which iterates over the tokens in a command into a function called check_unsafe_interpolations. Separating code logic into functions and removing nested loops improves code readability and should make it easier to build upon in the future.
1 parent 652577f commit 52becf3

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

lib/puppet-lint/plugins/check_unsafe_interpolations.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ def check
1919

2020
# Iterate over each command found in any exec
2121
exec_resources.each do |command_resources|
22-
# Iterate over each command in execs and check for unsafe interpolations
23-
command_resources[:tokens].each do |token|
24-
# We are only interested in tokens from command onwards
25-
next unless token.type == :NAME
26-
# Don't check the command if it is parameterised
27-
next if parameterised?(token)
22+
check_unsafe_interpolations(command_resources)
23+
end
24+
end
2825

29-
check_command(token).each do |t|
30-
notify_warning(t)
31-
end
26+
# Iterates over tokens in a command and raises a warning if an interpolated variable is found
27+
def check_unsafe_interpolations(command_resources)
28+
command_resources[:tokens].each do |token|
29+
# We are only interested in tokens from command onwards
30+
next unless token.type == :NAME
31+
# Don't check the command if it is parameterised
32+
next if parameterised?(token)
33+
34+
check_command(token).each do |t|
35+
notify_warning(t)
3236
end
3337
end
3438
end

0 commit comments

Comments
 (0)