rubocop 0.23.0 → 0.24.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 +1 -1
- data/CHANGELOG.md +35 -1
- data/CONTRIBUTING.md +1 -1
- data/README.md +6 -6
- data/config/default.yml +25 -6
- data/config/enabled.yml +20 -0
- data/lib/rubocop.rb +10 -13
- data/lib/rubocop/cli.rb +23 -20
- data/lib/rubocop/cop/lint/def_end_alignment.rb +47 -0
- data/lib/rubocop/cop/lint/end_alignment.rb +18 -65
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
- data/lib/rubocop/cop/lint/syntax.rb +28 -4
- data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +1 -1
- data/lib/rubocop/cop/lint/unused_block_argument.rb +13 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +3 -2
- data/lib/rubocop/cop/lint/useless_assignment.rb +1 -9
- data/lib/rubocop/cop/lint/useless_setter_call.rb +28 -20
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +18 -0
- data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +4 -2
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +42 -0
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +32 -0
- data/lib/rubocop/cop/mixin/surrounding_space.rb +7 -8
- data/lib/rubocop/cop/mixin/unused_argument.rb +1 -1
- data/lib/rubocop/cop/offense.rb +27 -14
- data/lib/rubocop/cop/style/access_modifier_indentation.rb +2 -9
- data/lib/rubocop/cop/style/attr.rb +3 -1
- data/lib/rubocop/cop/style/class_check.rb +42 -0
- data/lib/rubocop/cop/style/each_with_object.rb +5 -1
- data/lib/rubocop/cop/style/empty_lines.rb +1 -4
- data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +2 -8
- data/lib/rubocop/cop/style/empty_lines_around_body.rb +1 -4
- data/lib/rubocop/cop/style/encoding.rb +3 -5
- data/lib/rubocop/cop/style/end_of_line.rb +2 -5
- data/lib/rubocop/cop/style/file_name.rb +2 -4
- data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -1
- data/lib/rubocop/cop/style/indentation_consistency.rb +2 -1
- data/lib/rubocop/cop/style/indentation_width.rb +25 -5
- data/lib/rubocop/cop/style/line_length.rb +59 -5
- data/lib/rubocop/cop/style/next.rb +18 -18
- data/lib/rubocop/cop/style/numeric_literals.rb +22 -9
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +1 -3
- data/lib/rubocop/cop/style/space_after_colon.rb +7 -3
- data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +3 -1
- data/lib/rubocop/cop/style/space_before_comma.rb +16 -0
- data/lib/rubocop/cop/style/space_before_semicolon.rb +16 -0
- data/lib/rubocop/cop/style/tab.rb +6 -5
- data/lib/rubocop/cop/style/trailing_comma.rb +33 -6
- data/lib/rubocop/cop/style/trailing_whitespace.rb +4 -3
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +6 -0
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +45 -0
- data/lib/rubocop/cop/style/unneeded_percent_x.rb +2 -3
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/team.rb +6 -12
- data/lib/rubocop/cop/util.rb +26 -8
- data/lib/rubocop/cop/variable_force.rb +3 -6
- data/lib/rubocop/cop/variable_force/variable.rb +7 -3
- data/lib/rubocop/processed_source.rb +52 -12
- data/lib/rubocop/{file_inspector.rb → runner.rb} +50 -59
- data/lib/rubocop/version.rb +1 -1
- data/relnotes/v0.24.0.md +77 -0
- data/rubocop.gemspec +4 -4
- data/spec/rubocop/cli_spec.rb +38 -21
- data/spec/rubocop/config_loader_spec.rb +7 -6
- data/spec/rubocop/config_spec.rb +8 -8
- data/spec/rubocop/cop/cop_spec.rb +1 -1
- data/spec/rubocop/cop/lint/def_end_alignment_spec.rb +108 -0
- data/spec/rubocop/cop/lint/end_alignment_spec.rb +0 -47
- data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +6 -7
- data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +19 -0
- data/spec/rubocop/cop/lint/useless_assignment_spec.rb +8 -18
- data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +99 -51
- data/spec/rubocop/cop/offense_spec.rb +3 -3
- data/spec/rubocop/cop/rails/delegate_spec.rb +1 -1
- data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +12 -0
- data/spec/rubocop/cop/style/align_hash_spec.rb +4 -4
- data/spec/rubocop/cop/style/align_parameters_spec.rb +1 -1
- data/spec/rubocop/cop/style/attr_spec.rb +12 -2
- data/spec/rubocop/cop/style/class_check_spec.rb +41 -0
- data/spec/rubocop/cop/style/each_with_object_spec.rb +5 -0
- data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +5 -0
- data/spec/rubocop/cop/style/indentation_width_spec.rb +95 -0
- data/spec/rubocop/cop/style/line_length_spec.rb +75 -0
- data/spec/rubocop/cop/style/next_spec.rb +28 -0
- data/spec/rubocop/cop/style/numeric_literals_spec.rb +10 -0
- data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +5 -0
- data/spec/rubocop/cop/style/space_after_colon_spec.rb +17 -0
- data/spec/rubocop/cop/style/space_around_equals_in_parameter_default_spec.rb +11 -0
- data/spec/rubocop/cop/style/space_around_operators_spec.rb +1 -0
- data/spec/rubocop/cop/style/space_before_comma_spec.rb +42 -0
- data/spec/rubocop/cop/style/space_before_semicolon_spec.rb +28 -0
- data/spec/rubocop/cop/style/trailing_comma_spec.rb +37 -15
- data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +8 -10
- data/spec/rubocop/cop/style/unneeded_percent_q_spec.rb +72 -0
- data/spec/rubocop/cop/style/word_array_spec.rb +6 -0
- data/spec/rubocop/cop/team_spec.rb +8 -8
- data/spec/rubocop/cop/util_spec.rb +10 -0
- data/spec/rubocop/cop/variable_force/assignment_spec.rb +1 -1
- data/spec/rubocop/cop/variable_force/locatable_spec.rb +1 -1
- data/spec/rubocop/cop/variable_force/scope_spec.rb +1 -1
- data/spec/rubocop/cop/variable_force/variable_spec.rb +4 -4
- data/spec/rubocop/formatter/base_formatter_spec.rb +5 -5
- data/spec/rubocop/formatter/colorizable_spec.rb +2 -2
- data/spec/rubocop/formatter/json_formatter_spec.rb +1 -1
- data/spec/rubocop/path_util_spec.rb +15 -15
- data/spec/rubocop/processed_source_spec.rb +104 -50
- data/spec/rubocop/runner_spec.rb +64 -0
- data/spec/spec_helper.rb +8 -10
- data/spec/support/shared_examples.rb +22 -0
- metadata +39 -15
- data/lib/rubocop/source_parser.rb +0 -47
- data/spec/rubocop/file_inspector_spec.rb +0 -84
- data/spec/rubocop/source_parser_spec.rb +0 -85
@@ -363,16 +363,7 @@ describe RuboCop::Cop::Lint::UselessAssignment do
|
|
363
363
|
]
|
364
364
|
end
|
365
365
|
|
366
|
-
|
367
|
-
inspect_source(cop, source)
|
368
|
-
expect(cop.offenses.size).to eq(1)
|
369
|
-
expect(cop.offenses.first.message)
|
370
|
-
.to eq('Useless assignment to variable - `foo`.')
|
371
|
-
expect(cop.offenses.first.line).to eq(1)
|
372
|
-
expect(cop.highlights).to eq(['foo'])
|
373
|
-
end
|
374
|
-
|
375
|
-
include_examples 'mimics MRI 2.1'
|
366
|
+
include_examples 'accepts'
|
376
367
|
end
|
377
368
|
|
378
369
|
context 'when a block local variable is assigned and unreferenced' do
|
@@ -498,14 +489,13 @@ describe RuboCop::Cop::Lint::UselessAssignment do
|
|
498
489
|
|
499
490
|
it 'registers an offense' do
|
500
491
|
pending 'Requires an advanced logic that checks whether the return ' \
|
501
|
-
'value of an operator assignment is used or not.'
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
end
|
492
|
+
'value of an operator assignment is used or not.'
|
493
|
+
inspect_source(cop, source)
|
494
|
+
expect(cop.offenses.size).to eq(1)
|
495
|
+
expect(cop.offenses.first.message)
|
496
|
+
.to eq('Useless assignment to variable - `foo`.')
|
497
|
+
expect(cop.offenses.first.line).to eq(7)
|
498
|
+
expect(cop.highlights).to eq(['foo'])
|
509
499
|
end
|
510
500
|
end
|
511
501
|
|
@@ -5,61 +5,85 @@ require 'spec_helper'
|
|
5
5
|
describe RuboCop::Cop::Lint::UselessSetterCall do
|
6
6
|
subject(:cop) { described_class.new }
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
.
|
8
|
+
context 'with method ending with setter call on local object' do
|
9
|
+
it 'registers an offense' do
|
10
|
+
inspect_source(cop,
|
11
|
+
['def test',
|
12
|
+
' top = Top.new',
|
13
|
+
' top.attr = 5',
|
14
|
+
'end'
|
15
|
+
])
|
16
|
+
expect(cop.offenses.size).to eq(1)
|
17
|
+
expect(cop.messages)
|
18
|
+
.to eq(['Useless setter call to local variable `top`.'])
|
19
|
+
end
|
18
20
|
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
22
|
+
context 'with singleton method ending with setter call on local object' do
|
23
|
+
it 'registers an offense' do
|
24
|
+
inspect_source(cop,
|
25
|
+
['def Top.test',
|
26
|
+
' top = Top.new',
|
27
|
+
' top.attr = 5',
|
28
|
+
'end'
|
29
|
+
])
|
30
|
+
expect(cop.offenses.size).to eq(1)
|
31
|
+
end
|
28
32
|
end
|
29
33
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
context 'with method ending with square bracket setter on local object' do
|
35
|
+
it 'registers an offense' do
|
36
|
+
inspect_source(cop,
|
37
|
+
['def test',
|
38
|
+
' top = Top.new',
|
39
|
+
' top[:attr] = 5',
|
40
|
+
'end'
|
41
|
+
])
|
42
|
+
expect(cop.offenses.size).to eq(1)
|
43
|
+
expect(cop.messages)
|
44
|
+
.to eq(['Useless setter call to local variable `top`.'])
|
45
|
+
end
|
38
46
|
end
|
39
47
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
+
context 'with method ending with ivar assignment' do
|
49
|
+
it 'accepts' do
|
50
|
+
inspect_source(cop,
|
51
|
+
['def test',
|
52
|
+
' something',
|
53
|
+
' @top = 5',
|
54
|
+
'end'
|
55
|
+
])
|
56
|
+
expect(cop.offenses).to be_empty
|
57
|
+
end
|
48
58
|
end
|
49
59
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
60
|
+
context 'with method ending with setter call on ivar' do
|
61
|
+
it 'accepts' do
|
62
|
+
inspect_source(cop,
|
63
|
+
['def test',
|
64
|
+
' something',
|
65
|
+
' @top.attr = 5',
|
66
|
+
'end'
|
67
|
+
])
|
68
|
+
expect(cop.offenses).to be_empty
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'with method ending with setter call on argument' do
|
73
|
+
it 'accepts' do
|
74
|
+
inspect_source(cop,
|
75
|
+
['def test(some_arg)',
|
76
|
+
' unrelated_local_variable = Top.new',
|
77
|
+
' some_arg.attr = 5',
|
78
|
+
'end'
|
79
|
+
])
|
80
|
+
expect(cop.offenses).to be_empty
|
81
|
+
end
|
58
82
|
end
|
59
83
|
|
60
|
-
context 'when a lvar
|
84
|
+
context 'when a lvar contains an object passed as argument ' \
|
61
85
|
'at the end of the method' do
|
62
|
-
it 'accepts the
|
86
|
+
it 'accepts the setter call on the lvar' do
|
63
87
|
inspect_source(cop,
|
64
88
|
['def test(some_arg)',
|
65
89
|
' @some_ivar = some_arg',
|
@@ -73,9 +97,9 @@ describe RuboCop::Cop::Lint::UselessSetterCall do
|
|
73
97
|
end
|
74
98
|
end
|
75
99
|
|
76
|
-
context 'when a lvar
|
100
|
+
context 'when a lvar contains an object passed as argument ' \
|
77
101
|
'by multiple-assignment at the end of the method' do
|
78
|
-
it 'accepts the
|
102
|
+
it 'accepts the setter call on the lvar' do
|
79
103
|
inspect_source(cop,
|
80
104
|
['def test(some_arg)',
|
81
105
|
' _first, some_lvar, _third = 1, some_arg, 3',
|
@@ -86,7 +110,7 @@ describe RuboCop::Cop::Lint::UselessSetterCall do
|
|
86
110
|
end
|
87
111
|
end
|
88
112
|
|
89
|
-
context 'when a lvar does not
|
113
|
+
context 'when a lvar does not contain any object passed as argument ' \
|
90
114
|
'with multiple-assignment at the end of the method' do
|
91
115
|
it 'registers an offense' do
|
92
116
|
inspect_source(cop,
|
@@ -99,9 +123,9 @@ describe RuboCop::Cop::Lint::UselessSetterCall do
|
|
99
123
|
end
|
100
124
|
end
|
101
125
|
|
102
|
-
context 'when a lvar possibly
|
126
|
+
context 'when a lvar possibly contains an object passed as argument ' \
|
103
127
|
'by logical-operator-assignment at the end of the method' do
|
104
|
-
it 'accepts the
|
128
|
+
it 'accepts the setter call on the lvar' do
|
105
129
|
inspect_source(cop,
|
106
130
|
['def test(some_arg)',
|
107
131
|
' some_lvar = nil',
|
@@ -113,7 +137,7 @@ describe RuboCop::Cop::Lint::UselessSetterCall do
|
|
113
137
|
end
|
114
138
|
end
|
115
139
|
|
116
|
-
context 'when a lvar does not
|
140
|
+
context 'when a lvar does not contain any object passed as argument ' \
|
117
141
|
'by binary-operator-assignment at the end of the method' do
|
118
142
|
it 'registers an offense' do
|
119
143
|
inspect_source(cop,
|
@@ -129,7 +153,7 @@ describe RuboCop::Cop::Lint::UselessSetterCall do
|
|
129
153
|
|
130
154
|
context 'when a lvar declared as an argument ' \
|
131
155
|
'is no longer the passed object at the end of the method' do
|
132
|
-
it 'registers an offense for the
|
156
|
+
it 'registers an offense for the setter call on the lvar' do
|
133
157
|
inspect_source(cop,
|
134
158
|
['def test(some_arg)',
|
135
159
|
' some_arg = Top.new',
|
@@ -140,6 +164,30 @@ describe RuboCop::Cop::Lint::UselessSetterCall do
|
|
140
164
|
end
|
141
165
|
end
|
142
166
|
|
167
|
+
context 'when a lvar contains a local object instanciated with literal' do
|
168
|
+
it 'registers an offense for the setter call on the lvar' do
|
169
|
+
inspect_source(cop,
|
170
|
+
['def test',
|
171
|
+
' some_arg = {}',
|
172
|
+
' some_arg[:attr] = 1',
|
173
|
+
'end'
|
174
|
+
])
|
175
|
+
expect(cop.offenses.size).to eq(1)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
context 'when a lvar contains a non-local object returned by a method' do
|
180
|
+
it 'accepts' do
|
181
|
+
inspect_source(cop,
|
182
|
+
['def test',
|
183
|
+
' some_lvar = Foo.shared_object',
|
184
|
+
' some_lvar[:attr] = 1',
|
185
|
+
'end'
|
186
|
+
])
|
187
|
+
expect(cop.offenses).to be_empty
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
143
191
|
it 'is not confused by operators ending with =' do
|
144
192
|
inspect_source(cop,
|
145
193
|
['def test',
|
@@ -17,7 +17,7 @@ describe RuboCop::Cop::Offense do
|
|
17
17
|
expect(offense.line).to eq(1)
|
18
18
|
expect(offense.message).to eq('message')
|
19
19
|
expect(offense.cop_name).to eq('CopName')
|
20
|
-
expect(offense.corrected?).to
|
20
|
+
expect(offense.corrected?).to be_truthy
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'overrides #to_s' do
|
@@ -35,14 +35,14 @@ describe RuboCop::Cop::Offense do
|
|
35
35
|
o1 = described_class.new(:convention, location, 'message', 'CopName')
|
36
36
|
o2 = described_class.new(:convention, location, 'message', 'CopName')
|
37
37
|
|
38
|
-
expect(o1 == o2).to
|
38
|
+
expect(o1 == o2).to be_truthy
|
39
39
|
end
|
40
40
|
|
41
41
|
it 'is frozen' do
|
42
42
|
expect(offense).to be_frozen
|
43
43
|
end
|
44
44
|
|
45
|
-
[:severity, :location, :
|
45
|
+
[:severity, :location, :message, :cop_name].each do |a|
|
46
46
|
describe "##{a}" do
|
47
47
|
it 'is frozen' do
|
48
48
|
expect(offense.send(a)).to be_frozen
|
@@ -95,6 +95,18 @@ describe RuboCop::Cop::Style::AccessModifierIndentation, :config do
|
|
95
95
|
.to eq(['Indent access modifiers like `private`.'])
|
96
96
|
end
|
97
97
|
|
98
|
+
it 'accepts misaligned private in blocks that are not recognized as ' \
|
99
|
+
'class/module definitions' do
|
100
|
+
inspect_source(cop,
|
101
|
+
['Test = func do',
|
102
|
+
'',
|
103
|
+
'private',
|
104
|
+
'',
|
105
|
+
' def test; end',
|
106
|
+
'end'])
|
107
|
+
expect(cop.offenses).to be_empty
|
108
|
+
end
|
109
|
+
|
98
110
|
it 'registers an offense for misaligned private in module ' \
|
99
111
|
'defined with Module.new' do
|
100
112
|
inspect_source(cop,
|
@@ -25,13 +25,13 @@ describe RuboCop::Cop::Style::AlignHash, :config do
|
|
25
25
|
}
|
26
26
|
end
|
27
27
|
|
28
|
-
it 'registers
|
28
|
+
it 'registers offense for misaligned keys in implicit hash' do
|
29
29
|
inspect_source(cop, ['func(a: 0,',
|
30
30
|
' b: 1)'])
|
31
31
|
expect(cop.offenses.size).to eq(1)
|
32
32
|
end
|
33
33
|
|
34
|
-
it 'registers
|
34
|
+
it 'registers offense for misaligned keys in explicit hash' do
|
35
35
|
inspect_source(cop, ['func({a: 0,',
|
36
36
|
' b: 1})'])
|
37
37
|
expect(cop.offenses.size).to eq(1)
|
@@ -71,7 +71,7 @@ describe RuboCop::Cop::Style::AlignHash, :config do
|
|
71
71
|
expect(cop.offenses).to be_empty
|
72
72
|
end
|
73
73
|
|
74
|
-
it 'registers
|
74
|
+
it 'registers offense for misaligned keys in explicit hash' do
|
75
75
|
inspect_source(cop, ['func({a: 0,',
|
76
76
|
' b: 1})'])
|
77
77
|
expect(cop.offenses.size).to eq(1)
|
@@ -85,7 +85,7 @@ describe RuboCop::Cop::Style::AlignHash, :config do
|
|
85
85
|
}
|
86
86
|
end
|
87
87
|
|
88
|
-
it 'registers
|
88
|
+
it 'registers offense for misaligned keys in implicit hash' do
|
89
89
|
inspect_source(cop, ['func(a: 0,',
|
90
90
|
' b: 1)'])
|
91
91
|
expect(cop.offenses.size).to eq(1)
|
@@ -322,7 +322,7 @@ describe RuboCop::Cop::Style::AlignParameters, :config do
|
|
322
322
|
expect(cop.offenses).to be_empty
|
323
323
|
end
|
324
324
|
|
325
|
-
it 'registers
|
325
|
+
it 'registers offenses for double indentation from relevant method' do
|
326
326
|
inspect_source(cop, [' something',
|
327
327
|
' .method_name(',
|
328
328
|
' a,',
|
@@ -12,8 +12,18 @@ describe RuboCop::Cop::Style::Attr do
|
|
12
12
|
expect(cop.offenses.size).to eq(1)
|
13
13
|
end
|
14
14
|
|
15
|
+
it 'accepts attr when it does not take arguments' do
|
16
|
+
inspect_source(cop, 'func(attr)')
|
17
|
+
expect(cop.offenses).to be_empty
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'accepts attr when it has a receiver' do
|
21
|
+
inspect_source(cop, 'x.attr arg')
|
22
|
+
expect(cop.offenses).to be_empty
|
23
|
+
end
|
24
|
+
|
15
25
|
it 'auto-corrects attr to attr_reader' do
|
16
|
-
new_source = autocorrect_source(cop, 'attr')
|
17
|
-
expect(new_source).to eq('attr_reader')
|
26
|
+
new_source = autocorrect_source(cop, 'attr :name')
|
27
|
+
expect(new_source).to eq('attr_reader :name')
|
18
28
|
end
|
19
29
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe RuboCop::Cop::Style::ClassCheck, :config do
|
6
|
+
subject(:cop) { described_class.new(config) }
|
7
|
+
|
8
|
+
context 'when enforced style is is_a?' do
|
9
|
+
let(:cop_config) { { 'EnforcedStyle' => 'is_a?' } }
|
10
|
+
|
11
|
+
it 'registers an offense for kind_of?' do
|
12
|
+
inspect_source(cop, 'x.kind_of? y')
|
13
|
+
expect(cop.offenses.size).to eq(1)
|
14
|
+
expect(cop.highlights).to eq(['kind_of?'])
|
15
|
+
expect(cop.messages)
|
16
|
+
.to eq(['Prefer `Object#is_a?` over `Object#kind_of?`.'])
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'auto-corrects kind_of? to is_a?' do
|
20
|
+
corrected = autocorrect_source(cop, ['x.kind_of? y'])
|
21
|
+
expect(corrected).to eq 'x.is_a? y'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when enforced style is kind_of?' do
|
26
|
+
let(:cop_config) { { 'EnforcedStyle' => 'kind_of?' } }
|
27
|
+
|
28
|
+
it 'registers an offense for is_a?' do
|
29
|
+
inspect_source(cop, 'x.is_a? y')
|
30
|
+
expect(cop.offenses.size).to eq(1)
|
31
|
+
expect(cop.highlights).to eq(['is_a?'])
|
32
|
+
expect(cop.messages)
|
33
|
+
.to eq(['Prefer `Object#kind_of?` over `Object#is_a?`.'])
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'auto-corrects is_a? to kind_of?' do
|
37
|
+
corrected = autocorrect_source(cop, ['x.is_a? y'])
|
38
|
+
expect(corrected).to eq 'x.kind_of? y'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -65,4 +65,9 @@ describe RuboCop::Cop::Style::EachWithObject do
|
|
65
65
|
inspect_source(cop, ['[].inject(:+)', '[].reduce(:+)'])
|
66
66
|
expect(cop.offenses).to be_empty
|
67
67
|
end
|
68
|
+
|
69
|
+
it 'does not blow up for reduce with no arguments' do
|
70
|
+
inspect_source(cop, ['[1, 2, 3].inject { |a, e| a + e }'])
|
71
|
+
expect(cop.offenses).to be_empty
|
72
|
+
end
|
68
73
|
end
|
@@ -11,6 +11,11 @@ describe RuboCop::Cop::Style::IfWithSemicolon do
|
|
11
11
|
['Never use if x; Use the ternary operator instead.'])
|
12
12
|
end
|
13
13
|
|
14
|
+
it 'accepts one line if/then/end' do
|
15
|
+
inspect_source(cop, ['if cond then run else dont end'])
|
16
|
+
expect(cop.messages).to be_empty
|
17
|
+
end
|
18
|
+
|
14
19
|
it 'can handle modifier conditionals' do
|
15
20
|
inspect_source(cop, ['class Hash',
|
16
21
|
'end if RUBY_VERSION < "1.8.7"'])
|
@@ -149,6 +149,47 @@ describe RuboCop::Cop::Style::IndentationWidth do
|
|
149
149
|
' end',
|
150
150
|
'end'].join("\n")
|
151
151
|
end
|
152
|
+
|
153
|
+
it 'indents parenthesized expressions' do
|
154
|
+
src = ['var1 = nil',
|
155
|
+
'array_list = []',
|
156
|
+
'if var1.attr1 != 0 || array_list.select{ |w|',
|
157
|
+
' (w.attr2 == var1.attr2)',
|
158
|
+
' }.blank?',
|
159
|
+
' array_list << var1',
|
160
|
+
'end']
|
161
|
+
corrected = autocorrect_source(cop, src)
|
162
|
+
expect(corrected)
|
163
|
+
.to eq ['var1 = nil',
|
164
|
+
'array_list = []',
|
165
|
+
'if var1.attr1 != 0 || array_list.select{ |w|',
|
166
|
+
' (w.attr2 == var1.attr2)',
|
167
|
+
' }.blank?',
|
168
|
+
' array_list << var1',
|
169
|
+
'end'].join("\n")
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'leaves rescue ; end unchanged' do
|
173
|
+
src = ['if variable',
|
174
|
+
' begin',
|
175
|
+
' do_something',
|
176
|
+
' rescue ; end # consume any exception',
|
177
|
+
'end']
|
178
|
+
corrected = autocorrect_source(cop, src)
|
179
|
+
expect(corrected).to eq src.join("\n")
|
180
|
+
end
|
181
|
+
|
182
|
+
it 'leaves block unchanged if block end is not on its own line' do
|
183
|
+
src = ['a_function {',
|
184
|
+
' # a comment',
|
185
|
+
' result = AObject.find_by_attr(attr) if attr',
|
186
|
+
' result || AObject.make(',
|
187
|
+
' :attr => attr,',
|
188
|
+
' :attr2 => Other.get_value(),',
|
189
|
+
' :attr3 => Another.get_value()) }']
|
190
|
+
corrected = autocorrect_source(cop, src)
|
191
|
+
expect(corrected).to eq src.join("\n")
|
192
|
+
end
|
152
193
|
end
|
153
194
|
|
154
195
|
it 'accepts a one line if statement' do
|
@@ -505,6 +546,20 @@ describe RuboCop::Cop::Style::IndentationWidth do
|
|
505
546
|
expect(cop.offenses).to be_empty
|
506
547
|
end
|
507
548
|
|
549
|
+
it 'accepts aligned values in when clause' do
|
550
|
+
inspect_source(cop,
|
551
|
+
['case superclass',
|
552
|
+
'when /\A(#{NAMESPACEMATCH})(?:\s|\Z)/,',
|
553
|
+
' /\A(Struct|OStruct)\.new/,',
|
554
|
+
' /\ADelegateClass\((.+?)\)\s*\Z/,',
|
555
|
+
' /\A(#{NAMESPACEMATCH})\(/',
|
556
|
+
' $1',
|
557
|
+
'when "self"',
|
558
|
+
' namespace.path',
|
559
|
+
'end'])
|
560
|
+
expect(cop.offenses).to be_empty
|
561
|
+
end
|
562
|
+
|
508
563
|
it 'accepts case/when/else laid out as a table' do
|
509
564
|
inspect_source(cop,
|
510
565
|
['case sexp.loc.keyword.source',
|
@@ -703,6 +758,46 @@ describe RuboCop::Cop::Style::IndentationWidth do
|
|
703
758
|
end
|
704
759
|
end
|
705
760
|
|
761
|
+
context 'with begin/rescue/else/ensure/end' do
|
762
|
+
it 'registers an offense for bad indentation of bodies' do
|
763
|
+
inspect_source(cop,
|
764
|
+
['def my_func',
|
765
|
+
" puts 'do something outside block'",
|
766
|
+
' begin',
|
767
|
+
" puts 'do something error prone'",
|
768
|
+
' rescue SomeException, SomeOther => e',
|
769
|
+
" puts 'wrongly intended error handling'",
|
770
|
+
' rescue',
|
771
|
+
" puts 'wrongly intended error handling'",
|
772
|
+
' else',
|
773
|
+
" puts 'wrongly intended normal case handling'",
|
774
|
+
' ensure',
|
775
|
+
" puts 'wrongly intended common handling'",
|
776
|
+
' end',
|
777
|
+
'end'])
|
778
|
+
expect(cop.messages).to eq(['Use 2 (not 0) spaces for indentation.',
|
779
|
+
'Use 2 (not 1) spaces for indentation.',
|
780
|
+
'Use 2 (not 1) spaces for indentation.',
|
781
|
+
'Use 2 (not 3) spaces for indentation.',
|
782
|
+
'Use 2 (not 4) spaces for indentation.'])
|
783
|
+
end
|
784
|
+
end
|
785
|
+
|
786
|
+
context 'with def/rescue/end' do
|
787
|
+
it 'registers an offense for bad indentation of bodies' do
|
788
|
+
inspect_source(cop,
|
789
|
+
['def my_func',
|
790
|
+
" puts 'do something error prone'",
|
791
|
+
'rescue SomeException',
|
792
|
+
" puts 'wrongly intended error handling'",
|
793
|
+
'rescue',
|
794
|
+
" puts 'wrongly intended error handling'",
|
795
|
+
'end'])
|
796
|
+
expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.',
|
797
|
+
'Use 2 (not 1) spaces for indentation.'])
|
798
|
+
end
|
799
|
+
end
|
800
|
+
|
706
801
|
context 'with block' do
|
707
802
|
it 'registers an offense for bad indentation of a do/end body' do
|
708
803
|
inspect_source(cop,
|