Skip to content

Commit

Permalink
Merge pull request #7816 from koic/support_numblock_type
Browse files Browse the repository at this point in the history
Support Ruby 2.7's numbered parameter for `Style/Lambda`
  • Loading branch information
koic authored Mar 26, 2020
2 parents 3405fe6 + 500d0c5 commit 95e75ca
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* [#7740](https://github.com/rubocop-hq/rubocop/issues/7740): Add `AllowModifiersOnSymbols` configuration to `Style/AccessModifierDeclarations`. ([@tejasbubane][])
* [#7812](https://github.com/rubocop-hq/rubocop/pull/7812): Add auto-correction for `Lint/BooleanSymbol` cop. ([@tejasbubane][])
* [#7823](https://github.com/rubocop-hq/rubocop/pull/7823): Add `IgnoredMethods` configuration in `Metrics/AbcSize`, `Metrics/CyclomaticComplexity`, and `Metrics/PerceivedComplexity` cops. ([@drenmi][])
* [#7816](https://github.com/rubocop-hq/rubocop/pull/7816): Support Ruby 2.7's numbered parameter for `Style/Lambda`. ([@koic][])

### Bug fixes

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/ast/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Builder < Parser::Builders::Default
args: ArgsNode,
array: ArrayNode,
block: BlockNode,
numblock: BlockNode,
break: BreakNode,
case_match: CaseMatchNode,
case: CaseNode,
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ def guard_clause?
(send (const nil? :Proc) :new)}
PATTERN

def_node_matcher :lambda?, '(block (send nil? :lambda) ...)'
def_node_matcher :lambda?, '({block numblock} (send nil? :lambda) ...)'
def_node_matcher :lambda_or_proc?, '{lambda? proc?}'

def_node_matcher :class_constructor?, <<~PATTERN
Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/ast/node/block_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def send_node
#
# @return [Array<Node>]
def arguments
node_parts[1]
if numblock_type?
[] # Numbered parameters have no block arguments.
else
node_parts[1]
end
end

# The body of this block.
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/style/lambda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def on_block(node)
location: node.send_node.source_range,
message: message(node, selector))
end
alias on_numblock on_block

def autocorrect(node)
if node.send_node.source == 'lambda'
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/ast/block_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@

it { expect(block_node.arguments.size).to eq(2) }
end

context '>= Ruby 2.7', :ruby27 do
context 'using numbered parameters' do
let(:source) { 'foo { _1 }' }

it { expect(block_node.arguments.empty?).to be(true) }
end
end
end

describe '#arguments?' do
Expand All @@ -59,6 +67,14 @@

it { expect(block_node.arguments?).to be_truthy }
end

context '>= Ruby 2.7', :ruby27 do
context 'using numbered parameters' do
let(:source) { 'foo { _1 }' }

it { expect(block_node.arguments?).to be false }
end
end
end

describe '#braces?' do
Expand Down
32 changes: 32 additions & 0 deletions spec/rubocop/cop/style/lambda_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,38 @@
end
end

context '>= Ruby 2.7', :ruby27 do
context 'when using numbered parameter' do
context 'with a single line lambda method call' do
let(:source) { 'f = lambda { _1 }' }

it_behaves_like 'registers an offense',
'Use the `-> { ... }` lambda literal syntax for ' \
'single line lambdas.'
it_behaves_like 'auto-correct', 'f = -> { _1 }'
end

context 'with a multiline lambda method call' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
l = lambda do
_1
end
RUBY
end
end

context 'with a single line lambda literal' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
lambda = -> { _1 }
lambda.(1)
RUBY
end
end
end
end

context 'with a multiline lambda literal' do
context 'with arguments' do
let(:source) do
Expand Down

0 comments on commit 95e75ca

Please sign in to comment.