Skip to content

Commit

Permalink
Merge pull request #8152 from koic/support_autocorrect_for_nested_ter…
Browse files Browse the repository at this point in the history
…nary_operator

Support autocorrection for `Style/NestedTernaryOperator`
  • Loading branch information
koic authored Jun 16, 2020
2 parents 60ed05c + ad6e733 commit 9cdaa31
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#8111](https://github.com/rubocop-hq/rubocop/pull/8111): Add auto-correct for `Style/StructInheritance`. ([@tejasbubane][])
* [#8113](https://github.com/rubocop-hq/rubocop/pull/8113): Let `expect_offense` templates add variable-length whitespace with `_{foo}`. ([@eugeneius][])
* [#8148](https://github.com/rubocop-hq/rubocop/pull/8148): Support autocorrection for `Style/MultilineTernaryOperator`. ([@koic][])
* [#8151](https://github.com/rubocop-hq/rubocop/pull/8151): Support autocorrection for `Style/NestedTernaryOperator`. ([@koic][])

### Bug fixes

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3344,6 +3344,7 @@ Style/NestedTernaryOperator:
StyleGuide: '#no-nested-ternary'
Enabled: true
VersionAdded: '0.9'
VersionChanged: '0.86'

Style/Next:
Description: 'Use `next` to skip iteration instead of a condition at the end.'
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ROOT/pages/cops_style.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5670,9 +5670,9 @@ method1(method2 arg)

| Enabled
| Yes
| No
| Yes
| 0.9
| -
| 0.86
|===

This cop checks for nested ternary op expressions.
Expand Down
20 changes: 20 additions & 0 deletions lib/rubocop/cop/style/nested_ternary_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ def on_if(node)
add_offense(nested_ternary)
end
end

def autocorrect(node)
node = node.parent.parent

lambda do |corrector|
corrector.replace(node, <<~RUBY.chop)
if #{node.condition.source}
#{remove_parentheses(node.if_branch.source)}
else
#{node.else_branch.source}
end
RUBY
end
end

private

def remove_parentheses(source)
source.gsub(/\A\(/, '').gsub(/\)\z/, '')
end
end
end
end
Expand Down
10 changes: 9 additions & 1 deletion spec/rubocop/cop/style/nested_ternary_operator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
RSpec.describe RuboCop::Cop::Style::NestedTernaryOperator do
subject(:cop) { described_class.new }

it 'registers an offense for a nested ternary operator expression' do
it 'registers an offense and corrects for a nested ternary operator expression' do
expect_offense(<<~RUBY)
a ? (b ? b1 : b2) : a2
^^^^^^^^^^^ Ternary operators must not be nested. Prefer `if` or `else` constructs instead.
RUBY

expect_correction(<<~RUBY)
if a
b ? b1 : b2
else
a2
end
RUBY
end

it 'accepts a non-nested ternary operator within an if' do
Expand Down

0 comments on commit 9cdaa31

Please sign in to comment.