Skip to content

Commit

Permalink
[#91] Add nested interpolation cop
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-dubrovsky committed Feb 5, 2020
1 parent b6fbbcf commit c8f4ebf
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ inherit_mode:

AllCops:
TargetRubyVersion: 2.3

# for checking cops with interpolation
Lint/InterpolationCheck:
Exclude:
- 'spec/**/*.rb'
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is described in [Contributing notes](CONTRIBUTING.md#changelog-entry-
### Added

* Enable `rubocop-rails` cops for rails config. ([@ula][])
* Add `Style/NestedInterpolation` cop. ([@r.dubrovsky][])

## 0.7.0 (2020-01-27)

Expand Down
4 changes: 3 additions & 1 deletion config/base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Layout/ArrayAlignmentExtended:
- with_fixed_indentation
IndentationWidth: ~


Layout/HashAlignment:
EnforcedLastArgumentHashStyle: always_ignore

Expand Down Expand Up @@ -64,5 +63,8 @@ Style/EmptyMethod:
Style/FrozenStringLiteralComment:
Enabled: false

Style/NestedInterpolation:
Enabled: true

Style/StringLiterals:
EnforcedStyle: double_quotes
13 changes: 13 additions & 0 deletions doc/STYLE_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ This is a small list of differences which we have when compared with community s
Adopt a consistent string literal quoting style.
<sup>[[link](#style-string-quotes)]</sup>

* <a name="style-nested-interpolation"></a>
Avoid using nested interpolation.
<sup>[[link](#style-nested-interpolation)]</sup>

```ruby
# bad
"Hello, #{user.blank? ? 'guest' : "dear #{user.name}"}"

# good
user_name = user.blank? ? 'guest' : "dear #{user.name}"
"Hello, #{user_name}"
```

* <a name="style-hash-aligning"></a>
If elements of a hash literal span more than one line, we're aligning them by keys.
Also, the first hash key is aligned by an indentation level.
Expand Down
1 change: 1 addition & 0 deletions lib/datarockets/style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "datarockets/style/version"

require "datarockets/style/cop/layout/array_alignment_extended"
require "datarockets/style/cop/style/nested_interpolation"

module Datarockets
# Datarickors sharable config
Expand Down
29 changes: 29 additions & 0 deletions lib/datarockets/style/cop/style/nested_interpolation.rb
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
21 changes: 21 additions & 0 deletions manual/cops_layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,24 @@ Name | Default value | Configurable values
--- | --- | ---
EnforcedStyle | `with_first_parameter` | `with_first_parameter`, `with_fixed_indentation`
IndentationWidth | `<none>` | Integer

# Style

## Style/NestedInterpolation

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | Yes | No | 0.8.0 | -

This cop checks nested interpolations

### Example

```ruby
# bad
"Hello, #{user.blank? ? 'guest' : "dear #{user.name}"}"

# good
user_name = user.blank? ? 'guest' : "dear #{user.name}"
"Hello, #{user_name}"
```
39 changes: 39 additions & 0 deletions spec/datarockets/style/cop/style/nested_interpolation_spec.rb
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

0 comments on commit c8f4ebf

Please sign in to comment.