rubocop 0.7.2 → 0.8.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.
- checksums.yaml +4 -4
- data/.travis.yml +7 -1
- data/CHANGELOG.md +19 -0
- data/README.md +4 -8
- data/bin/rubocop +2 -2
- data/config/default.yml +8 -0
- data/config/enabled.yml +21 -24
- data/lib/rubocop.rb +9 -7
- data/lib/rubocop/cli.rb +73 -52
- data/lib/rubocop/config.rb +8 -5
- data/lib/rubocop/cop/access_control.rb +41 -0
- data/lib/rubocop/cop/alias.rb +7 -5
- data/lib/rubocop/cop/align_parameters.rb +20 -96
- data/lib/rubocop/cop/and_or.rb +26 -0
- data/lib/rubocop/cop/ascii_comments.rb +3 -8
- data/lib/rubocop/cop/ascii_identifiers.rb +6 -5
- data/lib/rubocop/cop/avoid_class_vars.rb +5 -10
- data/lib/rubocop/cop/avoid_for.rb +7 -5
- data/lib/rubocop/cop/avoid_global_vars.rb +19 -7
- data/lib/rubocop/cop/avoid_perl_backrefs.rb +7 -10
- data/lib/rubocop/cop/avoid_perlisms.rb +11 -10
- data/lib/rubocop/cop/block_comments.rb +4 -6
- data/lib/rubocop/cop/blocks.rb +11 -47
- data/lib/rubocop/cop/case_indentation.rb +9 -31
- data/lib/rubocop/cop/class_and_module_camel_case.rb +20 -11
- data/lib/rubocop/cop/class_methods.rb +5 -10
- data/lib/rubocop/cop/collection_methods.rb +16 -16
- data/lib/rubocop/cop/colon_method_call.rb +8 -32
- data/lib/rubocop/cop/constant_name.rb +24 -0
- data/lib/rubocop/cop/cop.rb +20 -78
- data/lib/rubocop/cop/def_parentheses.rb +43 -35
- data/lib/rubocop/cop/empty_line_between_defs.rb +11 -15
- data/lib/rubocop/cop/empty_lines.rb +20 -9
- data/lib/rubocop/cop/empty_literal.rb +47 -0
- data/lib/rubocop/cop/encoding.rb +3 -3
- data/lib/rubocop/cop/end_of_line.rb +3 -3
- data/lib/rubocop/cop/ensure_return.rb +6 -23
- data/lib/rubocop/cop/eval.rb +7 -10
- data/lib/rubocop/cop/favor_join.rb +9 -24
- data/lib/rubocop/cop/favor_modifier.rb +38 -48
- data/lib/rubocop/cop/favor_percent_r.rb +7 -7
- data/lib/rubocop/cop/favor_sprintf.rb +8 -24
- data/lib/rubocop/cop/favor_unless_over_negated_if.rb +19 -17
- data/lib/rubocop/cop/handle_exceptions.rb +7 -11
- data/lib/rubocop/cop/hash_syntax.rb +29 -14
- data/lib/rubocop/cop/if_then_else.rb +32 -29
- data/lib/rubocop/cop/leading_comment_space.rb +5 -8
- data/lib/rubocop/cop/line_continuation.rb +4 -7
- data/lib/rubocop/cop/line_length.rb +3 -3
- data/lib/rubocop/cop/loop.rb +33 -0
- data/lib/rubocop/cop/method_and_variable_snake_case.rb +42 -19
- data/lib/rubocop/cop/method_length.rb +34 -37
- data/lib/rubocop/cop/new_lambda_literal.rb +8 -6
- data/lib/rubocop/cop/not.rb +10 -4
- data/lib/rubocop/cop/numeric_literals.rb +9 -7
- data/lib/rubocop/cop/offence.rb +1 -1
- data/lib/rubocop/cop/op_method.rb +12 -22
- data/lib/rubocop/cop/parameter_lists.rb +12 -6
- data/lib/rubocop/cop/parentheses_around_condition.rb +11 -11
- data/lib/rubocop/cop/percent_r.rb +7 -7
- data/lib/rubocop/cop/reduce_arguments.rb +13 -51
- data/lib/rubocop/cop/rescue_exception.rb +13 -29
- data/lib/rubocop/cop/rescue_modifier.rb +5 -8
- data/lib/rubocop/cop/semicolon.rb +15 -74
- data/lib/rubocop/cop/single_line_methods.rb +28 -44
- data/lib/rubocop/cop/space_after_comma_etc.rb +29 -9
- data/lib/rubocop/cop/space_after_control_keyword.rb +16 -15
- data/lib/rubocop/cop/string_literals.rb +9 -35
- data/lib/rubocop/cop/surrounding_space.rb +213 -112
- data/lib/rubocop/cop/symbol_array.rb +9 -7
- data/lib/rubocop/cop/symbol_name.rb +23 -0
- data/lib/rubocop/cop/syntax.rb +14 -7
- data/lib/rubocop/cop/tab.rb +3 -3
- data/lib/rubocop/cop/ternary_operator.rb +26 -24
- data/lib/rubocop/cop/trailing_whitespace.rb +3 -5
- data/lib/rubocop/cop/trivial_accessors.rb +18 -95
- data/lib/rubocop/cop/unless_else.rb +11 -7
- data/lib/rubocop/cop/util.rb +26 -0
- data/lib/rubocop/cop/variable_interpolation.rb +18 -10
- data/lib/rubocop/cop/when_then.rb +6 -17
- data/lib/rubocop/cop/word_array.rb +18 -19
- data/lib/rubocop/version.rb +1 -1
- data/rubocop.gemspec +1 -0
- data/spec/project_spec.rb +1 -1
- data/spec/rubocop/cli_spec.rb +16 -9
- data/spec/rubocop/config_spec.rb +13 -3
- data/spec/rubocop/cops/access_control_spec.rb +129 -0
- data/spec/rubocop/cops/alias_spec.rb +2 -6
- data/spec/rubocop/cops/align_parameters_spec.rb +58 -71
- data/spec/rubocop/cops/and_or_spec.rb +37 -0
- data/spec/rubocop/cops/ascii_comments_spec.rb +3 -4
- data/spec/rubocop/cops/ascii_identifiers_spec.rb +3 -4
- data/spec/rubocop/cops/avoid_class_vars_spec.rb +7 -2
- data/spec/rubocop/cops/avoid_for_spec.rb +1 -4
- data/spec/rubocop/cops/{avoid_global_vars.rb → avoid_global_vars_spec.rb} +4 -4
- data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +1 -1
- data/spec/rubocop/cops/avoid_perlisms_spec.rb +5 -5
- data/spec/rubocop/cops/block_comments_spec.rb +0 -4
- data/spec/rubocop/cops/blocks_spec.rb +33 -0
- data/spec/rubocop/cops/case_indentation_spec.rb +5 -5
- data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +15 -5
- data/spec/rubocop/cops/class_methods_spec.rb +4 -4
- data/spec/rubocop/cops/collection_methods_spec.rb +9 -4
- data/spec/rubocop/cops/colon_method_call_spec.rb +11 -5
- data/spec/rubocop/cops/constant_name_spec.rb +42 -0
- data/spec/rubocop/cops/def_with_parentheses_spec.rb +13 -8
- data/spec/rubocop/cops/def_without_parentheses_spec.rb +11 -5
- data/spec/rubocop/cops/empty_line_between_defs_spec.rb +38 -38
- data/spec/rubocop/cops/empty_lines_spec.rb +15 -3
- data/spec/rubocop/cops/empty_literal_spec.rb +90 -0
- data/spec/rubocop/cops/encoding_spec.rb +9 -9
- data/spec/rubocop/cops/end_of_line_spec.rb +2 -2
- data/spec/rubocop/cops/ensure_return_spec.rb +1 -3
- data/spec/rubocop/cops/eval_spec.rb +8 -5
- data/spec/rubocop/cops/favor_join_spec.rb +1 -5
- data/spec/rubocop/cops/favor_modifier_spec.rb +16 -14
- data/spec/rubocop/cops/{favor_percent_r.rb → favor_percent_r_spec.rb} +6 -6
- data/spec/rubocop/cops/favor_sprintf_spec.rb +3 -9
- data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +4 -4
- data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +3 -3
- data/spec/rubocop/cops/handle_exceptions_spec.rb +1 -3
- data/spec/rubocop/cops/hash_syntax_spec.rb +11 -6
- data/spec/rubocop/cops/if_with_semicolon_spec.rb +7 -1
- data/spec/rubocop/cops/leading_comment_space_spec.rb +0 -7
- data/spec/rubocop/cops/line_continuation_spec.rb +2 -2
- data/spec/rubocop/cops/line_length_spec.rb +2 -2
- data/spec/rubocop/cops/loop_spec.rb +31 -0
- data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +38 -12
- data/spec/rubocop/cops/method_length_spec.rb +85 -85
- data/spec/rubocop/cops/multiline_if_then_spec.rb +15 -15
- data/spec/rubocop/cops/new_lambda_literal_spec.rb +3 -3
- data/spec/rubocop/cops/not_spec.rb +1 -4
- data/spec/rubocop/cops/numeric_literals_spec.rb +13 -13
- data/spec/rubocop/cops/one_line_conditional_spec.rb +1 -1
- data/spec/rubocop/cops/op_method_spec.rb +2 -9
- data/spec/rubocop/cops/parameter_lists_spec.rb +7 -7
- data/spec/rubocop/cops/parentheses_around_condition_spec.rb +41 -44
- data/spec/rubocop/cops/percent_r_spec.rb +6 -6
- data/spec/rubocop/cops/reduce_arguments_spec.rb +4 -4
- data/spec/rubocop/cops/rescue_exception_spec.rb +48 -8
- data/spec/rubocop/cops/rescue_modifier_spec.rb +2 -5
- data/spec/rubocop/cops/semicolon_spec.rb +2 -30
- data/spec/rubocop/cops/single_line_methods_spec.rb +13 -13
- data/spec/rubocop/cops/space_after_colon_spec.rb +3 -3
- data/spec/rubocop/cops/space_after_comma_spec.rb +14 -2
- data/spec/rubocop/cops/space_after_control_keyword_spec.rb +42 -3
- data/spec/rubocop/cops/space_after_semicolon_spec.rb +2 -2
- data/spec/rubocop/cops/space_around_braces_spec.rb +18 -3
- data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +4 -4
- data/spec/rubocop/cops/space_around_operators_spec.rb +82 -27
- data/spec/rubocop/cops/space_inside_brackets_spec.rb +13 -7
- data/spec/rubocop/cops/space_inside_hash_literal_braces_spec.rb +14 -9
- data/spec/rubocop/cops/space_inside_parens_spec.rb +7 -3
- data/spec/rubocop/cops/string_literals_spec.rb +17 -5
- data/spec/rubocop/cops/symbol_array_spec.rb +18 -2
- data/spec/rubocop/cops/symbol_name_spec.rb +119 -0
- data/spec/rubocop/cops/syntax_spec.rb +25 -18
- data/spec/rubocop/cops/tab_spec.rb +2 -2
- data/spec/rubocop/cops/ternary_operator_spec.rb +13 -17
- data/spec/rubocop/cops/trailing_whitespace_spec.rb +3 -3
- data/spec/rubocop/cops/trivial_accessors_spec.rb +17 -20
- data/spec/rubocop/cops/unless_else_spec.rb +8 -8
- data/spec/rubocop/cops/variable_interpolation_spec.rb +0 -5
- data/spec/rubocop/cops/when_then_spec.rb +14 -21
- data/spec/rubocop/cops/word_array_spec.rb +12 -4
- data/spec/spec_helper.rb +12 -4
- metadata +40 -31
- data/.document +0 -5
- data/lib/rubocop/cop/ampersands_pipes_vs_and_or.rb +0 -25
- data/lib/rubocop/cop/array_literal.rb +0 -61
- data/lib/rubocop/cop/brace_after_percent.rb +0 -32
- data/lib/rubocop/cop/grammar.rb +0 -138
- data/lib/rubocop/cop/hash_literal.rb +0 -61
- data/lib/rubocop/cop/percent_literals.rb +0 -25
- data/lib/rubocop/cop/symbol_snake_case.rb +0 -47
- data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +0 -57
- data/spec/rubocop/cops/array_literal_spec.rb +0 -46
- data/spec/rubocop/cops/brace_after_percent_spec.rb +0 -33
- data/spec/rubocop/cops/grammar_spec.rb +0 -81
- data/spec/rubocop/cops/hash_literal_spec.rb +0 -46
- data/spec/rubocop/cops/multiline_blocks_spec.rb +0 -24
- data/spec/rubocop/cops/percent_literals_spec.rb +0 -47
- data/spec/rubocop/cops/single_line_blocks_spec.rb +0 -22
- data/spec/rubocop/cops/symbol_snake_case_spec.rb +0 -93
@@ -9,40 +9,40 @@ module Rubocop
|
|
9
9
|
before { SingleLineMethods.config = { 'AllowIfMethodIsEmpty' => true } }
|
10
10
|
|
11
11
|
it 'registers an offence for a single-line method' do
|
12
|
-
inspect_source(slm,
|
12
|
+
inspect_source(slm,
|
13
13
|
['def some_method; body end',
|
14
14
|
'def link_to(name, url); {:name => name}; end',
|
15
15
|
'def @table.columns; super; end'])
|
16
16
|
expect(slm.offences.map(&:message)).to eq(
|
17
|
-
[SingleLineMethods::
|
17
|
+
[SingleLineMethods::MSG] * 3)
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'registers an offence for an empty method if so configured' do
|
21
21
|
SingleLineMethods.config = { 'AllowIfMethodIsEmpty' => false }
|
22
|
-
inspect_source(slm,
|
23
|
-
|
24
|
-
|
22
|
+
inspect_source(slm, ['def no_op; end',
|
23
|
+
'def self.resource_class=(klass); end',
|
24
|
+
'def @table.columns; end'])
|
25
25
|
expect(slm.offences.size).to eq(3)
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'accepts a single-line empty method if so configured' do
|
29
29
|
SingleLineMethods.config = { 'AllowIfMethodIsEmpty' => true }
|
30
|
-
inspect_source(slm,
|
31
|
-
|
32
|
-
|
30
|
+
inspect_source(slm, ['def no_op; end',
|
31
|
+
'def self.resource_class=(klass); end',
|
32
|
+
'def @table.columns; end'])
|
33
33
|
expect(slm.offences).to be_empty
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'accepts a multi-line method' do
|
37
|
-
inspect_source(slm,
|
38
|
-
|
39
|
-
|
37
|
+
inspect_source(slm, ['def some_method',
|
38
|
+
' body',
|
39
|
+
'end'])
|
40
40
|
expect(slm.offences).to be_empty
|
41
41
|
end
|
42
42
|
|
43
43
|
it 'does not crash on an method with a capitalized name' do
|
44
|
-
inspect_source(slm,
|
45
|
-
|
44
|
+
inspect_source(slm, ['def NoSnakeCase',
|
45
|
+
'end'])
|
46
46
|
expect(slm.offences).to be_empty
|
47
47
|
end
|
48
48
|
end
|
@@ -8,18 +8,18 @@ module Rubocop
|
|
8
8
|
let(:space) { SpaceAfterColon.new }
|
9
9
|
|
10
10
|
it 'registers an offence for colon without space after it' do
|
11
|
-
inspect_source(space,
|
11
|
+
inspect_source(space, ['x = w ? {a:3}:4'])
|
12
12
|
expect(space.offences.map(&:message)).to eq(
|
13
13
|
['Space missing after colon.'] * 2)
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'allows the colons in symbols' do
|
17
|
-
inspect_source(space,
|
17
|
+
inspect_source(space, ['x = :a'])
|
18
18
|
expect(space.offences.map(&:message)).to be_empty
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'allows colons in strings' do
|
22
|
-
inspect_source(space,
|
22
|
+
inspect_source(space, ["str << ':'"])
|
23
23
|
expect(space.offences.map(&:message)).to be_empty
|
24
24
|
end
|
25
25
|
end
|
@@ -7,8 +7,20 @@ module Rubocop
|
|
7
7
|
describe SpaceAfterComma do
|
8
8
|
let(:space) { SpaceAfterComma.new }
|
9
9
|
|
10
|
-
it 'registers an offence for block argument commas' do
|
11
|
-
inspect_source(space,
|
10
|
+
it 'registers an offence for block argument commas without space' do
|
11
|
+
inspect_source(space, ['each { |s,t| }'])
|
12
|
+
expect(space.offences.map(&:message)).to eq(
|
13
|
+
['Space missing after comma.'])
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'registers an offence for array index commas without space' do
|
17
|
+
inspect_source(space, ['formats[0,1]'])
|
18
|
+
expect(space.offences.map(&:message)).to eq(
|
19
|
+
['Space missing after comma.'])
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'registers an offence for method call arg commas without space' do
|
23
|
+
inspect_source(space, ['a(1,2)'])
|
12
24
|
expect(space.offences.map(&:message)).to eq(
|
13
25
|
['Space missing after comma.'])
|
14
26
|
end
|
@@ -8,21 +8,60 @@ module Rubocop
|
|
8
8
|
let(:ap) { SpaceAfterControlKeyword.new }
|
9
9
|
|
10
10
|
it 'registers an offence for normal if' do
|
11
|
-
inspect_source(ap,
|
11
|
+
inspect_source(ap,
|
12
12
|
['if(test) then result end'])
|
13
13
|
expect(ap.offences.size).to eq(1)
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'registers an offence for modifier unless' do
|
17
|
-
inspect_source(ap,
|
17
|
+
inspect_source(ap, ['action unless(test)'])
|
18
18
|
|
19
19
|
expect(ap.offences.size).to eq(1)
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'does not get confused by keywords' do
|
23
|
-
inspect_source(ap,
|
23
|
+
inspect_source(ap, ['[:if, :unless].action'])
|
24
24
|
expect(ap.offences).to be_empty
|
25
25
|
end
|
26
|
+
|
27
|
+
it 'does not get confused by the ternary operator' do
|
28
|
+
inspect_source(ap, ['a ? b : c'])
|
29
|
+
expect(ap.offences).to be_empty
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'registers an offence for if, elsif, and unless' do
|
33
|
+
inspect_source(ap,
|
34
|
+
['if(a)',
|
35
|
+
'elsif(b)',
|
36
|
+
' unless(c)',
|
37
|
+
' end',
|
38
|
+
'end'])
|
39
|
+
expect(ap.offences.map(&:line_number)).to eq([1, 2, 3])
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'registers an offence for case and when' do
|
43
|
+
inspect_source(ap,
|
44
|
+
['case(a)',
|
45
|
+
'when(0) then 1',
|
46
|
+
'end'])
|
47
|
+
expect(ap.offences.map(&:line_number)).to eq([1, 2])
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'registers an offence for case and when' do
|
51
|
+
inspect_source(ap,
|
52
|
+
['case(a)',
|
53
|
+
'when(0) then 1',
|
54
|
+
'end'])
|
55
|
+
expect(ap.offences.map(&:line_number)).to eq([1, 2])
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'registers an offence for while and until' do
|
59
|
+
inspect_source(ap,
|
60
|
+
['while(a)',
|
61
|
+
' b until(c)',
|
62
|
+
'end'])
|
63
|
+
expect(ap.offences.map(&:line_number)).to eq([1, 2])
|
64
|
+
end
|
26
65
|
end
|
27
66
|
end
|
28
67
|
end
|
@@ -8,13 +8,13 @@ module Rubocop
|
|
8
8
|
let(:space) { SpaceAfterSemicolon.new }
|
9
9
|
|
10
10
|
it 'registers an offence for semicolon without space after it' do
|
11
|
-
inspect_source(space,
|
11
|
+
inspect_source(space, ['x = 1;y = 2'])
|
12
12
|
expect(space.offences.map(&:message)).to eq(
|
13
13
|
['Space missing after semicolon.'])
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'does not crash if semicolon is the last character of the file' do
|
17
|
-
inspect_source(space,
|
17
|
+
inspect_source(space, ['x = 1;'])
|
18
18
|
expect(space.offences.map(&:message)).to be_empty
|
19
19
|
end
|
20
20
|
end
|
@@ -8,25 +8,40 @@ module Rubocop
|
|
8
8
|
let(:space) { SpaceAroundBraces.new }
|
9
9
|
|
10
10
|
it 'registers an offence for left brace without spaces' do
|
11
|
-
inspect_source(space,
|
11
|
+
inspect_source(space, ['each{ puts }'])
|
12
12
|
expect(space.offences.map(&:message)).to eq(
|
13
13
|
["Surrounding space missing for '{'."])
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'registers an offence for right brace without inner space' do
|
17
|
-
inspect_source(space,
|
17
|
+
inspect_source(space, ['each { puts}'])
|
18
18
|
expect(space.offences.map(&:message)).to eq(
|
19
19
|
["Space missing to the left of '}'."])
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'accepts an empty hash literal with no space inside' do
|
23
|
-
inspect_source(space,
|
23
|
+
inspect_source(space,
|
24
24
|
['view_hash.each do |view_key|',
|
25
25
|
'end',
|
26
26
|
'@views = {}',
|
27
27
|
''])
|
28
28
|
expect(space.offences.map(&:message)).to be_empty
|
29
29
|
end
|
30
|
+
|
31
|
+
it 'accepts string interpolation braces with no space inside' do
|
32
|
+
inspect_source(space,
|
33
|
+
['"A=#{a}"',
|
34
|
+
':"#{b}"',
|
35
|
+
'/#{c}/',
|
36
|
+
'`#{d}`',
|
37
|
+
'sprintf("#{message.gsub(/%/, \'%%\')}", line)'])
|
38
|
+
expect(space.offences.map(&:message)).to be_empty
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'accepts braces around a hash literal argument' do
|
42
|
+
inspect_source(space, ["new({'user' => user_params})"])
|
43
|
+
expect(space.offences.map(&:message)).to be_empty
|
44
|
+
end
|
30
45
|
end
|
31
46
|
end
|
32
47
|
end
|
@@ -8,23 +8,23 @@ module Rubocop
|
|
8
8
|
let(:space) { SpaceAroundEqualsInParameterDefault.new }
|
9
9
|
|
10
10
|
it 'registers an offence for default value assignment without space' do
|
11
|
-
inspect_source(space,
|
11
|
+
inspect_source(space, ['def f(x, y=0, z=1)', 'end'])
|
12
12
|
expect(space.offences.map(&:message)).to eq(
|
13
13
|
['Surrounding space missing in default value assignment.'] * 2)
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'registers an offence for assignment of empty string without space' do
|
17
|
-
inspect_source(space,
|
17
|
+
inspect_source(space, ['def f(x, y="", z=1)', 'end'])
|
18
18
|
expect(space.offences.size).to eq(2)
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'registers an offence for assignment of empty list without space' do
|
22
|
-
inspect_source(space,
|
22
|
+
inspect_source(space, ['def f(x, y=[])', 'end'])
|
23
23
|
expect(space.offences.size).to eq(1)
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'accepts default value assignment with space' do
|
27
|
-
inspect_source(space,
|
27
|
+
inspect_source(space, ['def f(x, y = 0, z = {})', 'end'])
|
28
28
|
expect(space.offences.map(&:message)).to be_empty
|
29
29
|
end
|
30
30
|
end
|
@@ -8,13 +8,13 @@ module Rubocop
|
|
8
8
|
let(:space) { SpaceAroundOperators.new }
|
9
9
|
|
10
10
|
it 'registers an offence for assignment without space on both sides' do
|
11
|
-
inspect_source(space,
|
11
|
+
inspect_source(space, ['x=0', 'y= 0', 'z =0'])
|
12
12
|
expect(space.offences.map(&:message)).to eq(
|
13
13
|
["Surrounding space missing for operator '='."] * 3)
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'registers an offence for ternary operator without space' do
|
17
|
-
inspect_source(space,
|
17
|
+
inspect_source(space, ['x == 0?1:2'])
|
18
18
|
expect(space.offences.map(&:message)).to eq(
|
19
19
|
["Surrounding space missing for operator '?'.",
|
20
20
|
"Surrounding space missing for operator ':'."])
|
@@ -39,14 +39,14 @@ module Rubocop
|
|
39
39
|
def check_modifier(keyword)
|
40
40
|
src = ["a=1 #{keyword} condition",
|
41
41
|
'c=2']
|
42
|
-
inspect_source(space,
|
42
|
+
inspect_source(space, src)
|
43
43
|
expect(space.offences.map(&:line_number)).to eq([1, 2])
|
44
44
|
expect(space.offences.map(&:message)).to eq(
|
45
45
|
["Surrounding space missing for operator '='."] * 2)
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'registers an offence for binary operators that could be unary' do
|
49
|
-
inspect_source(space,
|
49
|
+
inspect_source(space, ['a-3', 'x&0xff', 'z+0'])
|
50
50
|
expect(space.offences.map(&:message)).to eq(
|
51
51
|
["Surrounding space missing for operator '-'.",
|
52
52
|
"Surrounding space missing for operator '&'.",
|
@@ -54,99 +54,154 @@ module Rubocop
|
|
54
54
|
end
|
55
55
|
|
56
56
|
it 'registers an offence for arguments to a method' do
|
57
|
-
inspect_source(space,
|
57
|
+
inspect_source(space, ['puts 1+2'])
|
58
58
|
expect(space.offences.map(&:message)).to eq(
|
59
59
|
["Surrounding space missing for operator '+'."])
|
60
60
|
end
|
61
61
|
|
62
62
|
it 'accepts operator symbols' do
|
63
|
-
inspect_source(space,
|
63
|
+
inspect_source(space, ['func(:-)'])
|
64
64
|
expect(space.offences.map(&:message)).to be_empty
|
65
65
|
end
|
66
66
|
|
67
67
|
it 'accepts ranges' do
|
68
|
-
inspect_source(space,
|
68
|
+
inspect_source(space, ['a, b = (1..2), (1...3)'])
|
69
69
|
expect(space.offences.map(&:message)).to be_empty
|
70
70
|
end
|
71
71
|
|
72
72
|
it 'accepts scope operator' do
|
73
73
|
source = ['@io.class == Zlib::GzipWriter']
|
74
|
-
inspect_source(space,
|
74
|
+
inspect_source(space, source)
|
75
75
|
expect(space.offences.map(&:message)).to be_empty
|
76
76
|
end
|
77
77
|
|
78
78
|
it 'accepts ::Kernel::raise' do
|
79
79
|
source = ['::Kernel::raise IllegalBlockError.new']
|
80
|
-
inspect_source(space,
|
80
|
+
inspect_source(space, source)
|
81
81
|
expect(space.offences.map(&:message)).to be_empty
|
82
82
|
end
|
83
83
|
|
84
84
|
it 'accepts exclamation point negation' do
|
85
|
-
inspect_source(space,
|
85
|
+
inspect_source(space, ['x = !a&&!b'])
|
86
86
|
expect(space.offences.map(&:message)).to eq(
|
87
87
|
["Surrounding space missing for operator '&&'."])
|
88
88
|
end
|
89
89
|
|
90
90
|
it 'accepts exclamation point definition' do
|
91
|
-
inspect_source(space,
|
92
|
-
|
93
|
-
|
91
|
+
inspect_source(space, [' def !',
|
92
|
+
' !__getobj__',
|
93
|
+
' end'])
|
94
94
|
expect(space.offences).to be_empty
|
95
95
|
expect(space.offences.map(&:message)).to be_empty
|
96
96
|
end
|
97
97
|
|
98
98
|
it 'accepts a unary' do
|
99
|
-
inspect_source(space,
|
99
|
+
inspect_source(space,
|
100
100
|
[' def bm(label_width = 0, *labels, &blk)',
|
101
101
|
' benchmark(CAPTION, label_width, FORMAT,',
|
102
102
|
' *labels, &blk)',
|
103
103
|
' end',
|
104
|
+
'',
|
105
|
+
' def each &block',
|
106
|
+
' end',
|
107
|
+
'',
|
108
|
+
' def each *args',
|
109
|
+
' end',
|
104
110
|
''])
|
105
111
|
expect(space.offences.map(&:message)).to be_empty
|
106
112
|
end
|
107
113
|
|
108
114
|
it 'accepts splat operator' do
|
109
|
-
inspect_source(space,
|
115
|
+
inspect_source(space, ['return *list if options'])
|
110
116
|
expect(space.offences.map(&:message)).to be_empty
|
111
117
|
end
|
112
118
|
|
113
119
|
it 'accepts def of operator' do
|
114
|
-
inspect_source(space,
|
120
|
+
inspect_source(space, ['def +(other); end',
|
121
|
+
'def self.===(other); end'])
|
115
122
|
expect(space.offences.map(&:message)).to be_empty
|
116
123
|
end
|
117
124
|
|
125
|
+
it 'accepts an operator at the end of a line' do
|
126
|
+
inspect_source(space,
|
127
|
+
["['Favor unless over if for negative ' +",
|
128
|
+
" 'conditions.'] * 2"])
|
129
|
+
expect(space.offences.map(&:message)).to eq([])
|
130
|
+
end
|
131
|
+
|
118
132
|
it 'accepts an assignment with spaces' do
|
119
|
-
inspect_source(space,
|
133
|
+
inspect_source(space, ['x = 0'])
|
120
134
|
expect(space.offences).to be_empty
|
121
135
|
end
|
122
136
|
|
137
|
+
it 'registers an offence for operators without spaces' do
|
138
|
+
inspect_source(space,
|
139
|
+
['x+= a+b-c*d/e%f^g|h&i||j',
|
140
|
+
'y -=k&&l'])
|
141
|
+
expect(space.offences.map(&:message))
|
142
|
+
.to eq(["Surrounding space missing for operator '+='.",
|
143
|
+
"Surrounding space missing for operator '+'.",
|
144
|
+
"Surrounding space missing for operator '-'.",
|
145
|
+
"Surrounding space missing for operator '*'.",
|
146
|
+
"Surrounding space missing for operator '/'.",
|
147
|
+
"Surrounding space missing for operator '%'.",
|
148
|
+
"Surrounding space missing for operator '^'.",
|
149
|
+
"Surrounding space missing for operator '|'.",
|
150
|
+
"Surrounding space missing for operator '&'.",
|
151
|
+
"Surrounding space missing for operator '||'.",
|
152
|
+
"Surrounding space missing for operator '-='.",
|
153
|
+
"Surrounding space missing for operator '&&'."])
|
154
|
+
end
|
155
|
+
|
156
|
+
it 'accepts operators with spaces' do
|
157
|
+
inspect_source(space,
|
158
|
+
['x += a + b - c * d / e % f ^ g | h & i || j',
|
159
|
+
'y -= k && l'])
|
160
|
+
expect(space.offences.map(&:message)).to eq([])
|
161
|
+
end
|
162
|
+
|
123
163
|
it "accepts some operators that are exceptions and don't need spaces" do
|
124
|
-
inspect_source(space,
|
125
|
-
|
126
|
-
|
127
|
-
expect(space.offences.map(&:message)).to
|
164
|
+
inspect_source(space, ['(1..3)',
|
165
|
+
'ActionController::Base',
|
166
|
+
'each { |s, t| }'])
|
167
|
+
expect(space.offences.map(&:message)).to eq([])
|
128
168
|
end
|
129
169
|
|
130
170
|
it 'accepts an assignment followed by newline' do
|
131
|
-
inspect_source(space,
|
171
|
+
inspect_source(space, ['x =', '0'])
|
132
172
|
expect(space.offences).to be_empty
|
133
173
|
end
|
134
174
|
|
135
175
|
it 'registers an offences for exponent operator with spaces' do
|
136
|
-
inspect_source(space,
|
176
|
+
inspect_source(space, ['x = a * b ** 2'])
|
137
177
|
expect(space.offences.map(&:message)).to eq(
|
138
178
|
['Space around operator ** detected.'])
|
139
179
|
end
|
140
180
|
|
141
181
|
it 'accepts exponent operator without spaces' do
|
142
|
-
inspect_source(space,
|
182
|
+
inspect_source(space, ['x = a * b**2'])
|
143
183
|
expect(space.offences).to be_empty
|
144
184
|
end
|
145
185
|
|
146
186
|
it 'accepts unary operators without space' do
|
147
|
-
inspect_source(space,
|
148
|
-
|
149
|
-
|
187
|
+
inspect_source(space, ['[].map(&:size)',
|
188
|
+
'-3',
|
189
|
+
'x = +2'])
|
190
|
+
expect(space.offences.map(&:message)).to eq([])
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'accepts argument default values without space' do
|
194
|
+
# These are handled by SpaceAroundEqualsInParameterDefault,
|
195
|
+
# so SpaceAroundOperators leaves them alone.
|
196
|
+
inspect_source(space,
|
197
|
+
['def init(name=nil)',
|
198
|
+
'end'])
|
199
|
+
expect(space.offences.map(&:message)).to be_empty
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'accepts the construct class <<self with no space after <<' do
|
203
|
+
inspect_source(space, ['class <<self',
|
204
|
+
'end'])
|
150
205
|
expect(space.offences.map(&:message)).to be_empty
|
151
206
|
end
|
152
207
|
end
|