Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #7728] Fix an error for Style/OneLineConditional #7729

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you point me to the code that calls #arguments? on a self node? I think the call site probably should filter the relevant nodes instead. I can have a look. 🙂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SelfNode#arguments? is called in the following code.
https://github.com/rubocop-hq/rubocop/blob/v0.80.0/lib/rubocop/cop/style/one_line_conditional.rb#L96

I was worried about choosing between implementing SelfNode#arguments? or guarding the above with resopnd_to(:arguments?).

  def keyword_with_changed_precedence?(node)
    return false unless node.keyword?
    return true if node.prefix_not?

-   node.arguments? && !node.parenthesized_call?
+   node.respond_to?(:arguments?) && node.arguments? && !node.parenthesized_call?
  end

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koic Let me have a look. 🙂 Ideally we shouldn't do any of those. We should make sure incompatible nodes don't reach this code.

false
end
end
end
end
14 changes: 14 additions & 0 deletions spec/rubocop/ast/self_node_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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