-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
b6fbbcf
commit c8f4ebf
Showing
8 changed files
with
112 additions
and
1 deletion.
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
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,29 @@ | ||
module Datarockets | ||
module Style | ||
module Cop | ||
module Style | ||
# This cop checks nested interpolations | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# "Hello, #{user.blank? ? 'guest' : "dear #{user.name}"}" | ||
# | ||
# # good | ||
# user_name = user.blank? ? 'guest' : "dear #{user.name}" | ||
# "Hello, #{user_name}" | ||
class NestedInterpolation < RuboCop::Cop::Cop | ||
include RuboCop::Cop::Interpolation | ||
|
||
MSG = "Redundant nested interpolation.".freeze | ||
|
||
def on_interpolation(node) | ||
node.each_descendant(:begin) do |descendant_node| | ||
add_offense(descendant_node) | ||
end | ||
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
39 changes: 39 additions & 0 deletions
39
spec/datarockets/style/cop/style/nested_interpolation_spec.rb
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,39 @@ | ||
RSpec.describe Datarockets::Style::Cop::Style::NestedInterpolation do | ||
subject(:cop) { described_class.new } | ||
|
||
it "accepts single interpolation" do | ||
expect_no_offenses('"dear #{user.name}"') | ||
end | ||
|
||
it "accepts two single interpolation" do | ||
expect_no_offenses('"Hello #{user1.name} and #{user2.name}"') | ||
end | ||
|
||
it 'registers an offense for "Hello, #{user.blank? ? guest : "dear #{user.name}"}"' do | ||
expect_offense(<<~'RUBY') | ||
"Hello, #{user.blank? ? 'guest' : "dear #{user.name}"}" | ||
^^^^^^^^^^^^ Redundant nested interpolation. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for %|Hello, #{user.blank? ? guest : "dear #{user.name}"|' do | ||
expect_offense(<<~'RUBY') | ||
%|Hello, #{user.blank? ? 'guest' : "dear #{user.name}"}| | ||
^^^^^^^^^^^^ Redundant nested interpolation. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for %Q(Hello, #{user.blank? ? guest : "dear #{user.name}")' do | ||
expect_offense(<<~'RUBY') | ||
%Q(Hello, #{user.blank? ? 'guest' : "dear #{user.name}"}) | ||
^^^^^^^^^^^^ Redundant nested interpolation. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for ["Hello, #{user.blank? ? guest : "dear #{user.name}"}"]' do | ||
expect_offense(<<~'RUBY') | ||
["Hello, #{user.blank? ? 'guest' : "dear #{user.name}"}", 'foo'] | ||
^^^^^^^^^^^^ Redundant nested interpolation. | ||
RUBY | ||
end | ||
end |