|
96 | 96 | {foo: 1, bar: 2, baz: 3}.reject { |k, v| k.in?(%i[foo bar]) }
|
97 | 97 | RUBY
|
98 | 98 | end
|
| 99 | + |
| 100 | + it 'does not register offenses when using safe navigation `reject` and calling `key.in?` method with symbol array' do |
| 101 | + expect_no_offenses(<<~RUBY) |
| 102 | + {foo: 1, bar: 2, baz: 3}&.reject { |k, v| k.in?(%i[foo bar]) } |
| 103 | + RUBY |
| 104 | + end |
| 105 | + |
| 106 | + it 'does not register offenses when using `reject` and calling `in?` method with key' do |
| 107 | + expect_no_offenses(<<~RUBY) |
| 108 | + {foo: 1, bar: 2, baz: 3}.reject { |k, v| %i[foo bar].in?(k) } |
| 109 | + RUBY |
| 110 | + end |
99 | 111 | end
|
100 | 112 |
|
101 | 113 | context 'using `include?`' do
|
|
188 | 200 | RUBY
|
189 | 201 | end
|
190 | 202 |
|
| 203 | + it 'does not register offenses when using safe navigation `reject` and calling `!exclude?` method with symbol array' do |
| 204 | + expect_no_offenses(<<~RUBY) |
| 205 | + {foo: 1, bar: 2, baz: 3}&.reject { |k, v| !%i[foo bar].exclude?(k) } |
| 206 | + RUBY |
| 207 | + end |
| 208 | + |
191 | 209 | it 'does not register an offense when using `reject` and calling `exclude?` method on a key' do
|
192 | 210 | expect_no_offenses(<<~RUBY)
|
193 | 211 | {foo: 1, bar: 2, baz: 3}.reject { |k, v| k.exclude?('oo') }
|
|
207 | 225 | RUBY
|
208 | 226 | end
|
209 | 227 |
|
| 228 | + it 'does not register an offense when using `select` and other than comparison by string and symbol using `!=`' do |
| 229 | + expect_no_offenses(<<~RUBY) |
| 230 | + hash.select { |k, v| k != 0.0 } |
| 231 | + RUBY |
| 232 | + end |
| 233 | + |
| 234 | + it 'does not register an offense when using `select` and other than comparison by string and symbol using `==` with bang' do |
| 235 | + expect_no_offenses(<<~RUBY) |
| 236 | + hash.select { |k, v| !(k == 0.0) } |
| 237 | + RUBY |
| 238 | + end |
| 239 | + |
| 240 | + it 'does not register an offense when using `reject` and other than comparison by string and symbol using `!=` with bang' do |
| 241 | + expect_no_offenses(<<~RUBY) |
| 242 | + hash.reject { |k, v| !(k != 0.0) } |
| 243 | + RUBY |
| 244 | + end |
| 245 | + |
210 | 246 | it 'does not register an offense when using `delete_if` and comparing with `lvar == :sym`' do
|
211 | 247 | expect_no_offenses(<<~RUBY)
|
212 | 248 | {foo: 1, bar: 2, baz: 3}.delete_if { |k, v| k == :bar }
|
|
225 | 261 | RUBY
|
226 | 262 | end
|
227 | 263 |
|
| 264 | + it 'does not register an offense when using more than two block arguments' do |
| 265 | + expect_no_offenses(<<~RUBY) |
| 266 | + {foo: 1, bar: 2, baz: 3}.reject { |k, v, o| k == :bar } |
| 267 | + RUBY |
| 268 | + end |
| 269 | + |
| 270 | + it 'does not register an offense when calling `include?` method without a param' do |
| 271 | + expect_no_offenses(<<~RUBY) |
| 272 | + {foo: 1, bar: 2, baz: 3}.reject { |k, v| %i[foo bar].include? } |
| 273 | + RUBY |
| 274 | + end |
| 275 | + |
228 | 276 | context 'when `AllCops/ActiveSupportExtensionsEnabled: true`' do
|
229 | 277 | let(:config) do
|
230 | 278 | RuboCop::Config.new('AllCops' => {
|
|
322 | 370 | RUBY
|
323 | 371 | end
|
324 | 372 |
|
| 373 | + it 'registers and corrects an offense when using safe navigation `reject` and calling `key.in?` method with symbol array' do |
| 374 | + expect_offense(<<~RUBY) |
| 375 | + {foo: 1, bar: 2, baz: 3}&.reject { |k, v| k.in?(%i[foo bar]) } |
| 376 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(:foo, :bar)` instead. |
| 377 | + RUBY |
| 378 | + |
| 379 | + expect_correction(<<~RUBY) |
| 380 | + {foo: 1, bar: 2, baz: 3}&.except(:foo, :bar) |
| 381 | + RUBY |
| 382 | + end |
| 383 | + |
325 | 384 | it 'registers and corrects an offense when using `select` and calling `!key.in?` method with symbol array' do
|
326 | 385 | expect_offense(<<~RUBY)
|
327 | 386 | {foo: 1, bar: 2, baz: 3}.select { |k, v| !k.in?(%i[foo bar]) }
|
|
396 | 455 | RUBY
|
397 | 456 | end
|
398 | 457 |
|
| 458 | + it 'does not register an offense when using `reject` and calling `in?` method with key' do |
| 459 | + expect_no_offenses(<<~RUBY) |
| 460 | + {foo: 1, bar: 2, baz: 3}.reject { |k, v| %i[foo bar].in?(k) } |
| 461 | + RUBY |
| 462 | + end |
| 463 | + |
399 | 464 | it 'does not register an offense when using `reject` and calling `in?` method with symbol array and second block value' do
|
400 | 465 | expect_no_offenses(<<~RUBY)
|
401 | 466 | {foo: 1, bar: 2, baz: 3}.reject { |k, v| v.in?([1, 2]) }
|
|
503 | 568 | RUBY
|
504 | 569 | end
|
505 | 570 |
|
| 571 | + it 'registers and corrects an offense when using safe navigation `reject` and calling `!exclude?` method with symbol array' do |
| 572 | + expect_offense(<<~RUBY) |
| 573 | + {foo: 1, bar: 2, baz: 3}&.reject { |k, v| !%i[foo bar].exclude?(k) } |
| 574 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(:foo, :bar)` instead. |
| 575 | + RUBY |
| 576 | + |
| 577 | + expect_correction(<<~RUBY) |
| 578 | + {foo: 1, bar: 2, baz: 3}&.except(:foo, :bar) |
| 579 | + RUBY |
| 580 | + end |
| 581 | + |
506 | 582 | it 'registers and corrects an offense when using `select` and calling `exclude?` method with symbol array' do
|
507 | 583 | expect_offense(<<~RUBY)
|
508 | 584 | {foo: 1, bar: 2, baz: 3}.select { |k, v| %i[foo bar].exclude?(k) }
|
|
602 | 678 | RUBY
|
603 | 679 | end
|
604 | 680 |
|
| 681 | + it 'does not register an offense when using `select` and other than comparison by string and symbol using `!=`' do |
| 682 | + expect_no_offenses(<<~RUBY) |
| 683 | + hash.select { |k, v| k != 0.0 } |
| 684 | + RUBY |
| 685 | + end |
| 686 | + |
| 687 | + it 'does not register an offense when using `select` and other than comparison by string and symbol using `==` with bang' do |
| 688 | + expect_no_offenses(<<~RUBY) |
| 689 | + hash.select { |k, v| !(k == 0.0) } |
| 690 | + RUBY |
| 691 | + end |
| 692 | + |
| 693 | + it 'does not register an offense when using `reject` and other than comparison by string and symbol using `!=` with bang' do |
| 694 | + expect_no_offenses(<<~RUBY) |
| 695 | + hash.reject { |k, v| !(k != 0.0) } |
| 696 | + RUBY |
| 697 | + end |
| 698 | + |
605 | 699 | it 'does not register an offense when using `delete_if` and comparing with `lvar == :sym`' do
|
606 | 700 | expect_no_offenses(<<~RUBY)
|
607 | 701 | {foo: 1, bar: 2, baz: 3}.delete_if { |k, v| k == :bar }
|
|
619 | 713 | {foo: 1, bar: 2, baz: 3}.reject { |k, v| v.eql? :bar }
|
620 | 714 | RUBY
|
621 | 715 | end
|
| 716 | + |
| 717 | + it 'does not register an offense when using more than two block arguments' do |
| 718 | + expect_no_offenses(<<~RUBY) |
| 719 | + {foo: 1, bar: 2, baz: 3}.reject { |k, v, z| k == :bar } |
| 720 | + RUBY |
| 721 | + end |
| 722 | + |
| 723 | + it 'does not register an offense when calling `include?` method without a param' do |
| 724 | + expect_no_offenses(<<~RUBY) |
| 725 | + {foo: 1, bar: 2, baz: 3}.reject { |k, v| %i[foo bar].include? } |
| 726 | + RUBY |
| 727 | + end |
622 | 728 | end
|
623 | 729 |
|
624 | 730 | it 'does not register an offense when using `reject` and comparing with `lvar != :key`' do
|
|
0 commit comments