-
-
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
209 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,76 @@ | ||
# 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 - explicit MIME type to `text/plain` | ||
# render text: 'Ruby!', content_type: 'text/plain' | ||
# | ||
# # good - short and precise | ||
# render plain: 'Ruby!' | ||
# | ||
# # good - explicit MIME type not to `text/plain` | ||
# render text: 'Ruby!', content_type: 'text/html' | ||
# | ||
# @example ContentTypeCompatibility: true (default) | ||
# # good - sets MIME type to `text/html` | ||
# render text: 'Ruby!' | ||
# | ||
# @example ContentTypeCompatibility: false | ||
# # bad - sets MIME type to `text/html` | ||
# render text: 'Ruby!' | ||
# | ||
class RenderPlainText < Cop | ||
MSG = 'Prefer `render plain:` over `render text:`.' | ||
|
||
def_node_matcher :render_plain_text?, <<~PATTERN | ||
(send nil? :render $(hash <$(pair (sym :text) $_) ...>)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
render_plain_text?(node) do |options_node, _option_node, _option_value| | ||
content_type_node = find_content_type(options_node) | ||
add_offense(node) if compatible_content_type?(content_type_node) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
render_plain_text?(node) do |options_node, option_node, option_value| | ||
content_type_node = find_content_type(options_node) | ||
rest_options = options_node.pairs - [option_node, content_type_node].compact | ||
|
||
lambda do |corrector| | ||
corrector.replace( | ||
node, | ||
replacement(rest_options, option_value) | ||
) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def find_content_type(node) | ||
node.pairs.find { |p| p.key.value.to_sym == :content_type } | ||
end | ||
|
||
def compatible_content_type?(node) | ||
(node && node.value.value == 'text/plain') || | ||
(!node && !cop_config['ContentTypeCompatibility']) | ||
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,63 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::RenderPlainText, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
shared_examples 'checks_common_offense' do | ||
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 text:` with `content_type: "text/html"`' do | ||
expect_no_offenses(<<~RUBY) | ||
render text: 'Ruby!', content_type: 'text/html' | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `render plain:`' do | ||
expect_no_offenses(<<~RUBY) | ||
render plain: 'Ruby!' | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when ContentTypeCompatibility set to true' do | ||
let(:cop_config) do | ||
{ 'ContentTypeCompatibility' => true } | ||
end | ||
|
||
it 'does not register an offense when using `render text:`' do | ||
expect_no_offenses(<<~RUBY) | ||
render text: 'Ruby!' | ||
RUBY | ||
end | ||
|
||
it_behaves_like('checks_common_offense') | ||
end | ||
|
||
context 'when ContentTypeCompatibility set to false' do | ||
let(:cop_config) do | ||
{ 'ContentTypeCompatibility' => false } | ||
end | ||
|
||
it 'registers an offense and corrects when using `render text:`' do | ||
expect_offense(<<~RUBY) | ||
render text: 'Ruby!' | ||
^^^^^^^^^^^^^^^^^^^^ Prefer `render plain:` over `render text:`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
render plain: 'Ruby!' | ||
RUBY | ||
end | ||
|
||
it_behaves_like('checks_common_offense') | ||
end | ||
end |