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
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe SpaceAfterControlKeyword do
8
+ let(:ap) { SpaceAfterControlKeyword.new }
9
+
10
+ it 'registers an offence for normal if' do
11
+ inspect_source(ap, 'file.rb',
12
+ ['if(test) then result end'])
13
+ expect(ap.offences.size).to eq(1)
14
+ end
15
+
16
+ it 'registers an offence for modifier unless' do
17
+ inspect_source(ap, 'file.rb', ['action unless(test)'])
18
+
19
+ expect(ap.offences.size).to eq(1)
20
+ end
21
+
22
+ it 'does not get confused by keywords' do
23
+ inspect_source(ap, 'file.rb', ['[:if, :unless].action'])
24
+ expect(ap.offences).to be_empty
25
+ end
26
+ end
27
+ end
28
+ end
@@ -13,8 +13,18 @@ module Rubocop
13
13
  ['Surrounding space missing in default value assignment.'] * 2)
14
14
  end
15
15
 
16
+ it 'registers an offence for assignment of empty string without space' do
17
+ inspect_source(space, 'file.rb', ['def f(x, y="", z=1)', 'end'])
18
+ expect(space.offences.size).to eq(2)
19
+ end
20
+
21
+ it 'registers an offence for assignment of empty list without space' do
22
+ inspect_source(space, 'file.rb', ['def f(x, y=[])', 'end'])
23
+ expect(space.offences.size).to eq(1)
24
+ end
25
+
16
26
  it 'accepts default value assignment with space' do
17
- inspect_source(space, 'file.rb', ['def f(x, y = 0, z = 1)', 'end'])
27
+ inspect_source(space, 'file.rb', ['def f(x, y = 0, z = {})', 'end'])
18
28
  expect(space.offences.map(&:message)).to be_empty
19
29
  end
20
30
  end
@@ -0,0 +1,74 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe SpaceInsideHashLiteralBraces do
8
+ let(:sihlb) { SpaceInsideHashLiteralBraces.new }
9
+ before do
10
+ SpaceInsideHashLiteralBraces.config = {
11
+ 'EnforcedStyleIsWithSpaces' => true
12
+ }
13
+ end
14
+
15
+ it 'registers an offence for hashes with no spaces by default' do
16
+ inspect_source(sihlb, '',
17
+ ['h = {a: 1, b: 2}',
18
+ 'h = {a => 1 }'])
19
+ expect(sihlb.offences.map(&:message)).to eq(
20
+ ['Space inside hash literal braces missing.'] * 3)
21
+ end
22
+
23
+ it 'registers an offence for hashes with no spaces if so configured' do
24
+ inspect_source(sihlb, '',
25
+ ['h = {a: 1, b: 2}',
26
+ 'h = {a => 1 }'])
27
+ expect(sihlb.offences.map(&:message)).to eq(
28
+ ['Space inside hash literal braces missing.'] * 3)
29
+ end
30
+
31
+ it 'registers an offence for hashes with spaces if so configured' do
32
+ SpaceInsideHashLiteralBraces.config['EnforcedStyleIsWithSpaces'] =
33
+ false
34
+ inspect_source(sihlb, '',
35
+ ['h = { a: 1, b: 2 }'])
36
+ expect(sihlb.offences.map(&:message)).to eq(
37
+ ['Space inside hash literal braces detected.'] * 2)
38
+ end
39
+
40
+ it 'accepts hashes with spaces by default' do
41
+ inspect_source(sihlb, '',
42
+ ['h = { a: 1, b: 2 }',
43
+ 'h = { a => 1 }'])
44
+ expect(sihlb.offences.map(&:message)).to be_empty
45
+ end
46
+
47
+ it 'accepts hashes with no spaces if so configured' do
48
+ SpaceInsideHashLiteralBraces.config['EnforcedStyleIsWithSpaces'] =
49
+ false
50
+ inspect_source(sihlb, '',
51
+ ['h = {a: 1, b: 2}',
52
+ 'h = {a => 1}'])
53
+ expect(sihlb.offences.map(&:message)).to be_empty
54
+ end
55
+
56
+ it 'accepts empty hashes without spaces by default' do
57
+ inspect_source(sihlb, '', ['h = {}'])
58
+ expect(sihlb.offences).to be_empty
59
+ end
60
+
61
+ it 'accepts empty hashes without spaces if configured false' do
62
+ SpaceInsideHashLiteralBraces.config['EnforcedStyleIsWithSpaces'] =
63
+ false
64
+ inspect_source(sihlb, '', ['h = {}'])
65
+ expect(sihlb.offences).to be_empty
66
+ end
67
+
68
+ it 'accepts empty hashes without spaces even if configured true' do
69
+ inspect_source(sihlb, '', ['h = {}'])
70
+ expect(sihlb.offences).to be_empty
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe SymbolArray do
8
+ let(:sa) { SymbolArray.new }
9
+
10
+ it 'registers an offence for arrays of symbols', { ruby: 2.0 } do
11
+ inspect_source(sa,
12
+ 'file.rb',
13
+ ['[:one, :two, :three]'])
14
+ expect(sa.offences.size).to eq(1)
15
+ end
16
+
17
+ it 'does nothing on Ruby 1.9', { ruby: 1.9 } do
18
+ inspect_source(sa,
19
+ 'file.rb',
20
+ ['[:one, :two, :three]'])
21
+ expect(sa.offences).to be_empty
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,332 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe TrivialAccessors do
8
+ let(:trivial_accessors_finder) { TrivialAccessors.new }
9
+
10
+ before :each do
11
+ trivial_accessors_finder.offences.clear
12
+ end
13
+
14
+ it 'finds trivial reader' do
15
+ inspect_source(trivial_accessors_finder, '',
16
+ ['def foo',
17
+ ' @foo',
18
+ 'end',
19
+ '',
20
+ 'def Foo',
21
+ ' @Foo',
22
+ 'end'])
23
+ expect(trivial_accessors_finder.offences.size).to eq(2)
24
+ expect(trivial_accessors_finder.offences
25
+ .map(&:line_number).sort).to eq([1, 5])
26
+ end
27
+
28
+ it 'finds trivial reader in a class' do
29
+ inspect_source(trivial_accessors_finder, '',
30
+ ['class TrivialFoo',
31
+ ' def foo',
32
+ ' @foo',
33
+ ' end',
34
+ ' def bar',
35
+ ' !foo',
36
+ ' end',
37
+ 'end'])
38
+ expect(trivial_accessors_finder.offences.size).to eq(1)
39
+ expect(trivial_accessors_finder.offences
40
+ .map(&:line_number).sort).to eq([2])
41
+ end
42
+
43
+ it 'finds trivial reader in a nested class' do
44
+ inspect_source(trivial_accessors_finder, '',
45
+ ['class TrivialFoo',
46
+ ' class Nested',
47
+ ' def foo',
48
+ ' @foo',
49
+ ' end',
50
+ ' end',
51
+ 'end'])
52
+ expect(trivial_accessors_finder.offences.size).to eq(1)
53
+ expect(trivial_accessors_finder.offences
54
+ .map(&:line_number).sort).to eq([3])
55
+ end
56
+
57
+ it 'finds trivial readers in a little less trivial class' do
58
+ inspect_source(trivial_accessors_finder, '',
59
+ ['class TrivialFoo',
60
+ ' def foo',
61
+ ' @foo',
62
+ ' end',
63
+ ' def foo_and_bar',
64
+ ' @foo_bar = @foo + @bar',
65
+ ' end',
66
+ ' def foo_bar',
67
+ ' @foo_bar',
68
+ ' end',
69
+ ' def foo?',
70
+ ' foo.present?',
71
+ ' end',
72
+ ' def bar?',
73
+ ' !bar',
74
+ ' end',
75
+ ' def foobar',
76
+ ' foo? ? foo.value : "bar"',
77
+ ' end',
78
+ ' def bar',
79
+ ' foo.bar',
80
+ ' end',
81
+ ' def foo_required?',
82
+ ' super && !bar_required?',
83
+ ' end',
84
+ ' def self.from_omniauth(auth)',
85
+ ' foobars.each do |f|',
86
+ ' # do stuff',
87
+ ' end',
88
+ ' end',
89
+ ' def regex',
90
+ ' %r{\A#{visit node}\Z}',
91
+ ' end',
92
+ ' def array',
93
+ ' [foo, bar].join',
94
+ ' end',
95
+ ' def string',
96
+ ' "string"',
97
+ ' end',
98
+ ' def class',
99
+ ' Foo.class',
100
+ ' end',
101
+ ' def with_return',
102
+ ' return foo',
103
+ ' end',
104
+ ' def captures',
105
+ ' (length - 1).times.map { |i| self[i + 1] }',
106
+ ' end',
107
+ ' def foo val',
108
+ ' super',
109
+ ' @val',
110
+ ' end',
111
+ 'end'])
112
+ expect(trivial_accessors_finder.offences.size).to eq(2)
113
+ expect(trivial_accessors_finder.offences
114
+ .map(&:line_number).sort).to eq([2, 8])
115
+ end
116
+
117
+ it 'finds trivial reader with braces' do
118
+ inspect_source(trivial_accessors_finder, '',
119
+ ['class Test',
120
+ ' # trivial reader with braces',
121
+ ' def name()',
122
+ ' @name',
123
+ ' end',
124
+ 'end'])
125
+ expect(trivial_accessors_finder.offences.size).to eq(1)
126
+ expect(trivial_accessors_finder.offences
127
+ .map(&:line_number).sort).to eq([3])
128
+ end
129
+
130
+ it 'finds trivial writer without braces' do
131
+ inspect_source(trivial_accessors_finder, '',
132
+ ['class Test',
133
+ ' # trivial writer without braces',
134
+ ' def name= name',
135
+ ' @name = name',
136
+ ' end',
137
+ 'end'])
138
+ expect(trivial_accessors_finder.offences.size).to eq(1)
139
+ expect(trivial_accessors_finder.offences
140
+ .map(&:line_number).sort).to eq([3])
141
+ end
142
+
143
+ it 'does not find trivial writer with function calls' do
144
+ inspect_source(trivial_accessors_finder, '',
145
+ ['class TrivialTest',
146
+ ' def test=(val)',
147
+ ' @test = val',
148
+ ' some_function_call',
149
+ ' or_more_of_them',
150
+ ' end',
151
+ 'end'])
152
+ expect(trivial_accessors_finder.offences).to be_empty
153
+ end
154
+
155
+ it 'finds trivials with less peculiar methods' do
156
+ inspect_source(trivial_accessors_finder, '',
157
+ ['class NilStats',
158
+ 'def most_traded_pair',
159
+ 'end',
160
+ 'def win_ratio',
161
+ 'end',
162
+ 'def win_ratio_percentage()',
163
+ 'end',
164
+ 'def pips_won',
165
+ ' 0.0',
166
+ 'end',
167
+ 'def gain_at(date)',
168
+ ' 1',
169
+ 'end',
170
+ 'def gain= value',
171
+ ' @value = 0.1',
172
+ 'end',
173
+ 'def gain_percentage',
174
+ ' 0',
175
+ 'end',
176
+ 'def gain_breakdown(options = {})',
177
+ ' []',
178
+ 'end',
179
+ 'def copy_to_all_ratio',
180
+ ' nil',
181
+ 'end',
182
+ 'def trade_population',
183
+ ' {}',
184
+ 'end',
185
+ 'def average_leverage',
186
+ ' 1',
187
+ 'end',
188
+ 'def with_yield',
189
+ ' yield',
190
+ 'rescue Error => e',
191
+ ' #do stuff',
192
+ 'end',
193
+ 'end'])
194
+ expect(trivial_accessors_finder.offences).to be_empty
195
+ end
196
+
197
+ it 'finds oneliner trivials' do
198
+ inspect_source(trivial_accessors_finder, '',
199
+ ['class Oneliner',
200
+ ' def foo; @foo; end',
201
+ ' def foo= foo; @foo = foo; end',
202
+ 'end'])
203
+ expect(trivial_accessors_finder.offences.size).to eq(2)
204
+ expect(trivial_accessors_finder.offences
205
+ .map(&:line_number).sort).to eq([2, 3])
206
+ end
207
+
208
+ it 'does not find a trivial reader' do
209
+ inspect_source(trivial_accessors_finder, '',
210
+ ['def bar',
211
+ ' @bar + foo',
212
+ 'end'])
213
+ expect(trivial_accessors_finder.offences).to be_empty
214
+ end
215
+
216
+ it 'finds trivial writer' do
217
+ inspect_source(trivial_accessors_finder, '',
218
+ ['def foo=(val)',
219
+ ' @foo = val',
220
+ 'end'])
221
+ expect(trivial_accessors_finder.offences.size).to eq(1)
222
+ expect(trivial_accessors_finder.offences
223
+ .map(&:line_number).sort).to eq([1])
224
+ end
225
+
226
+ it 'finds trivial writer in a class' do
227
+ inspect_source(trivial_accessors_finder, '',
228
+ ['class TrivialFoo',
229
+ ' def foo=(val)',
230
+ ' @foo = val',
231
+ ' end',
232
+ ' def void(no_value)',
233
+ ' end',
234
+ ' def inspect(sexp)',
235
+ ' each(:def, sexp) do |item|',
236
+ ' #do stuff',
237
+ ' end',
238
+ ' end',
239
+ ' def if_method(foo)',
240
+ ' if true',
241
+ ' unless false',
242
+ ' #do stuff',
243
+ ' end',
244
+ ' end',
245
+ ' end',
246
+ 'end'])
247
+ expect(trivial_accessors_finder.offences.size).to eq(1)
248
+ expect(trivial_accessors_finder.offences
249
+ .map(&:line_number).sort).to eq([2])
250
+ end
251
+
252
+ it 'finds trivial accessors in a little less trivial class' do
253
+ inspect_source(trivial_accessors_finder, '',
254
+ ['class TrivialFoo',
255
+ ' def foo',
256
+ ' @foo',
257
+ ' end',
258
+ ' def foo_and_bar',
259
+ ' @foo_bar = @foo + @bar',
260
+ ' end',
261
+ ' def foo_bar',
262
+ ' @foo_bar',
263
+ ' end',
264
+ ' def bar=(bar_value)',
265
+ ' @bar = bar_value',
266
+ ' end',
267
+ 'end'])
268
+ expect(trivial_accessors_finder.offences.size).to eq(3)
269
+ expect(trivial_accessors_finder.offences
270
+ .map(&:line_number).sort).to eq([2, 8, 11])
271
+ end
272
+
273
+ it 'does not find a trivial writer' do
274
+ inspect_source(trivial_accessors_finder, '',
275
+ ['def bar=(value)',
276
+ ' @bar = value + 42',
277
+ 'end'])
278
+ expect(trivial_accessors_finder.offences).to be_empty
279
+ end
280
+
281
+ it 'finds trivial writers in a little less trivial class' do
282
+ inspect_source(trivial_accessors_finder, '',
283
+ ['class TrivialFoo',
284
+ ' def foo_bar=(foo, bar)',
285
+ ' @foo_bar = foo + bar',
286
+ ' end',
287
+ ' def universal=(answer=42)',
288
+ ' @universal = answer',
289
+ ' end',
290
+ ' def bar=(bar_value)',
291
+ ' @bar = bar_value',
292
+ ' end',
293
+ 'end'])
294
+ expect(trivial_accessors_finder.offences.size).to eq(2)
295
+ expect(trivial_accessors_finder.offences
296
+ .map(&:line_number).sort).to eq([5, 8])
297
+ end
298
+
299
+ it 'does not find trivial accessors with method calls' do
300
+ inspect_source(trivial_accessors_finder, '',
301
+ ['class TrivialFoo',
302
+ ' def foo_bar(foo)',
303
+ ' foo_bar = foo + 42',
304
+ ' end',
305
+ ' def foo(value)',
306
+ ' foo = []',
307
+ ' # do stuff',
308
+ ' foo',
309
+ ' end',
310
+ ' def bar',
311
+ ' foo_method',
312
+ ' end',
313
+ 'end'])
314
+ expect(trivial_accessors_finder.offences).to be_empty
315
+ end
316
+
317
+ it 'does not find trivial writer with exceptions' do
318
+ inspect_source(trivial_accessors_finder, '',
319
+ [' def expiration_formatted=(value)',
320
+ ' begin',
321
+ ' @expiration = foo_stuff',
322
+ ' rescue ArgumentError',
323
+ ' @expiration = nil',
324
+ ' end',
325
+ ' self[:expiration] = @expiration',
326
+ ' end'])
327
+ expect(trivial_accessors_finder.offences).to be_empty
328
+ end
329
+
330
+ end # describe TrivialAccessors
331
+ end # Cop
332
+ end # Rubocop