Skip to content
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

Add new Rails/RenderInline cop #271

Merged
merged 1 commit into from
Jun 29, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### New features

* [#271](https://github.com/rubocop-hq/rubocop-rails/pull/271): Add new `Rails/RenderInline` cop. ([@fatkodima][])
* [#281](https://github.com/rubocop-hq/rubocop-rails/pull/281): Add new `Rails/MailerName` cop. ([@fatkodima][])
* [#246](https://github.com/rubocop-hq/rubocop-rails/issues/246): Add new `Rails/PluckInWhere` cop. ([@fatkodima][])
* [#17](https://github.com/rubocop-hq/rubocop-rails/issues/17): Add new `Rails/NegateInclude` cop. ([@fatkodima][])
Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ Rails/RelativeDateConstant:
VersionChanged: '0.59'
AutoCorrect: false

Rails/RenderInline:
Description: 'Prefer using a template over inline rendering.'
StyleGuide: 'https://rails.rubystyle.guide/#inline-rendering'
Enabled: 'pending'
VersionAdded: '2.7'

Rails/RequestReferer:
Description: 'Use consistent syntax for request.referer.'
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* xref:cops_rails.adoc#railsreflectionclassname[Rails/ReflectionClassName]
* xref:cops_rails.adoc#railsrefutemethods[Rails/RefuteMethods]
* xref:cops_rails.adoc#railsrelativedateconstant[Rails/RelativeDateConstant]
* xref:cops_rails.adoc#railsrenderinline[Rails/RenderInline]
* xref:cops_rails.adoc#railsrequestreferer[Rails/RequestReferer]
* xref:cops_rails.adoc#railsreversiblemigration[Rails/ReversibleMigration]
* xref:cops_rails.adoc#railssafenavigation[Rails/SafeNavigation]
Expand Down
41 changes: 41 additions & 0 deletions docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2779,6 +2779,47 @@ end
| Boolean
|===

== Rails/RenderInline

|===
| Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged

| Pending
| Yes
| No
| 2.7
| -
|===

This cop looks for inline rendering within controller actions.

=== Examples

[source,ruby]
----
# 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
----

=== References

* https://rails.rubystyle.guide/#inline-rendering

== Rails/RequestReferer

|===
Expand Down
48 changes: 48 additions & 0 deletions lib/rubocop/cop/rails/render_inline.rb
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| %>
fatkodima marked this conversation as resolved.
Show resolved Hide resolved
# # <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
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
require_relative 'rails/reflection_class_name'
require_relative 'rails/refute_methods'
require_relative 'rails/relative_date_constant'
require_relative 'rails/render_inline'
require_relative 'rails/request_referer'
require_relative 'rails/reversible_migration'
require_relative 'rails/safe_navigation'
Expand Down
25 changes: 25 additions & 0 deletions spec/rubocop/cop/rails/render_inline_spec.rb
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
fatkodima marked this conversation as resolved.
Show resolved Hide resolved

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