-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds `AST::InPatternNode` node. It aims to make access to `in` pattern node of the pattern matching syntax convenient and readable. For example, pattern matching syntax AST can use `in_pattern.body` and prevents the following error: ```console NoMethodError: undefined method `body' for s(:in_pattern, s(:array_pattern, s(:match_var, :a)), nil, s(:int, 1)):RuboCop::AST::Node ```
- Loading branch information
Showing
6 changed files
with
115 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#183](https://github.com/rubocop-hq/rubocop-ast/pull/183): Add `AST::InPatternNode` node. ([@koic][]) |
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,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# A node extension for `in` nodes. This will be used in place of a plain | ||
# node when the builder constructs the AST, making its methods available | ||
# to all `in` nodes within RuboCop. | ||
class InPatternNode < Node | ||
# Returns the index of the `in` branch within the `case` statement. | ||
# | ||
# @return [Integer] the index of the `in` branch | ||
def branch_index | ||
parent.in_pattern_branches.index(self) | ||
end | ||
|
||
# Checks whether the `in` node has a `then` keyword. | ||
# | ||
# @return [Boolean] whether the `in` node has a `then` keyword | ||
def then? | ||
loc.begin&.is?('then') | ||
end | ||
|
||
# Returns the body of the `in` node. | ||
# | ||
# @return [Node, nil] the body of the `in` node | ||
def body | ||
node_parts[-1] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::InPatternNode do | ||
context 'when using Ruby 2.7 or newer', :ruby27 do | ||
let(:in_pattern_node) { parse_source(source).ast.children[1] } | ||
|
||
describe '.new' do | ||
let(:source) do | ||
['case condition', | ||
'in [42] then foo', | ||
'end'].join("\n") | ||
end | ||
|
||
it { expect(in_pattern_node).to be_a(described_class) } | ||
end | ||
|
||
describe '#then?' do | ||
context 'with a then keyword' do | ||
let(:source) do | ||
['case condition', | ||
'in [42] then foo', | ||
'end'].join("\n") | ||
end | ||
|
||
it { expect(in_pattern_node).to be_then } | ||
end | ||
|
||
context 'without a then keyword' do | ||
let(:source) do | ||
['case condition', | ||
'in [42]', | ||
' foo', | ||
'end'].join("\n") | ||
end | ||
|
||
it { expect(in_pattern_node).not_to be_then } | ||
end | ||
end | ||
|
||
describe '#body' do | ||
context 'with a then keyword' do | ||
let(:source) do | ||
['case condition', | ||
'in [42] then :foo', | ||
'end'].join("\n") | ||
end | ||
|
||
it { expect(in_pattern_node.body).to be_sym_type } | ||
end | ||
|
||
context 'without a then keyword' do | ||
let(:source) do | ||
['case condition', | ||
'in [42]', | ||
' [:foo, :bar]', | ||
'end'].join("\n") | ||
end | ||
|
||
it { expect(in_pattern_node.body).to be_array_type } | ||
end | ||
end | ||
|
||
describe '#branch_index' do | ||
let(:source) do | ||
['case condition', | ||
'in [42] then 1', | ||
'in [43] then 2', | ||
'in [44] then 3', | ||
'end'].join("\n") | ||
end | ||
|
||
let(:in_patterns) { parse_source(source).ast.children[1...-1] } | ||
|
||
it { expect(in_patterns[0].branch_index).to eq(0) } | ||
it { expect(in_patterns[1].branch_index).to eq(1) } | ||
it { expect(in_patterns[2].branch_index).to eq(2) } | ||
end | ||
end | ||
end |