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.
- data/.rubocop.yml +89 -0
- data/Rakefile +4 -3
- data/VERSION +1 -1
- data/lib/rubocop.rb +3 -0
- data/lib/rubocop/cli.rb +45 -7
- data/lib/rubocop/cop/ampersands_pipes_vs_and_or.rb +25 -0
- data/lib/rubocop/cop/blocks.rb +39 -28
- data/lib/rubocop/cop/cop.rb +7 -12
- data/lib/rubocop/cop/def_parentheses.rb +37 -24
- data/lib/rubocop/cop/encoding.rb +4 -4
- data/lib/rubocop/cop/if_then_else.rb +28 -15
- data/lib/rubocop/cop/offence.rb +1 -1
- data/lib/rubocop/cop/space_after_comma_etc.rb +27 -10
- data/lib/rubocop/cop/surrounding_space.rb +102 -53
- data/lib/rubocop/cop/ternary_operator.rb +27 -10
- data/lib/rubocop/cop/unless_else.rb +19 -0
- data/lib/rubocop/cop/when_then.rb +25 -0
- data/lib/rubocop/report/emacs_style.rb +2 -1
- data/rubocop.gemspec +24 -7
- data/spec/rubocop/cli_spec.rb +71 -1
- data/spec/rubocop/cops/align_parameters_spec.rb +4 -4
- data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +57 -0
- data/spec/rubocop/cops/cop_spec.rb +2 -2
- data/spec/rubocop/cops/{def_parentheses_spec.rb → def_with_parentheses_spec.rb} +3 -18
- data/spec/rubocop/cops/def_without_parentheses_spec.rb +26 -0
- data/spec/rubocop/cops/encoding_spec.rb +41 -0
- data/spec/rubocop/cops/end_of_line_spec.rb +6 -0
- data/spec/rubocop/cops/if_with_semicolon_spec.rb +17 -0
- data/spec/rubocop/cops/indentation_spec.rb +2 -1
- data/spec/rubocop/cops/{blocks_spec.rb → multiline_blocks_spec.rb} +2 -13
- data/spec/rubocop/cops/multiline_if_then_spec.rb +56 -0
- data/spec/rubocop/cops/one_line_conditional_spec.rb +17 -0
- data/spec/rubocop/cops/single_line_blocks_spec.rb +22 -0
- data/spec/rubocop/cops/{space_after_comma_etc_spec.rb → space_after_colon_spec.rb} +2 -14
- data/spec/rubocop/cops/space_after_comma_spec.rb +17 -0
- data/spec/rubocop/cops/space_after_semicolon_spec.rb +17 -0
- data/spec/rubocop/cops/space_around_braces_spec.rb +32 -0
- data/spec/rubocop/cops/{surrounding_space_spec.rb → space_around_operators_spec.rb} +4 -73
- data/spec/rubocop/cops/space_inside_brackets_spec.rb +43 -0
- data/spec/rubocop/cops/space_inside_parens_spec.rb +27 -0
- data/spec/rubocop/cops/ternary_operator_spec.rb +26 -6
- data/spec/rubocop/cops/unless_else_spec.rb +29 -0
- data/spec/rubocop/cops/when_then_spec.rb +38 -0
- data/spec/spec_helper.rb +5 -0
- metadata +25 -8
- data/spec/rubocop/cops/if_then_else_spec.rb +0 -74
data/spec/rubocop/cli_spec.rb
CHANGED
@@ -14,7 +14,9 @@ module Rubocop
|
|
14
14
|
lambda { cli.run ['--help'] }.should exit_with_code(0)
|
15
15
|
message = ['Usage: rubocop [options] [file1, file2, ...]',
|
16
16
|
' -v, --[no-]verbose Run verbosely',
|
17
|
-
' -e, --emacs Emacs style output'
|
17
|
+
' -e, --emacs Emacs style output',
|
18
|
+
' -c, --config FILE Configuration file',
|
19
|
+
' -s, --silent Silence summary']
|
18
20
|
$stdout.string.should == (message * 2).join("\n") + "\n"
|
19
21
|
end
|
20
22
|
|
@@ -67,6 +69,74 @@ module Rubocop
|
|
67
69
|
end
|
68
70
|
end
|
69
71
|
|
72
|
+
it 'ommits summary when --silent passed' do
|
73
|
+
File.open('example1.rb', 'w') { |f| f.puts 'x = 0 ' }
|
74
|
+
File.open('example2.rb', 'w') { |f| f.puts "\tx = 0" }
|
75
|
+
begin
|
76
|
+
cli.run(['--emacs',
|
77
|
+
'--silent',
|
78
|
+
'example1.rb',
|
79
|
+
'example2.rb']).should == 1
|
80
|
+
$stdout.string.should ==
|
81
|
+
['example1.rb:1: C: Missing encoding comment.',
|
82
|
+
'example1.rb:1: C: Trailing whitespace detected.',
|
83
|
+
'example2.rb:1: C: Missing encoding comment.',
|
84
|
+
'example2.rb:1: C: Tab detected.',
|
85
|
+
''].join("\n")
|
86
|
+
ensure
|
87
|
+
File.delete 'example1.rb'
|
88
|
+
File.delete 'example2.rb'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'can be configured with option to disable a certain error' do
|
93
|
+
File.open('example1.rb', 'w') { |f| f.puts 'x = 0 ' }
|
94
|
+
File.open('rubocop.yml', 'w') do |f|
|
95
|
+
f.puts('Encoding:',
|
96
|
+
' Enabled: false',
|
97
|
+
'',
|
98
|
+
'Indentation:',
|
99
|
+
' Enabled: false')
|
100
|
+
end
|
101
|
+
begin
|
102
|
+
return_code = cli.run(['-c', 'rubocop.yml', 'example1.rb'])
|
103
|
+
$stdout.string.should ==
|
104
|
+
['== example1.rb ==',
|
105
|
+
'C: 1: Trailing whitespace detected.',
|
106
|
+
'',
|
107
|
+
'1 files inspected, 1 offences detected',
|
108
|
+
''].join("\n")
|
109
|
+
return_code.should == 1
|
110
|
+
ensure
|
111
|
+
File.delete 'example1.rb'
|
112
|
+
File.delete 'rubocop.yml'
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'can be configured with project config to disable a certain error' do
|
117
|
+
FileUtils.mkdir 'example_src'
|
118
|
+
File.open('example_src/example1.rb', 'w') { |f| f.puts 'x = 0 ' }
|
119
|
+
File.open('example_src/.rubocop.yml', 'w') do |f|
|
120
|
+
f.puts('Encoding:',
|
121
|
+
' Enabled: false',
|
122
|
+
'',
|
123
|
+
'Indentation:',
|
124
|
+
' Enabled: false')
|
125
|
+
end
|
126
|
+
begin
|
127
|
+
return_code = cli.run(['example_src/example1.rb'])
|
128
|
+
$stdout.string.should ==
|
129
|
+
['== example_src/example1.rb ==',
|
130
|
+
'C: 1: Trailing whitespace detected.',
|
131
|
+
'',
|
132
|
+
'1 files inspected, 1 offences detected',
|
133
|
+
''].join("\n")
|
134
|
+
return_code.should == 1
|
135
|
+
ensure
|
136
|
+
FileUtils.rm_rf 'example_src'
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
70
140
|
it 'finds no violations when checking the rubocop source code' do
|
71
141
|
cli.run
|
72
142
|
$stdout.string.should =~ /files inspected, 0 offences detected\n/
|
@@ -54,20 +54,20 @@ module Rubocop
|
|
54
54
|
'func3(*a)',
|
55
55
|
])
|
56
56
|
align.offences.map(&:to_s).should ==
|
57
|
-
['C: 5: Align the parameters of a method call if they span
|
58
|
-
'than one line.']
|
57
|
+
['C: 5: Align the parameters of a method call if they span ' +
|
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
|
-
['add_offence(
|
63
|
+
['add_offence(:convention, x,',
|
64
64
|
' a)'])
|
65
65
|
align.offences.map(&:message).should == []
|
66
66
|
end
|
67
67
|
|
68
68
|
it 'can handle a string literal as other argument' do
|
69
69
|
inspect_source(align, '',
|
70
|
-
['add_offence(
|
70
|
+
['add_offence(:convention,',
|
71
71
|
' "", a)'])
|
72
72
|
align.offences.map(&:message).should == []
|
73
73
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe AmpersandsPipesVsAndOr do
|
8
|
+
let (:amp) { AmpersandsPipesVsAndOr.new }
|
9
|
+
|
10
|
+
it 'registers an offence for AND used in condition of if statement' do
|
11
|
+
check('if', 'and', '&&')
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'registers an offence for OR used in condition of if statement' do
|
15
|
+
check('if', 'or', '||')
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'registers an offence for AND used in condition of unless' do
|
19
|
+
check('unless', 'and', '&&')
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'registers an offence for OR used in condition of unless' do
|
23
|
+
check('unless', 'or', '||')
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'registers an offence for AND used in condition of while' do
|
27
|
+
check('while', 'and', '&&')
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'registers an offence for OR used in condition of while' do
|
31
|
+
check('while', 'or', '||')
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'registers an offence for AND used in condition of until' do
|
35
|
+
check('until', 'and', '&&')
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'registers an offence for OR used in condition of until' do
|
39
|
+
check('until', 'or', '||')
|
40
|
+
end
|
41
|
+
|
42
|
+
def check(keyword, bad_operator, good_operator)
|
43
|
+
inspect_source(amp, 'file.rb', ["#{keyword} a #{bad_operator} b",
|
44
|
+
' c',
|
45
|
+
'end',
|
46
|
+
"#{keyword} a #{good_operator} b",
|
47
|
+
' c',
|
48
|
+
'end'])
|
49
|
+
# Just one offence should be registered. The good_operator
|
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
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -16,13 +16,13 @@ module Rubocop
|
|
16
16
|
end
|
17
17
|
|
18
18
|
it 'keeps track of offences' do
|
19
|
-
cop.add_offence(
|
19
|
+
cop.add_offence(:convention, 1, 'message')
|
20
20
|
|
21
21
|
cop.offences.size.should == 1
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'will report registered offences' do
|
25
|
-
cop.add_offence(
|
25
|
+
cop.add_offence(:convention, 1, 'message')
|
26
26
|
|
27
27
|
cop.has_report?.should be_true
|
28
28
|
end
|
@@ -4,16 +4,8 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module Rubocop
|
6
6
|
module Cop
|
7
|
-
describe
|
8
|
-
let (:def_par) {
|
9
|
-
|
10
|
-
it 'reports an offence for def with parameters but no parens' do
|
11
|
-
src = ['def func a, b',
|
12
|
-
'end']
|
13
|
-
inspect_source(def_par, '', src)
|
14
|
-
def_par.offences.map(&:message).should ==
|
15
|
-
['Use def with parentheses when there are arguments.']
|
16
|
-
end
|
7
|
+
describe DefWithParentheses do
|
8
|
+
let (:def_par) { DefWithParentheses.new }
|
17
9
|
|
18
10
|
it 'reports an offence for def with empty parens' do
|
19
11
|
src = ['def func()',
|
@@ -31,18 +23,11 @@ module Rubocop
|
|
31
23
|
def_par.offences.map(&:message).should == []
|
32
24
|
end
|
33
25
|
|
34
|
-
it 'accepts def with no args and no parens' do
|
35
|
-
src = ['def func',
|
36
|
-
'end']
|
37
|
-
inspect_source(def_par, '', src)
|
38
|
-
def_par.offences.map(&:message).should == []
|
39
|
-
end
|
40
|
-
|
41
26
|
it 'accepts empty parentheses in one liners' do
|
42
27
|
src = ["def to_s() join '/' end"]
|
43
28
|
inspect_source(def_par, '', src)
|
44
29
|
def_par.offences.map(&:message).should == []
|
45
|
-
|
30
|
+
end
|
46
31
|
end
|
47
32
|
end
|
48
33
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe DefWithoutParentheses do
|
8
|
+
let (:def_par) { DefWithoutParentheses.new }
|
9
|
+
|
10
|
+
it 'reports an offence for def with parameters but no parens' do
|
11
|
+
src = ['def func a, b',
|
12
|
+
'end']
|
13
|
+
inspect_source(def_par, '', src)
|
14
|
+
def_par.offences.map(&:message).should ==
|
15
|
+
['Use def with parentheses when there are arguments.']
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'accepts def with no args and no parens' do
|
19
|
+
src = ['def func',
|
20
|
+
'end']
|
21
|
+
inspect_source(def_par, '', src)
|
22
|
+
def_par.offences.map(&:message).should == []
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe Encoding do
|
8
|
+
let (:encoding) { Encoding.new }
|
9
|
+
|
10
|
+
it 'registers an offence when no encoding present' do
|
11
|
+
inspect_source(encoding, 'file.rb', ['def foo() end'])
|
12
|
+
|
13
|
+
encoding.offences.map(&:message).should ==
|
14
|
+
['Missing encoding comment.']
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'accepts encoding on first line' do
|
18
|
+
inspect_source(encoding, 'file.rb', ['# encoding: utf-8',
|
19
|
+
'def foo() end'])
|
20
|
+
|
21
|
+
encoding.offences.should == []
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'accepts encoding on second line when shebang present' do
|
25
|
+
inspect_source(encoding, 'file.rb', ['#!/usr/bin/env ruby',
|
26
|
+
'# encoding: utf-8',
|
27
|
+
'def foo() end'])
|
28
|
+
|
29
|
+
encoding.offences.map(&:message).should == []
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'registers an offence when encoding is in the wrong place' do
|
33
|
+
inspect_source(encoding, 'file.rb', ['def foo() end',
|
34
|
+
'# encoding: utf-8'])
|
35
|
+
|
36
|
+
encoding.offences.map(&:message).should ==
|
37
|
+
['Missing encoding comment.']
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -12,6 +12,12 @@ module Rubocop
|
|
12
12
|
eol.offences.map(&:message).should ==
|
13
13
|
['Carriage return character detected.']
|
14
14
|
end
|
15
|
+
|
16
|
+
it 'registers an offence for CR at end of file' do
|
17
|
+
inspect_source(eol, 'file.rb', ["x=0\r"])
|
18
|
+
eol.offences.map(&:message).should ==
|
19
|
+
['Carriage return character detected.']
|
20
|
+
end
|
15
21
|
end
|
16
22
|
end
|
17
23
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe IfWithSemicolon do
|
8
|
+
let (:iws) { IfWithSemicolon.new }
|
9
|
+
|
10
|
+
it 'registers an offence for one line if/;/end' do
|
11
|
+
inspect_source(iws, '', ['if cond; run else dont end'])
|
12
|
+
iws.offences.map(&:message).should ==
|
13
|
+
['Never use if x; Use the ternary operator instead.']
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -15,7 +15,8 @@ module Rubocop
|
|
15
15
|
' end',
|
16
16
|
'end']
|
17
17
|
inspect_source(ind, 'file.rb', source)
|
18
|
-
ind.offences.
|
18
|
+
ind.offences.map(&:message).should ==
|
19
|
+
['Indent when as deep as case.'] * 2
|
19
20
|
end
|
20
21
|
|
21
22
|
it "accepts a when clause that's equally indented with case" do
|
@@ -4,8 +4,8 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module Rubocop
|
6
6
|
module Cop
|
7
|
-
describe
|
8
|
-
let (:blocks) {
|
7
|
+
describe MultilineBlocks do
|
8
|
+
let (:blocks) { MultilineBlocks.new }
|
9
9
|
|
10
10
|
it 'registers an offence for a multiline block with braces' do
|
11
11
|
inspect_source(blocks, '', ['each { |x|',
|
@@ -14,17 +14,6 @@ module Rubocop
|
|
14
14
|
['Avoid using {...} for multi-line blocks.']
|
15
15
|
end
|
16
16
|
|
17
|
-
it 'registers an offence for a single line block with do-end' do
|
18
|
-
inspect_source(blocks, '', ['each do |x| end'])
|
19
|
-
blocks.offences.map(&:message).should ==
|
20
|
-
['Prefer {...} over do...end for single-line blocks.']
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'accepts a single line block with braces' do
|
24
|
-
inspect_source(blocks, '', ['each { |x| }'])
|
25
|
-
blocks.offences.map(&:message).should == []
|
26
|
-
end
|
27
|
-
|
28
17
|
it 'accepts a multiline block with do-end' do
|
29
18
|
inspect_source(blocks, '', ['each do |x|',
|
30
19
|
'end'])
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe MultilineIfThen do
|
8
|
+
let (:mit) { MultilineIfThen.new }
|
9
|
+
|
10
|
+
# if
|
11
|
+
|
12
|
+
it 'registers an offence for then in multiline if' do
|
13
|
+
inspect_source(mit, '', ['if cond then',
|
14
|
+
'end',
|
15
|
+
"if cond then\t",
|
16
|
+
'end',
|
17
|
+
'if cond then ',
|
18
|
+
'end',
|
19
|
+
'if cond then # bad',
|
20
|
+
'end'])
|
21
|
+
mit.offences.map(&:line_number).should == [1, 3, 5, 7]
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'accepts multiline if without then' do
|
25
|
+
inspect_source(mit, '', ['if cond',
|
26
|
+
'end'])
|
27
|
+
mit.offences.map(&:message).should == []
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'accepts table style if/then/elsif/ends' do
|
31
|
+
inspect_source(mit, '',
|
32
|
+
['if @io == $stdout then str << "$stdout"',
|
33
|
+
'elsif @io == $stdin then str << "$stdin"',
|
34
|
+
'elsif @io == $stderr then str << "$stderr"',
|
35
|
+
'else str << @io.class.to_s',
|
36
|
+
'end'])
|
37
|
+
mit.offences.map(&:message).should == []
|
38
|
+
end
|
39
|
+
|
40
|
+
# unless
|
41
|
+
|
42
|
+
it 'registers an offence for then in multiline unless' do
|
43
|
+
inspect_source(mit, '', ['unless cond then',
|
44
|
+
'end'])
|
45
|
+
mit.offences.map(&:message).should ==
|
46
|
+
['Never use then for multi-line if/unless.']
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'accepts multiline unless without then' do
|
50
|
+
inspect_source(mit, '', ['unless cond',
|
51
|
+
'end'])
|
52
|
+
mit.offences.map(&:message).should == []
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe OneLineConditional do
|
8
|
+
let (:olc) { OneLineConditional.new }
|
9
|
+
|
10
|
+
it 'registers an offence for one line if/then/end' do
|
11
|
+
inspect_source(olc, '', ['if cond then run else dont end'])
|
12
|
+
olc.offences.map(&:message).should ==
|
13
|
+
['Favor the ternary operator (?:) over if/then/else/end constructs.']
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|