-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop looks for inline rendering within controller actions. | ||
# | ||
# @example | ||
# # bad | ||
# class ProductsController < ApplicationController | ||
# def index | ||
# render inline: "<% products.each do |p| %><p><%= p.name %></p><% end %>", type: :erb | ||
# end | ||
# end | ||
# | ||
# # good | ||
# # app/views/products/index.html.erb | ||
# # <% products.each do |p| %> | ||
# # <p><%= p.name %></p> | ||
# # <% end %> | ||
# | ||
# class ProductsController < ApplicationController | ||
# def index | ||
# end | ||
# end | ||
# | ||
class RenderInline < Cop | ||
MSG = 'Prefer using a template over inline rendering.' | ||
|
||
def_node_matcher :render_with_options?, <<~PATTERN | ||
(send nil? :render $(hash ...)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
render_with_options?(node) do |options| | ||
add_offense(node) if includes_inline_key?(options) | ||
end | ||
end | ||
|
||
private | ||
|
||
def includes_inline_key?(node) | ||
node.keys.find { |key| key.value.to_sym == :inline } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::RenderInline do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense when rendering inline with a symbol key' do | ||
expect_offense(<<~RUBY) | ||
render status: 200, inline: 'inline template' | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using a template over inline rendering. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when rendering inline with a string key' do | ||
expect_offense(<<~RUBY) | ||
render status: 200, 'inline' => 'inline template' | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using a template over inline rendering. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when rendering a template' do | ||
expect_no_offenses(<<~RUBY) | ||
render :index | ||
RUBY | ||
end | ||
end |