From af77d72f2ee4aac1b610ee2a551b7e98c7b4c329 Mon Sep 17 00:00:00 2001 From: Mike Griffin Date: Thu, 22 Sep 2022 15:21:53 +0100 Subject: [PATCH] Add autocorrect to RailsViewRenderShorthand The text is already provided in the error message, this reuses it to allow the problem to be auto corrected --- lib/rubocop/cop/github/rails_view_render_shorthand.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/rubocop/cop/github/rails_view_render_shorthand.rb b/lib/rubocop/cop/github/rails_view_render_shorthand.rb index e465d8d3..05952425 100644 --- a/lib/rubocop/cop/github/rails_view_render_shorthand.rb +++ b/lib/rubocop/cop/github/rails_view_render_shorthand.rb @@ -6,6 +6,8 @@ module RuboCop module Cop module GitHub class RailsViewRenderShorthand < Base + extend AutoCorrector + MSG = "Prefer `render` partial shorthand" def_node_matcher :render_with_options?, <<-PATTERN @@ -26,9 +28,13 @@ def on_send(node) locals_key = option_pairs.map { |pair| locals_key?(pair) }.compact.first if option_pairs.length == 1 && partial_key - add_offense(node, message: "Use `render #{partial_key.source}` instead") + add_offense(node, message: "Use `render #{partial_key.source}` instead") do |corrector| + corrector.replace(node.source_range, "render #{partial_key.source}") + end elsif option_pairs.length == 2 && partial_key && locals_key - add_offense(node, message: "Use `render #{partial_key.source}, #{locals_key.source}` instead") + add_offense(node, message: "Use `render #{partial_key.source}, #{locals_key.source}` instead") do |corrector| + corrector.replace(node.source_range, "render #{partial_key.source}, #{locals_key.source}") + end end end end