Skip to content

Commit 9e5a10a

Browse files
authored
Merge pull request #496 from koic/support_itblock_in_performance_cops
Support `it` block parameter in `Performance` cops
2 parents 300b997 + cee374c commit 9e5a10a

File tree

10 files changed

+50
-2
lines changed

10 files changed

+50
-2
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ jobs:
9292
sed -e "/gem 'rubocop', github: 'rubocop\/rubocop'/d" \
9393
-e "/gem 'rubocop-rspec',/d" -i Gemfile
9494
cat << EOF > Gemfile.local
95-
gem 'rubocop', '1.72.1' # Specify the oldest supported RuboCop version
95+
gem 'rubocop', '1.75.0' # Specify the oldest supported RuboCop version
9696
EOF
9797
- name: set up Ruby
9898
uses: ruby/setup-ruby@v1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#496](https://github.com/rubocop/rubocop-performance/pull/496): Support `it` block parameter in `Performance` cops. ([@koic][])

lib/rubocop/cop/performance/times_map.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def on_block(node)
5252
check(node)
5353
end
5454
alias on_numblock on_block
55+
alias on_itblock on_block
5556

5657
private
5758

lib/rubocop/cop/performance/zip_without_block.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ZipWithoutBlock < Base
2828
{
2929
(block (call !nil? RESTRICT_ON_SEND) (args (arg _)) (array (lvar _)))
3030
(numblock (call !nil? RESTRICT_ON_SEND) 1 (array (lvar _)))
31+
(itblock (call !nil? RESTRICT_ON_SEND) :it (array (lvar _)))
3132
}
3233
PATTERN
3334

rubocop-performance.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ Gem::Specification.new do |s|
3232
}
3333

3434
s.add_dependency('lint_roller', '~> 1.1')
35-
s.add_dependency('rubocop', '>= 1.72.1', '< 2.0')
35+
s.add_dependency('rubocop', '>= 1.75.0', '< 2.0')
3636
s.add_dependency('rubocop-ast', '>= 1.38.0', '< 2.0')
3737
end

spec/rubocop/cop/performance/chain_array_allocation_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@
8080
end
8181
end
8282

83+
describe 'when using `select` with block argument after `select` with `it` block', :ruby34, unsupported_on: :parser do
84+
it 'registers an offense' do
85+
expect_offense(<<~RUBY)
86+
model.select { it.foo }.select { |item| item.bar }
87+
^^^^^^ Use unchained `select` and `select!` (followed by `return array` if required) instead of chaining `select...select`.
88+
RUBY
89+
end
90+
end
91+
8392
describe 'when using `select` with positional arguments after `select`' do
8493
it 'does not register an offense' do
8594
expect_no_offenses(<<~RUBY)

spec/rubocop/cop/performance/redundant_block_call_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ def method(&block)
133133
RUBY
134134
end
135135

136+
it 'accepts when using `block.call` with `it` block argument', :ruby34, unsupported_on: :parser do
137+
expect_no_offenses(<<~RUBY)
138+
def method(&block)
139+
block.call { it.do_something }
140+
end
141+
RUBY
142+
end
143+
136144
it 'accepts another block being passed along with other args' do
137145
expect_no_offenses(<<~RUBY)
138146
def method(&block)

spec/rubocop/cop/performance/times_map_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@
138138
RUBY
139139
end
140140
end
141+
142+
context 'when using `it` parameter', :ruby34, unsupported_on: :parser do
143+
it 'registers an offense and corrects' do
144+
expect_offense(<<~RUBY, method: method)
145+
4.times.#{method} { it.to_s }
146+
^^^^^^^^^{method}^^^^^^^^^^^^ Use `Array.new(4)` with a block instead of `.times.#{method}`.
147+
RUBY
148+
149+
expect_correction(<<~RUBY)
150+
Array.new(4) { it.to_s }
151+
RUBY
152+
end
153+
end
141154
end
142155
end
143156

spec/rubocop/cop/performance/zip_without_block_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,17 @@
163163
[1, 2, 3].zip
164164
RUBY
165165
end
166+
167+
it 'registers an offense for collect with a numblock', :ruby34, unsupported_on: :parser do
168+
expect_offense(<<~RUBY)
169+
[1, 2, 3].collect { [it] }
170+
^^^^^^^^^^^^^^^^ Use `zip` without a block argument instead.
171+
RUBY
172+
173+
expect_correction(<<~RUBY)
174+
[1, 2, 3].zip
175+
RUBY
176+
end
166177
end
167178

168179
context 'when using select with an array literal' do

spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
# Prism supports Ruby 3.3+ parsing.
1717
config.filter_run_excluding unsupported_on: :prism if ENV['PARSER_ENGINE'] == 'parser_prism'
18+
19+
# With whitequark/parser, RuboCop supports Ruby syntax compatible with 2.0 to 3.3.
20+
config.filter_run_excluding unsupported_on: :parser if ENV['PARSER_ENGINE'] != 'parser_prism'
21+
1822
config.example_status_persistence_file_path = 'spec/examples.txt'
1923
config.disable_monkey_patching!
2024
config.warnings = true

0 commit comments

Comments
 (0)