Skip to content

Improve check for hash literals #141

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

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion lib/rubocop/cop/github/render_literal_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ module RenderLiteralHelpers
PATTERN

def hash_with_literal_keys?(hash)
hash.pairs.all? { |pair| literal?(pair.key) }
hash.children.all? { |child| child.pair_type? } &&
hash.pairs.all? { |pair| literal?(pair.key) }
end

def render_view_component?(node)
Expand Down
11 changes: 11 additions & 0 deletions test/test_rails_controller_render_literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,17 @@ def index
assert_equal 1, offenses.count
end

def test_render_literal_splat_locals_offense
offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb"
class ProductsController < ActionController::Base
def index
render "products/product", locals: { **locals }
end
end
RUBY

assert_equal 1, offenses.count
end

def test_render_literal_dynamic_local_key_offense
offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb"
Expand Down
16 changes: 16 additions & 0 deletions test/test_rails_view_render_literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ def test_render_literal_dynamic_local_key_offense
assert_equal 1, offenses.count
end

def test_render_literal_splat_locals_offense
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
<%= render "products/product", { **locals } %>
ERB

assert_equal 1, offenses.count
end

def test_render_options_static_locals_no_offense
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
<%= render partial: "products/product", locals: { product: product } %>
Expand All @@ -168,4 +176,12 @@ def test_render_options_dynamic_local_key_offense

assert_equal 1, offenses.count
end

def test_render_options_local_splat_offense
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
<%= render partial: "products/product", locals: { **locals } %>
ERB

assert_equal 1, offenses.count
end
end