diff --git a/CHANGELOG.md b/CHANGELOG.md index 98b262fd229..3bb05172c65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ * [#7812](https://github.com/rubocop-hq/rubocop/pull/7812): Add auto-correction for `Lint/BooleanSymbol` cop. ([@tejasbubane][]) * [#7823](https://github.com/rubocop-hq/rubocop/pull/7823): Add `IgnoredMethods` configuration in `Metrics/AbcSize`, `Metrics/CyclomaticComplexity`, and `Metrics/PerceivedComplexity` cops. ([@drenmi][]) * [#7816](https://github.com/rubocop-hq/rubocop/pull/7816): Support Ruby 2.7's numbered parameter for `Style/Lambda`. ([@koic][]) +* [#7829](https://github.com/rubocop-hq/rubocop/issues/7829): Fix an error for `Style/OneLineConditional` when one of the branches contains `next` keyword. ([@koic][]) ### Bug fixes diff --git a/lib/rubocop.rb b/lib/rubocop.rb index c1850cae7f4..d9f4d8d6706 100644 --- a/lib/rubocop.rb +++ b/lib/rubocop.rb @@ -59,7 +59,6 @@ require_relative 'rubocop/ast/node/retry_node' require_relative 'rubocop/ast/node/return_node' require_relative 'rubocop/ast/node/self_class_node' -require_relative 'rubocop/ast/node/self_node' require_relative 'rubocop/ast/node/send_node' require_relative 'rubocop/ast/node/str_node' require_relative 'rubocop/ast/node/super_node' diff --git a/lib/rubocop/ast/builder.rb b/lib/rubocop/ast/builder.rb index ddd276f55a8..84ef5abcbde 100644 --- a/lib/rubocop/ast/builder.rb +++ b/lib/rubocop/ast/builder.rb @@ -45,7 +45,6 @@ class Builder < Parser::Builders::Default resbody: ResbodyNode, retry: RetryNode, return: ReturnNode, - self: SelfNode, csend: SendNode, send: SendNode, str: StrNode, diff --git a/lib/rubocop/ast/node/self_node.rb b/lib/rubocop/ast/node/self_node.rb deleted file mode 100644 index 0b2bef5dec6..00000000000 --- a/lib/rubocop/ast/node/self_node.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -module RuboCop - module AST - # A node extension for `self` nodes. This will be used in place of a - # plain node when the builder constructs the AST, making its methods - # available to all `self` nodes within RuboCop. - class SelfNode < Node - include MethodIdentifierPredicates - - # Always return `false` because `self` cannot have arguments. - def arguments? - false - end - end - end -end diff --git a/lib/rubocop/cop/style/one_line_conditional.rb b/lib/rubocop/cop/style/one_line_conditional.rb index 0462da7997b..49f22c25065 100644 --- a/lib/rubocop/cop/style/one_line_conditional.rb +++ b/lib/rubocop/cop/style/one_line_conditional.rb @@ -91,9 +91,10 @@ def method_call_with_changed_precedence?(node) def keyword_with_changed_precedence?(node) return false unless node.keyword? - return true if node.prefix_not? + return true if node.respond_to?(:prefix_not?) && node.prefix_not? - node.arguments? && !node.parenthesized_call? + node.respond_to?(:arguments?) && node.arguments? && + !node.parenthesized_call? end end end diff --git a/spec/rubocop/ast/self_node_spec.rb b/spec/rubocop/ast/self_node_spec.rb deleted file mode 100644 index 4771431d33e..00000000000 --- a/spec/rubocop/ast/self_node_spec.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe RuboCop::AST::SelfNode do - let(:source) { 'self' } - let(:self_node) { parse_source(source).ast } - - describe '.new' do - it { expect(self_node.is_a?(described_class)).to be(true) } - end - - describe '#arguments?' do - it { expect(self_node.arguments?).to be(false) } - end -end diff --git a/spec/rubocop/cop/style/one_line_conditional_spec.rb b/spec/rubocop/cop/style/one_line_conditional_spec.rb index 3fdca29091b..73e8daa19eb 100644 --- a/spec/rubocop/cop/style/one_line_conditional_spec.rb +++ b/spec/rubocop/cop/style/one_line_conditional_spec.rb @@ -141,4 +141,15 @@ true ? self : 7 RUBY end + + it 'does not break when one of the branches contains `next` keyword' do + expect_offense(<<~RUBY) + map{ |line| if line.match(/^\s*#/) || line.strip.empty? then next else line end } + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor the ternary operator (`?:`) over `if/then/else/end` constructs. + RUBY + + expect_correction(<<~RUBY) + map{ |line| (line.match(/^ *#/) || line.strip.empty?) ? next : line } + RUBY + end end