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.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/{rubocop-todo.yml → .rubocop_todo.yml} +5 -5
- data/CHANGELOG.md +41 -0
- data/README.md +21 -11
- data/config/default.yml +33 -10
- data/config/disabled.yml +0 -4
- data/config/enabled.yml +27 -1
- data/lib/rubocop.rb +17 -1
- data/lib/rubocop/config.rb +32 -27
- data/lib/rubocop/config_loader.rb +21 -8
- data/lib/rubocop/cop/cop.rb +8 -8
- data/lib/rubocop/cop/lint/block_alignment.rb +22 -22
- data/lib/rubocop/cop/lint/condition_position.rb +3 -5
- data/lib/rubocop/cop/lint/debugger.rb +4 -5
- data/lib/rubocop/cop/lint/end_alignment.rb +11 -10
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +10 -11
- data/lib/rubocop/cop/lint/require_parentheses.rb +7 -7
- data/lib/rubocop/cop/lint/rescue_exception.rb +3 -3
- data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +6 -7
- data/lib/rubocop/cop/lint/unreachable_code.rb +2 -3
- data/lib/rubocop/cop/lint/unused_block_argument.rb +2 -15
- data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -14
- data/lib/rubocop/cop/lint/useless_comparison.rb +4 -5
- data/lib/rubocop/cop/lint/void.rb +6 -8
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +4 -2
- data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +1 -3
- data/lib/rubocop/cop/mixin/code_length.rb +5 -5
- data/lib/rubocop/cop/mixin/negative_conditional.rb +6 -6
- data/lib/rubocop/cop/mixin/percent_literal.rb +2 -3
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +5 -5
- data/lib/rubocop/cop/mixin/space_inside.rb +10 -11
- data/lib/rubocop/cop/mixin/unused_argument.rb +35 -0
- data/lib/rubocop/cop/offense.rb +1 -1
- data/lib/rubocop/cop/rails/action_filter.rb +6 -8
- data/lib/rubocop/cop/rails/default_scope.rb +2 -4
- data/lib/rubocop/cop/rails/delegate.rb +2 -0
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +2 -3
- data/lib/rubocop/cop/rails/output.rb +2 -3
- data/lib/rubocop/cop/rails/read_write_attribute.rb +3 -4
- data/lib/rubocop/cop/rails/validation.rb +2 -3
- data/lib/rubocop/cop/style/alias.rb +3 -3
- data/lib/rubocop/cop/style/align_hash.rb +4 -4
- data/lib/rubocop/cop/style/align_parameters.rb +13 -1
- data/lib/rubocop/cop/style/and_or.rb +2 -5
- data/lib/rubocop/cop/style/array_join.rb +3 -4
- data/lib/rubocop/cop/style/ascii_identifiers.rb +2 -3
- data/lib/rubocop/cop/style/block_comments.rb +2 -3
- data/lib/rubocop/cop/style/block_nesting.rb +2 -3
- data/lib/rubocop/cop/style/blocks.rb +9 -9
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +5 -5
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/class_methods.rb +3 -5
- data/lib/rubocop/cop/style/collection_methods.rb +8 -11
- data/lib/rubocop/cop/style/comment_annotation.rb +9 -9
- data/lib/rubocop/cop/style/comment_indentation.rb +66 -0
- data/lib/rubocop/cop/style/constant_name.rb +3 -3
- data/lib/rubocop/cop/style/cyclomatic_complexity.rb +5 -5
- data/lib/rubocop/cop/style/deprecated_hash_methods.rb +6 -6
- data/lib/rubocop/cop/style/each_with_object.rb +38 -0
- data/lib/rubocop/cop/style/empty_lines.rb +8 -8
- data/lib/rubocop/cop/style/empty_lines_around_body.rb +7 -7
- data/lib/rubocop/cop/style/encoding.rb +6 -7
- data/lib/rubocop/cop/style/end_of_line.rb +10 -10
- data/lib/rubocop/cop/style/file_name.rb +11 -7
- data/lib/rubocop/cop/style/guard_clause.rb +32 -22
- data/lib/rubocop/cop/style/indentation_width.rb +25 -26
- data/lib/rubocop/cop/style/lambda.rb +8 -9
- data/lib/rubocop/cop/style/leading_comment_space.rb +4 -5
- data/lib/rubocop/cop/style/line_end_concatenation.rb +29 -6
- data/lib/rubocop/cop/style/line_length.rb +9 -9
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +7 -6
- data/lib/rubocop/cop/style/module_function.rb +3 -4
- data/lib/rubocop/cop/style/multiline_if_then.rb +3 -3
- data/lib/rubocop/cop/style/negated_if.rb +2 -0
- data/lib/rubocop/cop/style/next.rb +80 -0
- data/lib/rubocop/cop/style/nil_comparison.rb +2 -3
- data/lib/rubocop/cop/style/non_nil_check.rb +15 -3
- data/lib/rubocop/cop/style/not.rb +4 -4
- data/lib/rubocop/cop/style/numeric_literals.rb +7 -8
- data/lib/rubocop/cop/style/op_method.rb +3 -5
- data/lib/rubocop/cop/style/parameter_lists.rb +4 -4
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +5 -6
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +5 -5
- data/lib/rubocop/cop/style/predicate_name.rb +2 -4
- data/lib/rubocop/cop/style/redundant_self.rb +11 -12
- data/lib/rubocop/cop/style/self_assignment.rb +8 -13
- data/lib/rubocop/cop/style/semicolon.rb +16 -19
- data/lib/rubocop/cop/style/signal_exception.rb +10 -11
- data/lib/rubocop/cop/style/single_line_block_params.rb +2 -3
- data/lib/rubocop/cop/style/single_line_methods.rb +3 -4
- data/lib/rubocop/cop/style/space_after_colon.rb +9 -9
- data/lib/rubocop/cop/style/space_after_control_keyword.rb +3 -3
- data/lib/rubocop/cop/style/space_after_method_name.rb +3 -3
- data/lib/rubocop/cop/style/space_after_not.rb +3 -4
- data/lib/rubocop/cop/style/space_around_operators.rb +5 -4
- data/lib/rubocop/cop/style/space_before_comment.rb +29 -0
- data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +6 -7
- data/lib/rubocop/cop/style/symbol_array.rb +2 -3
- data/lib/rubocop/cop/style/tab.rb +8 -8
- data/lib/rubocop/cop/style/trailing_blank_lines.rb +10 -10
- data/lib/rubocop/cop/style/trailing_whitespace.rb +6 -7
- data/lib/rubocop/cop/style/trivial_accessors.rb +3 -4
- data/lib/rubocop/cop/style/unless_else.rb +2 -3
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +3 -3
- data/lib/rubocop/cop/style/unneeded_percent_x.rb +26 -0
- data/lib/rubocop/cop/style/when_then.rb +3 -3
- data/lib/rubocop/cop/style/while_until_do.rb +3 -6
- data/lib/rubocop/cop/style/word_array.rb +8 -7
- data/lib/rubocop/cop/team.rb +9 -11
- data/lib/rubocop/cop/util.rb +2 -3
- data/lib/rubocop/cop/variable_force.rb +2 -3
- data/lib/rubocop/cop/variable_force/variable_table.rb +1 -2
- data/lib/rubocop/file_inspector.rb +5 -0
- data/lib/rubocop/formatter/clang_style_formatter.rb +3 -4
- data/lib/rubocop/formatter/disabled_config_formatter.rb +6 -6
- data/lib/rubocop/options.rb +14 -11
- data/lib/rubocop/target_finder.rb +12 -1
- data/lib/rubocop/version.rb +1 -1
- data/relnotes/v0.21.0.md +11 -0
- data/relnotes/v0.22.0.md +77 -0
- data/spec/rubocop/cli_spec.rb +86 -40
- data/spec/rubocop/comment_config_spec.rb +1 -1
- data/spec/rubocop/config_loader_spec.rb +3 -3
- data/spec/rubocop/config_spec.rb +14 -3
- data/spec/rubocop/cop/force_spec.rb +1 -1
- data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +164 -99
- data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +195 -85
- data/spec/rubocop/cop/lint/void_spec.rb +8 -0
- data/spec/rubocop/cop/rails/delegate_spec.rb +17 -0
- data/spec/rubocop/cop/rails/output_spec.rb +3 -3
- data/spec/rubocop/cop/severity_spec.rb +5 -5
- data/spec/rubocop/cop/style/align_parameters_spec.rb +108 -0
- data/spec/rubocop/cop/style/block_nesting_spec.rb +3 -3
- data/spec/rubocop/cop/style/class_and_module_children_spec.rb +12 -10
- data/spec/rubocop/cop/style/comment_indentation_spec.rb +174 -0
- data/spec/rubocop/cop/style/each_with_object_spec.rb +44 -0
- data/spec/rubocop/cop/style/end_of_line_spec.rb +3 -5
- data/spec/rubocop/cop/style/guard_clause_spec.rb +135 -57
- data/spec/rubocop/cop/style/if_unless_modifier_spec.rb +7 -7
- data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +75 -1
- data/spec/rubocop/cop/style/line_length_spec.rb +7 -7
- data/spec/rubocop/cop/style/negated_if_spec.rb +5 -1
- data/spec/rubocop/cop/style/next_spec.rb +210 -0
- data/spec/rubocop/cop/style/non_nil_check_spec.rb +44 -13
- data/spec/rubocop/cop/style/self_assignment_spec.rb +1 -1
- data/spec/rubocop/cop/style/space_around_operators_spec.rb +1 -0
- data/spec/rubocop/cop/style/space_before_comment_spec.rb +34 -0
- data/spec/rubocop/cop/style/unneeded_percent_x_spec.rb +34 -0
- data/spec/rubocop/cop/style/while_until_modifier_spec.rb +2 -2
- data/spec/rubocop/cop/util_spec.rb +2 -2
- data/spec/rubocop/cop/variable_force_spec.rb +1 -1
- data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +4 -4
- data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +2 -2
- data/spec/rubocop/formatter/offense_count_formatter_spec.rb +2 -2
- data/spec/rubocop/options_spec.rb +3 -0
- data/spec/support/shared_context.rb +1 -3
- data/spec/support/statement_modifier_helper.rb +2 -2
- metadata +20 -3
data/spec/rubocop/cli_spec.rb
CHANGED
@@ -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 '
|
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 '
|
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('
|
460
|
-
|
461
|
-
create_file('.rubocop.yml', ['inherit_from:
|
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('
|
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
|
512
|
-
'Run `rubocop --config
|
513
|
-
'add inherit_from:
|
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('
|
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('
|
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('
|
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
|
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:
|
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:
|
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,
|
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:
|
1063
|
-
"#{abs(target_file)}:2:
|
1064
|
-
'[90/
|
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:
|
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:
|
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 '
|
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 '
|
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
|
-
|
1179
|
+
interrupt_handlers << block
|
1153
1180
|
end
|
1154
1181
|
end
|
1155
1182
|
|
1156
1183
|
def interrupt
|
1157
|
-
|
1184
|
+
interrupt_handlers.each(&:call)
|
1158
1185
|
end
|
1159
1186
|
|
1160
1187
|
it 'adds a handler for SIGINT' do
|
1161
|
-
expect(
|
1188
|
+
expect(interrupt_handlers).to be_empty
|
1162
1189
|
cli.trap_interrupt
|
1163
|
-
expect(
|
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:
|
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:
|
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:
|
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:
|
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:
|
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:
|
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) {
|
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) {
|
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:
|
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:
|
302
|
+
' Max: 80'
|
303
303
|
])
|
304
304
|
expect(config.cop_enabled?('SymbolArray')).to be_false
|
305
305
|
end
|
data/spec/rubocop/config_spec.rb
CHANGED
@@ -107,7 +107,10 @@ describe Rubocop::Config do
|
|
107
107
|
let(:hash) do
|
108
108
|
{
|
109
109
|
'AllCops' => {
|
110
|
-
'Exclude' => [
|
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 =
|
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 =
|
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
|
@@ -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
|
-
|
9
|
-
|
10
|
-
|
8
|
+
context 'inspection' do
|
9
|
+
before do
|
10
|
+
inspect_source(cop, source)
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
33
|
+
end
|
20
34
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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 '
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
72
|
+
end
|
73
|
+
end
|
41
74
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
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 |
|
58
|
-
puts
|
117
|
+
1.times do |_index|
|
118
|
+
puts 'foo'
|
59
119
|
end
|
60
120
|
END
|
61
121
|
|
62
|
-
it '
|
63
|
-
expect(cop.offenses
|
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
|
-
|
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
|
-
|
129
|
+
def some_method(foo)
|
130
|
+
end
|
78
131
|
END
|
79
132
|
|
80
|
-
it '
|
81
|
-
expect(cop.offenses
|
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 '
|
138
|
+
context 'when a variable is not used' do
|
96
139
|
let(:source) { <<-END }
|
97
|
-
|
140
|
+
1.times do
|
141
|
+
foo = 1
|
142
|
+
end
|
98
143
|
END
|
99
144
|
|
100
|
-
it '
|
101
|
-
expect(cop.offenses
|
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 '
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
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 '
|
121
|
-
expect(cop
|
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
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
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 '
|
132
|
-
expect(cop
|
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
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
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
|
-
|
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
|