rubocop 0.27.1 → 0.28.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 (83) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +44 -4
  3. data/Gemfile +1 -1
  4. data/README.md +16 -1
  5. data/config/default.yml +15 -1
  6. data/config/disabled.yml +5 -0
  7. data/config/enabled.yml +10 -2
  8. data/lib/rubocop.rb +3 -0
  9. data/lib/rubocop/config.rb +1 -1
  10. data/lib/rubocop/cop/mixin/access_modifier_node.rb +2 -2
  11. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +10 -26
  12. data/lib/rubocop/cop/mixin/on_method_def.rb +1 -1
  13. data/lib/rubocop/cop/mixin/string_help.rb +10 -1
  14. data/lib/rubocop/cop/style/align_hash.rb +2 -2
  15. data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -1
  16. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +21 -19
  17. data/lib/rubocop/cop/style/else_alignment.rb +29 -16
  18. data/lib/rubocop/cop/style/empty_else.rb +47 -0
  19. data/lib/rubocop/cop/style/empty_lines_around_block_body.rb +38 -0
  20. data/lib/rubocop/cop/style/extra_spacing.rb +35 -0
  21. data/lib/rubocop/cop/style/indentation_width.rb +1 -1
  22. data/lib/rubocop/cop/style/leading_comment_space.rb +1 -1
  23. data/lib/rubocop/cop/style/line_end_concatenation.rb +5 -1
  24. data/lib/rubocop/cop/style/multiline_operation_indentation.rb +20 -4
  25. data/lib/rubocop/cop/style/negated_while.rb +3 -1
  26. data/lib/rubocop/cop/style/perl_backrefs.rb +8 -3
  27. data/lib/rubocop/cop/style/single_line_block_params.rb +7 -1
  28. data/lib/rubocop/cop/style/special_global_vars.rb +8 -3
  29. data/lib/rubocop/cop/style/string_literals.rb +4 -11
  30. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +2 -7
  31. data/lib/rubocop/cop/style/symbol_proc.rb +14 -6
  32. data/lib/rubocop/cop/style/unneeded_capital_w.rb +2 -1
  33. data/lib/rubocop/cop/util.rb +1 -1
  34. data/lib/rubocop/cop/variable_force.rb +3 -3
  35. data/lib/rubocop/cop/variable_force/scope.rb +1 -1
  36. data/lib/rubocop/cop/variable_force/variable.rb +1 -1
  37. data/lib/rubocop/formatter/html_formatter.rb +1 -1
  38. data/lib/rubocop/options.rb +13 -6
  39. data/lib/rubocop/rake_task.rb +0 -1
  40. data/lib/rubocop/runner.rb +19 -6
  41. data/lib/rubocop/target_finder.rb +32 -1
  42. data/lib/rubocop/version.rb +1 -1
  43. data/relnotes/v0.28.0.md +90 -0
  44. data/spec/project_spec.rb +1 -0
  45. data/spec/rubocop/cli_spec.rb +115 -24
  46. data/spec/rubocop/comment_config_spec.rb +1 -1
  47. data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +0 -1
  48. data/spec/rubocop/cop/lint/block_alignment_spec.rb +4 -4
  49. data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +1 -1
  50. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +2 -3
  51. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +1 -1
  52. data/spec/rubocop/cop/lint/void_spec.rb +0 -1
  53. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +2 -2
  54. data/spec/rubocop/cop/style/blocks_spec.rb +8 -0
  55. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +196 -215
  56. data/spec/rubocop/cop/style/case_indentation_spec.rb +4 -4
  57. data/spec/rubocop/cop/style/documentation_spec.rb +0 -1
  58. data/spec/rubocop/cop/style/else_alignment_spec.rb +63 -4
  59. data/spec/rubocop/cop/style/empty_else_spec.rb +100 -0
  60. data/spec/rubocop/cop/style/empty_lines_around_block_body_spec.rb +103 -0
  61. data/spec/rubocop/cop/style/empty_lines_around_method_body_spec.rb +1 -1
  62. data/spec/rubocop/cop/style/end_of_line_spec.rb +2 -2
  63. data/spec/rubocop/cop/style/extra_spacing_spec.rb +68 -0
  64. data/spec/rubocop/cop/style/leading_comment_space_spec.rb +5 -0
  65. data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +8 -0
  66. data/spec/rubocop/cop/style/multiline_operation_indentation_spec.rb +35 -2
  67. data/spec/rubocop/cop/style/negated_if_spec.rb +1 -1
  68. data/spec/rubocop/cop/style/negated_while_spec.rb +5 -3
  69. data/spec/rubocop/cop/style/percent_q_literals_spec.rb +1 -1
  70. data/spec/rubocop/cop/style/perl_backrefs_spec.rb +5 -0
  71. data/spec/rubocop/cop/style/signal_exception_spec.rb +0 -1
  72. data/spec/rubocop/cop/style/single_line_block_params_spec.rb +13 -1
  73. data/spec/rubocop/cop/style/special_global_vars_spec.rb +5 -0
  74. data/spec/rubocop/cop/style/string_literals_spec.rb +11 -1
  75. data/spec/rubocop/cop/style/symbol_proc_spec.rb +20 -1
  76. data/spec/rubocop/cop/style/tab_spec.rb +2 -2
  77. data/spec/rubocop/cop/style/trailing_comma_spec.rb +1 -1
  78. data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +5 -0
  79. data/spec/rubocop/cop/team_spec.rb +2 -2
  80. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +0 -1
  81. data/spec/rubocop/options_spec.rb +2 -0
  82. data/spec/rubocop/target_finder_spec.rb +23 -2
  83. metadata +12 -2
@@ -18,7 +18,7 @@ describe RuboCop::Cop::Style::CaseIndentation do
18
18
  end
19
19
 
20
20
  context 'regarding assignment where the right hand side is a case' do
21
- it 'accepts a correcty indented assignment' do
21
+ it 'accepts a correctly indented assignment' do
22
22
  source = ['output = case variable',
23
23
  " when 'value1'",
24
24
  " 'output1'",
@@ -156,7 +156,7 @@ describe RuboCop::Cop::Style::CaseIndentation do
156
156
  end
157
157
 
158
158
  context 'regarding assignment where the right hand side is a case' do
159
- it 'accepts a correcty indented assignment' do
159
+ it 'accepts a correctly indented assignment' do
160
160
  source = ['output = case variable',
161
161
  " when 'value1'",
162
162
  " 'output1'",
@@ -220,7 +220,7 @@ describe RuboCop::Cop::Style::CaseIndentation do
220
220
  end
221
221
 
222
222
  context 'regarding assignment where the right hand side is a case' do
223
- it 'accepts a correcty indented assignment' do
223
+ it 'accepts a correctly indented assignment' do
224
224
  source = ['output = case variable',
225
225
  "when 'value1'",
226
226
  " 'output1'",
@@ -250,7 +250,7 @@ describe RuboCop::Cop::Style::CaseIndentation do
250
250
  end
251
251
 
252
252
  context 'regarding assignment where the right hand side is a case' do
253
- it 'accepts a correcty indented assignment' do
253
+ it 'accepts a correctly indented assignment' do
254
254
  source = ['output = case variable',
255
255
  " when 'value1'",
256
256
  " 'output1'",
@@ -3,7 +3,6 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe RuboCop::Cop::Style::Documentation do
6
-
7
6
  subject(:cop) { described_class.new(config) }
8
7
  let(:config) do
9
8
  RuboCop::Config.new('Style/CommentAnnotation' => {
@@ -84,7 +84,7 @@ describe RuboCop::Cop::Style::ElseAlignment do
84
84
  context 'with assignment' do
85
85
  context 'when alignment style is variable' do
86
86
  context 'and end is aligned with variable' do
87
- it 'accepts an if with end aligned with setter' do
87
+ it 'accepts an if-else with end aligned with setter' do
88
88
  inspect_source(cop,
89
89
  ['foo.bar = if baz',
90
90
  ' derp1',
@@ -94,6 +94,18 @@ describe RuboCop::Cop::Style::ElseAlignment do
94
94
  expect(cop.offenses).to be_empty
95
95
  end
96
96
 
97
+ it 'accepts an if-elsif-else with end aligned with setter' do
98
+ inspect_source(cop,
99
+ ['foo.bar = if baz',
100
+ ' derp1',
101
+ 'elsif meh',
102
+ ' derp2',
103
+ 'else',
104
+ ' derp3',
105
+ 'end'])
106
+ expect(cop.offenses).to be_empty
107
+ end
108
+
97
109
  it 'accepts an if with end aligned with element assignment' do
98
110
  inspect_source(cop,
99
111
  ['foo[bar] = if baz',
@@ -134,14 +146,17 @@ describe RuboCop::Cop::Style::ElseAlignment do
134
146
  end
135
147
 
136
148
  context 'and end is aligned with keyword' do
137
- it 'registers an offense for an if with setter' do
149
+ it 'registers offenses for an if with setter' do
138
150
  inspect_source(cop,
139
151
  ['foo.bar = if baz',
140
152
  ' derp1',
141
- ' else',
153
+ ' elsif meh',
142
154
  ' derp2',
155
+ ' else',
156
+ ' derp3',
143
157
  ' end'])
144
- expect(cop.messages).to eq(['Align `else` with `foo.bar`.'])
158
+ expect(cop.messages).to eq(['Align `elsif` with `foo.bar`.',
159
+ 'Align `else` with `foo.bar`.'])
145
160
  end
146
161
 
147
162
  it 'registers an offense for an if with element assignment' do
@@ -431,7 +446,51 @@ describe RuboCop::Cop::Style::ElseAlignment do
431
446
  end
432
447
  end
433
448
 
449
+ context 'with def/rescue/else/ensure/end' do
450
+ it 'accepts a correctly aligned else' do
451
+ inspect_source(cop,
452
+ ['def my_func(string)',
453
+ ' puts string',
454
+ 'rescue => e',
455
+ ' puts e',
456
+ 'else',
457
+ ' puts e',
458
+ 'ensure',
459
+ " puts 'I love methods that print'",
460
+ 'end'])
461
+ expect(cop.offenses).to be_empty
462
+ end
463
+
464
+ it 'registers an offense for misaligned else' do
465
+ inspect_source(cop,
466
+ ['def my_func(string)',
467
+ ' puts string',
468
+ 'rescue => e',
469
+ ' puts e',
470
+ ' else',
471
+ ' puts e',
472
+ 'ensure',
473
+ " puts 'I love methods that print'",
474
+ 'end'])
475
+ expect(cop.messages).to eq(['Align `else` with `def`.'])
476
+ end
477
+ end
478
+
434
479
  context 'with def/rescue/else/end' do
480
+ it 'accepts a correctly aligned else' do
481
+ inspect_source(cop,
482
+ ['def my_func',
483
+ " puts 'do something error prone'",
484
+ 'rescue SomeException',
485
+ " puts 'error handling'",
486
+ 'rescue',
487
+ " puts 'error handling'",
488
+ 'else',
489
+ " puts 'normal handling'",
490
+ 'end'])
491
+ expect(cop.messages).to be_empty
492
+ end
493
+
435
494
  it 'registers an offense for misaligned else' do
436
495
  inspect_source(cop,
437
496
  ['def my_func',
@@ -0,0 +1,100 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe RuboCop::Cop::Style::EmptyElse do
6
+ subject(:cop) { described_class.new(config) }
7
+ let(:config) do
8
+ RuboCop::Config.new
9
+ end
10
+
11
+ context 'given an if-statement' do
12
+ context 'with a completely empty else-clause' do
13
+ it 'registers an offense' do
14
+ inspect_source(cop, 'if a; foo else end')
15
+ expect(cop.messages).to eq(['Redundant empty `else`-clause.'])
16
+ end
17
+ end
18
+
19
+ context 'with an else-clause containing only the literal nil' do
20
+ it 'registers an offense' do
21
+ inspect_source(cop, 'if a; foo elsif b; bar else nil end')
22
+ expect(cop.messages).to eq(['Redundant empty `else`-clause.'])
23
+ end
24
+ end
25
+
26
+ context 'with an else-clause with side-effects' do
27
+ it "doesn't register an offence" do
28
+ inspect_source(cop, 'if cond; foo else bar; nil end')
29
+ expect(cop.messages).to be_empty
30
+ end
31
+ end
32
+
33
+ context 'with no else-clause' do
34
+ it "doesn't register an offence" do
35
+ inspect_source(cop, 'if cond; foo end')
36
+ expect(cop.messages).to be_empty
37
+ end
38
+ end
39
+ end
40
+
41
+ context 'given an unless-statement' do
42
+ context 'with a completely empty else-clause' do
43
+ it 'registers an offense' do
44
+ inspect_source(cop, 'unless cond; foo else end')
45
+ expect(cop.messages).to eq(['Redundant empty `else`-clause.'])
46
+ end
47
+ end
48
+
49
+ context 'with an else-clause containing only the literal nil' do
50
+ it 'registers an offense' do
51
+ inspect_source(cop, 'unless cond; foo else nil end')
52
+ expect(cop.messages).to eq(['Redundant empty `else`-clause.'])
53
+ end
54
+ end
55
+
56
+ context 'with an else-clause with side-effects' do
57
+ it "doesn't register an offence" do
58
+ inspect_source(cop, 'unless cond; foo else bar; nil end')
59
+ expect(cop.messages).to be_empty
60
+ end
61
+ end
62
+
63
+ context 'with no else-clause' do
64
+ it "doesn't register an offence" do
65
+ inspect_source(cop, 'unless cond; foo end')
66
+ expect(cop.messages).to be_empty
67
+ end
68
+ end
69
+ end
70
+
71
+ context 'given a case statement' do
72
+ context 'with a completely empty else-clause' do
73
+ it 'registers an offense' do
74
+ inspect_source(cop, 'case v; when a; foo else end')
75
+ expect(cop.messages).to eq(['Redundant empty `else`-clause.'])
76
+ end
77
+ end
78
+
79
+ context 'with an else-clause containing only the literal nil' do
80
+ it 'registers an offense' do
81
+ inspect_source(cop, 'case v; when a; foo; when b; bar; else nil end')
82
+ expect(cop.messages).to eq(['Redundant empty `else`-clause.'])
83
+ end
84
+ end
85
+
86
+ context 'with an else-clause with side-effects' do
87
+ it "doesn't register an offence" do
88
+ inspect_source(cop, 'case v; when a; foo; else b; nil end')
89
+ expect(cop.messages).to be_empty
90
+ end
91
+ end
92
+
93
+ context 'with no else-clause' do
94
+ it "doesn't register an offence" do
95
+ inspect_source(cop, 'case v; when a; foo; when b; bar; end')
96
+ expect(cop.messages).to be_empty
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,103 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe RuboCop::Cop::Style::EmptyLinesAroundBlockBody, :config do
6
+ subject(:cop) { described_class.new(config) }
7
+
8
+ # Test blocks using both {} and do..end
9
+ [%w({ }), %w(do end)].each do |open, close|
10
+ context "when EnforcedStyle is no_empty_lines for #{open} #{close} block" do
11
+ let(:cop_config) { { 'EnforcedStyle' => 'no_empty_lines' } }
12
+
13
+ it 'registers an offense for block body starting with a blank' do
14
+ inspect_source(cop,
15
+ ["some_method #{open}",
16
+ '',
17
+ ' do_something',
18
+ "#{close}"])
19
+ expect(cop.messages)
20
+ .to eq(['Extra empty line detected at block body beginning.'])
21
+ end
22
+
23
+ it 'autocorrects block body containing only a blank' do
24
+ corrected = autocorrect_source(cop,
25
+ ["some_method #{open}",
26
+ '',
27
+ "#{close}"])
28
+ expect(corrected).to eq ["some_method #{open}",
29
+ "#{close}"].join("\n")
30
+ end
31
+
32
+ it 'registers an offense for block body ending with a blank' do
33
+ inspect_source(cop,
34
+ ["some_method #{open}",
35
+ ' do_something',
36
+ '',
37
+ "#{close}"])
38
+ expect(cop.messages)
39
+ .to eq(['Extra empty line detected at block body end.'])
40
+ end
41
+
42
+ it 'accepts block body starting with a line with spaces' do
43
+ inspect_source(cop,
44
+ ["some_method #{open}",
45
+ ' ',
46
+ ' do_something',
47
+ "#{close}"])
48
+ expect(cop.offenses).to be_empty
49
+ end
50
+
51
+ it 'is not fooled by single line blocks' do
52
+ inspect_source(cop,
53
+ ["some_method #{open} do_something #{close}",
54
+ '',
55
+ 'something_else'])
56
+ expect(cop.offenses).to be_empty
57
+ end
58
+ end
59
+
60
+ context "when EnforcedStyle is empty_lines for #{open} #{close} block" do
61
+ let(:cop_config) { { 'EnforcedStyle' => 'empty_lines' } }
62
+
63
+ it 'registers an offense for block body not starting or ending with a ' \
64
+ 'blank' do
65
+ inspect_source(cop,
66
+ ["some_method #{open}",
67
+ ' do_something',
68
+ "#{close}"])
69
+ expect(cop.messages).to eq(['Empty line missing at block body '\
70
+ 'beginning.',
71
+ 'Empty line missing at block body end.'])
72
+ end
73
+
74
+ it 'autocorrects block body containing nothing' do
75
+ corrected = autocorrect_source(cop,
76
+ ["some_method #{open}",
77
+ "#{close}"])
78
+ expect(corrected).to eq ["some_method #{open}",
79
+ '',
80
+ "#{close}"].join("\n")
81
+ end
82
+
83
+ it 'autocorrects beginning and end' do
84
+ new_source = autocorrect_source(cop,
85
+ ["some_method #{open}",
86
+ ' do_something',
87
+ "#{close}"])
88
+ expect(new_source).to eq(["some_method #{open}",
89
+ '',
90
+ ' do_something',
91
+ '',
92
+ "#{close}"].join("\n"))
93
+ end
94
+
95
+ it 'is not fooled by single line blocks' do
96
+ inspect_source(cop,
97
+ ["some_method #{open} do_something #{close}",
98
+ 'something_else'])
99
+ expect(cop.offenses).to be_empty
100
+ end
101
+ end
102
+ end
103
+ end
@@ -15,7 +15,7 @@ describe RuboCop::Cop::Style::EmptyLinesAroundMethodBody do
15
15
  .to eq(['Extra empty line detected at method body beginning.'])
16
16
  end
17
17
 
18
- # The cop only registers an offense if the extra line is completely emtpy. If
18
+ # The cop only registers an offense if the extra line is completely empty. If
19
19
  # there is trailing whitespace, then that must be dealt with first. Having
20
20
  # two cops registering offense for the line with only spaces would cause
21
21
  # havoc in auto-correction.
@@ -11,7 +11,7 @@ describe RuboCop::Cop::Style::EndOfLine do
11
11
  expect(cop.messages).to eq(['Carriage return character detected.'])
12
12
  end
13
13
 
14
- it 'highlights the whole offendng line' do
14
+ it 'highlights the whole offending line' do
15
15
  inspect_source_file(cop, ['x=0', '', "y=1\r"])
16
16
  expect(cop.highlights).to eq(["y=1\r"])
17
17
  end
@@ -56,7 +56,7 @@ describe RuboCop::Cop::Style::EndOfLine do
56
56
  end
57
57
 
58
58
  context 'when source is a string' do
59
- it 'registers an offence' do
59
+ it 'registers an offense' do
60
60
  inspect_source(cop, ["x=0\r"])
61
61
 
62
62
  expect(cop.messages).to eq(['Carriage return character detected.'])
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe RuboCop::Cop::Style::ExtraSpacing do
6
+ subject(:cop) { described_class.new }
7
+
8
+ it 'registers an offense for double extra spacing on variable assignment' do
9
+ inspect_source(cop, ['m = "hello"'])
10
+ expect(cop.offenses.size).to eq(1)
11
+ end
12
+
13
+ it 'ignores whitespace at the beginning of the line' do
14
+ inspect_source(cop, [' m = "hello"'])
15
+ expect(cop.offenses.size).to eq(0)
16
+ end
17
+
18
+ it 'ignores whitespace inside a string' do
19
+ inspect_source(cop, ['m = "hello this"'])
20
+ expect(cop.offenses.size).to eq(0)
21
+ end
22
+
23
+ it 'does not permit you to line up assignments' do
24
+ inspect_source(cop, [
25
+ 'website = "example.org"',
26
+ 'name = "Jill"'
27
+ ])
28
+ expect(cop.offenses.size).to eq(1)
29
+ end
30
+
31
+ it 'gives the correct line' do
32
+ inspect_source(cop, [
33
+ 'website = "example.org"',
34
+ 'name = "Jill"'
35
+ ])
36
+ expect(cop.offenses.first.location.line).to eq(2)
37
+ end
38
+
39
+ it 'registers an offense on class inheritance' do
40
+ inspect_source(cop, [
41
+ 'class A < String',
42
+ 'end'
43
+ ])
44
+ expect(cop.offenses.size).to eq(1)
45
+ end
46
+
47
+ it 'auto-corrects a line indented with mixed whitespace' do
48
+ new_source = autocorrect_source(cop, [
49
+ 'website = "example.org"',
50
+ 'name = "Jill"'
51
+ ])
52
+ expect(new_source).to eq([
53
+ 'website = "example.org"',
54
+ 'name = "Jill"'
55
+ ].join("\n"))
56
+ end
57
+
58
+ it 'auto-corrects the class inheritance' do
59
+ new_source = autocorrect_source(cop, [
60
+ 'class A < String',
61
+ 'end'
62
+ ])
63
+ expect(new_source).to eq([
64
+ 'class A < String',
65
+ 'end'
66
+ ].join("\n"))
67
+ end
68
+ end
@@ -57,6 +57,11 @@ describe RuboCop::Cop::Style::LeadingCommentSpace do
57
57
  expect(cop.offenses).to be_empty
58
58
  end
59
59
 
60
+ it 'accepts sprockets directives' do
61
+ inspect_source(cop, '#= require_tree .')
62
+ expect(cop.offenses).to be_empty
63
+ end
64
+
60
65
  it 'auto-corrects missing space' do
61
66
  new_source = autocorrect_source(cop, '#comment')
62
67
  expect(new_source).to eq('# comment')