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

Add pattern method for AST::InPatternNode #186

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/new_add_pattern_method_for_ast_in_pattern_node
@@ -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
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
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