From 4d058aa8c2651bf2d59f554281a8fe9545b39c0d Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 9 Jan 2024 19:27:09 +0900 Subject: [PATCH] [Fix #12603] Fix an infinite loop error for `Style/MultilineTernaryOperator` Fixes #12603. This PR fixes an infinite loop error for `Style/MultilineTernaryOperator` when using a method call as a ternary operator condition with a line break between receiver and method. --- ...sitive_for_style_multiline_ternary_operator.md | 1 + .../cop/style/multiline_ternary_operator.rb | 4 +--- spec/rubocop/cli/autocorrect_spec.rb | 15 +++++++++++++++ .../cop/style/multiline_ternary_operator_spec.rb | 9 +++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 changelog/fix_a_false_positive_for_style_multiline_ternary_operator.md diff --git a/changelog/fix_a_false_positive_for_style_multiline_ternary_operator.md b/changelog/fix_a_false_positive_for_style_multiline_ternary_operator.md new file mode 100644 index 000000000000..a011bb855fe4 --- /dev/null +++ b/changelog/fix_a_false_positive_for_style_multiline_ternary_operator.md @@ -0,0 +1 @@ +* [#12603](https://github.com/rubocop/rubocop/issues/12603): Fix an infinite loop error for `Style/MultilineTernaryOperator` when using a method call as a ternary operator condition with a line break between receiver and method. ([@koic][]) diff --git a/lib/rubocop/cop/style/multiline_ternary_operator.rb b/lib/rubocop/cop/style/multiline_ternary_operator.rb index e9a375bed8ca..23a656eed724 100644 --- a/lib/rubocop/cop/style/multiline_ternary_operator.rb +++ b/lib/rubocop/cop/style/multiline_ternary_operator.rb @@ -54,12 +54,10 @@ def on_if(node) private def offense?(node) - node.ternary? && node.multiline? + node.ternary? && node.multiline? && node.source != replacement(node) end def autocorrect(corrector, node) - return unless offense?(node) - corrector.replace(node, replacement(node)) return unless (parent = node.parent) return unless (comments_in_condition = comments_in_condition(node)) diff --git a/spec/rubocop/cli/autocorrect_spec.rb b/spec/rubocop/cli/autocorrect_spec.rb index 0bbde744e18a..c356e5f3b504 100644 --- a/spec/rubocop/cli/autocorrect_spec.rb +++ b/spec/rubocop/cli/autocorrect_spec.rb @@ -2971,6 +2971,21 @@ module Foo#{trailing_whitespace} RUBY end + it 'does not cause an infinite loop error for `Style/MultilineTernaryOperator`' do + source_file = Pathname('example.rb') + create_file(source_file, <<~RUBY) + do_something(arg + .foo ? bar : baz) + RUBY + + status = cli.run(%w[--autocorrect-all --only Style/MultilineTernaryOperator]) + expect(status).to eq(0) + expect(source_file.read).to eq(<<~RUBY) + do_something(arg + .foo ? bar : baz) + RUBY + end + it 'respects `Lint/ConstantResolution` over `Style/RedundantConstantBase` when enabling`Lint/ConstantResolution`' do source_file = Pathname('example.rb') create_file(source_file, <<~RUBY) diff --git a/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb b/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb index 1fc189ee1b05..51848de34cf2 100644 --- a/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb +++ b/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb @@ -124,6 +124,15 @@ RUBY end + it 'does not register an offense when using a method call as a ternary operator condition with a line break ' \ + 'between receiver and method' do + # NOTE: Redundant line break is corrected by `Layout/RedundantLineBreak`. + expect_no_offenses(<<~RUBY) + do_something(arg + .foo ? bar : baz) + RUBY + end + it 'registers an offense and corrects when returning a multiline ternary operator expression with `return`' do expect_offense(<<~RUBY) return cond ?