rubocop 0.25.0 → 0.26.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rubocop might be problematic. Click here for more details.

Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/CHANGELOG.md +37 -0
  4. data/README.md +2 -2
  5. data/assets/output.html.erb +190 -0
  6. data/config/default.yml +14 -2
  7. data/config/disabled.yml +7 -0
  8. data/config/enabled.yml +132 -5
  9. data/lib/rubocop.rb +5 -0
  10. data/lib/rubocop/cop/commissioner.rb +4 -10
  11. data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -1
  12. data/lib/rubocop/cop/lint/end_in_method.rb +3 -8
  13. data/lib/rubocop/cop/lint/ensure_return.rb +2 -2
  14. data/lib/rubocop/cop/lint/space_before_first_arg.rb +8 -1
  15. data/lib/rubocop/cop/lint/useless_assignment.rb +35 -0
  16. data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -3
  17. data/lib/rubocop/cop/metrics/block_nesting.rb +3 -3
  18. data/lib/rubocop/cop/metrics/class_length.rb +1 -2
  19. data/lib/rubocop/cop/mixin/access_modifier_node.rb +5 -1
  20. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +2 -2
  21. data/lib/rubocop/cop/mixin/configurable_naming.rb +2 -2
  22. data/lib/rubocop/cop/mixin/method_complexity.rb +2 -4
  23. data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -3
  24. data/lib/rubocop/cop/rails/delegate.rb +1 -1
  25. data/lib/rubocop/cop/rails/validation.rb +25 -2
  26. data/lib/rubocop/cop/style/alias.rb +1 -1
  27. data/lib/rubocop/cop/style/and_or.rb +12 -2
  28. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +19 -12
  29. data/lib/rubocop/cop/style/documentation.rb +1 -1
  30. data/lib/rubocop/cop/style/dot_position.rb +20 -0
  31. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +5 -1
  32. data/lib/rubocop/cop/style/encoding.rb +4 -4
  33. data/lib/rubocop/cop/style/format_string.rb +12 -2
  34. data/lib/rubocop/cop/style/if_unless_modifier.rb +8 -11
  35. data/lib/rubocop/cop/style/infinite_loop.rb +57 -0
  36. data/lib/rubocop/cop/style/multiline_block_chain.rb +15 -16
  37. data/lib/rubocop/cop/style/multiline_if_then.rb +10 -0
  38. data/lib/rubocop/cop/style/nested_ternary_operator.rb +3 -3
  39. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +3 -1
  40. data/lib/rubocop/cop/style/predicate_name.rb +23 -5
  41. data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
  42. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  43. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +4 -8
  44. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +9 -11
  45. data/lib/rubocop/cop/style/space_inside_range_literal.rb +58 -0
  46. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  47. data/lib/rubocop/cop/style/symbol_proc.rb +71 -0
  48. data/lib/rubocop/cop/style/tab.rb +11 -3
  49. data/lib/rubocop/cop/style/unneeded_capital_w.rb +6 -2
  50. data/lib/rubocop/cop/style/variable_name.rb +4 -14
  51. data/lib/rubocop/cop/style/while_until_modifier.rb +12 -8
  52. data/lib/rubocop/cop/variable_force.rb +17 -30
  53. data/lib/rubocop/cop/variable_force/assignment.rb +15 -23
  54. data/lib/rubocop/cop/variable_force/locatable.rb +29 -8
  55. data/lib/rubocop/cop/variable_force/scope.rb +34 -23
  56. data/lib/rubocop/cop/variable_force/variable.rb +7 -10
  57. data/lib/rubocop/formatter/disabled_config_formatter.rb +3 -2
  58. data/lib/rubocop/formatter/formatter_set.rb +1 -0
  59. data/lib/rubocop/formatter/fuubar_style_formatter.rb +1 -1
  60. data/lib/rubocop/formatter/html_formatter.rb +90 -0
  61. data/lib/rubocop/formatter/progress_formatter.rb +1 -1
  62. data/lib/rubocop/options.rb +1 -0
  63. data/lib/rubocop/processed_source.rb +10 -1
  64. data/lib/rubocop/string_util.rb +153 -0
  65. data/lib/rubocop/target_finder.rb +1 -1
  66. data/lib/rubocop/version.rb +1 -1
  67. data/relnotes/v0.26.0.md +89 -0
  68. data/rubocop.gemspec +1 -0
  69. data/spec/rubocop/cli_spec.rb +60 -34
  70. data/spec/rubocop/config_loader_spec.rb +19 -15
  71. data/spec/rubocop/cop/commissioner_spec.rb +2 -2
  72. data/spec/rubocop/cop/lint/block_alignment_spec.rb +74 -58
  73. data/spec/rubocop/cop/lint/space_before_first_arg_spec.rb +7 -0
  74. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +173 -0
  75. data/spec/rubocop/cop/rails/validation_spec.rb +9 -2
  76. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +26 -0
  77. data/spec/rubocop/cop/style/and_or_spec.rb +52 -61
  78. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +26 -8
  79. data/spec/rubocop/cop/style/case_indentation_spec.rb +8 -8
  80. data/spec/rubocop/cop/style/def_with_parentheses_spec.rb +6 -2
  81. data/spec/rubocop/cop/style/dot_position_spec.rb +39 -0
  82. data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +12 -2
  83. data/spec/rubocop/cop/style/encoding_spec.rb +16 -28
  84. data/spec/rubocop/cop/style/format_string_spec.rb +12 -0
  85. data/spec/rubocop/cop/style/infinite_loop_spec.rb +48 -0
  86. data/spec/rubocop/cop/style/method_def_parentheses_spec.rb +3 -1
  87. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +9 -0
  88. data/spec/rubocop/cop/style/percent_literal_delimiters_spec.rb +21 -1
  89. data/spec/rubocop/cop/style/predicate_name_spec.rb +44 -13
  90. data/spec/rubocop/cop/style/redundant_begin_spec.rb +32 -0
  91. data/spec/rubocop/cop/style/space_inside_range_literal_spec.rb +52 -0
  92. data/spec/rubocop/cop/style/symbol_proc_spec.rb +76 -0
  93. data/spec/rubocop/cop/style/tab_spec.rb +30 -0
  94. data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +2 -1
  95. data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +18 -5
  96. data/spec/rubocop/cop/style/variable_name_spec.rb +5 -5
  97. data/spec/rubocop/cop/style/when_then_spec.rb +3 -1
  98. data/spec/rubocop/cop/style/while_until_do_spec.rb +4 -2
  99. data/spec/rubocop/cop/util_spec.rb +1 -9
  100. data/spec/rubocop/cop/variable_force/assignment_spec.rb +2 -15
  101. data/spec/rubocop/cop/variable_force/locatable_spec.rb +2 -37
  102. data/spec/rubocop/cop/variable_force/scope_spec.rb +156 -49
  103. data/spec/rubocop/cop/variable_force/variable_spec.rb +2 -1
  104. data/spec/rubocop/cop/variable_force_spec.rb +2 -1
  105. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +2 -1
  106. data/spec/rubocop/formatter/html_formatter_spec.rb +145 -0
  107. data/spec/rubocop/formatter/simple_text_formatter_spec.rb +18 -6
  108. data/spec/rubocop/options_spec.rb +1 -0
  109. data/spec/rubocop/path_util_spec.rb +6 -4
  110. data/spec/rubocop/processed_source_spec.rb +17 -1
  111. data/spec/rubocop/string_util_spec.rb +46 -0
  112. metadata +33 -4
  113. data/spec/support/ast_helper.rb +0 -15
@@ -187,21 +187,25 @@ describe RuboCop::ConfigLoader do
187
187
 
188
188
  it 'returns the ancestor configuration plus local overrides' do
189
189
  config =
190
- default_config
191
- .merge('Metrics/LineLength' => {
192
- 'Description' =>
193
- default_config['Metrics/LineLength']['Description'],
194
- 'Enabled' => true,
195
- 'Max' => 77,
196
- 'AllowURI' => true
197
- },
198
- 'Metrics/MethodLength' => {
199
- 'Description' =>
200
- default_config['Metrics/MethodLength']['Description'],
201
- 'Enabled' => true,
202
- 'CountComments' => false,
203
- 'Max' => 5
204
- })
190
+ default_config.merge(
191
+ 'Metrics/LineLength' => {
192
+ 'Description' =>
193
+ default_config['Metrics/LineLength']['Description'],
194
+ 'StyleGuide' =>
195
+ 'https://github.com/bbatsov/ruby-style-guide#80-character-limits',
196
+ 'Enabled' => true,
197
+ 'Max' => 77,
198
+ 'AllowURI' => true
199
+ },
200
+ 'Metrics/MethodLength' => {
201
+ 'Description' =>
202
+ default_config['Metrics/MethodLength']['Description'],
203
+ 'StyleGuide' =>
204
+ 'https://github.com/bbatsov/ruby-style-guide#short-methods',
205
+ 'Enabled' => true,
206
+ 'CountComments' => false,
207
+ 'Max' => 5
208
+ })
205
209
  expect(configuration_from_file).to eq(config)
206
210
  end
207
211
  end
@@ -1,5 +1,4 @@
1
1
  # encoding: utf-8
2
- # rubocop:disable Metrics/LineLength
3
2
 
4
3
  require 'spec_helper'
5
4
 
@@ -44,7 +43,8 @@ describe RuboCop::Cop::Commissioner do
44
43
  commissioner.investigate(processed_source)
45
44
  end
46
45
 
47
- it 'passes the input params to all cops/forces that implement their own #investigate method' do
46
+ it 'passes the input params to all cops/forces that implement their own' \
47
+ ' #investigate method' do
48
48
  source = []
49
49
  processed_source = parse_source(source)
50
50
  expect(cop).to receive(:investigate).with(processed_source)
@@ -1,5 +1,4 @@
1
1
  # encoding: utf-8
2
- # rubocop:disable Metrics/LineLength
3
2
 
4
3
  require 'spec_helper'
5
4
 
@@ -82,7 +81,8 @@ describe RuboCop::Cop::Lint::BlockAlignment do
82
81
  ' end'
83
82
  ])
84
83
  expect(cop.messages)
85
- .to eq(['`end` at 2, 4 is not aligned with `a = b = c = test do |ala|` at 1, 0'])
84
+ .to eq(['`end` at 2, 4 is not aligned with' \
85
+ ' `a = b = c = test do |ala|` at 1, 0'])
86
86
  end
87
87
 
88
88
  it 'accepts end aligned with the first variable' do
@@ -122,7 +122,8 @@ describe RuboCop::Cop::Lint::BlockAlignment do
122
122
  ' end'
123
123
  ])
124
124
  expect(cop.messages)
125
- .to eq(['`end` at 2, 2 is not aligned with `variable = test do |ala|` at 1, 0'])
125
+ .to eq(['`end` at 2, 2 is not aligned with `variable = test do |ala|`' \
126
+ ' at 1, 0'])
126
127
  end
127
128
 
128
129
  context 'when the block is defined on the next line' do
@@ -144,8 +145,8 @@ describe RuboCop::Cop::Lint::BlockAlignment do
144
145
  'end'
145
146
  ])
146
147
  expect(cop.messages)
147
- .to eq(['`end` at 4, 0 is not aligned with `a_long_method_that_dont_fit_on_the_line ' \
148
- 'do |v|` at 2, 2'])
148
+ .to eq(['`end` at 4, 0 is not aligned with' \
149
+ ' `a_long_method_that_dont_fit_on_the_line do |v|` at 2, 2'])
149
150
  end
150
151
 
151
152
  it 'auto-corrects alignment' do
@@ -169,41 +170,43 @@ describe RuboCop::Cop::Lint::BlockAlignment do
169
170
  context 'when the method part is a call chain that spans several lines' do
170
171
  # Example from issue 346 of bbatsov/rubocop on github:
171
172
  it 'accepts pretty alignment style' do
172
- src = ['def foo(bar)',
173
- ' bar.get_stuffs',
174
- ' .reject do |stuff| ',
175
- ' stuff.with_a_very_long_expression_that_doesnt_fit_the_line',
176
- ' end.select do |stuff|',
177
- ' stuff.another_very_long_expression_that_doesnt_fit_the_line',
178
- ' end',
179
- ' .select do |stuff|',
180
- ' stuff.another_very_long_expression_that_doesnt_fit_the_line',
181
- ' end',
182
- 'end']
173
+ src = [
174
+ 'def foo(bar)',
175
+ ' bar.get_stuffs',
176
+ ' .reject do |stuff| ',
177
+ ' stuff.with_a_very_long_expression_that_doesnt_fit_the_line',
178
+ ' end.select do |stuff|',
179
+ ' stuff.another_very_long_expression_that_doesnt_fit_the_line',
180
+ ' end',
181
+ ' .select do |stuff|',
182
+ ' stuff.another_very_long_expression_that_doesnt_fit_the_line',
183
+ ' end',
184
+ 'end']
183
185
  inspect_source(cop, src)
184
186
  expect(cop.offenses).to be_empty
185
187
  end
186
188
 
187
189
  it 'registers offenses for misaligned ends' do
188
- src = ['def foo(bar)',
189
- ' bar.get_stuffs',
190
- ' .reject do |stuff|',
191
- ' stuff.with_a_very_long_expression_that_doesnt_fit_the_line',
192
- ' end.select do |stuff|',
193
- ' stuff.another_very_long_expression_that_doesnt_fit_the_line',
194
- ' end',
195
- ' .select do |stuff|',
196
- ' stuff.another_very_long_expression_that_doesnt_fit_the_line',
197
- ' end',
198
- 'end']
190
+ src = [
191
+ 'def foo(bar)',
192
+ ' bar.get_stuffs',
193
+ ' .reject do |stuff|',
194
+ ' stuff.with_a_very_long_expression_that_doesnt_fit_the_line',
195
+ ' end.select do |stuff|',
196
+ ' stuff.another_very_long_expression_that_doesnt_fit_the_line',
197
+ ' end',
198
+ ' .select do |stuff|',
199
+ ' stuff.another_very_long_expression_that_doesnt_fit_the_line',
200
+ ' end',
201
+ 'end']
199
202
  inspect_source(cop, src)
200
203
  expect(cop.messages)
201
- .to eq(['`end` at 5, 8 is not aligned with `bar.get_stuffs` at 2, 2 or' \
202
- ' `.reject do |stuff|` at 3, 6',
203
- '`end` at 7, 4 is not aligned with `bar.get_stuffs` at 2, 2 or' \
204
- ' `end.select do |stuff|` at 5, 8',
205
- '`end` at 10, 8 is not aligned with `bar.get_stuffs` at 2, 2 or' \
206
- ' `.select do |stuff|` at 8, 6'])
204
+ .to eq(['`end` at 5, 8 is not aligned with `bar.get_stuffs` at 2, 2' \
205
+ ' or `.reject do |stuff|` at 3, 6',
206
+ '`end` at 7, 4 is not aligned with `bar.get_stuffs` at 2, 2' \
207
+ ' or `end.select do |stuff|` at 5, 8',
208
+ '`end` at 10, 8 is not aligned with `bar.get_stuffs` at 2, 2' \
209
+ ' or `.select do |stuff|` at 8, 6'])
207
210
  end
208
211
 
209
212
  # Example from issue 393 of bbatsov/rubocop on github:
@@ -242,17 +245,18 @@ describe RuboCop::Cop::Lint::BlockAlignment do
242
245
  end
243
246
 
244
247
  it 'auto-corrects misaligned ends with the start of the expression' do
245
- src = ['def foo(bar)',
246
- ' bar.get_stuffs',
247
- ' .reject do |stuff|',
248
- ' stuff.with_a_very_long_expression_that_doesnt_fit_the_line',
249
- ' end.select do |stuff|',
250
- ' stuff.another_very_long_expression_that_doesnt_fit_the_line',
251
- ' end',
252
- ' .select do |stuff|',
253
- ' stuff.another_very_long_expression_that_doesnt_fit_the_line',
254
- ' end',
255
- 'end']
248
+ src = [
249
+ 'def foo(bar)',
250
+ ' bar.get_stuffs',
251
+ ' .reject do |stuff|',
252
+ ' stuff.with_a_very_long_expression_that_doesnt_fit_the_line',
253
+ ' end.select do |stuff|',
254
+ ' stuff.another_very_long_expression_that_doesnt_fit_the_line',
255
+ ' end',
256
+ ' .select do |stuff|',
257
+ ' stuff.another_very_long_expression_that_doesnt_fit_the_line',
258
+ ' end',
259
+ 'end']
256
260
 
257
261
  aligned_src = [
258
262
  'def foo(bar)',
@@ -289,7 +293,8 @@ describe RuboCop::Cop::Lint::BlockAlignment do
289
293
  ' end']
290
294
  inspect_source(cop, src)
291
295
  expect(cop.messages)
292
- .to eq(['`end` at 4, 4 is not aligned with `e,` at 1, 0 or `f = [5, 6].map do |i|` at 2, 0'])
296
+ .to eq(['`end` at 4, 4 is not aligned with `e,` at 1, 0 or' \
297
+ ' `f = [5, 6].map do |i|` at 2, 0'])
293
298
  end
294
299
 
295
300
  it 'can not auto-correct' do
@@ -311,13 +316,15 @@ describe RuboCop::Cop::Lint::BlockAlignment do
311
316
  expect(cop.offenses).to be_empty
312
317
  end
313
318
 
314
- it 'registers an offense for mismatched block end with an instance variable' do
319
+ it 'registers an offense for mismatched block end with' \
320
+ ' an instance variable' do
315
321
  inspect_source(cop,
316
322
  ['@variable = test do |ala|',
317
323
  ' end'
318
324
  ])
319
325
  expect(cop.messages)
320
- .to eq(['`end` at 2, 2 is not aligned with `@variable = test do |ala|` at 1, 0'])
326
+ .to eq(['`end` at 2, 2 is not aligned with `@variable = test do |ala|`' \
327
+ ' at 1, 0'])
321
328
  end
322
329
 
323
330
  it 'accepts end aligned with a class variable' do
@@ -334,7 +341,8 @@ describe RuboCop::Cop::Lint::BlockAlignment do
334
341
  ' end'
335
342
  ])
336
343
  expect(cop.messages)
337
- .to eq(['`end` at 2, 2 is not aligned with `@@variable = test do |ala|` at 1, 0'])
344
+ .to eq(['`end` at 2, 2 is not aligned with `@@variable = test do |ala|`' \
345
+ ' at 1, 0'])
338
346
  end
339
347
 
340
348
  it 'accepts end aligned with a global variable' do
@@ -351,7 +359,8 @@ describe RuboCop::Cop::Lint::BlockAlignment do
351
359
  ' end'
352
360
  ])
353
361
  expect(cop.messages)
354
- .to eq(['`end` at 2, 2 is not aligned with `$variable = test do |ala|` at 1, 0'])
362
+ .to eq(['`end` at 2, 2 is not aligned with `$variable = test do |ala|`' \
363
+ ' at 1, 0'])
355
364
  end
356
365
 
357
366
  it 'accepts end aligned with a constant' do
@@ -368,7 +377,8 @@ describe RuboCop::Cop::Lint::BlockAlignment do
368
377
  ' end'
369
378
  ])
370
379
  expect(cop.messages)
371
- .to eq(['`end` at 2, 2 is not aligned with `Module::CONSTANT = test do |ala|` at 1, 0'])
380
+ .to eq(['`end` at 2, 2 is not aligned with' \
381
+ ' `Module::CONSTANT = test do |ala|` at 1, 0'])
372
382
  end
373
383
 
374
384
  it 'accepts end aligned with a method call' do
@@ -387,7 +397,8 @@ describe RuboCop::Cop::Lint::BlockAlignment do
387
397
  ' end'
388
398
  ])
389
399
  expect(cop.messages)
390
- .to eq(['`end` at 3, 2 is not aligned with `parser.childs << lambda do |token|` at 1, 0'])
400
+ .to eq(['`end` at 3, 2 is not aligned with' \
401
+ ' `parser.childs << lambda do |token|` at 1, 0'])
391
402
  end
392
403
 
393
404
  it 'accepts end aligned with a method call with arguments' do
@@ -399,14 +410,16 @@ describe RuboCop::Cop::Lint::BlockAlignment do
399
410
  expect(cop.offenses).to be_empty
400
411
  end
401
412
 
402
- it 'registers an offense for mismatched end with a method call with arguments' do
413
+ it 'registers an offense for mismatched end with a method call' \
414
+ ' with arguments' do
403
415
  inspect_source(cop,
404
416
  ['@h[:f] = f.each_pair.map do |f, v|',
405
417
  ' v = 1',
406
418
  ' end'
407
419
  ])
408
420
  expect(cop.messages)
409
- .to eq(['`end` at 3, 2 is not aligned with `@h[:f] = f.each_pair.map do |f, v|` at 1, 0'])
421
+ .to eq(['`end` at 3, 2 is not aligned with' \
422
+ ' `@h[:f] = f.each_pair.map do |f, v|` at 1, 0'])
410
423
  end
411
424
 
412
425
  it 'does not raise an error for nested block in a method call' do
@@ -425,15 +438,16 @@ describe RuboCop::Cop::Lint::BlockAlignment do
425
438
  expect(cop.offenses).to be_empty
426
439
  end
427
440
 
428
- it 'registers an offense for mismatched end not aligned with the block that is an argument' do
441
+ it 'registers an offense for mismatched end not aligned with the block' \
442
+ ' that is an argument' do
429
443
  inspect_source(cop,
430
444
  ['expect(arr.all? do |o|',
431
445
  ' o.valid?',
432
446
  ' end)'
433
447
  ])
434
448
  expect(cop.messages)
435
- .to eq(['`end` at 3, 2 is not aligned with `arr.all? do |o|` at 1, 7 or ' \
436
- '`expect(arr.all? do |o|` at 1, 0'])
449
+ .to eq(['`end` at 3, 2 is not aligned with `arr.all? do |o|` at 1, 7 or' \
450
+ ' `expect(arr.all? do |o|` at 1, 0'])
437
451
  end
438
452
 
439
453
  it 'accepts end aligned with an op-asgn (+=, -=)' do
@@ -469,7 +483,8 @@ describe RuboCop::Cop::Lint::BlockAlignment do
469
483
  ' end'
470
484
  ])
471
485
  expect(cop.messages)
472
- .to eq(['`end` at 2, 2 is not aligned with `variable &&= test do |ala|` at 1, 0'])
486
+ .to eq(['`end` at 2, 2 is not aligned with `variable &&= test do |ala|`' \
487
+ ' at 1, 0'])
473
488
  end
474
489
 
475
490
  it 'accepts end aligned with an or-asgn (||=)' do
@@ -486,7 +501,8 @@ describe RuboCop::Cop::Lint::BlockAlignment do
486
501
  ' end'
487
502
  ])
488
503
  expect(cop.messages)
489
- .to eq(['`end` at 2, 2 is not aligned with `variable ||= test do |ala|` at 1, 0'])
504
+ .to eq(['`end` at 2, 2 is not aligned with `variable ||= test do |ala|`' \
505
+ ' at 1, 0'])
490
506
  end
491
507
 
492
508
  it 'accepts end aligned with a mass assignment' do
@@ -36,6 +36,13 @@ describe RuboCop::Cop::Lint::SpaceBeforeFirstArg do
36
36
  inspect_source(cop, ['a.something=c', 'a.something,b=c,d'])
37
37
  expect(cop.offenses).to be_empty
38
38
  end
39
+
40
+ it 'auto-corrects method call with no space before the first arg' do
41
+ new_source = autocorrect_source(cop, ['something?x',
42
+ 'a.something!y, z'])
43
+ expect(new_source).to eq(['something? x',
44
+ 'a.something! y, z'].join("\n"))
45
+ end
39
46
  end
40
47
 
41
48
  context 'for method calls with parentheses' do
@@ -687,6 +687,33 @@ describe RuboCop::Cop::Lint::UselessAssignment do
687
687
  end
688
688
  end
689
689
 
690
+ context 'when a variable is reassigned and unreferenced in a if branch ' \
691
+ 'while the variable is referenced in the paired else branch ' do
692
+ let(:source) do
693
+ [
694
+ 'def some_method(flag)',
695
+ ' foo = 1',
696
+ '',
697
+ ' if flag',
698
+ ' puts foo',
699
+ ' foo = 2',
700
+ ' else',
701
+ ' puts foo',
702
+ ' end',
703
+ 'end'
704
+ ]
705
+ end
706
+
707
+ it 'registers an offense for the reassignment in the if branch' do
708
+ inspect_source(cop, source)
709
+ expect(cop.offenses.size).to eq(1)
710
+ expect(cop.offenses.first.message)
711
+ .to eq('Useless assignment to variable - `foo`.')
712
+ expect(cop.offenses.first.line).to eq(6)
713
+ expect(cop.highlights).to eq(['foo'])
714
+ end
715
+ end
716
+
690
717
  context 'when a variable is assigned in branch of modifier if ' \
691
718
  'that references the variable in its conditional clause' \
692
719
  'and referenced after the branching' do
@@ -1243,6 +1270,29 @@ describe RuboCop::Cop::Lint::UselessAssignment do
1243
1270
  end
1244
1271
  end
1245
1272
 
1273
+ context 'when a rescued error variable is wrongly tried to be referenced ' \
1274
+ 'in another rescue body' do
1275
+ let(:source) do
1276
+ [
1277
+ 'begin',
1278
+ ' do_something',
1279
+ 'rescue FirstError => error',
1280
+ 'rescue SecondError',
1281
+ ' p error # => nil',
1282
+ 'end'
1283
+ ]
1284
+ end
1285
+
1286
+ it 'registers an offense' do
1287
+ inspect_source(cop, source)
1288
+ expect(cop.offenses.size).to eq(1)
1289
+ expect(cop.offenses.first.message)
1290
+ .to eq('Useless assignment to variable - `error`.')
1291
+ expect(cop.offenses.first.line).to eq(3)
1292
+ expect(cop.highlights).to eq(['error'])
1293
+ end
1294
+ end
1295
+
1246
1296
  context 'when a method argument is reassigned ' \
1247
1297
  'and zero arity super is called' do
1248
1298
  let(:source) do
@@ -1609,4 +1659,127 @@ describe RuboCop::Cop::Lint::UselessAssignment do
1609
1659
  include_examples 'accepts'
1610
1660
  include_examples 'mimics MRI 2.1'
1611
1661
  end
1662
+
1663
+ describe 'similar name suggestion' do
1664
+ context "when there's a similar variable-like method invocation" do
1665
+ let(:source) do
1666
+ [
1667
+ 'def some_method',
1668
+ ' enviromnent = {}',
1669
+ ' another_symbol',
1670
+ ' puts environment',
1671
+ 'end'
1672
+ ]
1673
+ end
1674
+
1675
+ it 'suggests the method name' do
1676
+ inspect_source(cop, source)
1677
+ expect(cop.offenses.size).to eq(1)
1678
+ expect(cop.offenses.first.message).to eq(
1679
+ 'Useless assignment to variable - `enviromnent`. ' \
1680
+ 'Did you mean `environment`?'
1681
+ )
1682
+ end
1683
+ end
1684
+
1685
+ context "when there's a similar variable" do
1686
+ let(:source) do
1687
+ [
1688
+ 'def some_method',
1689
+ ' environment = nil',
1690
+ ' another_symbol',
1691
+ ' enviromnent = {}',
1692
+ ' puts environment',
1693
+ 'end'
1694
+ ]
1695
+ end
1696
+
1697
+ it 'suggests the variable name' do
1698
+ inspect_source(cop, source)
1699
+ expect(cop.offenses.size).to eq(1)
1700
+ expect(cop.offenses.first.message).to eq(
1701
+ 'Useless assignment to variable - `enviromnent`. ' \
1702
+ 'Did you mean `environment`?'
1703
+ )
1704
+ end
1705
+ end
1706
+
1707
+ context 'when there are only less similar names' do
1708
+ let(:source) do
1709
+ [
1710
+ 'def some_method',
1711
+ ' enviromnent = {}',
1712
+ ' another_symbol',
1713
+ ' puts envelope',
1714
+ 'end'
1715
+ ]
1716
+ end
1717
+
1718
+ it 'does not suggest any name' do
1719
+ inspect_source(cop, source)
1720
+ expect(cop.offenses.size).to eq(1)
1721
+ expect(cop.offenses.first.message)
1722
+ .to eq('Useless assignment to variable - `enviromnent`.')
1723
+ end
1724
+ end
1725
+
1726
+ context "when there's a similar method invocation with explicit receiver" do
1727
+ let(:source) do
1728
+ [
1729
+ 'def some_method',
1730
+ ' enviromnent = {}',
1731
+ ' another_symbol',
1732
+ ' puts self.environment',
1733
+ 'end'
1734
+ ]
1735
+ end
1736
+
1737
+ it 'does not suggest any name' do
1738
+ inspect_source(cop, source)
1739
+ expect(cop.offenses.size).to eq(1)
1740
+ expect(cop.offenses.first.message)
1741
+ .to eq('Useless assignment to variable - `enviromnent`.')
1742
+ end
1743
+ end
1744
+
1745
+ context "when there's a similar method invocation with arguments" do
1746
+ let(:source) do
1747
+ [
1748
+ 'def some_method',
1749
+ ' enviromnent = {}',
1750
+ ' another_symbol',
1751
+ ' puts environment(1)',
1752
+ 'end'
1753
+ ]
1754
+ end
1755
+
1756
+ it 'does not suggest any name' do
1757
+ inspect_source(cop, source)
1758
+ expect(cop.offenses.size).to eq(1)
1759
+ expect(cop.offenses.first.message)
1760
+ .to eq('Useless assignment to variable - `enviromnent`.')
1761
+ end
1762
+ end
1763
+
1764
+ context "when there's a similar name but it's in inner scope" do
1765
+ let(:source) do
1766
+ [
1767
+ 'class SomeClass',
1768
+ ' enviromnent = {}',
1769
+ '',
1770
+ ' def some_method(environment)',
1771
+ ' puts environment',
1772
+ ' end',
1773
+ 'end'
1774
+ ]
1775
+ end
1776
+
1777
+ it 'does not suggest any name' do
1778
+ inspect_source(cop, source)
1779
+ expect(cop.offenses.size).to eq(1)
1780
+ expect(cop.offenses.first.message)
1781
+ .to eq('Useless assignment to variable - `enviromnent`.')
1782
+ end
1783
+ end
1784
+ end
1612
1785
  end