rubocop 0.21.0 → 0.22.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 (159) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/{rubocop-todo.yml → .rubocop_todo.yml} +5 -5
  4. data/CHANGELOG.md +41 -0
  5. data/README.md +21 -11
  6. data/config/default.yml +33 -10
  7. data/config/disabled.yml +0 -4
  8. data/config/enabled.yml +27 -1
  9. data/lib/rubocop.rb +17 -1
  10. data/lib/rubocop/config.rb +32 -27
  11. data/lib/rubocop/config_loader.rb +21 -8
  12. data/lib/rubocop/cop/cop.rb +8 -8
  13. data/lib/rubocop/cop/lint/block_alignment.rb +22 -22
  14. data/lib/rubocop/cop/lint/condition_position.rb +3 -5
  15. data/lib/rubocop/cop/lint/debugger.rb +4 -5
  16. data/lib/rubocop/cop/lint/end_alignment.rb +11 -10
  17. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +10 -11
  18. data/lib/rubocop/cop/lint/require_parentheses.rb +7 -7
  19. data/lib/rubocop/cop/lint/rescue_exception.rb +3 -3
  20. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +6 -7
  21. data/lib/rubocop/cop/lint/unreachable_code.rb +2 -3
  22. data/lib/rubocop/cop/lint/unused_block_argument.rb +2 -15
  23. data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -14
  24. data/lib/rubocop/cop/lint/useless_comparison.rb +4 -5
  25. data/lib/rubocop/cop/lint/void.rb +6 -8
  26. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +4 -2
  27. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +1 -3
  28. data/lib/rubocop/cop/mixin/code_length.rb +5 -5
  29. data/lib/rubocop/cop/mixin/negative_conditional.rb +6 -6
  30. data/lib/rubocop/cop/mixin/percent_literal.rb +2 -3
  31. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +5 -5
  32. data/lib/rubocop/cop/mixin/space_inside.rb +10 -11
  33. data/lib/rubocop/cop/mixin/unused_argument.rb +35 -0
  34. data/lib/rubocop/cop/offense.rb +1 -1
  35. data/lib/rubocop/cop/rails/action_filter.rb +6 -8
  36. data/lib/rubocop/cop/rails/default_scope.rb +2 -4
  37. data/lib/rubocop/cop/rails/delegate.rb +2 -0
  38. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +2 -3
  39. data/lib/rubocop/cop/rails/output.rb +2 -3
  40. data/lib/rubocop/cop/rails/read_write_attribute.rb +3 -4
  41. data/lib/rubocop/cop/rails/validation.rb +2 -3
  42. data/lib/rubocop/cop/style/alias.rb +3 -3
  43. data/lib/rubocop/cop/style/align_hash.rb +4 -4
  44. data/lib/rubocop/cop/style/align_parameters.rb +13 -1
  45. data/lib/rubocop/cop/style/and_or.rb +2 -5
  46. data/lib/rubocop/cop/style/array_join.rb +3 -4
  47. data/lib/rubocop/cop/style/ascii_identifiers.rb +2 -3
  48. data/lib/rubocop/cop/style/block_comments.rb +2 -3
  49. data/lib/rubocop/cop/style/block_nesting.rb +2 -3
  50. data/lib/rubocop/cop/style/blocks.rb +9 -9
  51. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +5 -5
  52. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  53. data/lib/rubocop/cop/style/class_methods.rb +3 -5
  54. data/lib/rubocop/cop/style/collection_methods.rb +8 -11
  55. data/lib/rubocop/cop/style/comment_annotation.rb +9 -9
  56. data/lib/rubocop/cop/style/comment_indentation.rb +66 -0
  57. data/lib/rubocop/cop/style/constant_name.rb +3 -3
  58. data/lib/rubocop/cop/style/cyclomatic_complexity.rb +5 -5
  59. data/lib/rubocop/cop/style/deprecated_hash_methods.rb +6 -6
  60. data/lib/rubocop/cop/style/each_with_object.rb +38 -0
  61. data/lib/rubocop/cop/style/empty_lines.rb +8 -8
  62. data/lib/rubocop/cop/style/empty_lines_around_body.rb +7 -7
  63. data/lib/rubocop/cop/style/encoding.rb +6 -7
  64. data/lib/rubocop/cop/style/end_of_line.rb +10 -10
  65. data/lib/rubocop/cop/style/file_name.rb +11 -7
  66. data/lib/rubocop/cop/style/guard_clause.rb +32 -22
  67. data/lib/rubocop/cop/style/indentation_width.rb +25 -26
  68. data/lib/rubocop/cop/style/lambda.rb +8 -9
  69. data/lib/rubocop/cop/style/leading_comment_space.rb +4 -5
  70. data/lib/rubocop/cop/style/line_end_concatenation.rb +29 -6
  71. data/lib/rubocop/cop/style/line_length.rb +9 -9
  72. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +7 -6
  73. data/lib/rubocop/cop/style/module_function.rb +3 -4
  74. data/lib/rubocop/cop/style/multiline_if_then.rb +3 -3
  75. data/lib/rubocop/cop/style/negated_if.rb +2 -0
  76. data/lib/rubocop/cop/style/next.rb +80 -0
  77. data/lib/rubocop/cop/style/nil_comparison.rb +2 -3
  78. data/lib/rubocop/cop/style/non_nil_check.rb +15 -3
  79. data/lib/rubocop/cop/style/not.rb +4 -4
  80. data/lib/rubocop/cop/style/numeric_literals.rb +7 -8
  81. data/lib/rubocop/cop/style/op_method.rb +3 -5
  82. data/lib/rubocop/cop/style/parameter_lists.rb +4 -4
  83. data/lib/rubocop/cop/style/parentheses_around_condition.rb +5 -6
  84. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +5 -5
  85. data/lib/rubocop/cop/style/predicate_name.rb +2 -4
  86. data/lib/rubocop/cop/style/redundant_self.rb +11 -12
  87. data/lib/rubocop/cop/style/self_assignment.rb +8 -13
  88. data/lib/rubocop/cop/style/semicolon.rb +16 -19
  89. data/lib/rubocop/cop/style/signal_exception.rb +10 -11
  90. data/lib/rubocop/cop/style/single_line_block_params.rb +2 -3
  91. data/lib/rubocop/cop/style/single_line_methods.rb +3 -4
  92. data/lib/rubocop/cop/style/space_after_colon.rb +9 -9
  93. data/lib/rubocop/cop/style/space_after_control_keyword.rb +3 -3
  94. data/lib/rubocop/cop/style/space_after_method_name.rb +3 -3
  95. data/lib/rubocop/cop/style/space_after_not.rb +3 -4
  96. data/lib/rubocop/cop/style/space_around_operators.rb +5 -4
  97. data/lib/rubocop/cop/style/space_before_comment.rb +29 -0
  98. data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +6 -7
  99. data/lib/rubocop/cop/style/symbol_array.rb +2 -3
  100. data/lib/rubocop/cop/style/tab.rb +8 -8
  101. data/lib/rubocop/cop/style/trailing_blank_lines.rb +10 -10
  102. data/lib/rubocop/cop/style/trailing_whitespace.rb +6 -7
  103. data/lib/rubocop/cop/style/trivial_accessors.rb +3 -4
  104. data/lib/rubocop/cop/style/unless_else.rb +2 -3
  105. data/lib/rubocop/cop/style/unneeded_capital_w.rb +3 -3
  106. data/lib/rubocop/cop/style/unneeded_percent_x.rb +26 -0
  107. data/lib/rubocop/cop/style/when_then.rb +3 -3
  108. data/lib/rubocop/cop/style/while_until_do.rb +3 -6
  109. data/lib/rubocop/cop/style/word_array.rb +8 -7
  110. data/lib/rubocop/cop/team.rb +9 -11
  111. data/lib/rubocop/cop/util.rb +2 -3
  112. data/lib/rubocop/cop/variable_force.rb +2 -3
  113. data/lib/rubocop/cop/variable_force/variable_table.rb +1 -2
  114. data/lib/rubocop/file_inspector.rb +5 -0
  115. data/lib/rubocop/formatter/clang_style_formatter.rb +3 -4
  116. data/lib/rubocop/formatter/disabled_config_formatter.rb +6 -6
  117. data/lib/rubocop/options.rb +14 -11
  118. data/lib/rubocop/target_finder.rb +12 -1
  119. data/lib/rubocop/version.rb +1 -1
  120. data/relnotes/v0.21.0.md +11 -0
  121. data/relnotes/v0.22.0.md +77 -0
  122. data/spec/rubocop/cli_spec.rb +86 -40
  123. data/spec/rubocop/comment_config_spec.rb +1 -1
  124. data/spec/rubocop/config_loader_spec.rb +3 -3
  125. data/spec/rubocop/config_spec.rb +14 -3
  126. data/spec/rubocop/cop/force_spec.rb +1 -1
  127. data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +164 -99
  128. data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +195 -85
  129. data/spec/rubocop/cop/lint/void_spec.rb +8 -0
  130. data/spec/rubocop/cop/rails/delegate_spec.rb +17 -0
  131. data/spec/rubocop/cop/rails/output_spec.rb +3 -3
  132. data/spec/rubocop/cop/severity_spec.rb +5 -5
  133. data/spec/rubocop/cop/style/align_parameters_spec.rb +108 -0
  134. data/spec/rubocop/cop/style/block_nesting_spec.rb +3 -3
  135. data/spec/rubocop/cop/style/class_and_module_children_spec.rb +12 -10
  136. data/spec/rubocop/cop/style/comment_indentation_spec.rb +174 -0
  137. data/spec/rubocop/cop/style/each_with_object_spec.rb +44 -0
  138. data/spec/rubocop/cop/style/end_of_line_spec.rb +3 -5
  139. data/spec/rubocop/cop/style/guard_clause_spec.rb +135 -57
  140. data/spec/rubocop/cop/style/if_unless_modifier_spec.rb +7 -7
  141. data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +75 -1
  142. data/spec/rubocop/cop/style/line_length_spec.rb +7 -7
  143. data/spec/rubocop/cop/style/negated_if_spec.rb +5 -1
  144. data/spec/rubocop/cop/style/next_spec.rb +210 -0
  145. data/spec/rubocop/cop/style/non_nil_check_spec.rb +44 -13
  146. data/spec/rubocop/cop/style/self_assignment_spec.rb +1 -1
  147. data/spec/rubocop/cop/style/space_around_operators_spec.rb +1 -0
  148. data/spec/rubocop/cop/style/space_before_comment_spec.rb +34 -0
  149. data/spec/rubocop/cop/style/unneeded_percent_x_spec.rb +34 -0
  150. data/spec/rubocop/cop/style/while_until_modifier_spec.rb +2 -2
  151. data/spec/rubocop/cop/util_spec.rb +2 -2
  152. data/spec/rubocop/cop/variable_force_spec.rb +1 -1
  153. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +4 -4
  154. data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +2 -2
  155. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +2 -2
  156. data/spec/rubocop/options_spec.rb +3 -0
  157. data/spec/support/shared_context.rb +1 -3
  158. data/spec/support/statement_modifier_helper.rb +2 -2
  159. metadata +20 -3
@@ -35,6 +35,18 @@ describe Rubocop::CLI, :isolated_environment do
35
35
  end
36
36
 
37
37
  describe '--auto-correct' do
38
+ it 'honors Exclude settings in individual cops' do
39
+ source = ['# encoding: utf-8',
40
+ 'puts %x(ls)']
41
+ create_file('example.rb', source)
42
+ create_file('.rubocop.yml', ['UnneededPercentX:',
43
+ ' Exclude:',
44
+ ' - example.rb'])
45
+ expect(cli.run(['--auto-correct'])).to eq(0)
46
+ expect($stdout.string).to include('no offenses detected')
47
+ expect(IO.read('example.rb')).to eq(source.join("\n") + "\n")
48
+ end
49
+
38
50
  it 'can correct two problems with blocks' do
39
51
  # {} should be do..end and space is missing.
40
52
  create_file('example.rb', ['# encoding: utf-8',
@@ -111,10 +123,6 @@ describe Rubocop::CLI, :isolated_environment do
111
123
  'comment.',
112
124
  "#{e}:3:1: C: [Corrected] Indent access modifiers like " \
113
125
  '`private`.',
114
- "#{e}:3:1: C: Keep a blank line before and after `private`.",
115
- "#{e}:3:1: W: Useless `private` access modifier.",
116
- # An offense that moves around during auto-correction will
117
- # appear to be duplicated:
118
126
  "#{e}:3:3: C: Keep a blank line before and after `private`.",
119
127
  "#{e}:3:3: W: Useless `private` access modifier.",
120
128
  "#{e}:4:7: C: [Corrected] Use `%w` or `%W` " \
@@ -122,7 +130,6 @@ describe Rubocop::CLI, :isolated_environment do
122
130
  "#{e}:4:8: C: [Corrected] Prefer single-quoted strings " \
123
131
  "when you don't need string interpolation or special " \
124
132
  'symbols.',
125
- # Another instance of the same offense reported twice:
126
133
  "#{e}:4:15: C: [Corrected] Prefer single-quoted strings " \
127
134
  "when you don't need string interpolation or special " \
128
135
  'symbols.',
@@ -422,7 +429,7 @@ describe Rubocop::CLI, :isolated_environment do
422
429
  ''].join("\n"))
423
430
  end
424
431
 
425
- it 'should not hang SpaceAfterPunctuation and SpaceInsideParens' do
432
+ it 'does not hang SpaceAfterPunctuation and SpaceInsideParens' do
426
433
  create_file('example.rb',
427
434
  ['# encoding: utf-8',
428
435
  'some_method(a, )'])
@@ -435,7 +442,7 @@ describe Rubocop::CLI, :isolated_environment do
435
442
  ''].join("\n"))
436
443
  end
437
444
 
438
- it 'should not hang SpaceAfterPunctuation and SpaceInsideBrackets' do
445
+ it 'does not hang SpaceAfterPunctuation and SpaceInsideBrackets' do
439
446
  create_file('example.rb',
440
447
  ['# encoding: utf-8',
441
448
  'puts [1, ]'])
@@ -456,11 +463,11 @@ describe Rubocop::CLI, :isolated_environment do
456
463
  '#' * 85,
457
464
  'y ',
458
465
  'puts x'])
459
- create_file('rubocop-todo.yml', ['LineLength:',
460
- ' Enabled: false'])
461
- create_file('.rubocop.yml', ['inherit_from: rubocop-todo.yml'])
466
+ create_file('.rubocop_todo.yml', ['LineLength:',
467
+ ' Enabled: false'])
468
+ create_file('.rubocop.yml', ['inherit_from: .rubocop_todo.yml'])
462
469
  expect(cli.run(['--auto-gen-config'])).to eq(1)
463
- expect(IO.readlines('rubocop-todo.yml')[7..-1].map(&:chomp))
470
+ expect(IO.readlines('.rubocop_todo.yml')[7..-1].map(&:chomp))
464
471
  .to eq(['# Offense count: 1',
465
472
  'LineLength:',
466
473
  ' Max: 85',
@@ -508,9 +515,9 @@ describe Rubocop::CLI, :isolated_environment do
508
515
  expect(cli.run(['--auto-gen-config'])).to eq(1)
509
516
  expect($stderr.string).to eq('')
510
517
  expect($stdout.string)
511
- .to include(['Created rubocop-todo.yml.',
512
- 'Run `rubocop --config rubocop-todo.yml`, or',
513
- 'add inherit_from: rubocop-todo.yml in a ' \
518
+ .to include(['Created .rubocop_todo.yml.',
519
+ 'Run `rubocop --config .rubocop_todo.yml`, or',
520
+ 'add inherit_from: .rubocop_todo.yml in a ' \
514
521
  '.rubocop.yml file.',
515
522
  ''].join("\n"))
516
523
  expected =
@@ -523,6 +530,11 @@ describe Rubocop::CLI, :isolated_environment do
523
530
  'again.',
524
531
  '',
525
532
  '# Offense count: 1',
533
+ '# Cop supports --auto-correct.',
534
+ 'CommentIndentation:',
535
+ ' Enabled: false',
536
+ '',
537
+ '# Offense count: 1',
526
538
  '# Configuration parameters: AllowedVariables.',
527
539
  'GlobalVars:',
528
540
  ' Enabled: false',
@@ -549,7 +561,7 @@ describe Rubocop::CLI, :isolated_environment do
549
561
  '# Cop supports --auto-correct.',
550
562
  'TrailingWhitespace:',
551
563
  ' Enabled: false']
552
- actual = IO.read('rubocop-todo.yml').split($RS)
564
+ actual = IO.read('.rubocop_todo.yml').split($RS)
553
565
  expected.each_with_index do |line, ix|
554
566
  if line.is_a?(String)
555
567
  expect(actual[ix]).to eq(line)
@@ -579,13 +591,18 @@ describe Rubocop::CLI, :isolated_environment do
579
591
  '',
580
592
  '# Offense count: 1',
581
593
  '# Cop supports --auto-correct.',
594
+ 'CommentIndentation:',
595
+ ' Enabled: false',
596
+ '',
597
+ '# Offense count: 1',
598
+ '# Cop supports --auto-correct.',
582
599
  'IndentationConsistency:',
583
600
  ' Enabled: false',
584
601
  '',
585
602
  '# Offense count: 1',
586
603
  'Tab:',
587
604
  ' Enabled: false']
588
- actual = IO.read('rubocop-todo.yml').split($RS)
605
+ actual = IO.read('.rubocop_todo.yml').split($RS)
589
606
  expect(actual.length).to eq(expected.length)
590
607
  expected.each_with_index do |line, ix|
591
608
  if line.is_a?(String)
@@ -617,7 +634,7 @@ describe Rubocop::CLI, :isolated_environment do
617
634
  '# Offense count: 1',
618
635
  'RegexpLiteral:',
619
636
  ' MaxSlashes: 0']
620
- actual = IO.read('rubocop-todo.yml').split($RS)
637
+ actual = IO.read('.rubocop_todo.yml').split($RS)
621
638
  expected.each_with_index do |line, ix|
622
639
  if line.is_a?(String)
623
640
  expect(actual[ix]).to eq(line)
@@ -626,7 +643,7 @@ describe Rubocop::CLI, :isolated_environment do
626
643
  end
627
644
  end
628
645
  $stdout = StringIO.new
629
- result = cli.run(%w(--config rubocop-todo.yml --format emacs))
646
+ result = cli.run(%w(--config .rubocop_todo.yml --format emacs))
630
647
  expect($stdout.string).to eq('')
631
648
  expect(result).to eq(0)
632
649
  end
@@ -908,7 +925,7 @@ describe Rubocop::CLI, :isolated_environment do
908
925
  cli.run(['--format', 'simple', 'example.rb'])
909
926
  expect($stdout.string)
910
927
  .to include(["== #{target_file} ==",
911
- 'C: 2: 80: Line is too long. [90/79]'].join("\n"))
928
+ 'C: 2: 81: Line is too long. [90/80]'].join("\n"))
912
929
  end
913
930
  end
914
931
 
@@ -941,14 +958,18 @@ describe Rubocop::CLI, :isolated_environment do
941
958
  'example1.rb:2:5: C: Trailing whitespace detected.',
942
959
  'x= 0 ',
943
960
  ' ^',
944
- 'example1.rb:3:80: C: Line is too long. [85/79]',
961
+ 'example1.rb:3:81: C: Line is too long. [85/80]',
945
962
  '###################################################' \
946
963
  '##################################',
947
964
  ' ' \
948
- ' ^^^^^^',
965
+ ' ^^^^^',
949
966
  'example1.rb:4:2: C: Trailing whitespace detected.',
950
967
  'y ',
951
968
  ' ^',
969
+ 'example2.rb:1:1: C: Incorrect indentation detected' \
970
+ ' (column 0 instead of 1).',
971
+ '# encoding: utf-8',
972
+ '^^^^^^^^^^^^^^^^^',
952
973
  'example2.rb:2:1: C: Tab detected.',
953
974
  "\tx",
954
975
  '^',
@@ -963,6 +984,10 @@ describe Rubocop::CLI, :isolated_environment do
963
984
  'example3.rb:2:5: C: Use snake_case for methods.',
964
985
  'def badName',
965
986
  ' ^^^^^^^',
987
+ 'example3.rb:3:3: C: Use a guard clause instead of ' \
988
+ 'wrapping the code inside a conditional expression.',
989
+ ' if something',
990
+ ' ^^',
966
991
  'example3.rb:3:3: C: Favor modifier if usage ' \
967
992
  'when having a single-line body. Another good ' \
968
993
  'alternative is the usage of control flow &&/||.',
@@ -973,7 +998,7 @@ describe Rubocop::CLI, :isolated_environment do
973
998
  ' end',
974
999
  ' ^^^',
975
1000
  '',
976
- '3 files inspected, 10 offenses detected',
1001
+ '3 files inspected, 12 offenses detected',
977
1002
  ''].join("\n"))
978
1003
  end
979
1004
  end
@@ -994,6 +1019,8 @@ describe Rubocop::CLI, :isolated_environment do
994
1019
  " for operator '='.",
995
1020
  "#{abs('example1.rb')}:2:5: C: Trailing whitespace detected.",
996
1021
  "#{abs('example1.rb')}:3:2: C: Trailing whitespace detected.",
1022
+ "#{abs('example2.rb')}:1:1: C: Incorrect indentation detected" \
1023
+ ' (column 0 instead of 1).',
997
1024
  "#{abs('example2.rb')}:2:1: C: Tab detected.",
998
1025
  "#{abs('example2.rb')}:3:1: C: Inconsistent indentation " \
999
1026
  'detected.',
@@ -1059,9 +1086,9 @@ describe Rubocop::CLI, :isolated_environment do
1059
1086
  cli.run(['--format', 'simple', '--format', 'emacs', 'example.rb'])
1060
1087
  expect($stdout.string)
1061
1088
  .to include(["== #{target_file} ==",
1062
- 'C: 2: 80: Line is too long. [90/79]',
1063
- "#{abs(target_file)}:2:80: C: Line is too long. " \
1064
- '[90/79]'].join("\n"))
1089
+ 'C: 2: 81: Line is too long. [90/80]',
1090
+ "#{abs(target_file)}:2:81: C: Line is too long. " \
1091
+ '[90/80]'].join("\n"))
1065
1092
  end
1066
1093
  end
1067
1094
 
@@ -1084,13 +1111,13 @@ describe Rubocop::CLI, :isolated_environment do
1084
1111
  target_file])
1085
1112
 
1086
1113
  expect($stdout.string).to eq(["== #{target_file} ==",
1087
- 'C: 2: 80: Line is too long. [90/79]',
1114
+ 'C: 2: 81: Line is too long. [90/80]',
1088
1115
  '',
1089
1116
  '1 file inspected, 1 offense detected',
1090
1117
  ''].join("\n"))
1091
1118
 
1092
1119
  expect(File.read('emacs_output.txt'))
1093
- .to eq("#{abs(target_file)}:2:80: C: Line is too long. [90/79]\n")
1120
+ .to eq("#{abs(target_file)}:2:81: C: Line is too long. [90/80]\n")
1094
1121
  end
1095
1122
  end
1096
1123
 
@@ -1102,11 +1129,11 @@ describe Rubocop::CLI, :isolated_environment do
1102
1129
  '#' * 90])
1103
1130
  end
1104
1131
 
1105
- it 'should be failed when option is less than the severity level' do
1132
+ it 'fails when option is less than the severity level' do
1106
1133
  expect(cli.run(['--fail-level', 'convention', target_file])).to eq(1)
1107
1134
  end
1108
1135
 
1109
- it 'should be success when option is greater than the severity level' do
1136
+ it 'succeed when option is greater than the severity level' do
1110
1137
  expect(cli.run(['--fail-level', 'warning', target_file])).to eq(0)
1111
1138
  end
1112
1139
  end
@@ -1146,21 +1173,21 @@ describe Rubocop::CLI, :isolated_environment do
1146
1173
  end
1147
1174
 
1148
1175
  describe '#trap_interrupt' do
1176
+ let(:interrupt_handlers) { [] }
1149
1177
  before do
1150
- @interrupt_handlers = []
1151
1178
  allow(Signal).to receive(:trap).with('INT') do |&block|
1152
- @interrupt_handlers << block
1179
+ interrupt_handlers << block
1153
1180
  end
1154
1181
  end
1155
1182
 
1156
1183
  def interrupt
1157
- @interrupt_handlers.each(&:call)
1184
+ interrupt_handlers.each(&:call)
1158
1185
  end
1159
1186
 
1160
1187
  it 'adds a handler for SIGINT' do
1161
- expect(@interrupt_handlers).to be_empty
1188
+ expect(interrupt_handlers).to be_empty
1162
1189
  cli.trap_interrupt
1163
- expect(@interrupt_handlers.size).to eq(1)
1190
+ expect(interrupt_handlers.size).to eq(1)
1164
1191
  end
1165
1192
 
1166
1193
  context 'with SIGINT once' do
@@ -1265,7 +1292,7 @@ describe Rubocop::CLI, :isolated_environment do
1265
1292
  # all cops were disabled, then 2 were enabled again, so we
1266
1293
  # should get 2 offenses reported.
1267
1294
  expect($stdout.string)
1268
- .to eq(["#{abs('example.rb')}:8:80: C: Line is too long. [95/79]",
1295
+ .to eq(["#{abs('example.rb')}:8:81: C: Line is too long. [95/80]",
1269
1296
  "#{abs('example.rb')}:10:5: C: Prefer single-quoted " \
1270
1297
  "strings when you don't need string interpolation or " \
1271
1298
  'special symbols.',
@@ -1290,7 +1317,7 @@ describe Rubocop::CLI, :isolated_environment do
1290
1317
  # 3 cops were disabled, then 2 were enabled again, so we
1291
1318
  # should get 2 offenses reported.
1292
1319
  expect($stdout.string)
1293
- .to eq(["#{abs('example.rb')}:8:80: C: Line is too long. [95/79]",
1320
+ .to eq(["#{abs('example.rb')}:8:81: C: Line is too long. [95/80]",
1294
1321
  "#{abs('example.rb')}:10:5: C: Prefer single-quoted " \
1295
1322
  "strings when you don't need string interpolation or " \
1296
1323
  'special symbols.',
@@ -1315,7 +1342,7 @@ describe Rubocop::CLI, :isolated_environment do
1315
1342
  expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
1316
1343
  expect($stdout.string)
1317
1344
  .to eq(
1318
- ["#{abs('example.rb')}:3:80: C: Line is too long. [95/79]",
1345
+ ["#{abs('example.rb')}:3:81: C: Line is too long. [95/80]",
1319
1346
  ''].join("\n"))
1320
1347
  end
1321
1348
  end
@@ -1715,7 +1742,7 @@ describe Rubocop::CLI, :isolated_environment do
1715
1742
  expect(cli.run(%w(--format simple example))).to eq(1)
1716
1743
  expect($stdout.string).to eq(
1717
1744
  ['== example/lib/example1.rb ==',
1718
- 'C: 2: 80: Line is too long. [90/79]',
1745
+ 'C: 2: 81: Line is too long. [90/80]',
1719
1746
  '',
1720
1747
  '2 files inspected, 1 offense detected',
1721
1748
  ''].join("\n"))
@@ -1755,7 +1782,7 @@ describe Rubocop::CLI, :isolated_environment do
1755
1782
  expect(cli.run(%w(--format simple example))).to eq(1)
1756
1783
  expect($stdout.string).to eq(
1757
1784
  ['== example/tmp/test/example1.rb ==',
1758
- 'C: 2: 80: Line is too long. [90/79]',
1785
+ 'C: 2: 81: Line is too long. [90/80]',
1759
1786
  '',
1760
1787
  '1 file inspected, 1 offense detected',
1761
1788
  ''].join("\n"))
@@ -1917,7 +1944,7 @@ describe Rubocop::CLI, :isolated_environment do
1917
1944
  cli.run(%w(--format simple -c rubocop.yml))
1918
1945
  expect($stdout.string)
1919
1946
  .to eq(['== example/example1.rb ==',
1920
- 'E: 2: 80: Line is too long. [90/79]',
1947
+ 'E: 2: 81: Line is too long. [90/80]',
1921
1948
  '',
1922
1949
  '1 file inspected, 1 offense detected',
1923
1950
  ''].join("\n"))
@@ -1937,5 +1964,24 @@ describe Rubocop::CLI, :isolated_environment do
1937
1964
  'Valid severities are refactor, convention, ' \
1938
1965
  "warning, error, fatal.\n")
1939
1966
  end
1967
+
1968
+ context 'when a file inherits from the old auto generated file' do
1969
+ before do
1970
+ create_file('rubocop-todo.yml', '')
1971
+ create_file('.rubocop.yml', ['inherit_from: rubocop-todo.yml'])
1972
+ end
1973
+
1974
+ it 'prints no warning when --auto-gen-config is not set' do
1975
+ expect { cli.run(%w(-c .rubocop.yml)) }.not_to exit_with_code(1)
1976
+ end
1977
+
1978
+ it 'prints a warning when --auto-gen-config is set' do
1979
+ expect { cli.run(%w(-c .rubocop.yml --auto-gen-config)) }
1980
+ .to exit_with_code(1)
1981
+ expect($stderr.string)
1982
+ .to eq('Attention: rubocop-todo.yml has been renamed to ' \
1983
+ ".rubocop_todo.yml\n")
1984
+ end
1985
+ end
1940
1986
  end
1941
1987
  end
@@ -3,7 +3,7 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Rubocop::CommentConfig do
6
- subject(:comment_config) { Rubocop::CommentConfig.new(parse_source(source)) }
6
+ subject(:comment_config) { described_class.new(parse_source(source)) }
7
7
 
8
8
  describe '#cop_enabled_at_line?' do
9
9
  let(:source) do
@@ -5,7 +5,7 @@ require 'spec_helper'
5
5
  describe Rubocop::ConfigLoader do
6
6
  include FileHelper
7
7
 
8
- let(:default_config) { Rubocop::ConfigLoader.default_configuration }
8
+ let(:default_config) { described_class.default_configuration }
9
9
 
10
10
  describe '.configuration_file_for', :isolated_environment do
11
11
  subject(:configuration_file_for) do
@@ -214,7 +214,7 @@ describe Rubocop::ConfigLoader do
214
214
  ['MethodLength:',
215
215
  ' Enabled: false',
216
216
  ' CountComments: true',
217
- ' Max: 79'])
217
+ ' Max: 80'])
218
218
 
219
219
  create_file('special.yml',
220
220
  ['MethodLength:',
@@ -299,7 +299,7 @@ describe Rubocop::ConfigLoader do
299
299
  it 'is disabled' do
300
300
  create_file('.rubocop.yml', [
301
301
  'LineLength:',
302
- ' Max: 79'
302
+ ' Max: 80'
303
303
  ])
304
304
  expect(config.cop_enabled?('SymbolArray')).to be_false
305
305
  end
@@ -107,7 +107,10 @@ describe Rubocop::Config do
107
107
  let(:hash) do
108
108
  {
109
109
  'AllCops' => {
110
- 'Exclude' => ['/home/foo/project/log/*']
110
+ 'Exclude' => [
111
+ "#{Dir.pwd}/log/**/*",
112
+ '**/bar.rb'
113
+ ]
111
114
  }
112
115
  }
113
116
  end
@@ -116,15 +119,23 @@ describe Rubocop::Config do
116
119
 
117
120
  context 'when the passed path matches any of patterns to exclude' do
118
121
  it 'returns true' do
119
- file_path = '/home/foo/project/log/foo.rb'
122
+ file_path = "#{Dir.pwd}/log/foo.rb"
120
123
  expect(configuration.file_to_exclude?(file_path)).to be_true
124
+
125
+ expect(configuration.file_to_exclude?('log/foo.rb')).to be_true
126
+
127
+ expect(configuration.file_to_exclude?('bar.rb')).to be_true
121
128
  end
122
129
  end
123
130
 
124
131
  context 'when the passed path does not match any of patterns to exclude' do
125
132
  it 'returns false' do
126
- file_path = '/home/foo/project/log_file.rb'
133
+ file_path = "#{Dir.pwd}/log_file.rb"
127
134
  expect(configuration.file_to_exclude?(file_path)).to be_false
135
+
136
+ expect(configuration.file_to_exclude?('app/controller.rb')).to be_false
137
+
138
+ expect(configuration.file_to_exclude?('baz.rb')).to be_false
128
139
  end
129
140
  end
130
141
  end
@@ -3,7 +3,7 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Rubocop::Cop::Force do
6
- subject(:force) { Rubocop::Cop::Force.new(cops) }
6
+ subject(:force) { described_class.new(cops) }
7
7
  let(:cops) { [double('cop1'), double('cop2')] }
8
8
 
9
9
  describe '.force_name' do
@@ -5,143 +5,208 @@ require 'spec_helper'
5
5
  describe Rubocop::Cop::Lint::UnusedBlockArgument do
6
6
  subject(:cop) { described_class.new }
7
7
 
8
- before do
9
- inspect_source(cop, source)
10
- end
8
+ context 'inspection' do
9
+ before do
10
+ inspect_source(cop, source)
11
+ end
11
12
 
12
- context 'when a block takes multiple arguments' do
13
- context 'and an argument is unused' do
14
- let(:source) { <<-END }
15
- hash = { foo: 'FOO', bar: 'BAR' }
16
- hash.each do |key, value|
17
- puts key
13
+ context 'when a block takes multiple arguments' do
14
+ context 'and an argument is unused' do
15
+ let(:source) { <<-END }
16
+ hash = { foo: 'FOO', bar: 'BAR' }
17
+ hash.each do |key, value|
18
+ puts key
19
+ end
20
+ END
21
+
22
+ it 'registers an offense' do
23
+ expect(cop.offenses.size).to eq(1)
24
+ expect(cop.offenses.first.message).to eq(
25
+ 'Unused block argument - `value`. ' \
26
+ "If it's necessary, use `_` or `_value` as an argument name " \
27
+ "to indicate that it won't be used."
28
+ )
29
+ expect(cop.offenses.first.severity.name).to eq(:warning)
30
+ expect(cop.offenses.first.line).to eq(2)
31
+ expect(cop.highlights).to eq(['value'])
18
32
  end
19
- END
33
+ end
20
34
 
21
- it 'registers an offense' do
22
- expect(cop.offenses.size).to eq(1)
23
- expect(cop.offenses.first.message).to eq(
24
- 'Unused block argument - `value`. ' \
25
- "If it's necessary, use `_` or `_value` as an argument name " \
26
- "to indicate that it won't be used."
27
- )
28
- expect(cop.offenses.first.severity.name).to eq(:warning)
29
- expect(cop.offenses.first.line).to eq(2)
30
- expect(cop.highlights).to eq(['value'])
35
+ context 'and all the arguments are unused' do
36
+ let(:source) { <<-END }
37
+ hash = { foo: 'FOO', bar: 'BAR' }
38
+ hash.each do |key, value|
39
+ puts :something
40
+ end
41
+ END
42
+
43
+ it 'registers offenses and suggests omitting them' do
44
+ expect(cop.offenses.size).to eq(2)
45
+ expect(cop.offenses.first.message).to eq(
46
+ 'Unused block argument - `key`. ' \
47
+ "You can omit all the arguments if you don't care about them."
48
+ )
49
+ expect(cop.offenses.first.line).to eq(2)
50
+ expect(cop.highlights).to eq(%w(key value))
51
+ end
31
52
  end
32
53
  end
33
54
 
34
- context 'and all the arguments are unused' do
35
- let(:source) { <<-END }
36
- hash = { foo: 'FOO', bar: 'BAR' }
37
- hash.each do |key, value|
38
- puts :something
55
+ context 'when a block takes single argument' do
56
+ context 'and the argument is unused' do
57
+ let(:source) { <<-END }
58
+ 1.times do |index|
59
+ puts :something
60
+ end
61
+ END
62
+
63
+ it 'registers an offense and suggests omitting that' do
64
+ expect(cop.offenses.size).to eq(1)
65
+ expect(cop.offenses.first.message).to eq(
66
+ 'Unused block argument - `index`. ' \
67
+ "You can omit the argument if you don't care about it."
68
+ )
69
+ expect(cop.offenses.first.line).to eq(1)
70
+ expect(cop.highlights).to eq(['index'])
39
71
  end
40
- END
72
+ end
73
+ end
41
74
 
42
- it 'registers offenses and suggests omitting them' do
43
- expect(cop.offenses.size).to eq(2)
44
- expect(cop.offenses.first.message).to eq(
45
- 'Unused block argument - `key`. ' \
46
- "You can omit all the arguments if you don't care about them."
47
- )
48
- expect(cop.offenses.first.line).to eq(2)
49
- expect(cop.highlights).to eq(%w(key value))
75
+ context 'when a lambda block takes arguments' do
76
+ context 'and all the arguments are unused' do
77
+ let(:source) { <<-END }
78
+ -> (foo, bar) { do_something }
79
+ END
80
+
81
+ it 'registers offenses and suggests using a proc' do
82
+ expect(cop.offenses.size).to eq(2)
83
+ expect(cop.offenses.first.message).to eq(
84
+ 'Unused block argument - `foo`. ' \
85
+ "If it's necessary, use `_` or `_foo` as an argument name " \
86
+ "to indicate that it won't be used. " \
87
+ 'Also consider using a proc without arguments instead of a ' \
88
+ "lambda if you want it to accept any arguments but don't care " \
89
+ 'about them.'
90
+
91
+ )
92
+ expect(cop.offenses.first.line).to eq(1)
93
+ expect(cop.highlights).to eq(%w(foo bar))
94
+ end
95
+ end
96
+
97
+ context 'and an arguments is unused' do
98
+ let(:source) { <<-END }
99
+ -> (foo, bar) { puts bar }
100
+ END
101
+
102
+ it 'registers an offense' do
103
+ expect(cop.offenses.size).to eq(1)
104
+ expect(cop.offenses.first.message).to eq(
105
+ 'Unused block argument - `foo`. ' \
106
+ "If it's necessary, use `_` or `_foo` as an argument name " \
107
+ "to indicate that it won't be used."
108
+ )
109
+ expect(cop.offenses.first.line).to eq(1)
110
+ expect(cop.highlights).to eq(['foo'])
111
+ end
50
112
  end
51
113
  end
52
- end
53
114
 
54
- context 'when a block takes single argument' do
55
- context 'and the argument is unused' do
115
+ context 'when an underscore-prefixed block argument is not used' do
56
116
  let(:source) { <<-END }
57
- 1.times do |index|
58
- puts :something
117
+ 1.times do |_index|
118
+ puts 'foo'
59
119
  end
60
120
  END
61
121
 
62
- it 'registers an offense and suggests omitting that' do
63
- expect(cop.offenses.size).to eq(1)
64
- expect(cop.offenses.first.message).to eq(
65
- 'Unused block argument - `index`. ' \
66
- "You can omit the argument if you don't care about it."
67
- )
68
- expect(cop.offenses.first.line).to eq(1)
69
- expect(cop.highlights).to eq(['index'])
122
+ it 'accepts' do
123
+ expect(cop.offenses).to be_empty
70
124
  end
71
125
  end
72
- end
73
126
 
74
- context 'when a lambda block takes arguments' do
75
- context 'and all the arguments are unused' do
127
+ context 'when a method argument is not used' do
76
128
  let(:source) { <<-END }
77
- -> (foo, bar) { do_something }
129
+ def some_method(foo)
130
+ end
78
131
  END
79
132
 
80
- it 'registers offenses and suggests using a proc' do
81
- expect(cop.offenses.size).to eq(2)
82
- expect(cop.offenses.first.message).to eq(
83
- 'Unused block argument - `foo`. ' \
84
- "If it's necessary, use `_` or `_foo` as an argument name " \
85
- "to indicate that it won't be used. " \
86
- 'Also consider using a proc without arguments instead of a lambda ' \
87
- "if you want it to accept any arguments but don't care about them."
88
-
89
- )
90
- expect(cop.offenses.first.line).to eq(1)
91
- expect(cop.highlights).to eq(%w(foo bar))
133
+ it 'does not care' do
134
+ expect(cop.offenses).to be_empty
92
135
  end
93
136
  end
94
137
 
95
- context 'and an arguments is unused' do
138
+ context 'when a variable is not used' do
96
139
  let(:source) { <<-END }
97
- -> (foo, bar) { puts bar }
140
+ 1.times do
141
+ foo = 1
142
+ end
98
143
  END
99
144
 
100
- it 'registers an offense' do
101
- expect(cop.offenses.size).to eq(1)
102
- expect(cop.offenses.first.message).to eq(
103
- 'Unused block argument - `foo`. ' \
104
- "If it's necessary, use `_` or `_foo` as an argument name " \
105
- "to indicate that it won't be used."
106
- )
107
- expect(cop.offenses.first.line).to eq(1)
108
- expect(cop.highlights).to eq(['foo'])
145
+ it 'does not care' do
146
+ expect(cop.offenses).to be_empty
109
147
  end
110
148
  end
111
149
  end
112
150
 
113
- context 'when an underscore-prefixed block argument is not used' do
114
- let(:source) { <<-END }
115
- 1.times do |_index|
116
- puts 'foo'
117
- end
118
- END
151
+ context 'auto-correct' do
152
+ it 'fixes single' do
153
+ expect(autocorrect_source(cop, <<-SOURCE
154
+ arr.map { |foo| stuff }
155
+ SOURCE
156
+ )).to eq(<<-CORRECTED_SOURCE
157
+ arr.map { |_foo| stuff }
158
+ CORRECTED_SOURCE
159
+ )
160
+ end
119
161
 
120
- it 'accepts' do
121
- expect(cop.offenses).to be_empty
162
+ it 'fixes multiple' do
163
+ expect(autocorrect_source(cop, <<-SOURCE
164
+ hash.map { |key, val| stuff }
165
+ SOURCE
166
+ )).to eq(<<-CORRECTED_SOURCE
167
+ hash.map { |_key, _val| stuff }
168
+ CORRECTED_SOURCE
169
+ )
122
170
  end
123
- end
124
171
 
125
- context 'when a method argument is not used' do
126
- let(:source) { <<-END }
127
- def some_method(foo)
128
- end
129
- END
172
+ it 'preserves whitespace' do
173
+ expect(autocorrect_source(cop, <<-SOURCE
174
+ hash.map { |key,
175
+ val| stuff }
176
+ SOURCE
177
+ )).to eq(<<-CORRECTED_SOURCE
178
+ hash.map { |_key,
179
+ _val| stuff }
180
+ CORRECTED_SOURCE
181
+ )
182
+ end
130
183
 
131
- it 'does not care' do
132
- expect(cop.offenses).to be_empty
184
+ it 'preserves splat' do
185
+ expect(autocorrect_source(cop, <<-SOURCE
186
+ obj.method { |foo, *bars, baz| stuff(foo, baz) }
187
+ SOURCE
188
+ )).to eq(<<-CORRECTED_SOURCE
189
+ obj.method { |foo, *_bars, baz| stuff(foo, baz) }
190
+ CORRECTED_SOURCE
191
+ )
133
192
  end
134
- end
135
193
 
136
- context 'when a variable is not used' do
137
- let(:source) { <<-END }
138
- 1.times do
139
- foo = 1
140
- end
141
- END
194
+ it 'preserves default' do
195
+ expect(autocorrect_source(cop, <<-SOURCE
196
+ obj.method { |foo, bar = baz| stuff(foo) }
197
+ SOURCE
198
+ )).to eq(<<-CORRECTED_SOURCE
199
+ obj.method { |foo, _bar = baz| stuff(foo) }
200
+ CORRECTED_SOURCE
201
+ )
202
+ end
203
+
204
+ it 'ignores used' do
205
+ original_source = <<-SOURCE
206
+ obj.method { |foo, baz| stuff(foo, baz) }
207
+ SOURCE
142
208
 
143
- it 'does not care' do
144
- expect(cop.offenses).to be_empty
209
+ expect(autocorrect_source(cop, original_source)).to eq(original_source)
145
210
  end
146
211
  end
147
212
  end