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

Enable BlockNode#argument_list for numblocks. #155

Merged
merged 1 commit into from
Nov 24, 2020
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/change_enable_blocknodeargument_list_for.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#155](https://github.com/rubocop-hq/rubocop-ast/pull/155): Enable `BlockNode#argument_list` for `numblock`s. ([@dvandersluis][])
20 changes: 17 additions & 3 deletions lib/rubocop/ast/node/block_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ def arguments
#
# @return [Array<Node>]
def argument_list
return [].freeze unless arguments?

arguments.argument_list
if numblock_type?
numbered_arguments
else
arguments.argument_list
end
end

# The body of this block.
Expand Down Expand Up @@ -130,6 +132,18 @@ def lambda?
def void_context?
VOID_CONTEXT_METHODS.include?(method_name)
end

private

# Numbered arguments of this `numblock`.
def numbered_arguments
return [].freeze unless numblock_type?

max_param = children[1]
1.upto(max_param).map do |i|
ArgNode.new(:arg, [:"_#{i}"])
end.freeze
end
end
end
end
20 changes: 16 additions & 4 deletions spec/rubocop/ast/block_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,37 @@
describe '#argument_list' do
subject(:argument_list) { block_node.argument_list }

let(:names) { argument_list.map(&:name) }

context 'with no arguments' do
let(:source) { 'foo { bar }' }

it { is_expected.to be_empty }
end

context 'all argument types' do
let(:source) { 'foo { |a, b = 42, (c, *d), e:, f: 42, **g, &h| nil }' }
let(:source) { 'foo { |a, b = 42, (c, *d), e:, f: 42, **g, &h; i| nil }' }

it { expect(argument_list.size).to eq(8) }
it { expect(names).to eq(%i[a b c d e f g h i]) }
end

# rubocop:disable Naming/VariableNumber
context '>= Ruby 2.7', :ruby27 do
context 'using numbered parameters' do
let(:source) { 'foo { _1 }' }
context 'with skipped params' do
let(:source) { 'foo { _7 }' }

it { expect(names).to eq(%i[_1 _2 _3 _4 _5 _6 _7]) }
end

context 'with sequential params' do
let(:source) { 'foo { _1 + _2 }' }

it { is_expected.to be_empty }
it { expect(names).to eq(%i[_1 _2]) }
end
end
end
# rubocop:enable Naming/VariableNumber
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened rubocop/rubocop#9091...

end

describe '#arguments?' do
Expand Down