rubocop 0.9.1 → 0.10.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/.travis.yml +3 -1
- data/CHANGELOG.md +38 -0
- data/README.md +34 -0
- data/Rakefile +3 -0
- data/config/default.yml +14 -1
- data/config/enabled.yml +30 -7
- data/lib/rubocop.rb +15 -0
- data/lib/rubocop/cli.rb +48 -154
- data/lib/rubocop/config.rb +19 -22
- data/lib/rubocop/config_store.rb +2 -4
- data/lib/rubocop/cop/commissioner.rb +90 -0
- data/lib/rubocop/cop/cop.rb +38 -31
- data/lib/rubocop/cop/corrector.rb +84 -0
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +0 -3
- data/lib/rubocop/cop/lint/block_alignment.rb +151 -0
- data/lib/rubocop/cop/lint/empty_ensure.rb +18 -0
- data/lib/rubocop/cop/lint/end_alignment.rb +0 -124
- data/lib/rubocop/cop/lint/end_in_method.rb +0 -2
- data/lib/rubocop/cop/lint/ensure_return.rb +3 -3
- data/lib/rubocop/cop/lint/eval.rb +0 -2
- data/lib/rubocop/cop/lint/handle_exceptions.rb +0 -2
- data/lib/rubocop/cop/lint/literal_in_condition.rb +0 -10
- data/lib/rubocop/cop/lint/loop.rb +0 -2
- data/lib/rubocop/cop/lint/rescue_exception.rb +0 -2
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +2 -2
- data/lib/rubocop/cop/lint/unreachable_code.rb +0 -2
- data/lib/rubocop/cop/lint/unused_local_variable.rb +2 -2
- data/lib/rubocop/cop/lint/void.rb +0 -2
- data/lib/rubocop/cop/offence.rb +9 -0
- data/lib/rubocop/cop/rails/validation.rb +2 -1
- data/lib/rubocop/cop/style/access_control.rb +4 -3
- data/lib/rubocop/cop/style/alias.rb +2 -4
- data/lib/rubocop/cop/style/align_parameters.rb +0 -2
- data/lib/rubocop/cop/style/and_or.rb +4 -6
- data/lib/rubocop/cop/style/ascii_comments.rb +2 -2
- data/lib/rubocop/cop/style/ascii_identifiers.rb +2 -2
- data/lib/rubocop/cop/style/attr.rb +0 -2
- data/lib/rubocop/cop/style/avoid_class_vars.rb +0 -1
- data/lib/rubocop/cop/style/avoid_for.rb +0 -2
- data/lib/rubocop/cop/style/avoid_global_vars.rb +3 -7
- data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +0 -2
- data/lib/rubocop/cop/style/avoid_perlisms.rb +2 -4
- data/lib/rubocop/cop/style/begin_block.rb +0 -2
- data/lib/rubocop/cop/style/block_comments.rb +2 -2
- data/lib/rubocop/cop/style/block_nesting.rb +3 -3
- data/lib/rubocop/cop/style/blocks.rb +0 -2
- data/lib/rubocop/cop/style/case_equality.rb +0 -2
- data/lib/rubocop/cop/style/case_indentation.rb +0 -2
- data/lib/rubocop/cop/style/character_literal.rb +10 -6
- data/lib/rubocop/cop/style/class_and_module_camel_case.rb +0 -4
- data/lib/rubocop/cop/style/class_methods.rb +1 -1
- data/lib/rubocop/cop/style/collection_methods.rb +3 -5
- data/lib/rubocop/cop/style/colon_method_call.rb +3 -3
- data/lib/rubocop/cop/style/comment_annotation.rb +44 -0
- data/lib/rubocop/cop/style/constant_name.rb +0 -2
- data/lib/rubocop/cop/style/def_parentheses.rb +0 -8
- data/lib/rubocop/cop/style/documentation.rb +6 -2
- data/lib/rubocop/cop/style/dot_position.rb +0 -2
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +0 -2
- data/lib/rubocop/cop/style/empty_lines.rb +10 -8
- data/lib/rubocop/cop/style/empty_literal.rb +3 -1
- data/lib/rubocop/cop/style/encoding.rb +7 -6
- data/lib/rubocop/cop/style/end_block.rb +0 -2
- data/lib/rubocop/cop/style/end_of_line.rb +4 -3
- data/lib/rubocop/cop/style/favor_join.rb +0 -2
- data/lib/rubocop/cop/style/favor_modifier.rb +9 -9
- data/lib/rubocop/cop/style/favor_sprintf.rb +0 -2
- data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +0 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +0 -2
- data/lib/rubocop/cop/style/if_then_else.rb +0 -2
- data/lib/rubocop/cop/style/lambda.rb +0 -2
- data/lib/rubocop/cop/style/leading_comment_space.rb +2 -2
- data/lib/rubocop/cop/style/line_continuation.rb +4 -3
- data/lib/rubocop/cop/style/line_length.rb +4 -3
- data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +4 -3
- data/lib/rubocop/cop/style/method_call_parentheses.rb +0 -2
- data/lib/rubocop/cop/style/method_length.rb +0 -4
- data/lib/rubocop/cop/style/not.rb +0 -2
- data/lib/rubocop/cop/style/op_method.rb +0 -2
- data/lib/rubocop/cop/style/parameter_lists.rb +0 -2
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -6
- data/lib/rubocop/cop/style/proc.rb +0 -2
- data/lib/rubocop/cop/style/reduce_arguments.rb +0 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +45 -0
- data/lib/rubocop/cop/style/redundant_return.rb +59 -0
- data/lib/rubocop/cop/style/redundant_self.rb +83 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +0 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +13 -21
- data/lib/rubocop/cop/style/semicolon.rb +15 -9
- data/lib/rubocop/cop/style/single_line_methods.rb +0 -4
- data/lib/rubocop/cop/style/space_after_comma_etc.rb +2 -2
- data/lib/rubocop/cop/style/space_after_control_keyword.rb +0 -1
- data/lib/rubocop/cop/style/string_literals.rb +5 -2
- data/lib/rubocop/cop/style/surrounding_space.rb +106 -91
- data/lib/rubocop/cop/style/tab.rb +4 -3
- data/lib/rubocop/cop/style/ternary_operator.rb +0 -4
- data/lib/rubocop/cop/style/trailing_whitespace.rb +4 -3
- data/lib/rubocop/cop/style/trivial_accessors.rb +51 -6
- data/lib/rubocop/cop/style/unless_else.rb +0 -2
- data/lib/rubocop/cop/style/variable_interpolation.rb +0 -2
- data/lib/rubocop/cop/style/when_then.rb +3 -3
- data/lib/rubocop/cop/style/while_until_do.rb +3 -5
- data/lib/rubocop/cop/style/word_array.rb +0 -2
- data/lib/rubocop/cop/util.rb +0 -4
- data/lib/rubocop/formatter/file_list_formatter.rb +18 -0
- data/lib/rubocop/formatter/formatter_set.rb +2 -1
- data/lib/rubocop/processed_source.rb +27 -0
- data/lib/rubocop/rake_task.rb +50 -0
- data/lib/rubocop/source_parser.rb +105 -0
- data/lib/rubocop/target_finder.rb +67 -0
- data/lib/rubocop/token.rb +22 -0
- data/lib/rubocop/version.rb +1 -1
- data/rubocop.gemspec +5 -3
- data/spec/project_spec.rb +0 -11
- data/spec/rubocop/cli_spec.rb +112 -6
- data/spec/rubocop/config_spec.rb +13 -17
- data/spec/rubocop/config_store_spec.rb +8 -23
- data/spec/rubocop/cops/commissioner_spec.rb +72 -0
- data/spec/rubocop/cops/corrector_spec.rb +63 -0
- data/spec/rubocop/cops/lint/assignment_in_condition_spec.rb +2 -2
- data/spec/rubocop/cops/lint/block_alignment_spec.rb +357 -0
- data/spec/rubocop/cops/lint/empty_ensure_spec.rb +33 -0
- data/spec/rubocop/cops/lint/end_alignment_spec.rb +0 -263
- data/spec/rubocop/cops/lint/ensure_return_spec.rb +6 -9
- data/spec/rubocop/cops/offence_spec.rb +28 -0
- data/spec/rubocop/cops/style/and_or_spec.rb +21 -11
- data/spec/rubocop/cops/style/ascii_identifiers_spec.rb +14 -0
- data/spec/rubocop/cops/style/avoid_global_vars_spec.rb +10 -14
- data/spec/rubocop/cops/style/character_literal_spec.rb +17 -2
- data/spec/rubocop/cops/style/colon_method_call_spec.rb +20 -15
- data/spec/rubocop/cops/style/comment_annotation_spec.rb +62 -0
- data/spec/rubocop/cops/style/encoding_spec.rb +7 -0
- data/spec/rubocop/cops/style/parentheses_around_condition_spec.rb +37 -9
- data/spec/rubocop/cops/style/redundant_begin_spec.rb +63 -0
- data/spec/rubocop/cops/style/redundant_return_spec.rb +64 -0
- data/spec/rubocop/cops/style/redundant_self_spec.rb +76 -0
- data/spec/rubocop/cops/style/string_literals_spec.rb +18 -13
- data/spec/rubocop/cops/style/trivial_accessors_spec.rb +110 -52
- data/spec/rubocop/cops/style/when_then_spec.rb +14 -7
- data/spec/rubocop/cops/style/while_until_do_spec.rb +12 -0
- data/spec/rubocop/cops/variable_inspector_spec.rb +3 -5
- data/spec/rubocop/formatter/file_list_formatter_spec.rb +33 -0
- data/spec/rubocop/processed_source_spec.rb +67 -0
- data/spec/rubocop/source_parser_spec.rb +141 -0
- data/spec/rubocop/target_finder_spec.rb +180 -0
- data/spec/rubocop/token_spec.rb +27 -0
- data/spec/spec_helper.rb +24 -4
- metadata +108 -18
- checksums.yaml +0 -7
@@ -99,10 +99,10 @@ module Rubocop
|
|
99
99
|
|
100
100
|
it 'does not accepts = in condition surrounded with braces' do
|
101
101
|
inspect_source(cop,
|
102
|
-
['if (test
|
102
|
+
['if (test = 10)',
|
103
103
|
'end'
|
104
104
|
])
|
105
|
-
expect(cop.offences).to
|
105
|
+
expect(cop.offences.size).to eq(1)
|
106
106
|
end
|
107
107
|
end
|
108
108
|
end
|
@@ -0,0 +1,357 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# rubocop:disable LineLength
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
|
6
|
+
module Rubocop
|
7
|
+
module Cop
|
8
|
+
module Lint
|
9
|
+
describe BlockAlignment do
|
10
|
+
let(:cop) { BlockAlignment.new }
|
11
|
+
|
12
|
+
it 'registers an offence for mismatched block end' do
|
13
|
+
inspect_source(cop,
|
14
|
+
['test do |ala|',
|
15
|
+
' end'
|
16
|
+
])
|
17
|
+
expect(cop.offences.size).to eq(1)
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when the block is a logical operand' do
|
21
|
+
it 'accepts a correctly aligned block end' do
|
22
|
+
inspect_source(cop,
|
23
|
+
['(value.is_a? Array) && value.all? do |subvalue|',
|
24
|
+
' type_check_value(subvalue, array_type)',
|
25
|
+
'end',
|
26
|
+
'a || b do',
|
27
|
+
'end',
|
28
|
+
])
|
29
|
+
expect(cop.offences).to be_empty
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'accepts end aligned with a variable' do
|
34
|
+
inspect_source(cop,
|
35
|
+
['variable = test do |ala|',
|
36
|
+
'end'
|
37
|
+
])
|
38
|
+
expect(cop.offences).to be_empty
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when there is an assignment chain' do
|
42
|
+
it 'registers an offence for an end aligned with the 2nd variable' do
|
43
|
+
inspect_source(cop,
|
44
|
+
['a = b = c = test do |ala|',
|
45
|
+
' end'
|
46
|
+
])
|
47
|
+
expect(cop.offences.size).to eq(1)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'accepts end aligned with the first variable' do
|
51
|
+
inspect_source(cop,
|
52
|
+
['a = b = c = test do |ala|',
|
53
|
+
'end',
|
54
|
+
])
|
55
|
+
expect(cop.offences).to be_empty
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'and the block is an operand' do
|
60
|
+
it 'accepts end aligned with a variable' do
|
61
|
+
inspect_source(cop,
|
62
|
+
['b = 1 + preceding_line.reduce(0) do |a, e|',
|
63
|
+
' a + e.length + newline_length',
|
64
|
+
'end + 1'
|
65
|
+
])
|
66
|
+
expect(cop.offences).to be_empty
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'registers an offence for mismatched block end with a variable' do
|
71
|
+
inspect_source(cop,
|
72
|
+
['variable = test do |ala|',
|
73
|
+
' end'
|
74
|
+
])
|
75
|
+
expect(cop.offences.size).to eq(1)
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'when the block is defined on the next line' do
|
79
|
+
it 'accepts end aligned with the block expression' do
|
80
|
+
inspect_source(cop,
|
81
|
+
['variable =',
|
82
|
+
' a_long_method_that_dont_fit_on_the_line do |v|',
|
83
|
+
' v.foo',
|
84
|
+
' end'
|
85
|
+
])
|
86
|
+
expect(cop.offences).to be_empty
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'registers an offences for mismatched end alignment' do
|
90
|
+
inspect_source(cop,
|
91
|
+
['variable =',
|
92
|
+
' a_long_method_that_dont_fit_on_the_line do |v|',
|
93
|
+
' v.foo',
|
94
|
+
'end'
|
95
|
+
])
|
96
|
+
expect(cop.offences.size).to eq(1)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'when the method part is a call chain that spans several lines' do
|
101
|
+
it 'accepts end aligned with first character of line where do is' do
|
102
|
+
src = ['def foo(bar)',
|
103
|
+
' bar.get_stuffs',
|
104
|
+
' .reject do |stuff| ',
|
105
|
+
' stuff.with_a_very_long_expression_that_doesnt_fit_the_line',
|
106
|
+
' end.select do |stuff|',
|
107
|
+
' stuff.another_very_long_expression_that_doesnt_fit_the_line',
|
108
|
+
' end',
|
109
|
+
' .select do |stuff|',
|
110
|
+
' stuff.another_very_long_expression_that_doesnt_fit_the_line',
|
111
|
+
' end',
|
112
|
+
'end']
|
113
|
+
inspect_source(cop, src)
|
114
|
+
expect(cop.offences.map(&:message)).to eq([])
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'registers offences for misaligned ends' do
|
118
|
+
src = ['def foo(bar)',
|
119
|
+
' bar.get_stuffs',
|
120
|
+
' .reject do |stuff| ',
|
121
|
+
' stuff.with_a_very_long_expression_that_doesnt_fit_the_line',
|
122
|
+
' end.select do |stuff|',
|
123
|
+
' stuff.another_very_long_expression_that_doesnt_fit_the_line',
|
124
|
+
' end',
|
125
|
+
' .select do |stuff|',
|
126
|
+
' stuff.another_very_long_expression_that_doesnt_fit_the_line',
|
127
|
+
' end',
|
128
|
+
'end']
|
129
|
+
inspect_source(cop, src)
|
130
|
+
expect(cop.offences).to have(3).items
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'when variables of a mass assignment spans several lines' do
|
135
|
+
it 'accepts end aligned with the variables' do
|
136
|
+
src = ['e,',
|
137
|
+
'f = [5, 6].map do |i|',
|
138
|
+
' i - 5',
|
139
|
+
'end']
|
140
|
+
inspect_source(cop, src)
|
141
|
+
expect(cop.offences.map(&:message)).to eq([])
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'registers an offence for end aligned with the block' do
|
145
|
+
src = ['e,',
|
146
|
+
'f = [5, 6].map do |i|',
|
147
|
+
' i - 5',
|
148
|
+
' end']
|
149
|
+
inspect_source(cop, src)
|
150
|
+
expect(cop.offences).to have(1).item
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'accepts end aligned with an instance variable' do
|
155
|
+
inspect_source(cop,
|
156
|
+
['@variable = test do |ala|',
|
157
|
+
'end'
|
158
|
+
])
|
159
|
+
expect(cop.offences).to be_empty
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'registers an offence for mismatched block end with an instance variable' do
|
163
|
+
inspect_source(cop,
|
164
|
+
['@variable = test do |ala|',
|
165
|
+
' end'
|
166
|
+
])
|
167
|
+
expect(cop.offences.size).to eq(1)
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'accepts end aligned with a class variable' do
|
171
|
+
inspect_source(cop,
|
172
|
+
['@@variable = test do |ala|',
|
173
|
+
'end'
|
174
|
+
])
|
175
|
+
expect(cop.offences).to be_empty
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'registers an offence for mismatched block end with a class variable' do
|
179
|
+
inspect_source(cop,
|
180
|
+
['@@variable = test do |ala|',
|
181
|
+
' end'
|
182
|
+
])
|
183
|
+
expect(cop.offences.size).to eq(1)
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'accepts end aligned with a global variable' do
|
187
|
+
inspect_source(cop,
|
188
|
+
['$variable = test do |ala|',
|
189
|
+
'end'
|
190
|
+
])
|
191
|
+
expect(cop.offences).to be_empty
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'registers an offence for mismatched block end with a global variable' do
|
195
|
+
inspect_source(cop,
|
196
|
+
['$variable = test do |ala|',
|
197
|
+
' end'
|
198
|
+
])
|
199
|
+
expect(cop.offences.size).to eq(1)
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'accepts end aligned with a constant' do
|
203
|
+
inspect_source(cop,
|
204
|
+
['CONSTANT = test do |ala|',
|
205
|
+
'end'
|
206
|
+
])
|
207
|
+
expect(cop.offences).to be_empty
|
208
|
+
end
|
209
|
+
|
210
|
+
it 'registers an offence for mismatched block end with a constant' do
|
211
|
+
inspect_source(cop,
|
212
|
+
['Module::CONSTANT = test do |ala|',
|
213
|
+
' end'
|
214
|
+
])
|
215
|
+
expect(cop.offences.size).to eq(1)
|
216
|
+
end
|
217
|
+
|
218
|
+
it 'accepts end aligned with a method call' do
|
219
|
+
inspect_source(cop,
|
220
|
+
['parser.childs << lambda do |token|',
|
221
|
+
' token << 1',
|
222
|
+
'end'
|
223
|
+
])
|
224
|
+
expect(cop.offences).to be_empty
|
225
|
+
end
|
226
|
+
|
227
|
+
it 'registers an offence for mismatched block end with a method call' do
|
228
|
+
inspect_source(cop,
|
229
|
+
['parser.childs << lambda do |token|',
|
230
|
+
' token << 1',
|
231
|
+
' end'
|
232
|
+
])
|
233
|
+
expect(cop.offences.size).to eq(1)
|
234
|
+
end
|
235
|
+
|
236
|
+
it 'accepts end aligned with a method call with arguments' do
|
237
|
+
inspect_source(cop,
|
238
|
+
['@h[:f] = f.each_pair.map do |f, v|',
|
239
|
+
' v = 1',
|
240
|
+
'end'
|
241
|
+
])
|
242
|
+
expect(cop.offences).to be_empty
|
243
|
+
end
|
244
|
+
|
245
|
+
it 'registers an offence for mismatched end with a method call with arguments' do
|
246
|
+
inspect_source(cop,
|
247
|
+
['@h[:f] = f.each_pair.map do |f, v|',
|
248
|
+
' v = 1',
|
249
|
+
' end'
|
250
|
+
])
|
251
|
+
expect(cop.offences.size).to eq(1)
|
252
|
+
end
|
253
|
+
|
254
|
+
it 'does not raise an error for nested block in a method call' do
|
255
|
+
inspect_source(cop,
|
256
|
+
['expect(arr.all? { |o| o.valid? })'
|
257
|
+
])
|
258
|
+
expect(cop.offences).to be_empty
|
259
|
+
end
|
260
|
+
|
261
|
+
it 'accepts end aligned with the block when the block is a method argument' do
|
262
|
+
inspect_source(cop,
|
263
|
+
['expect(arr.all? do |o|',
|
264
|
+
' o.valid?',
|
265
|
+
' end)'
|
266
|
+
])
|
267
|
+
expect(cop.offences).to be_empty
|
268
|
+
end
|
269
|
+
|
270
|
+
it 'registers an offence for mismatched end not aligned with the block that is an argument' do
|
271
|
+
inspect_source(cop,
|
272
|
+
['expect(arr.all? do |o|',
|
273
|
+
' o.valid?',
|
274
|
+
' end)'
|
275
|
+
])
|
276
|
+
expect(cop.offences.size).to eq(1)
|
277
|
+
end
|
278
|
+
|
279
|
+
it 'accepts end aligned with an op-asgn (+=, -=)' do
|
280
|
+
inspect_source(cop,
|
281
|
+
['rb += files.select do |file|',
|
282
|
+
' file << something',
|
283
|
+
'end'
|
284
|
+
])
|
285
|
+
expect(cop.offences).to be_empty
|
286
|
+
end
|
287
|
+
|
288
|
+
it 'registers an offence for mismatched block end with an op-asgn (+=, -=)' do
|
289
|
+
inspect_source(cop,
|
290
|
+
['rb += files.select do |file|',
|
291
|
+
' file << something',
|
292
|
+
' end'
|
293
|
+
])
|
294
|
+
expect(cop.offences.size).to eq(1)
|
295
|
+
end
|
296
|
+
|
297
|
+
it 'accepts end aligned with an and-asgn (&&=)' do
|
298
|
+
inspect_source(cop,
|
299
|
+
['variable &&= test do |ala|',
|
300
|
+
'end'
|
301
|
+
])
|
302
|
+
expect(cop.offences).to be_empty
|
303
|
+
end
|
304
|
+
|
305
|
+
it 'registers an offence for mismatched block end with an and-asgn (&&=)' do
|
306
|
+
inspect_source(cop,
|
307
|
+
['variable &&= test do |ala|',
|
308
|
+
' end'
|
309
|
+
])
|
310
|
+
expect(cop.offences.size).to eq(1)
|
311
|
+
end
|
312
|
+
|
313
|
+
it 'accepts end aligned with an or-asgn (||=)' do
|
314
|
+
inspect_source(cop,
|
315
|
+
['variable ||= test do |ala|',
|
316
|
+
'end'
|
317
|
+
])
|
318
|
+
expect(cop.offences).to be_empty
|
319
|
+
end
|
320
|
+
|
321
|
+
it 'registers an offence for mismatched block end with an or-asgn (||=)' do
|
322
|
+
inspect_source(cop,
|
323
|
+
['variable ||= test do |ala|',
|
324
|
+
' end'
|
325
|
+
])
|
326
|
+
expect(cop.offences.size).to eq(1)
|
327
|
+
end
|
328
|
+
|
329
|
+
it 'accepts end aligned with a mass assignment' do
|
330
|
+
inspect_source(cop,
|
331
|
+
['var1, var2 = lambda do |test|',
|
332
|
+
' [1, 2]',
|
333
|
+
'end'
|
334
|
+
])
|
335
|
+
expect(cop.offences).to be_empty
|
336
|
+
end
|
337
|
+
|
338
|
+
it 'accepts end aligned with a call chain left hand side' do
|
339
|
+
inspect_source(cop,
|
340
|
+
['parser.diagnostics.consumer = lambda do |diagnostic|',
|
341
|
+
' diagnostics << diagnostic',
|
342
|
+
'end'])
|
343
|
+
expect(cop.offences).to be_empty
|
344
|
+
end
|
345
|
+
|
346
|
+
it 'registers an offence for mismatched block end with a mass assignment' do
|
347
|
+
inspect_source(cop,
|
348
|
+
['var1, var2 = lambda do |test|',
|
349
|
+
' [1, 2]',
|
350
|
+
' end'
|
351
|
+
])
|
352
|
+
expect(cop.offences.size).to eq(1)
|
353
|
+
end
|
354
|
+
end
|
355
|
+
end
|
356
|
+
end
|
357
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
module Lint
|
8
|
+
describe EmptyEnsure do
|
9
|
+
let(:cop) { EmptyEnsure.new }
|
10
|
+
|
11
|
+
it 'registers an offence for empty ensure' do
|
12
|
+
inspect_source(cop,
|
13
|
+
['begin',
|
14
|
+
' something',
|
15
|
+
'ensure',
|
16
|
+
'end'])
|
17
|
+
expect(cop.offences).to have(1).item
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'does not register an offence for non-empty ensure' do
|
21
|
+
inspect_source(cop,
|
22
|
+
['begin',
|
23
|
+
' something',
|
24
|
+
' return',
|
25
|
+
'ensure',
|
26
|
+
' file.close',
|
27
|
+
'end'])
|
28
|
+
expect(cop.offences).to be_empty
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -64,269 +64,6 @@ module Rubocop
|
|
64
64
|
])
|
65
65
|
expect(cop.offences.size).to eq(1)
|
66
66
|
end
|
67
|
-
|
68
|
-
it 'registers an offence for mismatched block end' do
|
69
|
-
inspect_source(cop,
|
70
|
-
['test do |ala|',
|
71
|
-
' end'
|
72
|
-
])
|
73
|
-
expect(cop.offences.size).to eq(1)
|
74
|
-
end
|
75
|
-
|
76
|
-
context 'when the block is a logical operand' do
|
77
|
-
it 'accepts a correctly aligned block end' do
|
78
|
-
inspect_source(cop,
|
79
|
-
['(value.is_a? Array) && value.all? do |subvalue|',
|
80
|
-
' type_check_value(subvalue, array_type)',
|
81
|
-
'end',
|
82
|
-
'a || b do',
|
83
|
-
'end',
|
84
|
-
])
|
85
|
-
expect(cop.offences).to be_empty
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
it 'accepts end aligned with a variable' do
|
90
|
-
inspect_source(cop,
|
91
|
-
['variable = test do |ala|',
|
92
|
-
'end'
|
93
|
-
])
|
94
|
-
expect(cop.offences).to be_empty
|
95
|
-
end
|
96
|
-
|
97
|
-
context 'and the block is an operand' do
|
98
|
-
it 'accepts end aligned with a variable' do
|
99
|
-
inspect_source(cop,
|
100
|
-
['b = 1 + preceding_line.reduce(0) do |a, e|',
|
101
|
-
' a + e.length + newline_length',
|
102
|
-
'end + 1'
|
103
|
-
])
|
104
|
-
expect(cop.offences).to be_empty
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
it 'registers an offence for mismatched block end with a variable' do
|
109
|
-
inspect_source(cop,
|
110
|
-
['variable = test do |ala|',
|
111
|
-
' end'
|
112
|
-
])
|
113
|
-
expect(cop.offences.size).to eq(1)
|
114
|
-
end
|
115
|
-
|
116
|
-
context 'when the block is defined on the next line' do
|
117
|
-
it 'accepts end aligned with the block expression' do
|
118
|
-
inspect_source(cop,
|
119
|
-
['variable =',
|
120
|
-
' a_long_method_that_dont_fit_on_the_line do |v|',
|
121
|
-
' v.foo',
|
122
|
-
' end'
|
123
|
-
])
|
124
|
-
expect(cop.offences).to be_empty
|
125
|
-
end
|
126
|
-
|
127
|
-
it 'registers an offences for mismatched end alignment' do
|
128
|
-
inspect_source(cop,
|
129
|
-
['variable =',
|
130
|
-
' a_long_method_that_dont_fit_on_the_line do |v|',
|
131
|
-
' v.foo',
|
132
|
-
'end'
|
133
|
-
])
|
134
|
-
expect(cop.offences.size).to eq(1)
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
it 'accepts end aligned with an instance variable' do
|
139
|
-
inspect_source(cop,
|
140
|
-
['@variable = test do |ala|',
|
141
|
-
'end'
|
142
|
-
])
|
143
|
-
expect(cop.offences).to be_empty
|
144
|
-
end
|
145
|
-
|
146
|
-
it 'registers an offence for mismatched block end with an instance variable' do
|
147
|
-
inspect_source(cop,
|
148
|
-
['@variable = test do |ala|',
|
149
|
-
' end'
|
150
|
-
])
|
151
|
-
expect(cop.offences.size).to eq(1)
|
152
|
-
end
|
153
|
-
|
154
|
-
it 'accepts end aligned with a class variable' do
|
155
|
-
inspect_source(cop,
|
156
|
-
['@@variable = test do |ala|',
|
157
|
-
'end'
|
158
|
-
])
|
159
|
-
expect(cop.offences).to be_empty
|
160
|
-
end
|
161
|
-
|
162
|
-
it 'registers an offence for mismatched block end with a class variable' do
|
163
|
-
inspect_source(cop,
|
164
|
-
['@@variable = test do |ala|',
|
165
|
-
' end'
|
166
|
-
])
|
167
|
-
expect(cop.offences.size).to eq(1)
|
168
|
-
end
|
169
|
-
|
170
|
-
it 'accepts end aligned with a global variable' do
|
171
|
-
inspect_source(cop,
|
172
|
-
['$variable = test do |ala|',
|
173
|
-
'end'
|
174
|
-
])
|
175
|
-
expect(cop.offences).to be_empty
|
176
|
-
end
|
177
|
-
|
178
|
-
it 'registers an offence for mismatched block end with a global variable' do
|
179
|
-
inspect_source(cop,
|
180
|
-
['$variable = test do |ala|',
|
181
|
-
' end'
|
182
|
-
])
|
183
|
-
expect(cop.offences.size).to eq(1)
|
184
|
-
end
|
185
|
-
|
186
|
-
it 'accepts end aligned with a constant' do
|
187
|
-
inspect_source(cop,
|
188
|
-
['CONSTANT = test do |ala|',
|
189
|
-
'end'
|
190
|
-
])
|
191
|
-
expect(cop.offences).to be_empty
|
192
|
-
end
|
193
|
-
|
194
|
-
it 'registers an offence for mismatched block end with a constant' do
|
195
|
-
inspect_source(cop,
|
196
|
-
['Module::CONSTANT = test do |ala|',
|
197
|
-
' end'
|
198
|
-
])
|
199
|
-
expect(cop.offences.size).to eq(1)
|
200
|
-
end
|
201
|
-
|
202
|
-
it 'accepts end aligned with a method call' do
|
203
|
-
inspect_source(cop,
|
204
|
-
['parser.childs << lambda do |token|',
|
205
|
-
' token << 1',
|
206
|
-
'end'
|
207
|
-
])
|
208
|
-
expect(cop.offences).to be_empty
|
209
|
-
end
|
210
|
-
|
211
|
-
it 'registers an offence for mismatched block end with a method call' do
|
212
|
-
inspect_source(cop,
|
213
|
-
['parser.childs << lambda do |token|',
|
214
|
-
' token << 1',
|
215
|
-
' end'
|
216
|
-
])
|
217
|
-
expect(cop.offences.size).to eq(1)
|
218
|
-
end
|
219
|
-
|
220
|
-
it 'accepts end aligned with a method call with arguments' do
|
221
|
-
inspect_source(cop,
|
222
|
-
['@h[:f] = f.each_pair.map do |f, v|',
|
223
|
-
' v = 1',
|
224
|
-
'end'
|
225
|
-
])
|
226
|
-
expect(cop.offences).to be_empty
|
227
|
-
end
|
228
|
-
|
229
|
-
it 'registers an offence for mismatched end with a method call with arguments' do
|
230
|
-
inspect_source(cop,
|
231
|
-
['@h[:f] = f.each_pair.map do |f, v|',
|
232
|
-
' v = 1',
|
233
|
-
' end'
|
234
|
-
])
|
235
|
-
expect(cop.offences.size).to eq(1)
|
236
|
-
end
|
237
|
-
|
238
|
-
it 'does not raise an error for nested block in a method call' do
|
239
|
-
inspect_source(cop,
|
240
|
-
['expect(arr.all? { |o| o.valid? })'
|
241
|
-
])
|
242
|
-
expect(cop.offences).to be_empty
|
243
|
-
end
|
244
|
-
|
245
|
-
it 'accepts end aligned with the outermost method in the method chain that calls the block' do
|
246
|
-
inspect_source(cop,
|
247
|
-
['expect(arr.all? do |o|',
|
248
|
-
' o.valid?',
|
249
|
-
'end)'
|
250
|
-
])
|
251
|
-
expect(cop.offences).to be_empty
|
252
|
-
end
|
253
|
-
|
254
|
-
it 'registers an offence for mismatched end aligned with the outermost method in the method chain that calls the block' do
|
255
|
-
inspect_source(cop,
|
256
|
-
['expect(arr.all? do |o|',
|
257
|
-
' o.valid?',
|
258
|
-
' end)'
|
259
|
-
])
|
260
|
-
expect(cop.offences.size).to eq(1)
|
261
|
-
end
|
262
|
-
|
263
|
-
it 'accepts end aligned with an op-asgn (+=, -=)' do
|
264
|
-
inspect_source(cop,
|
265
|
-
['rb += files.select do |file|',
|
266
|
-
' file << something',
|
267
|
-
'end'
|
268
|
-
])
|
269
|
-
expect(cop.offences).to be_empty
|
270
|
-
end
|
271
|
-
|
272
|
-
it 'registers an offence for mismatched block end with an op-asgn (+=, -=)' do
|
273
|
-
inspect_source(cop,
|
274
|
-
['rb += files.select do |file|',
|
275
|
-
' file << something',
|
276
|
-
' end'
|
277
|
-
])
|
278
|
-
expect(cop.offences.size).to eq(1)
|
279
|
-
end
|
280
|
-
|
281
|
-
it 'accepts end aligned with an and-asgn (&&=)' do
|
282
|
-
inspect_source(cop,
|
283
|
-
['variable &&= test do |ala|',
|
284
|
-
'end'
|
285
|
-
])
|
286
|
-
expect(cop.offences).to be_empty
|
287
|
-
end
|
288
|
-
|
289
|
-
it 'registers an offence for mismatched block end with an and-asgn (&&=)' do
|
290
|
-
inspect_source(cop,
|
291
|
-
['variable &&= test do |ala|',
|
292
|
-
' end'
|
293
|
-
])
|
294
|
-
expect(cop.offences.size).to eq(1)
|
295
|
-
end
|
296
|
-
|
297
|
-
it 'accepts end aligned with an or-asgn (||=)' do
|
298
|
-
inspect_source(cop,
|
299
|
-
['variable ||= test do |ala|',
|
300
|
-
'end'
|
301
|
-
])
|
302
|
-
expect(cop.offences).to be_empty
|
303
|
-
end
|
304
|
-
|
305
|
-
it 'registers an offence for mismatched block end with an or-asgn (||=)' do
|
306
|
-
inspect_source(cop,
|
307
|
-
['variable ||= test do |ala|',
|
308
|
-
' end'
|
309
|
-
])
|
310
|
-
expect(cop.offences.size).to eq(1)
|
311
|
-
end
|
312
|
-
|
313
|
-
it 'accepts end aligned with a mass assignment' do
|
314
|
-
inspect_source(cop,
|
315
|
-
['var1, var2 = lambda do |test|',
|
316
|
-
' [1, 2]',
|
317
|
-
'end'
|
318
|
-
])
|
319
|
-
expect(cop.offences).to be_empty
|
320
|
-
end
|
321
|
-
|
322
|
-
it 'registers an offence for mismatched block end with a mass assignment' do
|
323
|
-
inspect_source(cop,
|
324
|
-
['var1, var2 = lambda do |test|',
|
325
|
-
' [1, 2]',
|
326
|
-
' end'
|
327
|
-
])
|
328
|
-
expect(cop.offences.size).to eq(1)
|
329
|
-
end
|
330
67
|
end
|
331
68
|
end
|
332
69
|
end
|