rubocop 0.10.0 → 0.11.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 (79) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +35 -0
  3. data/CONTRIBUTING.md +2 -0
  4. data/README.md +102 -5
  5. data/config/default.yml +31 -3
  6. data/config/enabled.yml +21 -3
  7. data/lib/rubocop.rb +5 -0
  8. data/lib/rubocop/cli.rb +27 -7
  9. data/lib/rubocop/config.rb +21 -2
  10. data/lib/rubocop/config_store.rb +4 -1
  11. data/lib/rubocop/cop/commissioner.rb +2 -4
  12. data/lib/rubocop/cop/cop.rb +8 -8
  13. data/lib/rubocop/cop/lint/assignment_in_condition.rb +3 -0
  14. data/lib/rubocop/cop/lint/block_alignment.rb +10 -20
  15. data/lib/rubocop/cop/lint/useless_assignment.rb +63 -0
  16. data/lib/rubocop/cop/lint/useless_comparison.rb +30 -0
  17. data/lib/rubocop/cop/style/align_parameters.rb +23 -13
  18. data/lib/rubocop/cop/style/and_or.rb +13 -1
  19. data/lib/rubocop/cop/style/blocks.rb +35 -0
  20. data/lib/rubocop/cop/style/character_literal.rb +1 -1
  21. data/lib/rubocop/cop/style/comment_annotation.rb +20 -5
  22. data/lib/rubocop/cop/style/dot_position.rb +7 -1
  23. data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -1
  24. data/lib/rubocop/cop/style/favor_modifier.rb +4 -4
  25. data/lib/rubocop/cop/style/module_function.rb +34 -0
  26. data/lib/rubocop/cop/style/multiline_if_then.rb +7 -9
  27. data/lib/rubocop/cop/style/redundant_begin.rb +7 -7
  28. data/lib/rubocop/cop/style/redundant_return.rb +9 -11
  29. data/lib/rubocop/cop/style/redundant_self.rb +5 -1
  30. data/lib/rubocop/cop/style/regexp_literal.rb +2 -1
  31. data/lib/rubocop/cop/style/signal_exception.rb +40 -0
  32. data/lib/rubocop/cop/style/string_literals.rb +2 -2
  33. data/lib/rubocop/cop/style/symbol_name.rb +11 -0
  34. data/lib/rubocop/cop/style/trivial_accessors.rb +22 -10
  35. data/lib/rubocop/cop/variable_inspector.rb +92 -71
  36. data/lib/rubocop/formatter/clang_style_formatter.rb +8 -3
  37. data/lib/rubocop/formatter/disabled_config_formatter.rb +32 -0
  38. data/lib/rubocop/formatter/formatter_set.rb +7 -4
  39. data/lib/rubocop/target_finder.rb +3 -4
  40. data/lib/rubocop/version.rb +1 -1
  41. data/rubocop.gemspec +1 -1
  42. data/spec/rubocop/cli_spec.rb +90 -1
  43. data/spec/rubocop/cops/commissioner_spec.rb +1 -1
  44. data/spec/rubocop/cops/lint/assignment_in_condition_spec.rb +6 -0
  45. data/spec/rubocop/cops/lint/block_alignment_spec.rb +16 -4
  46. data/spec/rubocop/cops/lint/empty_ensure_spec.rb +1 -1
  47. data/spec/rubocop/cops/lint/ensure_return_spec.rb +1 -1
  48. data/spec/rubocop/cops/lint/shadowing_outer_local_variable_spec.rb +4 -4
  49. data/spec/rubocop/cops/lint/unused_local_variable_spec.rb +49 -13
  50. data/spec/rubocop/cops/lint/useless_assignment_spec.rb +62 -0
  51. data/spec/rubocop/cops/lint/useless_comparison_spec.rb +31 -0
  52. data/spec/rubocop/cops/style/align_parameters_spec.rb +9 -0
  53. data/spec/rubocop/cops/style/and_or_spec.rb +12 -0
  54. data/spec/rubocop/cops/style/avoid_global_vars_spec.rb +1 -1
  55. data/spec/rubocop/cops/style/blocks_spec.rb +57 -14
  56. data/spec/rubocop/cops/style/character_literal_spec.rb +2 -2
  57. data/spec/rubocop/cops/style/comment_annotation_spec.rb +32 -4
  58. data/spec/rubocop/cops/style/dot_position_spec.rb +10 -0
  59. data/spec/rubocop/cops/style/empty_line_between_defs_spec.rb +12 -0
  60. data/spec/rubocop/cops/style/end_of_line_spec.rb +1 -0
  61. data/spec/rubocop/cops/style/favor_modifier_spec.rb +18 -0
  62. data/spec/rubocop/cops/style/hash_syntax_spec.rb +7 -2
  63. data/spec/rubocop/cops/style/module_function_spec.rb +30 -0
  64. data/spec/rubocop/cops/style/redundant_begin_spec.rb +2 -2
  65. data/spec/rubocop/cops/style/redundant_return_spec.rb +4 -4
  66. data/spec/rubocop/cops/style/redundant_self_spec.rb +36 -2
  67. data/spec/rubocop/cops/style/regexp_literal_spec.rb +1 -0
  68. data/spec/rubocop/cops/style/signal_exception_spec.rb +74 -0
  69. data/spec/rubocop/cops/style/string_literals_spec.rb +10 -0
  70. data/spec/rubocop/cops/style/symbol_name_spec.rb +13 -0
  71. data/spec/rubocop/cops/style/trivial_accessors_spec.rb +28 -3
  72. data/spec/rubocop/cops/variable_inspector_spec.rb +217 -36
  73. data/spec/rubocop/formatter/base_formatter_spec.rb +3 -3
  74. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +19 -0
  75. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +48 -0
  76. data/spec/rubocop/formatter/formatter_set_spec.rb +1 -1
  77. data/spec/rubocop/processed_source_spec.rb +1 -1
  78. data/spec/spec_helper.rb +18 -13
  79. metadata +31 -38
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ module Lint
8
+ describe UselessAssignment do
9
+ let(:cop) { UselessAssignment.new }
10
+
11
+ it 'registers an offence for def ending with lvar assignment' do
12
+ inspect_source(cop,
13
+ ['def test',
14
+ ' something',
15
+ ' top = 5',
16
+ 'end'
17
+ ])
18
+ expect(cop.offences.size).to eq(1)
19
+ end
20
+
21
+ it 'registers an offence for defs ending with lvar assignment' do
22
+ inspect_source(cop,
23
+ ['def Top.test',
24
+ ' something',
25
+ ' top = 5',
26
+ 'end'
27
+ ])
28
+ expect(cop.offences.size).to eq(1)
29
+ end
30
+
31
+ it 'registers an offence for def ending with lvar attr assignment' do
32
+ inspect_source(cop,
33
+ ['def test',
34
+ ' top = Top.new',
35
+ ' top.attr = 5',
36
+ 'end'
37
+ ])
38
+ expect(cop.offences.size).to eq(1)
39
+ end
40
+
41
+ it 'registers an offence for defs ending with lvar attr assignment' do
42
+ inspect_source(cop,
43
+ ['def Top.test',
44
+ ' top = Top.new',
45
+ ' top.attr = 5',
46
+ 'end'
47
+ ])
48
+ expect(cop.offences.size).to eq(1)
49
+ end
50
+
51
+ it 'is not confused by operators ending with =' do
52
+ inspect_source(cop,
53
+ ['def test',
54
+ ' top == 5',
55
+ 'end'
56
+ ])
57
+ expect(cop.offences).to be_empty
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ module Lint
8
+ describe UselessComparison do
9
+ let(:cop) { UselessComparison.new }
10
+
11
+ UselessComparison::OPS.each do |op|
12
+ it "registers an offence for a simple comparison with #{op}" do
13
+ inspect_source(cop,
14
+ ["5 #{op} 5",
15
+ "a #{op} a"
16
+ ])
17
+ expect(cop.offences.size).to eq(2)
18
+ end
19
+
20
+ it "registers an offence for a complex comparison with #{op}" do
21
+ inspect_source(cop,
22
+ ["5 + 10 * 30 #{op} 5 + 10 * 30",
23
+ "a.top(x) #{op} a.top(x)"
24
+ ])
25
+ expect(cop.offences.size).to eq(2)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -193,6 +193,15 @@ module Rubocop
193
193
  inspect_source(align, src)
194
194
  expect(align.offences).to be_empty
195
195
  end
196
+
197
+ it 'auto-corrects alignment' do
198
+ new_source = autocorrect_source(align, ['func(a,',
199
+ ' b,',
200
+ 'c)'])
201
+ expect(new_source.split("\n")).to eq(['func(a,',
202
+ ' b,',
203
+ ' c)'])
204
+ end
196
205
  end
197
206
  end
198
207
  end
@@ -43,6 +43,18 @@ module Rubocop
43
43
  new_source = autocorrect_source(cop, 'true or false')
44
44
  expect(new_source).to eq('true || false')
45
45
  end
46
+
47
+ it 'leaves *or* as is if auto-correction changes the meaning' do
48
+ src = "teststring.include? 'a' or teststring.include? 'b'"
49
+ new_source = autocorrect_source(cop, src)
50
+ expect(new_source).to eq(src)
51
+ end
52
+
53
+ it 'leaves *and* as is if auto-correction changes the meaning' do
54
+ src = 'x = a + b and return x'
55
+ new_source = autocorrect_source(cop, src)
56
+ expect(new_source).to eq(src)
57
+ end
46
58
  end
47
59
  end
48
60
  end
@@ -10,7 +10,7 @@ module Rubocop
10
10
 
11
11
  it 'registers an offence for $custom' do
12
12
  inspect_source(cop, ['puts $custom'])
13
- expect(cop.offences).to have(1).item
13
+ expect(cop.offences.size).to eq(1)
14
14
  end
15
15
 
16
16
  AvoidGlobalVars::BUILT_IN_VARS.each do |var|
@@ -6,28 +6,71 @@ module Rubocop
6
6
  module Cop
7
7
  module Style
8
8
  describe Blocks do
9
- let(:blocks) { Blocks.new }
10
-
11
- it 'registers an offence for a multiline block with braces' do
12
- inspect_source(blocks, ['each { |x|',
13
- '}'])
14
- expect(blocks.messages).to eq([Blocks::MULTI_LINE_MSG])
15
- end
9
+ let(:cop) { Blocks.new }
16
10
 
17
11
  it 'accepts a multiline block with do-end' do
18
- inspect_source(blocks, ['each do |x|',
19
- 'end'])
20
- expect(blocks.offences.map(&:message)).to be_empty
12
+ inspect_source(cop, ['each do |x|',
13
+ 'end'])
14
+ expect(cop.offences).to be_empty
21
15
  end
22
16
 
23
17
  it 'registers an offence for a single line block with do-end' do
24
- inspect_source(blocks, ['each do |x| end'])
25
- expect(blocks.messages).to eq([Blocks::SINGLE_LINE_MSG])
18
+ inspect_source(cop, ['each do |x| end'])
19
+ expect(cop.messages).to eq([Blocks::SINGLE_LINE_MSG])
26
20
  end
27
21
 
28
22
  it 'accepts a single line block with braces' do
29
- inspect_source(blocks, ['each { |x| }'])
30
- expect(blocks.offences.map(&:message)).to be_empty
23
+ inspect_source(cop, ['each { |x| }'])
24
+ expect(cop.offences).to be_empty
25
+ end
26
+
27
+ context 'when there are braces around a multi-line block' do
28
+ it 'registers an offence in the simple case' do
29
+ inspect_source(cop, ['each { |x|',
30
+ '}'])
31
+ expect(cop.messages).to eq([Blocks::MULTI_LINE_MSG])
32
+ end
33
+
34
+ it 'accepts braces if do-end would change the meaning' do
35
+ src = ['scope :foo, lambda { |f|',
36
+ ' where(condition: "value")',
37
+ '}',
38
+ '',
39
+ 'expect { something }.to raise_error(ErrorClass) { |error|',
40
+ ' # ...',
41
+ '}',
42
+ '',
43
+ 'expect { x }.to change {',
44
+ ' Counter.count',
45
+ '}.from(0).to(1)']
46
+ inspect_source(cop, src)
47
+ expect(cop.offences).to be_empty
48
+ end
49
+
50
+ it 'registers an offence for braces if do-end would not change ' +
51
+ 'the meaning' do
52
+ src = ['scope :foo, (lambda { |f|',
53
+ ' where(condition: "value")',
54
+ '})',
55
+ '',
56
+ 'expect { something }.to(raise_error(ErrorClass) { |error|',
57
+ ' # ...',
58
+ '})']
59
+ inspect_source(cop, src)
60
+ expect(cop.offences.size).to eq(2)
61
+ end
62
+
63
+ it 'can handle special method names such as []= and done?' do
64
+ src = ['h2[k2] = Hash.new { |h3,k3|',
65
+ ' h3[k3] = 0',
66
+ '}',
67
+ '',
68
+ 'x = done? list.reject { |e|',
69
+ ' e.nil?',
70
+ '}']
71
+ inspect_source(cop, src)
72
+ expect(cop.messages).to eq([Blocks::MULTI_LINE_MSG])
73
+ end
31
74
  end
32
75
  end
33
76
  end
@@ -10,12 +10,12 @@ module Rubocop
10
10
 
11
11
  it 'registers an offence for character literals' do
12
12
  inspect_source(cop, ['x = ?x'])
13
- expect(cop.offences).to have(1).item
13
+ expect(cop.offences.size).to eq(1)
14
14
  end
15
15
 
16
16
  it 'registers an offence for literals like \n' do
17
17
  inspect_source(cop, ['x = ?\n'])
18
- expect(cop.offences).to have(1).item
18
+ expect(cop.offences.size).to eq(1)
19
19
  end
20
20
 
21
21
  it 'accepts literals like ?\C-\M-d' do
@@ -6,11 +6,24 @@ module Rubocop
6
6
  module Cop
7
7
  module Style
8
8
  describe CommentAnnotation do
9
+ before do
10
+ CommentAnnotation.config = {
11
+ 'Keywords' => %w(TODO FIXME OPTIMIZE HACK REVIEW)
12
+ }
13
+ end
9
14
  let(:cop) { CommentAnnotation.new }
10
15
 
11
16
  it 'registers an offence for a missing colon' do
12
17
  inspect_source(cop, ['# TODO make better'])
13
- expect(cop.offences).to have(1).item
18
+ expect(cop.offences.size).to eq(1)
19
+ end
20
+
21
+ it 'registers an offence for a missing colon after configured word' do
22
+ CommentAnnotation.config = {
23
+ 'Keywords' => %w(ISSUE)
24
+ }
25
+ inspect_source(cop, ['# ISSUE wrong order'])
26
+ expect(cop.offences.size).to eq(1)
14
27
  end
15
28
 
16
29
  context 'when used with the clang formatter' do
@@ -29,17 +42,17 @@ module Rubocop
29
42
 
30
43
  it 'registers an offence for lower case' do
31
44
  inspect_source(cop, ['# fixme: does not work'])
32
- expect(cop.offences).to have(1).item
45
+ expect(cop.offences.size).to eq(1)
33
46
  end
34
47
 
35
48
  it 'registers an offence for capitalized annotation keyword' do
36
49
  inspect_source(cop, ['# Optimize: does not work'])
37
- expect(cop.offences).to have(1).item
50
+ expect(cop.offences.size).to eq(1)
38
51
  end
39
52
 
40
53
  it 'registers an offence for upper case with colon but no note' do
41
54
  inspect_source(cop, ['# HACK:'])
42
- expect(cop.offences).to have(1).item
55
+ expect(cop.offences.size).to eq(1)
43
56
  end
44
57
 
45
58
  it 'accepts upper case keyword with colon, space and note' do
@@ -56,6 +69,21 @@ module Rubocop
56
69
  inspect_source(cop, ['# Todo.destroy(1)'])
57
70
  expect(cop.offences).to be_empty
58
71
  end
72
+
73
+ it 'accepts a keyword that is just the beginning of a sentence' do
74
+ inspect_source(cop,
75
+ ["# Optimize if you want. I wouldn't recommend it.",
76
+ '# Hack is a fun game.'])
77
+ expect(cop.offences).to be_empty
78
+ end
79
+
80
+ it 'accepts a word without colon if not in the configuration' do
81
+ CommentAnnotation.config = {
82
+ 'Keywords' => %w(FIXME OPTIMIZE HACK REVIEW)
83
+ }
84
+ inspect_source(cop, ['# TODO make better'])
85
+ expect(cop.offences).to be_empty
86
+ end
59
87
  end
60
88
  end
61
89
  end
@@ -27,6 +27,11 @@ module Rubocop
27
27
  inspect_source(cop, ['puts something'])
28
28
  expect(cop.offences).to be_empty
29
29
  end
30
+
31
+ it 'does not err on method call without a method name' do
32
+ inspect_source(cop, ['l.', '(1)'])
33
+ expect(cop.offences.size).to eq(1)
34
+ end
30
35
  end
31
36
 
32
37
  context 'Trailing dots style' do
@@ -48,6 +53,11 @@ module Rubocop
48
53
  inspect_source(cop, ['puts something'])
49
54
  expect(cop.offences).to be_empty
50
55
  end
56
+
57
+ it 'does not err on method call without a method name' do
58
+ inspect_source(cop, ['l', '.(1)'])
59
+ expect(cop.offences.size).to eq(1)
60
+ end
51
61
  end
52
62
 
53
63
  context 'Unknown style' do
@@ -79,6 +79,18 @@ module Rubocop
79
79
  inspect_source(empty_lines, source)
80
80
  expect(empty_lines.offences.map(&:message)).to be_empty
81
81
  end
82
+
83
+ it 'accepts a nested def' do
84
+ source = ['def mock_model(*attributes)',
85
+ ' Class.new do',
86
+ ' def initialize(attrs)',
87
+ ' end',
88
+ ' end',
89
+ 'end',
90
+ ]
91
+ inspect_source(empty_lines, source)
92
+ expect(empty_lines.offences.map(&:message)).to be_empty
93
+ end
82
94
  end
83
95
  end
84
96
  end
@@ -9,6 +9,7 @@ module Rubocop
9
9
  let(:eol) { EndOfLine.new }
10
10
 
11
11
  it 'registers an offence for CR+LF' do
12
+ pending 'Fails after upgdate to parser-2.0.0.pre3.'
12
13
  inspect_source(eol, ["x=0\r", ''])
13
14
  expect(eol.offences.map(&:message)).to eq(
14
15
  ['Carriage return character detected.'])
@@ -27,6 +27,24 @@ module Rubocop
27
27
  ' &&/||.'])
28
28
  end
29
29
 
30
+ it 'registers an offence for short multiline if near an else etc' do
31
+ inspect_source(if_unless,
32
+ ['if x',
33
+ ' y',
34
+ 'elsif x1',
35
+ ' y1',
36
+ 'else',
37
+ ' z',
38
+ 'end',
39
+ 'n = a ? 0 : 1',
40
+ 'm = 3 if m0',
41
+ '',
42
+ 'if a',
43
+ ' b',
44
+ 'end'])
45
+ expect(if_unless.offences.size).to eq(1)
46
+ end
47
+
30
48
  it "accepts multiline if that doesn't fit on one line" do
31
49
  check_too_long(if_unless, 'if')
32
50
  end
@@ -31,8 +31,13 @@ module Rubocop
31
31
  expect(hash_syntax.offences.map(&:message)).to be_empty
32
32
  end
33
33
 
34
- it 'accepts hash rockets when keys special symbols in them' do
35
- inspect_source(hash_syntax, ['x = { :"t o" => 0, :"\xab" => 1 }'])
34
+ it 'accepts hash rockets when keys have whitespaces in them' do
35
+ inspect_source(hash_syntax, ['x = { :"t o" => 0 }'])
36
+ expect(hash_syntax.offences.map(&:message)).to be_empty
37
+ end
38
+
39
+ it 'accepts hash rockets when keys have special symbols in them' do
40
+ inspect_source(hash_syntax, ['x = { :"\tab" => 1 }'])
36
41
  expect(hash_syntax.offences.map(&:message)).to be_empty
37
42
  end
38
43
 
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ module Style
8
+ describe ModuleFunction do
9
+ let(:cop) { described_class.new }
10
+
11
+ it 'registers an offence for extend self in module' do
12
+ inspect_source(cop,
13
+ ['module Test',
14
+ ' extend self',
15
+ ' def test; end',
16
+ 'end'])
17
+ expect(cop.offences.size).to eq(1)
18
+ end
19
+
20
+ it 'accepts extend self in class' do
21
+ inspect_source(cop,
22
+ ['class Test',
23
+ ' extend self',
24
+ 'end'])
25
+ expect(cop.offences).to be_empty
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -17,7 +17,7 @@ module Rubocop
17
17
  ' end',
18
18
  'end']
19
19
  inspect_source(cop, src)
20
- expect(cop.offences).to have(1).item
20
+ expect(cop.offences.size).to eq(1)
21
21
  end
22
22
 
23
23
  it 'reports an offence for defs with redundant begin block' do
@@ -29,7 +29,7 @@ module Rubocop
29
29
  ' end',
30
30
  'end']
31
31
  inspect_source(cop, src)
32
- expect(cop.offences).to have(1).item
32
+ expect(cop.offences.size).to eq(1)
33
33
  end
34
34
 
35
35
  it 'accepts a def with required begin block' do