-
-
Notifications
You must be signed in to change notification settings - Fork 264
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
136 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,63 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop identifies places where `render text:` can be | ||
# replaced with `render plain:`. | ||
# | ||
# @example | ||
# # bad - sets MIME type to `text/html` | ||
# render text: 'Ruby!' | ||
# | ||
# # bad - requires explicit MIME type declaration | ||
# render text: 'Ruby!', content_type: 'text/plain' | ||
# | ||
# # good - short and precise | ||
# render plain: 'Ruby!' | ||
# | ||
class RenderText < Cop | ||
MSG = 'Prefer `render plain:` over `render text:`.' | ||
|
||
def_node_matcher :render_text_call?, <<~PATTERN | ||
(send nil? :render $(hash <$(pair (sym :text) $_) ...>)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
add_offense(node) if render_text_call?(node) | ||
end | ||
|
||
def autocorrect(node) | ||
render_text_call?(node) do |options_node, option_node, option_value| | ||
content_type_node = find_content_type(options_node) | ||
|
||
if content_type_node && content_type_node.value.value == 'text/plain' | ||
rest_options = options_node.pairs - [option_node, content_type_node] | ||
|
||
lambda do |corrector| | ||
corrector.replace( | ||
node, | ||
replacement(rest_options, option_value) | ||
) | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def find_content_type(node) | ||
node.pairs.find { |p| p.key.value.to_sym == :content_type } | ||
end | ||
|
||
def replacement(rest_options, option_value) | ||
if rest_options.any? | ||
"render plain: #{option_value.source}, #{rest_options.map(&:source).join(', ')}" | ||
else | ||
"render plain: #{option_value.source}" | ||
end | ||
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,31 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::RenderText do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense when using `render text:`' do | ||
expect_offense(<<~RUBY) | ||
render text: 'Ruby!' | ||
^^^^^^^^^^^^^^^^^^^^ Prefer `render plain:` over `render text:`. | ||
RUBY | ||
|
||
expect_no_corrections | ||
end | ||
|
||
it 'registers an offense and corrects when using `render text:` with `content_type: "text/plain"`' do | ||
expect_offense(<<~RUBY) | ||
render text: 'Ruby!', content_type: 'text/plain' | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `render plain:` over `render text:`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
render plain: 'Ruby!' | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `render plain:`' do | ||
expect_no_offenses(<<~RUBY) | ||
render plain: 'Ruby!' | ||
RUBY | ||
end | ||
end |