rubocop 0.3.2 → 0.4.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 (66) hide show
  1. data/.rubocop.yml +6 -0
  2. data/README.md +18 -6
  3. data/VERSION +1 -1
  4. data/bin/rubocop +1 -1
  5. data/lib/rubocop.rb +3 -0
  6. data/lib/rubocop/cli.rb +33 -16
  7. data/lib/rubocop/cop/collection_methods.rb +29 -0
  8. data/lib/rubocop/cop/cop.rb +1 -1
  9. data/lib/rubocop/cop/def_parentheses.rb +4 -1
  10. data/lib/rubocop/cop/encoding.rb +6 -4
  11. data/lib/rubocop/cop/favor_modifier.rb +5 -5
  12. data/lib/rubocop/cop/indentation.rb +1 -1
  13. data/lib/rubocop/cop/syntax.rb +19 -0
  14. data/lib/rubocop/version.rb +1 -1
  15. data/rubocop.gemspec +6 -5
  16. data/spec/rubocop/cli_spec.rb +98 -52
  17. data/spec/rubocop/cops/align_parameters_spec.rb +29 -29
  18. data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +4 -4
  19. data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +4 -4
  20. data/spec/rubocop/cops/collection_methods_spec.rb +39 -0
  21. data/spec/rubocop/cops/cop_spec.rb +5 -5
  22. data/spec/rubocop/cops/def_with_parentheses_spec.rb +5 -5
  23. data/spec/rubocop/cops/def_without_parentheses_spec.rb +4 -4
  24. data/spec/rubocop/cops/empty_lines_spec.rb +8 -8
  25. data/spec/rubocop/cops/encoding_spec.rb +17 -11
  26. data/spec/rubocop/cops/end_of_line_spec.rb +5 -5
  27. data/spec/rubocop/cops/favor_modifier_spec.rb +12 -12
  28. data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +8 -8
  29. data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +7 -7
  30. data/spec/rubocop/cops/grammar_spec.rb +14 -9
  31. data/spec/rubocop/cops/hash_syntax_spec.rb +10 -10
  32. data/spec/rubocop/cops/if_with_semicolon_spec.rb +3 -3
  33. data/spec/rubocop/cops/indentation_spec.rb +7 -7
  34. data/spec/rubocop/cops/line_length_spec.rb +4 -4
  35. data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +7 -7
  36. data/spec/rubocop/cops/multiline_blocks_spec.rb +4 -4
  37. data/spec/rubocop/cops/multiline_if_then_spec.rb +7 -7
  38. data/spec/rubocop/cops/new_lambda_literal_spec.rb +4 -4
  39. data/spec/rubocop/cops/numeric_literals_spec.rb +10 -10
  40. data/spec/rubocop/cops/offence_spec.rb +4 -4
  41. data/spec/rubocop/cops/one_line_conditional_spec.rb +2 -3
  42. data/spec/rubocop/cops/parameter_lists_spec.rb +4 -4
  43. data/spec/rubocop/cops/parentheses_around_condition_spec.rb +6 -6
  44. data/spec/rubocop/cops/single_line_blocks_spec.rb +4 -4
  45. data/spec/rubocop/cops/space_after_colon_spec.rb +5 -5
  46. data/spec/rubocop/cops/space_after_comma_spec.rb +3 -3
  47. data/spec/rubocop/cops/space_after_semicolon_spec.rb +3 -3
  48. data/spec/rubocop/cops/space_around_braces_spec.rb +6 -6
  49. data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +4 -4
  50. data/spec/rubocop/cops/space_around_operators_spec.rb +28 -28
  51. data/spec/rubocop/cops/space_inside_brackets_spec.rb +8 -8
  52. data/spec/rubocop/cops/space_inside_parens_spec.rb +4 -4
  53. data/spec/rubocop/cops/string_literals_spec.rb +5 -5
  54. data/spec/rubocop/cops/syntax_spec.rb +18 -0
  55. data/spec/rubocop/cops/tab_spec.rb +3 -3
  56. data/spec/rubocop/cops/ternary_operator_spec.rb +8 -8
  57. data/spec/rubocop/cops/trailing_whitespace_spec.rb +4 -4
  58. data/spec/rubocop/cops/unless_else_spec.rb +4 -4
  59. data/spec/rubocop/cops/when_then_spec.rb +5 -5
  60. data/spec/rubocop/reports/emacs_style_spec.rb +3 -3
  61. data/spec/rubocop/reports/report_spec.rb +5 -5
  62. data/spec/spec_helper.rb +6 -0
  63. metadata +7 -6
  64. data/features/rubocop.feature +0 -9
  65. data/features/step_definitions/rubocop_steps.rb +0 -1
  66. data/features/support/env.rb +0 -15
@@ -5,22 +5,22 @@ require 'spec_helper'
5
5
  module Rubocop
6
6
  module Cop
7
7
  describe AlignParameters do
8
- let (:align) { AlignParameters.new }
8
+ let(:align) { AlignParameters.new }
9
9
 
10
10
  it 'registers an offence for parameters with single indent' do
11
11
  inspect_source(align, 'file.rb', ['function(a,',
12
12
  ' if b then c else d end)'])
13
- align.offences.map(&:message).should ==
13
+ expect(align.offences.map(&:message)).to eq(
14
14
  ['Align the parameters of a method call if they span more than ' +
15
- 'one line.']
15
+ 'one line.'])
16
16
  end
17
17
 
18
18
  it 'registers an offence for parameters with double indent' do
19
19
  inspect_source(align, 'file.rb', ['function(a,',
20
20
  ' if b then c else d end)'])
21
- align.offences.map(&:message).should ==
21
+ expect(align.offences.map(&:message)).to eq(
22
22
  ['Align the parameters of a method call if they span more than ' +
23
- 'one line.']
23
+ 'one line.'])
24
24
  end
25
25
 
26
26
  it 'accepts correctly aligned parameters' do
@@ -28,19 +28,19 @@ module Rubocop
28
28
  ' 0, 1,',
29
29
  ' (x + y),',
30
30
  ' if b then c else d end)'])
31
- align.offences.map(&:message).should == []
31
+ expect(align.offences.map(&:message)).to be_empty
32
32
  end
33
33
 
34
34
  it 'accepts calls that only span one line' do
35
35
  inspect_source(align, 'file.rb', ['find(path, s, @special[sexp[0]])'])
36
- align.offences.map(&:message).should == []
36
+ expect(align.offences.map(&:message)).to be_empty
37
37
  end
38
38
 
39
39
  it "doesn't get confused by a symbol argument" do
40
40
  inspect_source(align, '',
41
41
  ['add_offence(:convention, index,',
42
42
  ' ERROR_MESSAGE % kind)'])
43
- align.offences.map(&:message).should == []
43
+ expect(align.offences.map(&:message)).to be_empty
44
44
  end
45
45
 
46
46
  it "doesn't get confused by splat operator" do
@@ -53,49 +53,49 @@ module Rubocop
53
53
  ' c)',
54
54
  'func3(*a)',
55
55
  ])
56
- align.offences.map(&:to_s).should ==
56
+ expect(align.offences.map(&:to_s)).to eq(
57
57
  ['C: 5: Align the parameters of a method call if they span ' +
58
- 'more than one line.']
58
+ 'more than one line.'])
59
59
  end
60
60
 
61
61
  it 'can handle a correctly aligned string literal as first argument' do
62
62
  inspect_source(align, '',
63
63
  ['add_offence(:convention, x,',
64
64
  ' a)'])
65
- align.offences.map(&:message).should == []
65
+ expect(align.offences.map(&:message)).to be_empty
66
66
  end
67
67
 
68
68
  it 'can handle a string literal as other argument' do
69
69
  inspect_source(align, '',
70
70
  ['add_offence(:convention,',
71
71
  ' "", a)'])
72
- align.offences.map(&:message).should == []
72
+ expect(align.offences.map(&:message)).to be_empty
73
73
  end
74
74
 
75
75
  it "doesn't get confused by a line break inside a parameter" do
76
76
  inspect_source(align, '',
77
77
  ['read(path, { headers: true,',
78
78
  ' converters: :numeric })'])
79
- align.offences.map(&:message).should == []
79
+ expect(align.offences.map(&:message)).to be_empty
80
80
  end
81
81
 
82
82
  it "doesn't get confused by symbols with embedded expressions" do
83
83
  inspect_source(align, '',
84
84
  ['send(:"#{name}_comments_path")'])
85
- align.offences.map(&:message).should == []
85
+ expect(align.offences.map(&:message)).to be_empty
86
86
  end
87
87
 
88
88
  it "doesn't get confused by regexen with embedded expressions" do
89
89
  inspect_source(align, '',
90
90
  ['a(/#{name}/)'])
91
- align.offences.map(&:message).should == []
91
+ expect(align.offences.map(&:message)).to be_empty
92
92
  end
93
93
 
94
94
  it 'accepts braceless hashes' do
95
95
  inspect_source(align, '',
96
96
  ['run(collection, :entry_name => label,',
97
97
  ' :paginator => paginator)'])
98
- align.offences.map(&:message).should == []
98
+ expect(align.offences.map(&:message)).to be_empty
99
99
  end
100
100
 
101
101
  it 'accepts the first parameter being on a new row' do
@@ -104,7 +104,7 @@ module Rubocop
104
104
  ' a,',
105
105
  ' b',
106
106
  ' )'])
107
- align.offences.map(&:message).should == []
107
+ expect(align.offences.map(&:message)).to be_empty
108
108
  end
109
109
 
110
110
  it 'can handle heredoc strings' do
@@ -115,7 +115,7 @@ module Rubocop
115
115
  ' end',
116
116
  ' EOS']
117
117
  inspect_source(align, '', src)
118
- align.offences.map(&:message).should == []
118
+ expect(align.offences.map(&:message)).to be_empty
119
119
  end
120
120
 
121
121
  it 'can handle a method call within a method call' do
@@ -124,13 +124,13 @@ module Rubocop
124
124
  ' b(b1,',
125
125
  ' b2),',
126
126
  ' a2)'])
127
- align.offences.map(&:message).should == []
127
+ expect(align.offences.map(&:message)).to be_empty
128
128
  end
129
129
 
130
130
  it 'can handle a call embedded in a string' do
131
131
  inspect_source(align, '',
132
132
  ['model("#{index(name)}", child)'])
133
- align.offences.map(&:message).should == []
133
+ expect(align.offences.map(&:message)).to be_empty
134
134
  end
135
135
 
136
136
  it 'can handle do-end' do
@@ -138,7 +138,7 @@ module Rubocop
138
138
  [' run(lambda do |e|',
139
139
  " w = e['warden']",
140
140
  ' end)'])
141
- align.offences.map(&:message).should == []
141
+ expect(align.offences.map(&:message)).to be_empty
142
142
  end
143
143
 
144
144
  it 'can handle a call with a block inside another call' do
@@ -147,19 +147,19 @@ module Rubocop
147
147
  " col['name']",
148
148
  ' })']
149
149
  inspect_source(align, '', src)
150
- align.offences.map(&:message).should == []
150
+ expect(align.offences.map(&:message)).to be_empty
151
151
  end
152
152
 
153
153
  it 'can handle a ternary condition with a block reference' do
154
154
  src = ['cond ? a : func(&b)']
155
155
  inspect_source(align, '', src)
156
- align.offences.map(&:message).should == []
156
+ expect(align.offences.map(&:message)).to be_empty
157
157
  end
158
158
 
159
159
  it 'can handle parentheses used with no parameters' do
160
160
  src = ['func()']
161
161
  inspect_source(align, '', src)
162
- align.offences.map(&:message).should == []
162
+ expect(align.offences.map(&:message)).to be_empty
163
163
  end
164
164
 
165
165
  it 'can handle a multiline hash as first parameter' do
@@ -167,7 +167,7 @@ module Rubocop
167
167
  ' :space_before => "",',
168
168
  '}, state)']
169
169
  inspect_source(align, '', src)
170
- align.offences.map(&:message).should == []
170
+ expect(align.offences.map(&:message)).to be_empty
171
171
  end
172
172
 
173
173
  it 'can handle a multiline hash as second parameter' do
@@ -175,26 +175,26 @@ module Rubocop
175
175
  ' :value => value',
176
176
  '})']
177
177
  inspect_source(align, '', src)
178
- align.offences.map(&:message).should == []
178
+ expect(align.offences.map(&:message)).to be_empty
179
179
  end
180
180
 
181
181
  it 'can handle method calls without parentheses' do
182
182
  src = ['a(b c, d)']
183
183
  inspect_source(align, '', src)
184
- align.offences.map(&:message).should == []
184
+ expect(align.offences.map(&:message)).to be_empty
185
185
  end
186
186
 
187
187
  it 'can handle other method calls without parentheses' do
188
188
  src = ['chars(Unicode.apply_mapping @wrapped_string, :uppercase)']
189
189
  inspect_source(align, '', src)
190
- align.offences.map(&:message).should == []
190
+ expect(align.offences.map(&:message)).to be_empty
191
191
  end
192
192
 
193
193
  it "doesn't check alignment if tabs are used to indent" do
194
194
  src = ['a(b,',
195
195
  "\tc)"]
196
196
  inspect_source(align, '', src)
197
- align.offences.map(&:message).should == []
197
+ expect(align.offences.map(&:message)).to be_empty
198
198
  end
199
199
  end
200
200
  end
@@ -5,7 +5,7 @@ require 'spec_helper'
5
5
  module Rubocop
6
6
  module Cop
7
7
  describe AmpersandsPipesVsAndOr do
8
- let (:amp) { AmpersandsPipesVsAndOr.new }
8
+ let(:amp) { AmpersandsPipesVsAndOr.new }
9
9
 
10
10
  it 'registers an offence for AND used in condition of if statement' do
11
11
  check('if', 'and', '&&')
@@ -48,9 +48,9 @@ module Rubocop
48
48
  'end'])
49
49
  # Just one offence should be registered. The good_operator
50
50
  # should be accepted.
51
- amp.offences.map(&:message).should ==
52
- ['Use &&/|| for boolean expressions, and/or for control flow.']
53
- amp.offences[0].line_number.should == 1
51
+ expect(amp.offences.map(&:message)).to eq(
52
+ ['Use &&/|| for boolean expressions, and/or for control flow.'])
53
+ expect(amp.offences[0].line_number).to eq(1)
54
54
  end
55
55
  end
56
56
  end
@@ -5,7 +5,7 @@ require 'spec_helper'
5
5
  module Rubocop
6
6
  module Cop
7
7
  describe ClassAndModuleCamelCase do
8
- let (:camel_case) { ClassAndModuleCamelCase.new }
8
+ let(:camel_case) { ClassAndModuleCamelCase.new }
9
9
 
10
10
  it 'registers an offence for underscore in class and module name' do
11
11
  inspect_source(camel_case, 'file.rb',
@@ -15,8 +15,8 @@ module Rubocop
15
15
  'module My_Module',
16
16
  'end',
17
17
  ])
18
- camel_case.offences.map(&:message).should ==
19
- ['Use CamelCase for classes and modules.'] * 2
18
+ expect(camel_case.offences.map(&:message)).to eq(
19
+ ['Use CamelCase for classes and modules.'] * 2)
20
20
  end
21
21
 
22
22
  it 'accepts CamelCase names' do
@@ -27,7 +27,7 @@ module Rubocop
27
27
  'module Mine',
28
28
  'end',
29
29
  ])
30
- camel_case.offences.map(&:message).should == []
30
+ expect(camel_case.offences.map(&:message)).to be_empty
31
31
  end
32
32
  end
33
33
  end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe CollectionMethods do
8
+ let(:cm) { CollectionMethods.new }
9
+
10
+ it 'registers an offence for collect' do
11
+ inspect_source(cm, 'file.rb', ['[1, 2, 3].collect { |e| e + 1 }'])
12
+ expect(cm.offences.size).to eq(1)
13
+ expect(cm.offences.map(&:message))
14
+ .to eq(['Prefer map over collect.'])
15
+ end
16
+
17
+ it 'registers an offence for inject' do
18
+ inspect_source(cm, 'file.rb', ['[1, 2, 3].inject { |e| e + 1 }'])
19
+ expect(cm.offences.size).to eq(1)
20
+ expect(cm.offences.map(&:message))
21
+ .to eq(['Prefer reduce over inject.'])
22
+ end
23
+
24
+ it 'registers an offence for detect' do
25
+ inspect_source(cm, 'file.rb', ['[1, 2, 3].detect { |e| e + 1 }'])
26
+ expect(cm.offences.size).to eq(1)
27
+ expect(cm.offences.map(&:message))
28
+ .to eq(['Prefer find over detect.'])
29
+ end
30
+
31
+ it 'registers an offence for find_all' do
32
+ inspect_source(cm, 'file.rb', ['[1, 2, 3].find_all { |e| e + 1 }'])
33
+ expect(cm.offences.size).to eq(1)
34
+ expect(cm.offences.map(&:message))
35
+ .to eq(['Prefer select over find_all.'])
36
+ end
37
+ end
38
+ end
39
+ end
@@ -5,26 +5,26 @@ require 'spec_helper'
5
5
  module Rubocop
6
6
  module Cop
7
7
  describe Cop do
8
- let (:cop) { Cop.new }
8
+ let(:cop) { Cop.new }
9
9
 
10
10
  it 'initially has 0 offences' do
11
- cop.offences.size.should == 0
11
+ expect(cop.offences).to be_empty
12
12
  end
13
13
 
14
14
  it 'initially has nothing to report' do
15
- cop.has_report?.should be_false
15
+ expect(cop.has_report?).to be_false
16
16
  end
17
17
 
18
18
  it 'keeps track of offences' do
19
19
  cop.add_offence(:convention, 1, 'message')
20
20
 
21
- cop.offences.size.should == 1
21
+ expect(cop.offences.size).to eq(1)
22
22
  end
23
23
 
24
24
  it 'will report registered offences' do
25
25
  cop.add_offence(:convention, 1, 'message')
26
26
 
27
- cop.has_report?.should be_true
27
+ expect(cop.has_report?).to be_true
28
28
  end
29
29
  end
30
30
  end
@@ -5,28 +5,28 @@ require 'spec_helper'
5
5
  module Rubocop
6
6
  module Cop
7
7
  describe DefWithParentheses do
8
- let (:def_par) { DefWithParentheses.new }
8
+ let(:def_par) { DefWithParentheses.new }
9
9
 
10
10
  it 'reports an offence for def with empty parens' do
11
11
  src = ['def func()',
12
12
  'end']
13
13
  inspect_source(def_par, '', src)
14
- def_par.offences.map(&:message).should ==
14
+ expect(def_par.offences.map(&:message)).to eq(
15
15
  ["Omit the parentheses in defs when the method doesn't accept any " +
16
- 'arguments.']
16
+ 'arguments.'])
17
17
  end
18
18
 
19
19
  it 'accepts def with arg and parens' do
20
20
  src = ['def func(a)',
21
21
  'end']
22
22
  inspect_source(def_par, '', src)
23
- def_par.offences.map(&:message).should == []
23
+ expect(def_par.offences.map(&:message)).to be_empty
24
24
  end
25
25
 
26
26
  it 'accepts empty parentheses in one liners' do
27
27
  src = ["def to_s() join '/' end"]
28
28
  inspect_source(def_par, '', src)
29
- def_par.offences.map(&:message).should == []
29
+ expect(def_par.offences.map(&:message)).to be_empty
30
30
  end
31
31
  end
32
32
  end
@@ -5,21 +5,21 @@ require 'spec_helper'
5
5
  module Rubocop
6
6
  module Cop
7
7
  describe DefWithoutParentheses do
8
- let (:def_par) { DefWithoutParentheses.new }
8
+ let(:def_par) { DefWithoutParentheses.new }
9
9
 
10
10
  it 'reports an offence for def with parameters but no parens' do
11
11
  src = ['def func a, b',
12
12
  'end']
13
13
  inspect_source(def_par, '', src)
14
- def_par.offences.map(&:message).should ==
15
- ['Use def with parentheses when there are arguments.']
14
+ expect(def_par.offences.map(&:message)).to eq(
15
+ ['Use def with parentheses when there are arguments.'])
16
16
  end
17
17
 
18
18
  it 'accepts def with no args and no parens' do
19
19
  src = ['def func',
20
20
  'end']
21
21
  inspect_source(def_par, '', src)
22
- def_par.offences.map(&:message).should == []
22
+ expect(def_par.offences.map(&:message)).to be_empty
23
23
  end
24
24
  end
25
25
  end
@@ -5,7 +5,7 @@ require 'spec_helper'
5
5
  module Rubocop
6
6
  module Cop
7
7
  describe EmptyLines do
8
- let (:empty_lines) { EmptyLines.new }
8
+ let(:empty_lines) { EmptyLines.new }
9
9
 
10
10
  it 'finds offences in inner classes' do
11
11
  inspect_source(empty_lines, '', ['class K',
@@ -21,8 +21,8 @@ module Rubocop
21
21
  ' def p',
22
22
  ' end',
23
23
  'end'])
24
- empty_lines.offences.size.should == 2
25
- empty_lines.offences.map(&:line_number).sort.should == [7, 11]
24
+ expect(empty_lines.offences.size).to eq(2)
25
+ expect(empty_lines.offences.map(&:line_number).sort).to eq([7, 11])
26
26
  end
27
27
 
28
28
  # Only one def, so rule about empty line *between* defs does not
@@ -31,7 +31,7 @@ module Rubocop
31
31
  inspect_source(empty_lines, '', ['x = 0',
32
32
  'def m',
33
33
  'end'])
34
- empty_lines.offences.size.should == 0
34
+ expect(empty_lines.offences).to be_empty
35
35
  end
36
36
 
37
37
  # Only one def, so rule about empty line *between* defs does not
@@ -41,7 +41,7 @@ module Rubocop
41
41
  ' # 123',
42
42
  ' def m',
43
43
  ' end'])
44
- empty_lines.offences.size.should == 0
44
+ expect(empty_lines.offences).to be_empty
45
45
  end
46
46
 
47
47
  it 'accepts the first def without leading empty line in a class' do
@@ -49,7 +49,7 @@ module Rubocop
49
49
  ' def m',
50
50
  ' end',
51
51
  'end'])
52
- empty_lines.offences.size.should == 0
52
+ expect(empty_lines.offences).to be_empty
53
53
  end
54
54
 
55
55
  it 'accepts a def that follows an empty line and then a comment' do
@@ -64,7 +64,7 @@ module Rubocop
64
64
  ' end',
65
65
  'end',
66
66
  ])
67
- empty_lines.offences.size.should == 0
67
+ expect(empty_lines.offences).to be_empty
68
68
  end
69
69
 
70
70
  it 'accepts a def that is the first of a module' do
@@ -76,7 +76,7 @@ module Rubocop
76
76
  'end',
77
77
  ]
78
78
  inspect_source(empty_lines, '', source)
79
- empty_lines.offences.map(&:message).should == []
79
+ expect(empty_lines.offences.map(&:message)).to be_empty
80
80
  end
81
81
  end
82
82
  end