rubocop 0.2.1 → 0.3.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 (46) hide show
  1. data/.rubocop.yml +89 -0
  2. data/Rakefile +4 -3
  3. data/VERSION +1 -1
  4. data/lib/rubocop.rb +3 -0
  5. data/lib/rubocop/cli.rb +45 -7
  6. data/lib/rubocop/cop/ampersands_pipes_vs_and_or.rb +25 -0
  7. data/lib/rubocop/cop/blocks.rb +39 -28
  8. data/lib/rubocop/cop/cop.rb +7 -12
  9. data/lib/rubocop/cop/def_parentheses.rb +37 -24
  10. data/lib/rubocop/cop/encoding.rb +4 -4
  11. data/lib/rubocop/cop/if_then_else.rb +28 -15
  12. data/lib/rubocop/cop/offence.rb +1 -1
  13. data/lib/rubocop/cop/space_after_comma_etc.rb +27 -10
  14. data/lib/rubocop/cop/surrounding_space.rb +102 -53
  15. data/lib/rubocop/cop/ternary_operator.rb +27 -10
  16. data/lib/rubocop/cop/unless_else.rb +19 -0
  17. data/lib/rubocop/cop/when_then.rb +25 -0
  18. data/lib/rubocop/report/emacs_style.rb +2 -1
  19. data/rubocop.gemspec +24 -7
  20. data/spec/rubocop/cli_spec.rb +71 -1
  21. data/spec/rubocop/cops/align_parameters_spec.rb +4 -4
  22. data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +57 -0
  23. data/spec/rubocop/cops/cop_spec.rb +2 -2
  24. data/spec/rubocop/cops/{def_parentheses_spec.rb → def_with_parentheses_spec.rb} +3 -18
  25. data/spec/rubocop/cops/def_without_parentheses_spec.rb +26 -0
  26. data/spec/rubocop/cops/encoding_spec.rb +41 -0
  27. data/spec/rubocop/cops/end_of_line_spec.rb +6 -0
  28. data/spec/rubocop/cops/if_with_semicolon_spec.rb +17 -0
  29. data/spec/rubocop/cops/indentation_spec.rb +2 -1
  30. data/spec/rubocop/cops/{blocks_spec.rb → multiline_blocks_spec.rb} +2 -13
  31. data/spec/rubocop/cops/multiline_if_then_spec.rb +56 -0
  32. data/spec/rubocop/cops/one_line_conditional_spec.rb +17 -0
  33. data/spec/rubocop/cops/single_line_blocks_spec.rb +22 -0
  34. data/spec/rubocop/cops/{space_after_comma_etc_spec.rb → space_after_colon_spec.rb} +2 -14
  35. data/spec/rubocop/cops/space_after_comma_spec.rb +17 -0
  36. data/spec/rubocop/cops/space_after_semicolon_spec.rb +17 -0
  37. data/spec/rubocop/cops/space_around_braces_spec.rb +32 -0
  38. data/spec/rubocop/cops/{surrounding_space_spec.rb → space_around_operators_spec.rb} +4 -73
  39. data/spec/rubocop/cops/space_inside_brackets_spec.rb +43 -0
  40. data/spec/rubocop/cops/space_inside_parens_spec.rb +27 -0
  41. data/spec/rubocop/cops/ternary_operator_spec.rb +26 -6
  42. data/spec/rubocop/cops/unless_else_spec.rb +29 -0
  43. data/spec/rubocop/cops/when_then_spec.rb +38 -0
  44. data/spec/spec_helper.rb +5 -0
  45. metadata +25 -8
  46. data/spec/rubocop/cops/if_then_else_spec.rb +0 -74
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe SingleLineBlocks do
8
+ let (:blocks) { SingleLineBlocks.new }
9
+
10
+ it 'registers an offence for a single line block with do-end' do
11
+ inspect_source(blocks, '', ['each do |x| end'])
12
+ blocks.offences.map(&:message).should ==
13
+ ['Prefer {...} over do...end for single-line blocks.']
14
+ end
15
+
16
+ it 'accepts a single line block with braces' do
17
+ inspect_source(blocks, '', ['each { |x| }'])
18
+ blocks.offences.map(&:message).should == []
19
+ end
20
+ end
21
+ end
22
+ end
@@ -4,14 +4,8 @@ require 'spec_helper'
4
4
 
5
5
  module Rubocop
6
6
  module Cop
7
- describe SpaceAfterCommaEtc do
8
- let (:space) { SpaceAfterCommaEtc.new }
9
-
10
- it 'registers an offence for block argument commas' do
11
- inspect_source(space, 'file.rb', ['each { |s,t| }'])
12
- space.offences.map(&:message).should ==
13
- ['Space missing after comma.']
14
- end
7
+ describe SpaceAfterColon do
8
+ let (:space) { SpaceAfterColon.new }
15
9
 
16
10
  it 'registers an offence for colon without space after it' do
17
11
  inspect_source(space, 'file.rb', ['x = w ? {a:3}:4'])
@@ -19,12 +13,6 @@ module Rubocop
19
13
  ['Space missing after colon.'] * 2
20
14
  end
21
15
 
22
- it 'registers an offence for semicolon without space after it' do
23
- inspect_source(space, 'file.rb', ['x = 1;y = 2'])
24
- space.offences.map(&:message).should ==
25
- ['Space missing after semicolon.']
26
- end
27
-
28
16
  it 'allows the colons in symbols' do
29
17
  inspect_source(space, 'file.rb', ['x = :a'])
30
18
  space.offences.map(&:message).should == []
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe SpaceAfterComma do
8
+ let (:space) { SpaceAfterComma.new }
9
+
10
+ it 'registers an offence for block argument commas' do
11
+ inspect_source(space, 'file.rb', ['each { |s,t| }'])
12
+ space.offences.map(&:message).should ==
13
+ ['Space missing after comma.']
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe SpaceAfterSemicolon do
8
+ let (:space) { SpaceAfterSemicolon.new }
9
+
10
+ it 'registers an offence for semicolon without space after it' do
11
+ inspect_source(space, 'file.rb', ['x = 1;y = 2'])
12
+ space.offences.map(&:message).should ==
13
+ ['Space missing after semicolon.']
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe SpaceAroundBraces do
8
+ let (:space) { SpaceAroundBraces.new }
9
+
10
+ it 'registers an offence for left brace without spaces' do
11
+ inspect_source(space, 'file.rb', ['each{ puts }'])
12
+ space.offences.map(&:message).should ==
13
+ ["Surrounding space missing for '{'."]
14
+ end
15
+
16
+ it 'registers an offence for right brace without inner space' do
17
+ inspect_source(space, 'file.rb', ['each { puts}'])
18
+ space.offences.map(&:message).should ==
19
+ ["Space missing to the left of '}'."]
20
+ end
21
+
22
+ it 'accepts an empty hash literal with no space inside' do
23
+ inspect_source(space, 'file.rb',
24
+ ['view_hash.each do |view_key|',
25
+ 'end',
26
+ '@views = {}',
27
+ ''])
28
+ space.offences.map(&:message).should == []
29
+ end
30
+ end
31
+ end
32
+ end
@@ -4,14 +4,13 @@ require 'spec_helper'
4
4
 
5
5
  module Rubocop
6
6
  module Cop
7
- describe SurroundingSpace do
8
- let (:space) { SurroundingSpace.new }
7
+ describe SpaceAroundOperators do
8
+ let (:space) { SpaceAroundOperators.new }
9
9
 
10
10
  it 'registers an offence for assignment without space on both sides' do
11
11
  inspect_source(space, 'file.rb', ['x=0', 'y= 0', 'z =0'])
12
- space.offences.size.should == 3
13
- space.offences.first.message.should ==
14
- "Surrounding space missing for operator '='."
12
+ space.offences.map(&:message).should ==
13
+ ["Surrounding space missing for operator '='."] * 3
15
14
  end
16
15
 
17
16
  it 'registers an offence for binary operators that could be unary' do
@@ -22,63 +21,12 @@ module Rubocop
22
21
  "Surrounding space missing for operator '+'."]
23
22
  end
24
23
 
25
- it 'registers an offence for left brace without spaces' do
26
- inspect_source(space, 'file.rb', ['each{ puts }'])
27
- space.offences.map(&:message).should ==
28
- ["Surrounding space missing for '{'."]
29
- end
30
-
31
- it 'registers an offence for right brace without inner space' do
32
- inspect_source(space, 'file.rb', ['each { puts}'])
33
- space.offences.map(&:message).should ==
34
- ["Space missing to the left of '}'."]
35
- end
36
-
37
- it 'accepts an empty hash literal with no space inside' do
38
- inspect_source(space, 'file.rb',
39
- ['view_hash.each do |view_key|',
40
- 'end',
41
- '@views = {}',
42
- ''])
43
- space.offences.map(&:message).should == []
44
- end
45
-
46
24
  it 'registers an offence for arguments to a method' do
47
25
  inspect_source(space, 'file.rb', ['puts 1+2'])
48
26
  space.offences.map(&:message).should ==
49
27
  ["Surrounding space missing for operator '+'."]
50
28
  end
51
29
 
52
- it 'registers an offence for an array literal with spaces inside' do
53
- inspect_source(space, 'file.rb', ['a = [1, 2 ]',
54
- 'b = [ 1, 2]'])
55
- space.offences.map(&:message).should ==
56
- ['Space inside square brackets detected.',
57
- 'Space inside square brackets detected.']
58
- end
59
-
60
- it 'accepts space inside square brackets if on its own row' do
61
- inspect_source(space, 'file.rb', ['a = [',
62
- ' 1, 2',
63
- ' ]'])
64
- space.offences.map(&:message).should == []
65
- end
66
-
67
- it 'registers an offence for spaces inside parens' do
68
- inspect_source(space, 'file.rb', ['f( 3)',
69
- 'g(3 )'])
70
- space.offences.map(&:message).should ==
71
- ['Space inside parentheses detected.',
72
- 'Space inside parentheses detected.']
73
- end
74
-
75
- it 'accepts parentheses in block parameter list' do
76
- inspect_source(space, 'file.rb',
77
- ['list.inject(Tms.new) { |sum, (label, item)|',
78
- '}'])
79
- space.offences.map(&:message).should == []
80
- end
81
-
82
30
  it 'accepts operator symbols' do
83
31
  inspect_source(space, 'file.rb', ['func(:-)'])
84
32
  space.offences.map(&:message).should == []
@@ -130,12 +78,6 @@ module Rubocop
130
78
  space.offences.map(&:message).should == []
131
79
  end
132
80
 
133
- it 'accepts square brackets as method name' do
134
- inspect_source(space, 'file.rb', ['def Vector.[](*array)',
135
- 'end'])
136
- space.offences.map(&:message).should == []
137
- end
138
-
139
81
  it 'accepts def of operator' do
140
82
  inspect_source(space, 'file.rb', ['def +(other); end'])
141
83
  space.offences.map(&:message).should == []
@@ -175,17 +117,6 @@ module Rubocop
175
117
  'x = +2'])
176
118
  space.offences.map(&:message).should == []
177
119
  end
178
-
179
- it 'accepts square brackets called with method call syntax' do
180
- inspect_source(space, 'file.rb', ['subject.[](0)'])
181
- space.offences.map(&:message).should == []
182
- end
183
-
184
- it 'only reports a single space once' do
185
- inspect_source(space, 'file.rb', ['[ ]'])
186
- space.offences.map(&:message).should ==
187
- ['Space inside square brackets detected.']
188
- end
189
120
  end
190
121
  end
191
122
  end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe SpaceInsideBrackets do
8
+ let (:space) { SpaceInsideBrackets.new }
9
+
10
+ it 'registers an offence for an array literal with spaces inside' do
11
+ inspect_source(space, 'file.rb', ['a = [1, 2 ]',
12
+ 'b = [ 1, 2]'])
13
+ space.offences.map(&:message).should ==
14
+ ['Space inside square brackets detected.',
15
+ 'Space inside square brackets detected.']
16
+ end
17
+
18
+ it 'accepts space inside square brackets if on its own row' do
19
+ inspect_source(space, 'file.rb', ['a = [',
20
+ ' 1, 2',
21
+ ' ]'])
22
+ space.offences.map(&:message).should == []
23
+ end
24
+
25
+ it 'accepts square brackets as method name' do
26
+ inspect_source(space, 'file.rb', ['def Vector.[](*array)',
27
+ 'end'])
28
+ space.offences.map(&:message).should == []
29
+ end
30
+
31
+ it 'accepts square brackets called with method call syntax' do
32
+ inspect_source(space, 'file.rb', ['subject.[](0)'])
33
+ space.offences.map(&:message).should == []
34
+ end
35
+
36
+ it 'only reports a single space once' do
37
+ inspect_source(space, 'file.rb', ['[ ]'])
38
+ space.offences.map(&:message).should ==
39
+ ['Space inside square brackets detected.']
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe SpaceInsideParens do
8
+ let (:space) { SpaceInsideParens.new }
9
+
10
+ it 'registers an offence for spaces inside parens' do
11
+ inspect_source(space, 'file.rb', ['f( 3)',
12
+ 'g(3 )'])
13
+ space.offences.map(&:message).should ==
14
+ ['Space inside parentheses detected.',
15
+ 'Space inside parentheses detected.']
16
+ end
17
+
18
+ it 'accepts parentheses in block parameter list' do
19
+ inspect_source(space, 'file.rb',
20
+ ['list.inject(Tms.new) { |sum, (label, item)|',
21
+ '}'])
22
+ space.offences.map(&:message).should == []
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -4,20 +4,40 @@ require 'spec_helper'
4
4
 
5
5
  module Rubocop
6
6
  module Cop
7
- describe TernaryOperator do
8
- let (:num) { TernaryOperator.new }
7
+ describe MultilineTernaryOperator do
8
+ let (:op) { MultilineTernaryOperator.new }
9
9
 
10
10
  it 'registers an offence for a multiline ternary operator expression' do
11
- inspect_source(num, 'file.rb', ['a = cond ?',
11
+ inspect_source(op, 'file.rb', ['a = cond ?',
12
12
  ' b : c'])
13
- num.offences.map(&:message).should ==
13
+ op.offences.map(&:message).should ==
14
14
  ['Avoid multi-line ?: (the ternary operator); use if/unless ' +
15
15
  'instead.']
16
16
  end
17
17
 
18
18
  it 'accepts a single line ternary operator expression' do
19
- inspect_source(num, 'file.rb', ['a = cond ? b : c'])
20
- num.offences.map(&:message).should == []
19
+ inspect_source(op, 'file.rb', ['a = cond ? b : c'])
20
+ op.offences.map(&:message).should == []
21
+ end
22
+ end
23
+
24
+ describe NestedTernaryOperator do
25
+ let (:op) { NestedTernaryOperator.new }
26
+
27
+ it 'registers an offence for a nested ternary operator expression' do
28
+ inspect_source(op, 'file.rb', ['a ? (b ? b1 : b2) : a2'])
29
+ op.offences.map(&:message).should ==
30
+ ['Ternary operators must not be nested. Prefer if/else constructs ' +
31
+ 'instead.']
32
+ end
33
+
34
+ it 'accepts a non-nested ternary operator within an if' do
35
+ inspect_source(op, 'file.rb', ['a = if x',
36
+ ' cond ? b : c',
37
+ 'else',
38
+ ' d',
39
+ 'end'])
40
+ op.offences.map(&:message).should == []
21
41
  end
22
42
  end
23
43
  end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe UnlessElse do
8
+ let (:ue) { UnlessElse.new }
9
+
10
+ it 'registers an offence for an unless with else' do
11
+ inspect_source(ue, 'file.rb', ['unless x',
12
+ ' a = 1',
13
+ 'else',
14
+ ' a = 0',
15
+ 'end'])
16
+ ue.offences.map(&:message).should ==
17
+ ['Never use unless with else. Rewrite these with the ' +
18
+ 'positive case first.']
19
+ end
20
+
21
+ it 'accepts an unless without else' do
22
+ inspect_source(ue, 'file.rb', ['unless x',
23
+ ' a = 1',
24
+ 'end'])
25
+ ue.offences.map(&:message).should == []
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe WhenThen do
8
+ let (:wt) { WhenThen.new }
9
+
10
+ it 'registers an offence for when x;' do
11
+ inspect_source(wt, 'file.rb', ['case a',
12
+ 'when b; c',
13
+ 'end'])
14
+ wt.offences.map(&:message).should ==
15
+ ['Never use "when x;". Use "when x then" instead.']
16
+ end
17
+
18
+ it 'accepts when x then' do
19
+ inspect_source(wt, 'file.rb', ['case a',
20
+ 'when b then c',
21
+ 'end'])
22
+ wt.offences.map(&:message).should == []
23
+ end
24
+
25
+ it 'accepts ; separating statements in the body of when' do
26
+ inspect_source(wt, 'file.rb', ['case a',
27
+ 'when b then c; d',
28
+ 'end',
29
+ '',
30
+ 'case e',
31
+ 'when f',
32
+ ' g; h',
33
+ 'end'])
34
+ wt.offences.map(&:message).should == []
35
+ end
36
+ end
37
+ end
38
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,10 @@
1
1
  # encoding: utf-8
2
2
 
3
+ if ENV['COVERAGE']
4
+ require 'simplecov'
5
+ SimpleCov.start
6
+ end
7
+
3
8
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
9
  $LOAD_PATH.unshift(File.dirname(__FILE__))
5
10
  require 'rspec'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-12 00:00:00.000000000 Z
12
+ date: 2013-02-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -120,6 +120,7 @@ files:
120
120
  - .document
121
121
  - .rbenv-version
122
122
  - .rspec
123
+ - .rubocop.yml
123
124
  - .rvmrc
124
125
  - .travis.yml
125
126
  - CONTRIBUTING.md
@@ -136,6 +137,7 @@ files:
136
137
  - lib/rubocop.rb
137
138
  - lib/rubocop/cli.rb
138
139
  - lib/rubocop/cop/align_parameters.rb
140
+ - lib/rubocop/cop/ampersands_pipes_vs_and_or.rb
139
141
  - lib/rubocop/cop/blocks.rb
140
142
  - lib/rubocop/cop/cop.rb
141
143
  - lib/rubocop/cop/def_parentheses.rb
@@ -156,6 +158,8 @@ files:
156
158
  - lib/rubocop/cop/tab.rb
157
159
  - lib/rubocop/cop/ternary_operator.rb
158
160
  - lib/rubocop/cop/trailing_whitespace.rb
161
+ - lib/rubocop/cop/unless_else.rb
162
+ - lib/rubocop/cop/when_then.rb
159
163
  - lib/rubocop/report/emacs_style.rb
160
164
  - lib/rubocop/report/plain_text.rb
161
165
  - lib/rubocop/report/report.rb
@@ -163,25 +167,38 @@ files:
163
167
  - rubocop.gemspec
164
168
  - spec/rubocop/cli_spec.rb
165
169
  - spec/rubocop/cops/align_parameters_spec.rb
166
- - spec/rubocop/cops/blocks_spec.rb
170
+ - spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb
167
171
  - spec/rubocop/cops/cop_spec.rb
168
- - spec/rubocop/cops/def_parentheses_spec.rb
172
+ - spec/rubocop/cops/def_with_parentheses_spec.rb
173
+ - spec/rubocop/cops/def_without_parentheses_spec.rb
169
174
  - spec/rubocop/cops/empty_lines_spec.rb
175
+ - spec/rubocop/cops/encoding_spec.rb
170
176
  - spec/rubocop/cops/end_of_line_spec.rb
171
177
  - spec/rubocop/cops/grammar_spec.rb
172
178
  - spec/rubocop/cops/hash_syntax_spec.rb
173
- - spec/rubocop/cops/if_then_else_spec.rb
179
+ - spec/rubocop/cops/if_with_semicolon_spec.rb
174
180
  - spec/rubocop/cops/indentation_spec.rb
175
181
  - spec/rubocop/cops/line_length_spec.rb
182
+ - spec/rubocop/cops/multiline_blocks_spec.rb
183
+ - spec/rubocop/cops/multiline_if_then_spec.rb
176
184
  - spec/rubocop/cops/numeric_literals_spec.rb
177
185
  - spec/rubocop/cops/offence_spec.rb
186
+ - spec/rubocop/cops/one_line_conditional_spec.rb
178
187
  - spec/rubocop/cops/parameter_lists_spec.rb
179
- - spec/rubocop/cops/space_after_comma_etc_spec.rb
188
+ - spec/rubocop/cops/single_line_blocks_spec.rb
189
+ - spec/rubocop/cops/space_after_colon_spec.rb
190
+ - spec/rubocop/cops/space_after_comma_spec.rb
191
+ - spec/rubocop/cops/space_after_semicolon_spec.rb
192
+ - spec/rubocop/cops/space_around_braces_spec.rb
193
+ - spec/rubocop/cops/space_around_operators_spec.rb
194
+ - spec/rubocop/cops/space_inside_brackets_spec.rb
195
+ - spec/rubocop/cops/space_inside_parens_spec.rb
180
196
  - spec/rubocop/cops/string_literals_spec.rb
181
- - spec/rubocop/cops/surrounding_space_spec.rb
182
197
  - spec/rubocop/cops/tab_spec.rb
183
198
  - spec/rubocop/cops/ternary_operator_spec.rb
184
199
  - spec/rubocop/cops/trailing_whitespace_spec.rb
200
+ - spec/rubocop/cops/unless_else_spec.rb
201
+ - spec/rubocop/cops/when_then_spec.rb
185
202
  - spec/rubocop/reports/emacs_style_spec.rb
186
203
  - spec/rubocop/reports/report_spec.rb
187
204
  - spec/spec_helper.rb
@@ -200,7 +217,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
200
217
  version: '0'
201
218
  segments:
202
219
  - 0
203
- hash: 3978981872359853745
220
+ hash: 1293678541270492107
204
221
  required_rubygems_version: !ruby/object:Gem::Requirement
205
222
  none: false
206
223
  requirements: