Skip to content

Commit 5affeba

Browse files
authored
Merge pull request #141 from github/hash_splats
Improve check for hash literals
2 parents 1f06af9 + 3aa9fa8 commit 5affeba

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

lib/rubocop/cop/github/render_literal_helpers.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ module RenderLiteralHelpers
4141
PATTERN
4242

4343
def hash_with_literal_keys?(hash)
44-
hash.pairs.all? { |pair| literal?(pair.key) }
44+
hash.children.all? { |child| child.pair_type? } &&
45+
hash.pairs.all? { |pair| literal?(pair.key) }
4546
end
4647

4748
def render_view_component?(node)

test/test_rails_controller_render_literal.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,17 @@ def index
442442
assert_equal 1, offenses.count
443443
end
444444

445+
def test_render_literal_splat_locals_offense
446+
offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb"
447+
class ProductsController < ActionController::Base
448+
def index
449+
render "products/product", locals: { **locals }
450+
end
451+
end
452+
RUBY
453+
454+
assert_equal 1, offenses.count
455+
end
445456

446457
def test_render_literal_dynamic_local_key_offense
447458
offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb"

test/test_rails_view_render_literal.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ def test_render_literal_dynamic_local_key_offense
145145
assert_equal 1, offenses.count
146146
end
147147

148+
def test_render_literal_splat_locals_offense
149+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
150+
<%= render "products/product", { **locals } %>
151+
ERB
152+
153+
assert_equal 1, offenses.count
154+
end
155+
148156
def test_render_options_static_locals_no_offense
149157
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
150158
<%= render partial: "products/product", locals: { product: product } %>
@@ -168,4 +176,12 @@ def test_render_options_dynamic_local_key_offense
168176

169177
assert_equal 1, offenses.count
170178
end
179+
180+
def test_render_options_local_splat_offense
181+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
182+
<%= render partial: "products/product", locals: { **locals } %>
183+
ERB
184+
185+
assert_equal 1, offenses.count
186+
end
171187
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy