Skip to content

Commit 5a5e50f

Browse files
authored
Merge pull request #75 from github/render_to_string
Lint :render_to_string the same as :render
2 parents e734c4a + d19eda5 commit 5a5e50f

10 files changed

+35
-22
lines changed

lib/rubocop/cop/github/rails_controller_render_action_symbol.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ class RailsControllerRenderActionSymbol < Cop
99
MSG = "Prefer `render` with string instead of symbol"
1010

1111
def_node_matcher :render_sym?, <<-PATTERN
12-
(send nil? :render $(sym _))
12+
(send nil? {:render :render_to_string} $(sym _))
1313
PATTERN
1414

1515
def_node_matcher :render_with_options?, <<-PATTERN
16-
(send nil? :render (hash $...))
16+
(send nil? {:render :render_to_string} (hash $...))
1717
PATTERN
1818

1919
def_node_matcher :action_key?, <<-PATTERN

lib/rubocop/cop/github/rails_controller_render_literal.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ class RailsControllerRenderLiteral < Cop
1313
PATTERN
1414

1515
def_node_matcher :render?, <<-PATTERN
16-
(send nil? :render ...)
16+
(send nil? {:render :render_to_string} ...)
1717
PATTERN
1818

1919
def_node_matcher :render_literal?, <<-PATTERN
20-
(send nil? :render ({str sym} $_) $...)
20+
(send nil? {:render :render_to_string} ({str sym} $_) $...)
2121
PATTERN
2222

2323
def_node_matcher :render_const?, <<-PATTERN
24-
(send nil? :render (const _ _) ...)
24+
(send nil? {:render :render_to_string} (const _ _) ...)
2525
PATTERN
2626

2727
def_node_matcher :render_inst?, <<-PATTERN
28-
(send nil? :render (send _ :new ...) ...)
28+
(send nil? {:render :render_to_string} (send _ :new ...) ...)
2929
PATTERN
3030

3131
def_node_matcher :render_with_options?, <<-PATTERN
32-
(send nil? :render (hash $...))
32+
(send nil? {:render :render_to_string} (hash $...))
3333
PATTERN
3434

3535
def_node_matcher :ignore_key?, <<-PATTERN

lib/rubocop/cop/github/rails_controller_render_paths_exist.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ module Cop
77
module GitHub
88
class RailsControllerRenderPathsExist < Cop
99
def_node_matcher :render?, <<-PATTERN
10-
(send nil? :render $...)
10+
(send nil? {:render :render_to_string} $...)
1111
PATTERN
1212

1313
def_node_matcher :render_str?, <<-PATTERN
14-
(send nil? :render $({str sym} $_) ...)
14+
(send nil? {:render :render_to_string} $({str sym} $_) ...)
1515
PATTERN
1616

1717
def_node_matcher :render_options?, <<-PATTERN
18-
(send nil? :render (hash $...))
18+
(send nil? {:render :render_to_string} (hash $...))
1919
PATTERN
2020

2121
def_node_matcher :render_key?, <<-PATTERN

lib/rubocop/cop/github/rails_controller_render_shorthand.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class RailsControllerRenderShorthand < Cop
99
MSG = "Prefer `render` template shorthand"
1010

1111
def_node_matcher :render_with_options?, <<-PATTERN
12-
(send nil? :render (hash $...))
12+
(send nil? {:render :render_to_string} (hash $...))
1313
PATTERN
1414

1515
def_node_matcher :action_key?, <<-PATTERN

lib/rubocop/cop/github/rails_render_inline.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class RailsRenderInline < Cop
99
MSG = "Avoid `render inline:`"
1010

1111
def_node_matcher :render_with_options?, <<-PATTERN
12-
(send nil? :render (hash $...))
12+
(send nil? {:render :render_to_string} (hash $...))
1313
PATTERN
1414

1515
def_node_matcher :inline_key?, <<-PATTERN

lib/rubocop/cop/github/rails_render_object_collection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class RailsRenderObjectCollection < Cop
99
MSG = "Avoid `render object:`"
1010

1111
def_node_matcher :render_with_options?, <<-PATTERN
12-
(send nil? :render (hash $...) ...)
12+
(send nil? {:render :render_to_string} (hash $...) ...)
1313
PATTERN
1414

1515
def_node_matcher :partial_key?, <<-PATTERN

lib/rubocop/cop/github/rails_view_render_literal.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ class RailsViewRenderLiteral < Cop
1313
PATTERN
1414

1515
def_node_matcher :render?, <<-PATTERN
16-
(send nil? :render $...)
16+
(send nil? {:render :render_to_string} $...)
1717
PATTERN
1818

1919
def_node_matcher :render_literal?, <<-PATTERN
20-
(send nil? :render ({str sym} $_) $...)
20+
(send nil? {:render :render_to_string} ({str sym} $_) $...)
2121
PATTERN
2222

2323
def_node_matcher :render_inst?, <<-PATTERN
24-
(send nil? :render (send _ :new ...) ...)
24+
(send nil? {:render :render_to_string} (send _ :new ...) ...)
2525
PATTERN
2626

2727
def_node_matcher :render_collection?, <<-PATTERN
28-
(send nil? :render (send _ :with_collection ...) ...)
28+
(send nil? {:render :render_to_string} (send _ :with_collection ...) ...)
2929
PATTERN
3030

3131
def_node_matcher :render_with_options?, <<-PATTERN
32-
(send nil? :render (hash $...) ...)
32+
(send nil? {:render :render_to_string} (hash $...) ...)
3333
PATTERN
3434

3535
def_node_matcher :ignore_key?, <<-PATTERN

lib/rubocop/cop/github/rails_view_render_paths_exist.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ module Cop
77
module GitHub
88
class RailsViewRenderPathsExist < Cop
99
def_node_matcher :render?, <<-PATTERN
10-
(send nil? :render $...)
10+
(send nil? {:render :render_to_string} $...)
1111
PATTERN
1212

1313
def_node_matcher :render_str?, <<-PATTERN
14-
(send nil? :render $(str $_) ...)
14+
(send nil? {:render :render_to_string} $(str $_) ...)
1515
PATTERN
1616

1717
def_node_matcher :render_options?, <<-PATTERN
18-
(send nil? :render (hash $...))
18+
(send nil? {:render :render_to_string} (hash $...))
1919
PATTERN
2020

2121
def_node_matcher :partial_key?, <<-PATTERN

lib/rubocop/cop/github/rails_view_render_shorthand.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class RailsViewRenderShorthand < Cop
99
MSG = "Prefer `render` partial shorthand"
1010

1111
def_node_matcher :render_with_options?, <<-PATTERN
12-
(send nil? :render (hash $...))
12+
(send nil? {:render :render_to_string} (hash $...))
1313
PATTERN
1414

1515
def_node_matcher :partial_key?, <<-PATTERN

test/test_rails_controller_render_literal.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,19 @@ def index
316316
assert_equal "render must be used with a string literal or an instance of a Class", cop.offenses[0].message
317317
end
318318

319+
def test_render_to_string_variable_offense
320+
investigate cop, <<-RUBY, "app/controllers/products_controller.rb"
321+
class ProductsController < ActionController::Base
322+
def index
323+
render_to_string(magic_string)
324+
end
325+
end
326+
RUBY
327+
328+
assert_equal 1, cop.offenses.count
329+
assert_equal "render must be used with a string literal or an instance of a Class", cop.offenses[0].message
330+
end
331+
319332
def test_render_action_variable_offense
320333
investigate cop, <<-RUBY, "app/controllers/products_controller.rb"
321334
class ProductsController < ActionController::Base

0 commit comments

Comments
 (0)