Skip to content

Commit

Permalink
Add pattern method for AST::InPatternNode
Browse files Browse the repository at this point in the history
This PR adds `pattern` method for `AST::InPatternNode`.
  • Loading branch information
koic authored and marcandre committed May 28, 2021
1 parent 3b89788 commit cc96aa8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_add_pattern_method_for_ast_in_pattern_node
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#186](https://github.com/rubocop-hq/rubocop-ast/pull/186): Add `pattern` method for `AST::InPatternNode` node. ([@koic][])
7 changes: 7 additions & 0 deletions lib/rubocop/ast/node/in_pattern_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ module AST
# node when the builder constructs the AST, making its methods available
# to all `in` nodes within RuboCop.
class InPatternNode < Node
# Returns a node of the pattern in the `in` branch.
#
# @return [Node] a pattern node
def pattern
node_parts.first
end

# Returns the index of the `in` branch within the `case` statement.
#
# @return [Integer] the index of the `in` branch
Expand Down
72 changes: 72 additions & 0 deletions spec/rubocop/ast/in_pattern_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,78 @@
it { expect(in_pattern_node).to be_a(described_class) }
end

describe '#pattern' do
context 'with a value pattern' do
let(:source) do
['case condition',
'in 42 then foo',
'end'].join("\n")
end

it { expect(in_pattern_node.pattern).to be_int_type }
end

context 'with a variable pattern' do
let(:source) do
['case condition',
'in var then foo',
'end'].join("\n")
end

it { expect(in_pattern_node.pattern).to be_match_var_type }
end

context 'with an alternative pattern' do
let(:source) do
['case condition',
'in :foo | :bar | :baz then foo',
'end'].join("\n")
end

it { expect(in_pattern_node.pattern).to be_match_alt_type }
end

context 'with an as pattern' do
let(:source) do
['case condition',
'in Integer => var then foo',
'end'].join("\n")
end

it { expect(in_pattern_node.pattern).to be_match_as_type }
end

context 'with an array pattern' do
let(:source) do
['case condition',
'in :foo, :bar, :baz then foo',
'end'].join("\n")
end

it { expect(in_pattern_node.pattern).to be_array_pattern_type }
end

context 'with a hash pattern' do
let(:source) do
['case condition',
'in foo:, bar:, baz: then foo',
'end'].join("\n")
end

it { expect(in_pattern_node.pattern).to be_hash_pattern_type }
end

context 'with a pin operator', :ruby31 do
let(:source) do
['case condition',
'in ^(2 + 2) then foo',
'end'].join("\n")
end

it { expect(in_pattern_node.pattern).to be_pin_type }
end
end

describe '#then?' do
context 'with a then keyword' do
let(:source) do
Expand Down

0 comments on commit cc96aa8

Please sign in to comment.