From 0ac9f3e60248d6c66eae8496de54ab00f8d0d6e7 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Wed, 2 Oct 2019 13:39:12 -0700 Subject: [PATCH] Fix autocorrection of multiple platform helpers in a recipe This is a major facepalm here and looking back it makes total sense. All the on_* hooks run and then all the autocorrects run. That means when I set @type and @plat they'd get used by all the autocorrectors and every node['platform' ] == 'foo' would be autocorrected to the last platform found in the recipe. Signed-off-by: Tim Smith --- lib/rubocop/cop/chef/style/use_platform_helpers.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/rubocop/cop/chef/style/use_platform_helpers.rb b/lib/rubocop/cop/chef/style/use_platform_helpers.rb index 062c062e3..c295498b9 100644 --- a/lib/rubocop/cop/chef/style/use_platform_helpers.rb +++ b/lib/rubocop/cop/chef/style/use_platform_helpers.rb @@ -38,18 +38,16 @@ class UsePlatformHelpers < Cop PATTERN def on_send(node) - platform_check?(node) do |type, plat| - # set these so we can use them in the auto_correct method - @type = type - @plat = plat - + platform_check?(node) do add_offense(node, location: :expression, message: MSG, severity: :refactor) end end def autocorrect(node) lambda do |corrector| - corrector.replace(node.loc.expression, "#{@type.value}?('#{@plat.value}')") + platform_check?(node) do |type, plat| + corrector.replace(node.loc.expression, "#{type.value}?('#{plat.value}')") + end end end end