rubocop 0.6.1 → 0.7.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 (87) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +4 -266
  3. data/CHANGELOG.md +49 -7
  4. data/README.md +75 -2
  5. data/Rakefile +2 -2
  6. data/bin/rubocop +15 -10
  7. data/lib/rubocop.rb +19 -1
  8. data/lib/rubocop/cli.rb +113 -116
  9. data/lib/rubocop/config.rb +202 -0
  10. data/lib/rubocop/config_store.rb +37 -0
  11. data/lib/rubocop/cop/alias.rb +2 -5
  12. data/lib/rubocop/cop/align_parameters.rb +1 -1
  13. data/lib/rubocop/cop/array_literal.rb +43 -4
  14. data/lib/rubocop/cop/avoid_for.rb +2 -4
  15. data/lib/rubocop/cop/avoid_global_vars.rb +49 -0
  16. data/lib/rubocop/cop/block_comments.rb +17 -0
  17. data/lib/rubocop/cop/brace_after_percent.rb +9 -5
  18. data/lib/rubocop/cop/{indentation.rb → case_indentation.rb} +1 -1
  19. data/lib/rubocop/cop/class_methods.rb +20 -0
  20. data/lib/rubocop/cop/colon_method_call.rb +44 -0
  21. data/lib/rubocop/cop/cop.rb +30 -2
  22. data/lib/rubocop/cop/def_parentheses.rb +1 -1
  23. data/lib/rubocop/cop/empty_line_between_defs.rb +26 -0
  24. data/lib/rubocop/cop/empty_lines.rb +10 -13
  25. data/lib/rubocop/cop/eval.rb +22 -0
  26. data/lib/rubocop/cop/favor_join.rb +37 -0
  27. data/lib/rubocop/cop/grammar.rb +2 -2
  28. data/lib/rubocop/cop/hash_literal.rb +43 -4
  29. data/lib/rubocop/cop/hash_syntax.rb +2 -2
  30. data/lib/rubocop/cop/if_then_else.rb +1 -1
  31. data/lib/rubocop/cop/leading_comment_space.rb +20 -0
  32. data/lib/rubocop/cop/line_continuation.rb +18 -0
  33. data/lib/rubocop/cop/line_length.rb +1 -1
  34. data/lib/rubocop/cop/method_and_variable_snake_case.rb +7 -6
  35. data/lib/rubocop/cop/method_length.rb +4 -15
  36. data/lib/rubocop/cop/not.rb +15 -0
  37. data/lib/rubocop/cop/offence.rb +9 -0
  38. data/lib/rubocop/cop/semicolon.rb +74 -3
  39. data/lib/rubocop/cop/single_line_methods.rb +60 -0
  40. data/lib/rubocop/cop/space_after_control_keyword.rb +28 -0
  41. data/lib/rubocop/cop/surrounding_space.rb +48 -9
  42. data/lib/rubocop/cop/symbol_array.rb +29 -0
  43. data/lib/rubocop/cop/trivial_accessors.rb +103 -0
  44. data/lib/rubocop/cop/unless_else.rb +1 -1
  45. data/lib/rubocop/cop/variable_interpolation.rb +3 -2
  46. data/lib/rubocop/cop/word_array.rb +38 -0
  47. data/lib/rubocop/version.rb +1 -1
  48. data/rubocop.gemspec +11 -7
  49. data/spec/project_spec.rb +27 -0
  50. data/spec/rubocop/cli_spec.rb +549 -487
  51. data/spec/rubocop/config_spec.rb +399 -0
  52. data/spec/rubocop/config_store_spec.rb +66 -0
  53. data/spec/rubocop/cops/alias_spec.rb +7 -0
  54. data/spec/rubocop/cops/array_literal_spec.rb +8 -1
  55. data/spec/rubocop/cops/avoid_for_spec.rb +15 -1
  56. data/spec/rubocop/cops/avoid_global_vars.rb +32 -0
  57. data/spec/rubocop/cops/block_comments_spec.rb +29 -0
  58. data/spec/rubocop/cops/brace_after_percent_spec.rb +19 -13
  59. data/spec/rubocop/cops/{indentation_spec.rb → case_indentation_spec.rb} +2 -2
  60. data/spec/rubocop/cops/class_methods_spec.rb +49 -0
  61. data/spec/rubocop/cops/colon_method_call_spec.rb +47 -0
  62. data/spec/rubocop/cops/empty_line_between_defs_spec.rb +83 -0
  63. data/spec/rubocop/cops/empty_lines_spec.rb +6 -63
  64. data/spec/rubocop/cops/eval_spec.rb +36 -0
  65. data/spec/rubocop/cops/favor_join_spec.rb +39 -0
  66. data/spec/rubocop/cops/hash_literal_spec.rb +8 -1
  67. data/spec/rubocop/cops/leading_comment_space_spec.rb +60 -0
  68. data/spec/rubocop/cops/line_continuation_spec.rb +24 -0
  69. data/spec/rubocop/cops/line_length_spec.rb +1 -0
  70. data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +20 -0
  71. data/spec/rubocop/cops/method_length_spec.rb +2 -5
  72. data/spec/rubocop/cops/new_lambda_literal_spec.rb +2 -3
  73. data/spec/rubocop/cops/not_spec.rb +34 -0
  74. data/spec/rubocop/cops/offence_spec.rb +7 -0
  75. data/spec/rubocop/cops/semicolon_spec.rb +79 -4
  76. data/spec/rubocop/cops/single_line_methods_spec.rb +50 -0
  77. data/spec/rubocop/cops/space_after_control_keyword_spec.rb +28 -0
  78. data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +11 -1
  79. data/spec/rubocop/cops/space_inside_hash_literal_braces_spec.rb +74 -0
  80. data/spec/rubocop/cops/symbol_array_spec.rb +25 -0
  81. data/spec/rubocop/cops/trivial_accessors_spec.rb +332 -0
  82. data/spec/rubocop/cops/variable_interpolation_spec.rb +10 -1
  83. data/spec/rubocop/cops/word_array_spec.rb +39 -0
  84. data/spec/spec_helper.rb +16 -9
  85. data/spec/support/file_helper.rb +21 -0
  86. data/spec/support/isolated_environment.rb +27 -0
  87. metadata +66 -6
@@ -31,6 +31,13 @@ module Rubocop
31
31
  ['alias_method :ala, :bala'])
32
32
  expect(a.offences).to be_empty
33
33
  end
34
+
35
+ it 'does not register an offence for :alias' do
36
+ inspect_source(a,
37
+ 'file.rb',
38
+ ['[:alias, :ala, :bala]'])
39
+ expect(a.offences).to be_empty
40
+ end
34
41
  end
35
42
  end
36
43
  end
@@ -16,7 +16,14 @@ module Rubocop
16
16
  .to eq([ArrayLiteral::ERROR_MESSAGE])
17
17
  end
18
18
 
19
- it 'registers an offence for Array.new'
19
+ it 'registers an offence for Array.new' do
20
+ inspect_source(a,
21
+ 'file.rb',
22
+ ['test = Array.new'])
23
+ expect(a.offences.size).to eq(1)
24
+ expect(a.offences.map(&:message))
25
+ .to eq([ArrayLiteral::ERROR_MESSAGE])
26
+ end
20
27
 
21
28
  it 'does not register an offence for Array.new(3)' do
22
29
  inspect_source(a,
@@ -11,12 +11,26 @@ module Rubocop
11
11
  inspect_source(af,
12
12
  'file.rb',
13
13
  ['for n in [1, 2, 3] do',
14
- 'puts n',
14
+ ' puts n',
15
15
  'end'])
16
16
  expect(af.offences.size).to eq(1)
17
17
  expect(af.offences.map(&:message))
18
18
  .to eq([AvoidFor::ERROR_MESSAGE])
19
19
  end
20
+
21
+ it 'does not register an offence for :for' do
22
+ inspect_source(af,
23
+ 'file.rb',
24
+ ['[:for, :ala, :bala]'])
25
+ expect(af.offences).to be_empty
26
+ end
27
+
28
+ it 'does not register an offence for def for' do
29
+ inspect_source(af,
30
+ 'file.rb',
31
+ ['def for; end'])
32
+ expect(af.offences).to be_empty
33
+ end
20
34
  end
21
35
  end
22
36
  end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe AvoidGlobalVars do
8
+ let(:ap) { AvoidGlobalVars.new }
9
+
10
+ it 'registers an offence for $custom' do
11
+ inspect_source(ap, 'file.rb', ['puts $custom'])
12
+ expect(ap.offences.size).to eq(1)
13
+ end
14
+
15
+ it 'does not register an offence for $"' do
16
+ inspect_source(ap, 'file.rb', ['puts $"'])
17
+
18
+ expect(ap.offences).to be_empty
19
+ end
20
+
21
+ it 'does not register an offence for $ORS' do
22
+ inspect_source(ap, 'file.rb', ['puts $0'])
23
+ expect(ap.offences).to be_empty
24
+ end
25
+
26
+ it 'does not register an offence for backrefs like $1' do
27
+ inspect_source(ap, 'file.rb', ['puts $1'])
28
+ expect(ap.offences).to be_empty
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe BlockComments do
8
+ let(:block) { BlockComments.new }
9
+
10
+ it 'registers an offence for block comments' do
11
+ inspect_source(block,
12
+ 'file.rb',
13
+ ['=begin',
14
+ 'comment',
15
+ '=end'])
16
+ expect(block.offences.size).to eq(1)
17
+ expect(block.offences.map(&:message))
18
+ .to eq([BlockComments::ERROR_MESSAGE])
19
+ end
20
+
21
+ it 'accepts regular comments' do
22
+ inspect_source(block,
23
+ 'file.rb',
24
+ ['# comment'])
25
+ expect(block.offences).to be_empty
26
+ end
27
+ end
28
+ end
29
+ end
@@ -6,21 +6,27 @@ module Rubocop
6
6
  module Cop
7
7
  describe BraceAfterPercent do
8
8
  let(:bap) { BraceAfterPercent.new }
9
+ literals = %w(q Q r i I w W x s)
9
10
 
10
- it 'registers an offence for %w[' do
11
- inspect_source(bap,
12
- 'file.rb',
13
- ['puts %w[test top]'])
14
- expect(bap.offences.size).to eq(1)
15
- expect(bap.offences.map(&:message))
16
- .to eq([BraceAfterPercent::ERROR_MESSAGE])
17
- end
11
+ literals.each do |literal|
12
+ # %i and %I are new in ruby 2.0
13
+ tag = literal.downcase == 'i' ? { ruby: 2.0 } : {}
14
+
15
+ it "registers an offence for %#{literal}[", tag do
16
+ inspect_source(bap,
17
+ 'file.rb',
18
+ ["puts %#{literal}[test top]"])
19
+ expect(bap.offences.size).to eq(1)
20
+ expect(bap.offences.map(&:message))
21
+ .to eq([BraceAfterPercent::ERROR_MESSAGE])
22
+ end
18
23
 
19
- it 'registers an offence for %w(' do
20
- inspect_source(bap,
21
- 'file.rb',
22
- ['puts %w(test top)'])
23
- expect(bap.offences).to be_empty
24
+ it "does not registers an offence for %#{literal}(", tag do
25
+ inspect_source(bap,
26
+ 'file.rb',
27
+ ["puts %#{literal}(test top)"])
28
+ expect(bap.offences).to be_empty
29
+ end
24
30
  end
25
31
  end
26
32
  end
@@ -4,8 +4,8 @@ require 'spec_helper'
4
4
 
5
5
  module Rubocop
6
6
  module Cop
7
- describe Indentation do
8
- let(:ind) { Indentation.new }
7
+ describe CaseIndentation do
8
+ let(:ind) { CaseIndentation.new }
9
9
 
10
10
  it "registers an offence for a when clause that's deeper than case" do
11
11
  source = ['case a',
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe ClassMethods do
8
+ let(:cm) { ClassMethods.new }
9
+
10
+ it 'registers an offence for methods using a class name' do
11
+ inspect_source(cm, 'file.rb',
12
+ ['class Test',
13
+ ' def Test.some_method',
14
+ ' do_something',
15
+ ' end',
16
+ 'end'])
17
+ expect(cm.offences.size).to eq(1)
18
+ end
19
+
20
+ it 'registers an offence for methods using a module name' do
21
+ inspect_source(cm, 'file.rb',
22
+ ['module Test',
23
+ ' def Test.some_method',
24
+ ' do_something',
25
+ ' end',
26
+ 'end'])
27
+ expect(cm.offences.size).to eq(1)
28
+ end
29
+
30
+ it 'does not register an offence for methods using self' do
31
+ inspect_source(cm, 'file.rb',
32
+ ['module Test',
33
+ ' def self.some_method',
34
+ ' do_something',
35
+ ' end',
36
+ 'end'])
37
+ expect(cm.offences).to be_empty
38
+ end
39
+
40
+ it 'does not register an offence outside class/module bodies' do
41
+ inspect_source(cm, 'file.rb',
42
+ ['def self.some_method',
43
+ ' do_something',
44
+ 'end'])
45
+ expect(cm.offences).to be_empty
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe ColonMethodCall do
8
+ let(:smc) { ColonMethodCall.new }
9
+
10
+ it 'registers an offence for instance method call' do
11
+ inspect_source(smc,
12
+ 'file.rb',
13
+ ['test::method_name'])
14
+ expect(smc.offences.size).to eq(1)
15
+ end
16
+
17
+ it 'registers an offence for instance method call with arg' do
18
+ inspect_source(smc,
19
+ 'file.rb',
20
+ ['test::method_name(arg)'])
21
+ expect(smc.offences.size).to eq(1)
22
+ end
23
+
24
+ it 'registers an offence for class method call' do
25
+ inspect_source(smc,
26
+ 'file.rb',
27
+ ['Class::method_name'])
28
+ expect(smc.offences.size).to eq(1)
29
+ end
30
+
31
+ it 'registers an offence for class method call with arg' do
32
+ inspect_source(smc,
33
+ 'file.rb',
34
+ ['Class::method_name(arg, arg2)'])
35
+ expect(smc.offences.size).to eq(1)
36
+ end
37
+
38
+ it 'does not register an offence for constant access' do
39
+ inspect_source(smc,
40
+ 'file.rb',
41
+ ['Tip::Top::SOME_CONST'])
42
+ expect(smc.offences).to be_empty
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe EmptyLineBetweenDefs do
8
+ let(:empty_lines) { EmptyLineBetweenDefs.new }
9
+
10
+ it 'finds offences in inner classes' do
11
+ inspect_source(empty_lines, '', ['class K',
12
+ ' def m',
13
+ ' end',
14
+ ' class J',
15
+ ' def n',
16
+ ' end',
17
+ ' def o',
18
+ ' end',
19
+ ' end',
20
+ ' # checks something',
21
+ ' def p',
22
+ ' end',
23
+ 'end'])
24
+ expect(empty_lines.offences.size).to eq(2)
25
+ expect(empty_lines.offences.map(&:line_number).sort).to eq([7, 11])
26
+ end
27
+
28
+ # Only one def, so rule about empty line *between* defs does not
29
+ # apply.
30
+ it 'accepts a def that follows a line with code' do
31
+ inspect_source(empty_lines, '', ['x = 0',
32
+ 'def m',
33
+ 'end'])
34
+ expect(empty_lines.offences).to be_empty
35
+ end
36
+
37
+ # Only one def, so rule about empty line *between* defs does not
38
+ # apply.
39
+ it 'accepts a def that follows code and a comment' do
40
+ inspect_source(empty_lines, '', [' x = 0',
41
+ ' # 123',
42
+ ' def m',
43
+ ' end'])
44
+ expect(empty_lines.offences).to be_empty
45
+ end
46
+
47
+ it 'accepts the first def without leading empty line in a class' do
48
+ inspect_source(empty_lines, '', ['class K',
49
+ ' def m',
50
+ ' end',
51
+ 'end'])
52
+ expect(empty_lines.offences).to be_empty
53
+ end
54
+
55
+ it 'accepts a def that follows an empty line and then a comment' do
56
+ inspect_source(empty_lines, '', ['class A',
57
+ ' # calculates value',
58
+ ' def m',
59
+ ' end',
60
+ '',
61
+ ' private',
62
+ ' # calculates size',
63
+ ' def n',
64
+ ' end',
65
+ 'end',
66
+ ])
67
+ expect(empty_lines.offences).to be_empty
68
+ end
69
+
70
+ it 'accepts a def that is the first of a module' do
71
+ source = ['module Util',
72
+ ' public',
73
+ ' #',
74
+ ' def html_escape(s)',
75
+ ' end',
76
+ 'end',
77
+ ]
78
+ inspect_source(empty_lines, '', source)
79
+ expect(empty_lines.offences.map(&:message)).to be_empty
80
+ end
81
+ end
82
+ end
83
+ end
@@ -7,77 +7,20 @@ module Rubocop
7
7
  describe EmptyLines do
8
8
  let(:empty_lines) { EmptyLines.new }
9
9
 
10
- it 'finds offences in inner classes' do
11
- inspect_source(empty_lines, '', ['class K',
12
- ' def m',
13
- ' end',
14
- ' class J',
15
- ' def n',
16
- ' end',
17
- ' def o',
18
- ' end',
19
- ' end',
20
- ' # checks something',
21
- ' def p',
22
- ' end',
23
- 'end'])
10
+ it 'registers an offence for consecutive empty lines' do
11
+ inspect_source(empty_lines, 'file.rb',
12
+ ['test = 5', '', '', '', 'top'])
24
13
  expect(empty_lines.offences.size).to eq(2)
25
- expect(empty_lines.offences.map(&:line_number).sort).to eq([7, 11])
26
14
  end
27
15
 
28
- # Only one def, so rule about empty line *between* defs does not
29
- # apply.
30
- it 'accepts a def that follows a line with code' do
31
- inspect_source(empty_lines, '', ['x = 0',
32
- 'def m',
33
- 'end'])
34
- expect(empty_lines.offences).to be_empty
35
- end
16
+ it 'does not register an offence for empty lines in a string' do
17
+ inspect_source(empty_lines, 'file.rb', ['result = "test
36
18
 
37
- # Only one def, so rule about empty line *between* defs does not
38
- # apply.
39
- it 'accepts a def that follows code and a comment' do
40
- inspect_source(empty_lines, '', [' x = 0',
41
- ' # 123',
42
- ' def m',
43
- ' end'])
44
- expect(empty_lines.offences).to be_empty
45
- end
46
19
 
47
- it 'accepts the first def without leading empty line in a class' do
48
- inspect_source(empty_lines, '', ['class K',
49
- ' def m',
50
- ' end',
51
- 'end'])
52
- expect(empty_lines.offences).to be_empty
53
- end
54
20
 
55
- it 'accepts a def that follows an empty line and then a comment' do
56
- inspect_source(empty_lines, '', ['class A',
57
- ' # calculates value',
58
- ' def m',
59
- ' end',
60
- '',
61
- ' private',
62
- ' # calculates size',
63
- ' def n',
64
- ' end',
65
- 'end',
66
- ])
21
+ string"'])
67
22
  expect(empty_lines.offences).to be_empty
68
23
  end
69
-
70
- it 'accepts a def that is the first of a module' do
71
- source = ['module Util',
72
- ' public',
73
- ' #',
74
- ' def html_escape(s)',
75
- ' end',
76
- 'end',
77
- ]
78
- inspect_source(empty_lines, '', source)
79
- expect(empty_lines.offences.map(&:message)).to be_empty
80
- end
81
24
  end
82
25
  end
83
26
  end