Skip to content

Commit

Permalink
Enable BlockNode#argument_list for numblocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvandersluis committed Nov 24, 2020
1 parent 5cd306f commit d072623
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
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
end

describe '#arguments?' do
Expand Down

0 comments on commit d072623

Please sign in to comment.