Skip to content

Commit 95e75ca

Browse files
authored
Merge pull request #7816 from koic/support_numblock_type
Support Ruby 2.7's numbered parameter for `Style/Lambda`
2 parents 3405fe6 + 500d0c5 commit 95e75ca

File tree

7 files changed

+57
-2
lines changed

7 files changed

+57
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* [#7740](https://github.com/rubocop-hq/rubocop/issues/7740): Add `AllowModifiersOnSymbols` configuration to `Style/AccessModifierDeclarations`. ([@tejasbubane][])
2020
* [#7812](https://github.com/rubocop-hq/rubocop/pull/7812): Add auto-correction for `Lint/BooleanSymbol` cop. ([@tejasbubane][])
2121
* [#7823](https://github.com/rubocop-hq/rubocop/pull/7823): Add `IgnoredMethods` configuration in `Metrics/AbcSize`, `Metrics/CyclomaticComplexity`, and `Metrics/PerceivedComplexity` cops. ([@drenmi][])
22+
* [#7816](https://github.com/rubocop-hq/rubocop/pull/7816): Support Ruby 2.7's numbered parameter for `Style/Lambda`. ([@koic][])
2223

2324
### Bug fixes
2425

lib/rubocop/ast/builder.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Builder < Parser::Builders::Default
2020
args: ArgsNode,
2121
array: ArrayNode,
2222
block: BlockNode,
23+
numblock: BlockNode,
2324
break: BreakNode,
2425
case_match: CaseMatchNode,
2526
case: CaseNode,

lib/rubocop/ast/node.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ def guard_clause?
484484
(send (const nil? :Proc) :new)}
485485
PATTERN
486486

487-
def_node_matcher :lambda?, '(block (send nil? :lambda) ...)'
487+
def_node_matcher :lambda?, '({block numblock} (send nil? :lambda) ...)'
488488
def_node_matcher :lambda_or_proc?, '{lambda? proc?}'
489489

490490
def_node_matcher :class_constructor?, <<~PATTERN

lib/rubocop/ast/node/block_node.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ def send_node
2424
#
2525
# @return [Array<Node>]
2626
def arguments
27-
node_parts[1]
27+
if numblock_type?
28+
[] # Numbered parameters have no block arguments.
29+
else
30+
node_parts[1]
31+
end
2832
end
2933

3034
# The body of this block.

lib/rubocop/cop/style/lambda.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def on_block(node)
7373
location: node.send_node.source_range,
7474
message: message(node, selector))
7575
end
76+
alias on_numblock on_block
7677

7778
def autocorrect(node)
7879
if node.send_node.source == 'lambda'

spec/rubocop/ast/block_node_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333

3434
it { expect(block_node.arguments.size).to eq(2) }
3535
end
36+
37+
context '>= Ruby 2.7', :ruby27 do
38+
context 'using numbered parameters' do
39+
let(:source) { 'foo { _1 }' }
40+
41+
it { expect(block_node.arguments.empty?).to be(true) }
42+
end
43+
end
3644
end
3745

3846
describe '#arguments?' do
@@ -59,6 +67,14 @@
5967

6068
it { expect(block_node.arguments?).to be_truthy }
6169
end
70+
71+
context '>= Ruby 2.7', :ruby27 do
72+
context 'using numbered parameters' do
73+
let(:source) { 'foo { _1 }' }
74+
75+
it { expect(block_node.arguments?).to be false }
76+
end
77+
end
6278
end
6379

6480
describe '#braces?' do

spec/rubocop/cop/style/lambda_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,38 @@
194194
end
195195
end
196196

197+
context '>= Ruby 2.7', :ruby27 do
198+
context 'when using numbered parameter' do
199+
context 'with a single line lambda method call' do
200+
let(:source) { 'f = lambda { _1 }' }
201+
202+
it_behaves_like 'registers an offense',
203+
'Use the `-> { ... }` lambda literal syntax for ' \
204+
'single line lambdas.'
205+
it_behaves_like 'auto-correct', 'f = -> { _1 }'
206+
end
207+
208+
context 'with a multiline lambda method call' do
209+
it 'does not register an offense' do
210+
expect_no_offenses(<<~RUBY)
211+
l = lambda do
212+
_1
213+
end
214+
RUBY
215+
end
216+
end
217+
218+
context 'with a single line lambda literal' do
219+
it 'does not register an offense' do
220+
expect_no_offenses(<<~RUBY)
221+
lambda = -> { _1 }
222+
lambda.(1)
223+
RUBY
224+
end
225+
end
226+
end
227+
end
228+
197229
context 'with a multiline lambda literal' do
198230
context 'with arguments' do
199231
let(:source) do

0 commit comments

Comments
 (0)