Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow RailsViewRenderShorthand errors to be autocorrected #114

Merged
merged 1 commit into from
Oct 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/rubocop/cop/github/rails_view_render_shorthand.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down