Skip to content

Commit

Permalink
Add new BreakNode extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Drenmi committed Feb 17, 2019
1 parent 06fd3db commit 9d94d10
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
require_relative 'rubocop/ast/node/args_node'
require_relative 'rubocop/ast/node/array_node'
require_relative 'rubocop/ast/node/block_node'
require_relative 'rubocop/ast/node/break_node'
require_relative 'rubocop/ast/node/case_node'
require_relative 'rubocop/ast/node/def_node'
require_relative 'rubocop/ast/node/defined_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 @@ -19,6 +19,7 @@ class Builder < Parser::Builders::Default
args: ArgsNode,
array: ArrayNode,
block: BlockNode,
break: BreakNode,
case: CaseNode,
def: DefNode,
defined?: DefinedNode,
Expand Down
17 changes: 17 additions & 0 deletions lib/rubocop/ast/node/break_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 `break` nodes. This will be used in place of a
# plain node when the builder constructs the AST, making its methods
# available to all `break` nodes within RuboCop.
class BreakNode < Node
include MethodDispatchNode
include ParameterizedNode

def arguments
[]
end
end
end
end
5 changes: 5 additions & 0 deletions lib/rubocop/ast/node/retry_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ module AST
# available to all `retry` nodes within RuboCop.
class RetryNode < Node
include MethodDispatchNode
include ParameterizedNode

def arguments
[]
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/one_line_conditional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def keyword_with_changed_precedence?(node)
return false unless node.keyword?
return true if node.prefix_not?

!node.parenthesized_call?
node.arguments? && !node.parenthesized_call?
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/ast/break_node_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

RSpec.describe RuboCop::AST::BreakNode do
let(:break_node) { parse_source(source).ast }

describe '.new' do
context 'with a break node' do
let(:source) { 'break' }

it { expect(break_node.is_a?(described_class)).to be(true) }
end
end
end
4 changes: 2 additions & 2 deletions spec/rubocop/ast/retry_node_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# frozen_string_literal: true

RSpec.describe RuboCop::AST::RetryNode do
let(:super_node) { parse_source(source).ast }
let(:retry_node) { parse_source(source).ast }

describe '.new' do
context 'with a retry node' do
let(:source) { 'retry' }

it { expect(super_node.is_a?(described_class)).to be(true) }
it { expect(retry_node.is_a?(described_class)).to be(true) }
end
end
end
15 changes: 15 additions & 0 deletions spec/rubocop/cop/style/one_line_conditional_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,20 @@
if true then retry else 7 end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor the ternary operator (`?:`) over `if/then/else/end` constructs.
RUBY

expect_correction(<<-RUBY.strip_indent)
true ? retry : 7
RUBY
end

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

expect_correction(<<-RUBY.strip_indent)
true ? break : 7
RUBY
end
end

0 comments on commit 9d94d10

Please sign in to comment.