|
155 | 155 | RUBY |
156 | 156 | end |
157 | 157 | end |
| 158 | + |
| 159 | + context 'numbered parameters' do |
| 160 | + it 'registers an offense for `map { ... }.to_h`' do |
| 161 | + expect_offense(<<~RUBY) |
| 162 | + x.map { [_1, _1.to_sym] }.to_h |
| 163 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `index_with` over `map { ... }.to_h`. |
| 164 | + RUBY |
| 165 | + |
| 166 | + expect_correction(<<~RUBY) |
| 167 | + x.index_with { _1.to_sym } |
| 168 | + RUBY |
| 169 | + end |
| 170 | + |
| 171 | + context 'when keys are transformed' do |
| 172 | + it 'does not register an offense for `map { ... }.to_h`' do |
| 173 | + expect_no_offenses(<<~RUBY) |
| 174 | + x.map { [foo(_1), _1.to_sym] }.to_h |
| 175 | + RUBY |
| 176 | + end |
| 177 | + end |
| 178 | + |
| 179 | + it 'registers an offense for Hash[map { ... }]' do |
| 180 | + expect_offense(<<~RUBY) |
| 181 | + Hash[x.map { [_1, _1.to_sym] }] |
| 182 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `index_with` over `Hash[map { ... }]`. |
| 183 | + RUBY |
| 184 | + |
| 185 | + expect_correction(<<~RUBY) |
| 186 | + x.index_with { _1.to_sym } |
| 187 | + RUBY |
| 188 | + end |
| 189 | + |
| 190 | + context 'when the referenced numbered parameter is not _1' do |
| 191 | + it 'does not register an offense for Hash[map { ... }]' do |
| 192 | + expect_no_offenses(<<~RUBY) |
| 193 | + Hash[x.map { [_2, _1.to_sym] }] |
| 194 | + RUBY |
| 195 | + end |
| 196 | + end |
| 197 | + |
| 198 | + it 'registers an offense for `to_h { ... }`' do |
| 199 | + expect_offense(<<~RUBY) |
| 200 | + x.to_h { [_1, _1.to_sym] } |
| 201 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `index_with` over `to_h { ... }`. |
| 202 | + RUBY |
| 203 | + |
| 204 | + expect_correction(<<~RUBY) |
| 205 | + x.index_with { _1.to_sym } |
| 206 | + RUBY |
| 207 | + end |
| 208 | + |
| 209 | + context 'when a numbered parameter other than _1 is referenced in the value' do |
| 210 | + it 'does not register an offense for `to_h { ... }`' do |
| 211 | + expect_no_offenses(<<~RUBY) |
| 212 | + x.to_h { [_1, _2.to_sym] } |
| 213 | + RUBY |
| 214 | + end |
| 215 | + end |
| 216 | + end |
158 | 217 | end |
159 | 218 |
|
160 | 219 | context 'when using Rails 5.2 or older', :rails52 do |
|
0 commit comments