rubocop 0.1.0 → 0.2.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/.rbenv-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +8 -8
- data/README.md +17 -1
- data/VERSION +1 -1
- data/bin/rubocop +1 -1
- data/features/step_definitions/rubocop_steps.rb +1 -0
- data/features/support/env.rb +2 -0
- data/lib/rubocop.rb +6 -0
- data/lib/rubocop/cli.rb +37 -17
- data/lib/rubocop/cop/align_parameters.rb +112 -0
- data/lib/rubocop/cop/cop.rb +43 -21
- data/lib/rubocop/cop/def_parentheses.rb +38 -0
- data/lib/rubocop/cop/empty_lines.rb +7 -6
- data/lib/rubocop/cop/encoding.rb +1 -1
- data/lib/rubocop/cop/end_of_line.rb +17 -0
- data/lib/rubocop/cop/grammar.rb +69 -9
- data/lib/rubocop/cop/hash_syntax.rb +26 -0
- data/lib/rubocop/cop/if_then_else.rb +49 -0
- data/lib/rubocop/cop/indentation.rb +16 -27
- data/lib/rubocop/cop/line_length.rb +2 -2
- data/lib/rubocop/cop/numeric_literals.rb +19 -0
- data/lib/rubocop/cop/offence.rb +2 -3
- data/lib/rubocop/cop/space_after_comma_etc.rb +10 -9
- data/lib/rubocop/cop/surrounding_space.rb +66 -17
- data/lib/rubocop/cop/tab.rb +2 -2
- data/lib/rubocop/cop/trailing_whitespace.rb +2 -2
- data/lib/rubocop/report/emacs_style.rb +4 -3
- data/rubocop.gemspec +16 -2
- data/spec/rubocop/cli_spec.rb +20 -5
- data/spec/rubocop/cops/align_parameters_spec.rb +201 -0
- data/spec/rubocop/cops/cop_spec.rb +4 -2
- data/spec/rubocop/cops/def_parentheses_spec.rb +48 -0
- data/spec/rubocop/cops/empty_lines_spec.rb +9 -8
- data/spec/rubocop/cops/end_of_line_spec.rb +17 -0
- data/spec/rubocop/cops/grammar_spec.rb +51 -11
- data/spec/rubocop/cops/hash_syntax_spec.rb +44 -0
- data/spec/rubocop/cops/if_then_else_spec.rb +74 -0
- data/spec/rubocop/cops/indentation_spec.rb +29 -4
- data/spec/rubocop/cops/line_length_spec.rb +4 -2
- data/spec/rubocop/cops/numeric_literals_spec.rb +49 -0
- data/spec/rubocop/cops/offence_spec.rb +4 -3
- data/spec/rubocop/cops/space_after_comma_etc_spec.rb +7 -5
- data/spec/rubocop/cops/surrounding_space_spec.rb +89 -26
- data/spec/rubocop/cops/tab_spec.rb +4 -2
- data/spec/rubocop/cops/trailing_whitespace_spec.rb +5 -3
- data/spec/rubocop/reports/emacs_style_spec.rb +4 -2
- data/spec/rubocop/reports/report_spec.rb +3 -1
- data/spec/spec_helper.rb +9 -1
- metadata +17 -3
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe EndOfLine do
|
8
|
+
let (:eol) { EndOfLine.new }
|
9
|
+
|
10
|
+
it 'registers an offence for CR+LF' do
|
11
|
+
inspect_source(eol, 'file.rb', ["x=0\r", ''])
|
12
|
+
eol.offences.map(&:message).should ==
|
13
|
+
['Carriage return character detected.']
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -1,22 +1,62 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module Rubocop
|
4
6
|
module Cop
|
5
7
|
describe Grammar do
|
6
|
-
EXAMPLE = '3.times { |i| x =
|
7
|
-
|
8
|
+
EXAMPLE = '3.times { |i| x = "#{y}#{z}}" }'
|
9
|
+
tokens = Ripper.lex(EXAMPLE).map { |t| Token.new(*t) }
|
10
|
+
let (:grammar) { Grammar.new(tokens) }
|
8
11
|
|
9
12
|
it "correlates token indices to grammar paths" do
|
10
13
|
method_block = [:program, :method_add_block]
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
14
|
+
brace_block = method_block + [:brace_block]
|
15
|
+
Ripper.lex(EXAMPLE).should ==
|
16
|
+
[[[1, 0], :on_int, "3"],
|
17
|
+
[[1, 1], :on_period, "."],
|
18
|
+
[[1, 2], :on_ident, "times"],
|
19
|
+
[[1, 7], :on_sp, " "],
|
20
|
+
[[1, 8], :on_lbrace, "{"],
|
21
|
+
[[1, 9], :on_sp, " "], # 5
|
22
|
+
[[1, 10], :on_op, "|"],
|
23
|
+
[[1, 11], :on_ident, "i"],
|
24
|
+
[[1, 12], :on_op, "|"],
|
25
|
+
[[1, 13], :on_sp, " "],
|
26
|
+
[[1, 14], :on_ident, "x"], # 10
|
27
|
+
[[1, 15], :on_sp, " "],
|
28
|
+
[[1, 16], :on_op, "="],
|
29
|
+
[[1, 17], :on_sp, " "],
|
30
|
+
[[1, 18], :on_tstring_beg, "\""],
|
31
|
+
[[1, 19], :on_embexpr_beg, "\#{"], # 15
|
32
|
+
[[1, 21], :on_ident, "y"],
|
33
|
+
[[1, 22], :on_rbrace, "}"],
|
34
|
+
[[1, 23], :on_embexpr_beg, "\#{"],
|
35
|
+
[[1, 25], :on_ident, "z"],
|
36
|
+
[[1, 26], :on_rbrace, "}"], # 20
|
37
|
+
[[1, 27], :on_tstring_content, "}"],
|
38
|
+
[[1, 28], :on_tstring_end, "\""],
|
39
|
+
[[1, 29], :on_sp, " "],
|
40
|
+
[[1, 30], :on_rbrace, "}"]]
|
41
|
+
|
42
|
+
sexp = Ripper.sexp(EXAMPLE)
|
43
|
+
Position.make_position_objects(sexp)
|
44
|
+
grammar.correlate(sexp).should == {
|
45
|
+
0 => method_block + [:call, :@int], # 3
|
46
|
+
2 => method_block + [:call, :@ident], # times
|
47
|
+
4 => brace_block, # {
|
48
|
+
6 => brace_block + [:block_var], # |
|
49
|
+
7 => brace_block + [:block_var, :params, :@ident], # i
|
50
|
+
8 => brace_block + [:block_var], # |
|
51
|
+
10 => brace_block + [:assign, :var_field, :@ident], # x
|
52
|
+
12 => brace_block + [:assign], # =
|
53
|
+
16 => brace_block + [:assign, :string_literal, :string_content,
|
54
|
+
:string_embexpr, :vcall, :@ident], # y
|
55
|
+
19 => brace_block + [:assign, :string_literal, :string_content,
|
56
|
+
:string_embexpr, :vcall, :@ident], # z
|
57
|
+
21 => brace_block + [:assign, :string_literal, :string_content,
|
58
|
+
:@tstring_content], # }
|
59
|
+
24 => brace_block, # }
|
20
60
|
}
|
21
61
|
end
|
22
62
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe HashSyntax do
|
8
|
+
let (:hash_syntax) { HashSyntax.new }
|
9
|
+
|
10
|
+
it 'registers an offence for hash rocket syntax when new is possible' do
|
11
|
+
inspect_source(hash_syntax, "", ['x = { :a => 0 }'])
|
12
|
+
hash_syntax.offences.map(&:message).should ==
|
13
|
+
['Ruby 1.8 hash syntax detected']
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'registers an offence for mixed syntax when new is possible' do
|
17
|
+
inspect_source(hash_syntax, "", ['x = { :a => 0, b: 1 }'])
|
18
|
+
hash_syntax.offences.map(&:message).should ==
|
19
|
+
['Ruby 1.8 hash syntax detected']
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'registers an offence for hash rockets in method calls' do
|
23
|
+
inspect_source(hash_syntax, "", ['func(3, :a => 0)'])
|
24
|
+
hash_syntax.offences.map(&:message).should ==
|
25
|
+
['Ruby 1.8 hash syntax detected']
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'accepts hash rockets when keys have different types' do
|
29
|
+
inspect_source(hash_syntax, "", ['x = { :a => 0, "b" => 1 }'])
|
30
|
+
hash_syntax.offences.map(&:message).should == []
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'accepts new syntax in a hash literal' do
|
34
|
+
inspect_source(hash_syntax, "", ['x = { a: 0, b: 1 }'])
|
35
|
+
hash_syntax.offences.map(&:message).should == []
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'accepts new syntax in method calls' do
|
39
|
+
inspect_source(hash_syntax, "", ['func(3, a: 0)'])
|
40
|
+
hash_syntax.offences.map(&:message).should == []
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe IfThenElse do
|
8
|
+
let (:if_then_else) { IfThenElse.new }
|
9
|
+
|
10
|
+
# if
|
11
|
+
|
12
|
+
it 'registers an offence for then in multiline if' do
|
13
|
+
inspect_source(if_then_else, '', ['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
|
+
if_then_else.offences.size.should == 4
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'accepts multiline if without then' do
|
25
|
+
inspect_source(if_then_else, '', ['if cond',
|
26
|
+
'end'])
|
27
|
+
if_then_else.offences.map(&:message).sort.should == []
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'registers an offence for one line if/then/end' do
|
31
|
+
inspect_source(if_then_else, '', ['if cond then run else dont end'])
|
32
|
+
if_then_else.offences.map(&:message).sort.should ==
|
33
|
+
['Favor the ternary operator (?:) over if/then/else/end constructs.']
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'registers an offence for one line if/;/end' do
|
37
|
+
inspect_source(if_then_else, '', ['if cond; run else dont end'])
|
38
|
+
if_then_else.offences.map(&:message).sort.should ==
|
39
|
+
['Never use if x; Use the ternary operator instead.']
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'accepts table style if/then/elsif/ends' do
|
43
|
+
inspect_source(if_then_else, '',
|
44
|
+
['if @io == $stdout then str << "$stdout"',
|
45
|
+
'elsif @io == $stdin then str << "$stdin"',
|
46
|
+
'elsif @io == $stderr then str << "$stderr"',
|
47
|
+
'else str << @io.class.to_s',
|
48
|
+
'end'])
|
49
|
+
if_then_else.offences.map(&:message).sort.should == []
|
50
|
+
end
|
51
|
+
|
52
|
+
# unless
|
53
|
+
|
54
|
+
it 'registers an offence for then in multiline unless' do
|
55
|
+
inspect_source(if_then_else, '', ['unless cond then',
|
56
|
+
'end'])
|
57
|
+
if_then_else.offences.map(&:message).sort.should ==
|
58
|
+
['Never use then for multi-line if/unless.']
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'accepts multiline unless without then' do
|
62
|
+
inspect_source(if_then_else, '', ['unless cond',
|
63
|
+
'end'])
|
64
|
+
if_then_else.offences.map(&:message).sort.should == []
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'registers an offence for one line unless/then/ends' do
|
68
|
+
inspect_source(if_then_else, '', ['unless cond then run end'])
|
69
|
+
if_then_else.offences.map(&:message).sort.should ==
|
70
|
+
['Favor the ternary operator (?:) over if/then/else/end constructs.']
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module Rubocop
|
@@ -12,7 +14,7 @@ module Rubocop
|
|
12
14
|
' when 1 then return',
|
13
15
|
' end',
|
14
16
|
'end']
|
15
|
-
|
17
|
+
inspect_source(ind, 'file.rb', source)
|
16
18
|
ind.offences.size.should == 2
|
17
19
|
end
|
18
20
|
|
@@ -29,7 +31,7 @@ module Rubocop
|
|
29
31
|
'when 2 then encoding',
|
30
32
|
'end',
|
31
33
|
'']
|
32
|
-
|
34
|
+
inspect_source(ind, 'file.rb', source)
|
33
35
|
ind.offences.size.should == 0
|
34
36
|
end
|
35
37
|
|
@@ -39,7 +41,7 @@ module Rubocop
|
|
39
41
|
'when 0',
|
40
42
|
'end',
|
41
43
|
'']
|
42
|
-
|
44
|
+
inspect_source(ind, 'file.rb', source)
|
43
45
|
ind.offences.map(&:message).should == []
|
44
46
|
end
|
45
47
|
|
@@ -52,7 +54,30 @@ module Rubocop
|
|
52
54
|
' MethodCallNode',
|
53
55
|
'end',
|
54
56
|
'']
|
55
|
-
|
57
|
+
inspect_source(ind, 'file.rb', source)
|
58
|
+
ind.offences.map(&:message).should == []
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'accepts correctly indented whens in complex combinations' do
|
62
|
+
source = ['each {',
|
63
|
+
' case state',
|
64
|
+
' when 0',
|
65
|
+
' case name',
|
66
|
+
' when :a',
|
67
|
+
' end',
|
68
|
+
' when 1',
|
69
|
+
' loop {',
|
70
|
+
' case name',
|
71
|
+
' when :b',
|
72
|
+
' end',
|
73
|
+
' }',
|
74
|
+
' end',
|
75
|
+
'}',
|
76
|
+
'case s',
|
77
|
+
'when Array',
|
78
|
+
'end',
|
79
|
+
'']
|
80
|
+
inspect_source(ind, 'file.rb', source)
|
56
81
|
ind.offences.map(&:message).should == []
|
57
82
|
end
|
58
83
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module Rubocop
|
@@ -6,13 +8,13 @@ module Rubocop
|
|
6
8
|
let (:ll) { LineLength.new }
|
7
9
|
|
8
10
|
it "registers an offence for a line that's 80 characters wide" do
|
9
|
-
ll.inspect('file.rb', ['#' * 80])
|
11
|
+
ll.inspect('file.rb', ['#' * 80], nil, nil)
|
10
12
|
ll.offences.size.should == 1
|
11
13
|
ll.offences.first.message.should == 'Line is too long. [80/79]'
|
12
14
|
end
|
13
15
|
|
14
16
|
it "accepts a line that's 79 characters wide" do
|
15
|
-
ll.inspect('file.rb', ['#' * 79])
|
17
|
+
ll.inspect('file.rb', ['#' * 79], nil, nil)
|
16
18
|
ll.offences.size.should == 0
|
17
19
|
end
|
18
20
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe NumericLiterals do
|
8
|
+
let (:num) { NumericLiterals.new }
|
9
|
+
|
10
|
+
it "registers an offence for a long integer without underscores" do
|
11
|
+
inspect_source(num, 'file.rb', ['a = 123456'])
|
12
|
+
num.offences.map(&:message).should ==
|
13
|
+
['Add underscores to large numeric literals to improve their ' +
|
14
|
+
'readability.']
|
15
|
+
end
|
16
|
+
|
17
|
+
it "registers an offence for an integer with not enough underscores" do
|
18
|
+
inspect_source(num, 'file.rb', ['a = 123456_789000'])
|
19
|
+
num.offences.map(&:message).should ==
|
20
|
+
['Add underscores to large numeric literals to improve their ' +
|
21
|
+
'readability.']
|
22
|
+
end
|
23
|
+
|
24
|
+
it "registers an offence for a long float without underscores" do
|
25
|
+
inspect_source(num, 'file.rb', ['a = 1.234567'])
|
26
|
+
num.offences.map(&:message).should ==
|
27
|
+
['Add underscores to large numeric literals to improve their ' +
|
28
|
+
'readability.']
|
29
|
+
end
|
30
|
+
|
31
|
+
it "accepts long numbers with underscore" do
|
32
|
+
inspect_source(num, 'file.rb', ['a = 123_456',
|
33
|
+
'b = 1.234_56'])
|
34
|
+
num.offences.map(&:message).should == []
|
35
|
+
end
|
36
|
+
|
37
|
+
it "accepts a short integer without underscore" do
|
38
|
+
inspect_source(num, 'file.rb', ['a = 123'])
|
39
|
+
num.offences.map(&:message).should == []
|
40
|
+
end
|
41
|
+
|
42
|
+
it "accepts short numbers without underscore" do
|
43
|
+
inspect_source(num, 'file.rb', ['a = 123',
|
44
|
+
'b = 123.456'])
|
45
|
+
num.offences.map(&:message).should == []
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -1,19 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module Rubocop
|
4
6
|
module Cop
|
5
7
|
describe Offence do
|
6
8
|
it 'has a few required attributes' do
|
7
|
-
offence = Offence.new(:convention, 1, '
|
9
|
+
offence = Offence.new(:convention, 1, 'message')
|
8
10
|
|
9
11
|
offence.severity.should == :convention
|
10
12
|
offence.line_number.should == 1
|
11
|
-
offence.line.should == 'line'
|
12
13
|
offence.message.should == 'message'
|
13
14
|
end
|
14
15
|
|
15
16
|
it 'overrides #to_s' do
|
16
|
-
offence = Offence.new(:convention, 1, '
|
17
|
+
offence = Offence.new(:convention, 1, 'message')
|
17
18
|
|
18
19
|
offence.to_s.should == 'C: 1: message'
|
19
20
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module Rubocop
|
@@ -6,30 +8,30 @@ module Rubocop
|
|
6
8
|
let (:space) { SpaceAfterCommaEtc.new }
|
7
9
|
|
8
10
|
it 'registers an offence for block argument commas' do
|
9
|
-
|
11
|
+
inspect_source(space, 'file.rb', ['each { |s,t| }'])
|
10
12
|
space.offences.map(&:message).should ==
|
11
13
|
['Space missing after comma.']
|
12
14
|
end
|
13
15
|
|
14
16
|
it 'registers an offence for colon without space after it' do
|
15
|
-
|
17
|
+
inspect_source(space, 'file.rb', ['x = w ? {a:3}:4'])
|
16
18
|
space.offences.map(&:message).should ==
|
17
19
|
['Space missing after colon.'] * 2
|
18
20
|
end
|
19
21
|
|
20
22
|
it 'registers an offence for semicolon without space after it' do
|
21
|
-
|
23
|
+
inspect_source(space, 'file.rb', ['x = 1;y = 2'])
|
22
24
|
space.offences.map(&:message).should ==
|
23
25
|
['Space missing after semicolon.']
|
24
26
|
end
|
25
27
|
|
26
28
|
it 'allows the colons in symbols' do
|
27
|
-
|
29
|
+
inspect_source(space, 'file.rb', ['x = :a'])
|
28
30
|
space.offences.map(&:message).should == []
|
29
31
|
end
|
30
32
|
|
31
33
|
it 'allows colons in strings' do
|
32
|
-
|
34
|
+
inspect_source(space, 'file.rb', ["str << ':'"])
|
33
35
|
space.offences.map(&:message).should == []
|
34
36
|
end
|
35
37
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module Rubocop
|
@@ -6,63 +8,107 @@ module Rubocop
|
|
6
8
|
let (:space) { SurroundingSpace.new }
|
7
9
|
|
8
10
|
it 'registers an offence for assignment without space on both sides' do
|
9
|
-
|
11
|
+
inspect_source(space, 'file.rb', ['x=0', 'y= 0', 'z =0'])
|
10
12
|
space.offences.size.should == 3
|
11
13
|
space.offences.first.message.should ==
|
12
14
|
"Surrounding space missing for operator '='."
|
13
15
|
end
|
14
16
|
|
15
17
|
it 'registers an offence for binary operators that could be unary' do
|
16
|
-
|
18
|
+
inspect_source(space, 'file.rb', ['a-3', 'x&0xff', 'z+0'])
|
17
19
|
space.offences.map(&:message).should ==
|
18
20
|
["Surrounding space missing for operator '-'.",
|
19
21
|
"Surrounding space missing for operator '&'.",
|
20
22
|
"Surrounding space missing for operator '+'."]
|
21
23
|
end
|
22
24
|
|
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
|
+
|
23
46
|
it 'registers an offence for arguments to a method' do
|
24
|
-
|
47
|
+
inspect_source(space, 'file.rb', ['puts 1+2'])
|
25
48
|
space.offences.map(&:message).should ==
|
26
49
|
["Surrounding space missing for operator '+'."]
|
27
50
|
end
|
28
51
|
|
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
|
+
|
29
75
|
it 'accepts parentheses in block parameter list' do
|
30
|
-
|
31
|
-
|
32
|
-
|
76
|
+
inspect_source(space, 'file.rb',
|
77
|
+
['list.inject(Tms.new) { |sum, (label, item)|',
|
78
|
+
'}'])
|
33
79
|
space.offences.map(&:message).should == []
|
34
80
|
end
|
35
81
|
|
36
82
|
it 'accepts operator symbols' do
|
37
|
-
|
83
|
+
inspect_source(space, 'file.rb', ['func(:-)'])
|
38
84
|
space.offences.map(&:message).should == []
|
39
85
|
end
|
40
86
|
|
41
87
|
it 'accepts ranges' do
|
42
|
-
|
88
|
+
inspect_source(space, 'file.rb', ['a, b = (1..2), (1...3)'])
|
43
89
|
space.offences.map(&:message).should == []
|
44
90
|
end
|
45
91
|
|
46
92
|
it 'accepts scope operator' do
|
47
93
|
source = ['@io.class == Zlib::GzipWriter']
|
48
|
-
|
94
|
+
inspect_source(space, 'file.rb', source)
|
49
95
|
space.offences.map(&:message).should == []
|
50
96
|
end
|
51
97
|
|
52
98
|
it 'accepts ::Kernel::raise' do
|
53
99
|
source = ['::Kernel::raise IllegalBlockError.new']
|
54
|
-
|
100
|
+
inspect_source(space, 'file.rb', source)
|
55
101
|
space.offences.map(&:message).should == []
|
56
102
|
end
|
57
103
|
|
58
104
|
it 'accepts exclamation point negation' do
|
59
|
-
|
105
|
+
inspect_source(space, 'file.rb', ['x = !a&&!b'])
|
60
106
|
space.offences.map(&:message).should ==
|
61
107
|
["Surrounding space missing for operator '&&'."]
|
62
108
|
end
|
63
109
|
|
64
110
|
it 'accepts exclamation point definition' do
|
65
|
-
|
111
|
+
inspect_source(space, 'file.rb', [' def !',
|
66
112
|
' !__getobj__',
|
67
113
|
' end'])
|
68
114
|
space.offences.should == []
|
@@ -70,59 +116,76 @@ module Rubocop
|
|
70
116
|
end
|
71
117
|
|
72
118
|
it 'accepts a unary' do
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
119
|
+
inspect_source(space, 'file.rb',
|
120
|
+
[' def bm(label_width = 0, *labels, &blk)',
|
121
|
+
' benchmark(CAPTION, label_width, FORMAT,',
|
122
|
+
' *labels, &blk)',
|
123
|
+
' end',
|
124
|
+
''])
|
79
125
|
space.offences.map(&:message).should == []
|
80
126
|
end
|
81
127
|
|
82
128
|
it 'accepts splat operator' do
|
83
|
-
|
129
|
+
inspect_source(space, 'file.rb', ['return *list if options'])
|
84
130
|
space.offences.map(&:message).should == []
|
85
131
|
end
|
86
132
|
|
87
133
|
it 'accepts square brackets as method name' do
|
88
|
-
|
134
|
+
inspect_source(space, 'file.rb', ['def Vector.[](*array)',
|
89
135
|
'end'])
|
90
136
|
space.offences.map(&:message).should == []
|
91
137
|
end
|
92
138
|
|
93
139
|
it 'accepts def of operator' do
|
94
|
-
|
140
|
+
inspect_source(space, 'file.rb', ['def +(other); end'])
|
95
141
|
space.offences.map(&:message).should == []
|
96
142
|
end
|
97
143
|
|
98
144
|
it 'accepts an assignment with spaces' do
|
99
|
-
|
145
|
+
inspect_source(space, 'file.rb', ['x = 0'])
|
100
146
|
space.offences.size.should == 0
|
101
147
|
end
|
102
148
|
|
103
149
|
it "accepts some operators that are exceptions and don't need spaces" do
|
104
|
-
|
150
|
+
inspect_source(space, 'file.rb', ['(1..3)',
|
105
151
|
'ActionController::Base',
|
106
152
|
'each { |s, t| }'])
|
107
153
|
space.offences.map(&:message).should == []
|
108
154
|
end
|
109
155
|
|
110
156
|
it 'accepts an assignment followed by newline' do
|
111
|
-
|
157
|
+
inspect_source(space, 'file.rb', ['x =\n 0'])
|
158
|
+
space.offences.size.should == 0
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'registers an offences for exponent operator with spaces' do
|
162
|
+
inspect_source(space, 'file.rb', ['x = a * b ** 2'])
|
163
|
+
space.offences.map(&:message).should ==
|
164
|
+
["Space around operator ** detected."]
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'accepts exponent operator without spaces' do
|
168
|
+
inspect_source(space, 'file.rb', ['x = a * b**2'])
|
112
169
|
space.offences.size.should == 0
|
113
170
|
end
|
114
171
|
|
115
172
|
it 'accepts unary operators without space' do
|
116
|
-
|
173
|
+
inspect_source(space, 'file.rb', ['[].map(&:size)',
|
117
174
|
'-3',
|
118
175
|
'x = +2'])
|
119
176
|
space.offences.map(&:message).should == []
|
120
177
|
end
|
121
178
|
|
122
179
|
it 'accepts square brackets called with method call syntax' do
|
123
|
-
|
180
|
+
inspect_source(space, 'file.rb', ['subject.[](0)'])
|
124
181
|
space.offences.map(&:message).should == []
|
125
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
|
126
189
|
end
|
127
190
|
end
|
128
191
|
end
|