Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7786 from koic/add_case_match_node
Support Ruby 2.7's pattern match for `Layout/ElseAlignment` cop
- Loading branch information
Showing
8 changed files
with
266 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# A node extension for `case_match` nodes. This will be used in place of | ||
# a plain node when the builder constructs the AST, making its methods | ||
# available to all `case_match` nodes within RuboCop. | ||
class CaseMatchNode < Node | ||
include ConditionalNode | ||
|
||
# Returns the keyword of the `case` statement as a string. | ||
# | ||
# @return [String] the keyword of the `case` statement | ||
def keyword | ||
'case' | ||
end | ||
|
||
# Calls the given block for each `in_pattern` node in the `in` statement. | ||
# If no block is given, an `Enumerator` is returned. | ||
# | ||
# @return [self] if a block is given | ||
# @return [Enumerator] if no block is given | ||
def each_in_pattern | ||
return in_pattern_branches.to_enum(__method__) unless block_given? | ||
|
||
in_pattern_branches.each do |condition| | ||
yield condition | ||
end | ||
|
||
self | ||
end | ||
|
||
# Returns an array of all the when branches in the `case` statement. | ||
# | ||
# @return [Array<Node>] an array of `in_pattern` nodes | ||
def in_pattern_branches | ||
node_parts[1...-1] | ||
end | ||
|
||
# Returns the else branch of the `case` statement, if any. | ||
# | ||
# @return [Node] the else branch node of the `case` statement | ||
# @return [nil] if the case statement does not have an else branch. | ||
def else_branch | ||
node_parts[-1] | ||
end | ||
|
||
# Checks whether this case statement has an `else` branch. | ||
# | ||
# @return [Boolean] whether the `case` statement has an `else` branch | ||
def else? | ||
!loc.else.nil? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::CaseMatchNode do | ||
let(:case_match_node) { parse_source(source).ast } | ||
|
||
context 'when using Ruby 2.7 or newer', :ruby27 do | ||
describe '.new' do | ||
let(:source) do | ||
<<~RUBY | ||
case expr | ||
in pattern | ||
end | ||
RUBY | ||
end | ||
|
||
it { expect(case_match_node.is_a?(described_class)).to be(true) } | ||
end | ||
|
||
describe '#keyword' do | ||
let(:source) do | ||
<<~RUBY | ||
case expr | ||
in pattern | ||
end | ||
RUBY | ||
end | ||
|
||
it { expect(case_match_node.keyword).to eq('case') } | ||
end | ||
|
||
describe '#in_pattern_branches' do | ||
let(:source) do | ||
<<~RUBY | ||
case expr | ||
in pattern | ||
in pattern | ||
in pattern | ||
end | ||
RUBY | ||
end | ||
|
||
it { expect(case_match_node.in_pattern_branches.size).to eq(3) } | ||
it { | ||
expect(case_match_node.in_pattern_branches).to all(be_in_pattern_type) | ||
} | ||
end | ||
|
||
describe '#each_in_pattern' do | ||
let(:source) do | ||
<<~RUBY | ||
case expr | ||
in pattern | ||
in pattern | ||
in pattern | ||
end | ||
RUBY | ||
end | ||
|
||
context 'when not passed a block' do | ||
it { | ||
expect(case_match_node.each_in_pattern.is_a?(Enumerator)).to be(true) | ||
} | ||
end | ||
|
||
context 'when passed a block' do | ||
it 'yields all the conditions' do | ||
expect { |b| case_match_node.each_in_pattern(&b) } | ||
.to yield_successive_args(*case_match_node.in_pattern_branches) | ||
end | ||
end | ||
end | ||
|
||
describe '#else?' do | ||
context 'without an else statement' do | ||
let(:source) do | ||
<<~RUBY | ||
case expr | ||
in pattern | ||
end | ||
RUBY | ||
end | ||
|
||
it { expect(case_match_node.else?).to be(false) } | ||
end | ||
|
||
context 'with an else statement' do | ||
let(:source) do | ||
<<~RUBY | ||
case expr | ||
in pattern | ||
else | ||
end | ||
RUBY | ||
end | ||
|
||
it { expect(case_match_node.else?).to be(true) } | ||
end | ||
end | ||
|
||
describe '#else_branch' do | ||
describe '#else?' do | ||
context 'without an else statement' do | ||
let(:source) do | ||
<<~RUBY | ||
case expr | ||
in pattern | ||
end | ||
RUBY | ||
end | ||
|
||
it { expect(case_match_node.else_branch.nil?).to be(true) } | ||
end | ||
|
||
context 'with an else statement' do | ||
let(:source) do | ||
<<~RUBY | ||
case expr | ||
in pattern | ||
else | ||
:foo | ||
end | ||
RUBY | ||
end | ||
|
||
it { expect(case_match_node.else_branch.sym_type?).to be(true) } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters