Skip to content

Commit

Permalink
[Fix #7728] Fix an error for Style/OneLineConditional
Browse files Browse the repository at this point in the history
Fixes #7728.

This PR fixes an error for `Style/OneLineConditional`
when one of the branches contains a self keyword.
  • Loading branch information
koic committed Mar 25, 2020
1 parent ebae59b commit 8815dda
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -30,6 +30,7 @@
* [#7778](https://github.com/rubocop-hq/rubocop/issues/7778): Fix a false positive for `Layout/EndAlignment` when a non-whitespace is used before the `end` keyword. ([@koic][])
* [#7806](https://github.com/rubocop-hq/rubocop/pull/7806): Fix an error for `Lint/ErbNewArguments` cop when inspecting `ActionView::Template::Handlers::ERB.new`. ([@koic][])
* [#7814](https://github.com/rubocop-hq/rubocop/issues/7814): Fix a false positive for `Migrate/DepartmentName` cop when inspecting an unexpected disabled comment format. ([@koic][])
* [#7728](https://github.com/rubocop-hq/rubocop/issues/7728): Fix an error for `Style/OneLineConditional` when one of the branches contains a self keyword. ([@koic][])

### Changes

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Expand Up @@ -59,6 +59,7 @@
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'
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/ast/builder.rb
Expand Up @@ -44,6 +44,7 @@ class Builder < Parser::Builders::Default
resbody: ResbodyNode,
retry: RetryNode,
return: ReturnNode,
self: SelfNode,
csend: SendNode,
send: SendNode,
str: StrNode,
Expand Down
17 changes: 17 additions & 0 deletions lib/rubocop/ast/node/self_node.rb
@@ -0,0 +1,17 @@
# 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
14 changes: 14 additions & 0 deletions spec/rubocop/ast/self_node_spec.rb
@@ -0,0 +1,14 @@
# 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
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/one_line_conditional_spec.rb
Expand Up @@ -130,4 +130,15 @@
true ? break : 7
RUBY
end

it 'does not break when one of the branches contains a self keyword' do
expect_offense(<<~RUBY)
if true then self else 7 end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor the ternary operator (`?:`) over `if/then/else/end` constructs.
RUBY

expect_correction(<<~RUBY)
true ? self : 7
RUBY
end
end

0 comments on commit 8815dda

Please sign in to comment.