Skip to content

Commit

Permalink
Merge pull request #141 from github/hash_splats
Browse files Browse the repository at this point in the history
Improve check for hash literals
  • Loading branch information
bensheldon authored Jul 19, 2023
2 parents 1f06af9 + 3aa9fa8 commit 5affeba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
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

0 comments on commit 5affeba

Please sign in to comment.