|
261 | 261 | end
|
262 | 262 | end
|
263 | 263 | end
|
| 264 | + |
| 265 | + context '`it` block parameter', :ruby34, unsupported_on: :parser do |
| 266 | + it 'registers an offense for `map { ... }.to_h`' do |
| 267 | + expect_offense(<<~RUBY) |
| 268 | + x.map { [it, it.to_sym] }.to_h |
| 269 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `index_with` over `map { ... }.to_h`. |
| 270 | + RUBY |
| 271 | + |
| 272 | + expect_correction(<<~RUBY) |
| 273 | + x.index_with { it.to_sym } |
| 274 | + RUBY |
| 275 | + end |
| 276 | + |
| 277 | + context 'when keys are transformed' do |
| 278 | + it 'does not register an offense for `map { ... }.to_h`' do |
| 279 | + expect_no_offenses(<<~RUBY) |
| 280 | + x.map { [foo(it), it.to_sym] }.to_h |
| 281 | + RUBY |
| 282 | + end |
| 283 | + end |
| 284 | + |
| 285 | + it 'registers an offense for Hash[map { ... }]' do |
| 286 | + expect_offense(<<~RUBY) |
| 287 | + Hash[x.map { [it, it.to_sym] }] |
| 288 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `index_with` over `Hash[map { ... }]`. |
| 289 | + RUBY |
| 290 | + |
| 291 | + expect_correction(<<~RUBY) |
| 292 | + x.index_with { it.to_sym } |
| 293 | + RUBY |
| 294 | + end |
| 295 | + |
| 296 | + context 'when the referenced `it` parameter is not `it`' do |
| 297 | + it 'does not register an offense for Hash[map { ... }]' do |
| 298 | + expect_no_offenses(<<~RUBY) |
| 299 | + Hash[x.map { [y, it.to_sym] }] |
| 300 | + RUBY |
| 301 | + end |
| 302 | + end |
| 303 | + |
| 304 | + it 'registers an offense for `to_h { ... }`' do |
| 305 | + expect_offense(<<~RUBY) |
| 306 | + x.to_h { [it, it.to_sym] } |
| 307 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `index_with` over `to_h { ... }`. |
| 308 | + RUBY |
| 309 | + |
| 310 | + expect_correction(<<~RUBY) |
| 311 | + x.index_with { it.to_sym } |
| 312 | + RUBY |
| 313 | + end |
| 314 | + |
| 315 | + context 'when an `it` parameter other than `it` is referenced in the value' do |
| 316 | + it 'registers an offense for `to_h { ... }`' do |
| 317 | + expect_offense(<<~RUBY) |
| 318 | + x.to_h { [it, y.to_sym] } |
| 319 | + ^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `index_with` over `to_h { ... }`. |
| 320 | + RUBY |
| 321 | + |
| 322 | + expect_correction(<<~RUBY) |
| 323 | + x.index_with { y.to_sym } |
| 324 | + RUBY |
| 325 | + end |
| 326 | + end |
| 327 | + end |
264 | 328 | end
|
265 | 329 |
|
266 | 330 | context 'when using Rails 5.2 or older', :rails52 do
|
|
0 commit comments