rubocop 0.21.0 → 0.22.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/.rubocop.yml +1 -1
- data/{rubocop-todo.yml → .rubocop_todo.yml} +5 -5
- data/CHANGELOG.md +41 -0
- data/README.md +21 -11
- data/config/default.yml +33 -10
- data/config/disabled.yml +0 -4
- data/config/enabled.yml +27 -1
- data/lib/rubocop.rb +17 -1
- data/lib/rubocop/config.rb +32 -27
- data/lib/rubocop/config_loader.rb +21 -8
- data/lib/rubocop/cop/cop.rb +8 -8
- data/lib/rubocop/cop/lint/block_alignment.rb +22 -22
- data/lib/rubocop/cop/lint/condition_position.rb +3 -5
- data/lib/rubocop/cop/lint/debugger.rb +4 -5
- data/lib/rubocop/cop/lint/end_alignment.rb +11 -10
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +10 -11
- data/lib/rubocop/cop/lint/require_parentheses.rb +7 -7
- data/lib/rubocop/cop/lint/rescue_exception.rb +3 -3
- data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +6 -7
- data/lib/rubocop/cop/lint/unreachable_code.rb +2 -3
- data/lib/rubocop/cop/lint/unused_block_argument.rb +2 -15
- data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -14
- data/lib/rubocop/cop/lint/useless_comparison.rb +4 -5
- data/lib/rubocop/cop/lint/void.rb +6 -8
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +4 -2
- data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +1 -3
- data/lib/rubocop/cop/mixin/code_length.rb +5 -5
- data/lib/rubocop/cop/mixin/negative_conditional.rb +6 -6
- data/lib/rubocop/cop/mixin/percent_literal.rb +2 -3
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +5 -5
- data/lib/rubocop/cop/mixin/space_inside.rb +10 -11
- data/lib/rubocop/cop/mixin/unused_argument.rb +35 -0
- data/lib/rubocop/cop/offense.rb +1 -1
- data/lib/rubocop/cop/rails/action_filter.rb +6 -8
- data/lib/rubocop/cop/rails/default_scope.rb +2 -4
- data/lib/rubocop/cop/rails/delegate.rb +2 -0
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +2 -3
- data/lib/rubocop/cop/rails/output.rb +2 -3
- data/lib/rubocop/cop/rails/read_write_attribute.rb +3 -4
- data/lib/rubocop/cop/rails/validation.rb +2 -3
- data/lib/rubocop/cop/style/alias.rb +3 -3
- data/lib/rubocop/cop/style/align_hash.rb +4 -4
- data/lib/rubocop/cop/style/align_parameters.rb +13 -1
- data/lib/rubocop/cop/style/and_or.rb +2 -5
- data/lib/rubocop/cop/style/array_join.rb +3 -4
- data/lib/rubocop/cop/style/ascii_identifiers.rb +2 -3
- data/lib/rubocop/cop/style/block_comments.rb +2 -3
- data/lib/rubocop/cop/style/block_nesting.rb +2 -3
- data/lib/rubocop/cop/style/blocks.rb +9 -9
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +5 -5
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/class_methods.rb +3 -5
- data/lib/rubocop/cop/style/collection_methods.rb +8 -11
- data/lib/rubocop/cop/style/comment_annotation.rb +9 -9
- data/lib/rubocop/cop/style/comment_indentation.rb +66 -0
- data/lib/rubocop/cop/style/constant_name.rb +3 -3
- data/lib/rubocop/cop/style/cyclomatic_complexity.rb +5 -5
- data/lib/rubocop/cop/style/deprecated_hash_methods.rb +6 -6
- data/lib/rubocop/cop/style/each_with_object.rb +38 -0
- data/lib/rubocop/cop/style/empty_lines.rb +8 -8
- data/lib/rubocop/cop/style/empty_lines_around_body.rb +7 -7
- data/lib/rubocop/cop/style/encoding.rb +6 -7
- data/lib/rubocop/cop/style/end_of_line.rb +10 -10
- data/lib/rubocop/cop/style/file_name.rb +11 -7
- data/lib/rubocop/cop/style/guard_clause.rb +32 -22
- data/lib/rubocop/cop/style/indentation_width.rb +25 -26
- data/lib/rubocop/cop/style/lambda.rb +8 -9
- data/lib/rubocop/cop/style/leading_comment_space.rb +4 -5
- data/lib/rubocop/cop/style/line_end_concatenation.rb +29 -6
- data/lib/rubocop/cop/style/line_length.rb +9 -9
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +7 -6
- data/lib/rubocop/cop/style/module_function.rb +3 -4
- data/lib/rubocop/cop/style/multiline_if_then.rb +3 -3
- data/lib/rubocop/cop/style/negated_if.rb +2 -0
- data/lib/rubocop/cop/style/next.rb +80 -0
- data/lib/rubocop/cop/style/nil_comparison.rb +2 -3
- data/lib/rubocop/cop/style/non_nil_check.rb +15 -3
- data/lib/rubocop/cop/style/not.rb +4 -4
- data/lib/rubocop/cop/style/numeric_literals.rb +7 -8
- data/lib/rubocop/cop/style/op_method.rb +3 -5
- data/lib/rubocop/cop/style/parameter_lists.rb +4 -4
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +5 -6
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +5 -5
- data/lib/rubocop/cop/style/predicate_name.rb +2 -4
- data/lib/rubocop/cop/style/redundant_self.rb +11 -12
- data/lib/rubocop/cop/style/self_assignment.rb +8 -13
- data/lib/rubocop/cop/style/semicolon.rb +16 -19
- data/lib/rubocop/cop/style/signal_exception.rb +10 -11
- data/lib/rubocop/cop/style/single_line_block_params.rb +2 -3
- data/lib/rubocop/cop/style/single_line_methods.rb +3 -4
- data/lib/rubocop/cop/style/space_after_colon.rb +9 -9
- data/lib/rubocop/cop/style/space_after_control_keyword.rb +3 -3
- data/lib/rubocop/cop/style/space_after_method_name.rb +3 -3
- data/lib/rubocop/cop/style/space_after_not.rb +3 -4
- data/lib/rubocop/cop/style/space_around_operators.rb +5 -4
- data/lib/rubocop/cop/style/space_before_comment.rb +29 -0
- data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +6 -7
- data/lib/rubocop/cop/style/symbol_array.rb +2 -3
- data/lib/rubocop/cop/style/tab.rb +8 -8
- data/lib/rubocop/cop/style/trailing_blank_lines.rb +10 -10
- data/lib/rubocop/cop/style/trailing_whitespace.rb +6 -7
- data/lib/rubocop/cop/style/trivial_accessors.rb +3 -4
- data/lib/rubocop/cop/style/unless_else.rb +2 -3
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +3 -3
- data/lib/rubocop/cop/style/unneeded_percent_x.rb +26 -0
- data/lib/rubocop/cop/style/when_then.rb +3 -3
- data/lib/rubocop/cop/style/while_until_do.rb +3 -6
- data/lib/rubocop/cop/style/word_array.rb +8 -7
- data/lib/rubocop/cop/team.rb +9 -11
- data/lib/rubocop/cop/util.rb +2 -3
- data/lib/rubocop/cop/variable_force.rb +2 -3
- data/lib/rubocop/cop/variable_force/variable_table.rb +1 -2
- data/lib/rubocop/file_inspector.rb +5 -0
- data/lib/rubocop/formatter/clang_style_formatter.rb +3 -4
- data/lib/rubocop/formatter/disabled_config_formatter.rb +6 -6
- data/lib/rubocop/options.rb +14 -11
- data/lib/rubocop/target_finder.rb +12 -1
- data/lib/rubocop/version.rb +1 -1
- data/relnotes/v0.21.0.md +11 -0
- data/relnotes/v0.22.0.md +77 -0
- data/spec/rubocop/cli_spec.rb +86 -40
- data/spec/rubocop/comment_config_spec.rb +1 -1
- data/spec/rubocop/config_loader_spec.rb +3 -3
- data/spec/rubocop/config_spec.rb +14 -3
- data/spec/rubocop/cop/force_spec.rb +1 -1
- data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +164 -99
- data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +195 -85
- data/spec/rubocop/cop/lint/void_spec.rb +8 -0
- data/spec/rubocop/cop/rails/delegate_spec.rb +17 -0
- data/spec/rubocop/cop/rails/output_spec.rb +3 -3
- data/spec/rubocop/cop/severity_spec.rb +5 -5
- data/spec/rubocop/cop/style/align_parameters_spec.rb +108 -0
- data/spec/rubocop/cop/style/block_nesting_spec.rb +3 -3
- data/spec/rubocop/cop/style/class_and_module_children_spec.rb +12 -10
- data/spec/rubocop/cop/style/comment_indentation_spec.rb +174 -0
- data/spec/rubocop/cop/style/each_with_object_spec.rb +44 -0
- data/spec/rubocop/cop/style/end_of_line_spec.rb +3 -5
- data/spec/rubocop/cop/style/guard_clause_spec.rb +135 -57
- data/spec/rubocop/cop/style/if_unless_modifier_spec.rb +7 -7
- data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +75 -1
- data/spec/rubocop/cop/style/line_length_spec.rb +7 -7
- data/spec/rubocop/cop/style/negated_if_spec.rb +5 -1
- data/spec/rubocop/cop/style/next_spec.rb +210 -0
- data/spec/rubocop/cop/style/non_nil_check_spec.rb +44 -13
- data/spec/rubocop/cop/style/self_assignment_spec.rb +1 -1
- data/spec/rubocop/cop/style/space_around_operators_spec.rb +1 -0
- data/spec/rubocop/cop/style/space_before_comment_spec.rb +34 -0
- data/spec/rubocop/cop/style/unneeded_percent_x_spec.rb +34 -0
- data/spec/rubocop/cop/style/while_until_modifier_spec.rb +2 -2
- data/spec/rubocop/cop/util_spec.rb +2 -2
- data/spec/rubocop/cop/variable_force_spec.rb +1 -1
- data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +4 -4
- data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +2 -2
- data/spec/rubocop/formatter/offense_count_formatter_spec.rb +2 -2
- data/spec/rubocop/options_spec.rb +3 -0
- data/spec/support/shared_context.rb +1 -3
- data/spec/support/statement_modifier_helper.rb +2 -2
- metadata +20 -3
@@ -149,8 +149,8 @@ describe Rubocop::Cop::Style::BlockNesting, :config do
|
|
149
149
|
expect(cop.offenses.map(&:line)).to eq(lines)
|
150
150
|
expect(cop.messages).to eq(
|
151
151
|
['Avoid more than 2 levels of block nesting.'] * lines.length)
|
152
|
-
|
153
|
-
|
154
|
-
|
152
|
+
return unless cop.offenses.size > 0
|
153
|
+
|
154
|
+
expect(cop.config_to_allow_offenses['Max']).to eq(max_to_allow)
|
155
155
|
end
|
156
156
|
end
|
@@ -6,11 +6,7 @@ describe Rubocop::Cop::Style::ClassAndModuleChildren, :config do
|
|
6
6
|
subject(:cop) { described_class.new(config) }
|
7
7
|
|
8
8
|
context 'nested style' do
|
9
|
-
let(:cop_config)
|
10
|
-
{
|
11
|
-
'EnforcedStyle' => 'nested'
|
12
|
-
}
|
13
|
-
end
|
9
|
+
let(:cop_config) { { 'EnforcedStyle' => 'nested' } }
|
14
10
|
|
15
11
|
it 'registers an offense for not nested classes' do
|
16
12
|
inspect_source(cop, ['class FooClass::BarClass', 'end'])
|
@@ -62,11 +58,7 @@ describe Rubocop::Cop::Style::ClassAndModuleChildren, :config do
|
|
62
58
|
end
|
63
59
|
|
64
60
|
context 'compact style' do
|
65
|
-
let(:cop_config)
|
66
|
-
{
|
67
|
-
'EnforcedStyle' => 'compact'
|
68
|
-
}
|
69
|
-
end
|
61
|
+
let(:cop_config) { { 'EnforcedStyle' => 'compact' } }
|
70
62
|
|
71
63
|
it 'registers a offense for classes with nested children' do
|
72
64
|
inspect_source(cop,
|
@@ -125,5 +117,15 @@ describe Rubocop::Cop::Style::ClassAndModuleChildren, :config do
|
|
125
117
|
])
|
126
118
|
expect(cop.offenses).to be_empty
|
127
119
|
end
|
120
|
+
|
121
|
+
it 'accepts class / module with single method' do
|
122
|
+
inspect_source(cop,
|
123
|
+
['class FooClass',
|
124
|
+
' def bar_method',
|
125
|
+
' end',
|
126
|
+
'end'
|
127
|
+
])
|
128
|
+
expect(cop.offenses).to be_empty
|
129
|
+
end
|
128
130
|
end
|
129
131
|
end
|
@@ -0,0 +1,174 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Rubocop::Cop::Style::CommentIndentation do
|
6
|
+
subject(:cop) { described_class.new }
|
7
|
+
|
8
|
+
context 'on outer level' do
|
9
|
+
it 'accepts a correctly indented comment' do
|
10
|
+
inspect_source(cop, '# comment')
|
11
|
+
expect(cop.offenses).to be_empty
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'accepts a comment that follows code' do
|
15
|
+
inspect_source(cop, 'hello # comment')
|
16
|
+
expect(cop.offenses).to be_empty
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'accepts a documentation comment' do
|
20
|
+
inspect_source(cop, ['=begin',
|
21
|
+
'Doc comment',
|
22
|
+
'=end',
|
23
|
+
' hello',
|
24
|
+
' #',
|
25
|
+
'hi'])
|
26
|
+
expect(cop.highlights).to eq(['#'])
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'registers an offense for an incorrectly indented (1) comment' do
|
30
|
+
inspect_source(cop, ' # comment')
|
31
|
+
expect(cop.messages)
|
32
|
+
.to eq(['Incorrect indentation detected (column 1 instead of 0).'])
|
33
|
+
expect(cop.highlights).to eq(['# comment'])
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'registers an offense for an incorrectly indented (2) comment' do
|
37
|
+
inspect_source(cop, ' # comment')
|
38
|
+
expect(cop.messages)
|
39
|
+
.to eq(['Incorrect indentation detected (column 2 instead of 0).'])
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'registers an offense for each incorrectly indented comment' do
|
43
|
+
inspect_source(cop, ['# a',
|
44
|
+
' # b',
|
45
|
+
' # c',
|
46
|
+
'# d',
|
47
|
+
'def test; end'])
|
48
|
+
expect(cop.messages)
|
49
|
+
.to eq(['Incorrect indentation detected (column 0 instead of 2).',
|
50
|
+
'Incorrect indentation detected (column 2 instead of 4).',
|
51
|
+
'Incorrect indentation detected (column 4 instead of 0).'])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'around program structure keywords' do
|
56
|
+
it 'accepts correctly indented comments' do
|
57
|
+
inspect_source(cop, ['#',
|
58
|
+
'def m',
|
59
|
+
' #',
|
60
|
+
' if a',
|
61
|
+
' #',
|
62
|
+
' b',
|
63
|
+
' # this is accepted',
|
64
|
+
' elsif aa',
|
65
|
+
' # this is accepted',
|
66
|
+
' else',
|
67
|
+
' #',
|
68
|
+
' end',
|
69
|
+
' #',
|
70
|
+
' case a',
|
71
|
+
' # this is accepted',
|
72
|
+
' when 0',
|
73
|
+
' #',
|
74
|
+
' b',
|
75
|
+
' end',
|
76
|
+
' # this is accepted',
|
77
|
+
'rescue',
|
78
|
+
'# this is accepted',
|
79
|
+
'ensure',
|
80
|
+
' #',
|
81
|
+
'end',
|
82
|
+
'#'])
|
83
|
+
expect(cop.offenses).to eq([])
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'with a blank line following the comment' do
|
87
|
+
it 'accepts a correctly indented comment' do
|
88
|
+
inspect_source(cop, ['def m',
|
89
|
+
' # comment',
|
90
|
+
'',
|
91
|
+
'end'])
|
92
|
+
expect(cop.offenses).to be_empty
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'near various kinds of brackets' do
|
98
|
+
it 'accepts correctly indented comments' do
|
99
|
+
inspect_source(cop, ['#',
|
100
|
+
'a = {',
|
101
|
+
' #',
|
102
|
+
' x: [',
|
103
|
+
' 1',
|
104
|
+
' #',
|
105
|
+
' ],',
|
106
|
+
' #',
|
107
|
+
' y: func(',
|
108
|
+
' 1',
|
109
|
+
' #',
|
110
|
+
' )',
|
111
|
+
' #',
|
112
|
+
'}',
|
113
|
+
'#'])
|
114
|
+
expect(cop.offenses).to eq([])
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'is unaffected by closing bracket that does not begin a line' do
|
118
|
+
inspect_source(cop, ['#',
|
119
|
+
'result = []'])
|
120
|
+
expect(cop.messages).to eq([])
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'auto-corrects' do
|
125
|
+
new_source = autocorrect_source(cop, [' # comment',
|
126
|
+
'hash1 = { a: 0,',
|
127
|
+
' # comment',
|
128
|
+
' bb: 1,',
|
129
|
+
' ccc: 2 }',
|
130
|
+
' if a',
|
131
|
+
' #',
|
132
|
+
' b',
|
133
|
+
' # this is accepted',
|
134
|
+
' elsif aa',
|
135
|
+
' # so is this',
|
136
|
+
' elsif bb',
|
137
|
+
'#',
|
138
|
+
' else',
|
139
|
+
' #',
|
140
|
+
' end',
|
141
|
+
' case a',
|
142
|
+
' # this is accepted',
|
143
|
+
' when 0',
|
144
|
+
' # so is this',
|
145
|
+
' when 1',
|
146
|
+
' #',
|
147
|
+
' b',
|
148
|
+
' end'])
|
149
|
+
expect(new_source).to eq(['# comment',
|
150
|
+
'hash1 = { a: 0,',
|
151
|
+
' # comment',
|
152
|
+
' bb: 1,',
|
153
|
+
' ccc: 2 }',
|
154
|
+
' if a',
|
155
|
+
' #',
|
156
|
+
' b',
|
157
|
+
' # this is accepted',
|
158
|
+
' elsif aa',
|
159
|
+
' # so is this',
|
160
|
+
' elsif bb',
|
161
|
+
' #',
|
162
|
+
' else',
|
163
|
+
' #',
|
164
|
+
' end',
|
165
|
+
' case a',
|
166
|
+
' # this is accepted',
|
167
|
+
' when 0',
|
168
|
+
' # so is this',
|
169
|
+
' when 1',
|
170
|
+
' #',
|
171
|
+
' b',
|
172
|
+
' end'].join("\n"))
|
173
|
+
end
|
174
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Rubocop::Cop::Style::EachWithObject do
|
6
|
+
subject(:cop) { described_class.new }
|
7
|
+
|
8
|
+
it 'finds inject and reduce with passed in and returned hash' do
|
9
|
+
inspect_source(cop,
|
10
|
+
['[].inject({}) do |a, e|',
|
11
|
+
' a[e] = 1',
|
12
|
+
' a',
|
13
|
+
'end',
|
14
|
+
'',
|
15
|
+
'[].reduce({}) do |a, e|',
|
16
|
+
' a[e] = 1',
|
17
|
+
' a[e] = 1',
|
18
|
+
' a',
|
19
|
+
'end'])
|
20
|
+
expect(cop.offenses.size).to eq(2)
|
21
|
+
expect(cop.offenses.map(&:line).sort).to eq([1, 6])
|
22
|
+
expect(cop.messages)
|
23
|
+
.to eq(['Use `each_with_object` instead of `inject`.',
|
24
|
+
'Use `each_with_object` instead of `reduce`.'])
|
25
|
+
expect(cop.highlights).to eq(%w(inject reduce))
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'ignores inject and reduce with passed in, but not returned hash' do
|
29
|
+
inspect_source(cop,
|
30
|
+
['[].inject({}) do |a, e|',
|
31
|
+
' a + e',
|
32
|
+
'end',
|
33
|
+
'',
|
34
|
+
'[].reduce({}) do |a, e|',
|
35
|
+
' a + e',
|
36
|
+
'end'])
|
37
|
+
expect(cop.offenses).to be_empty
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'ignores inject and reduce passed in symbol' do
|
41
|
+
inspect_source(cop, ['[].inject(:+)', '[].reduce(:+)'])
|
42
|
+
expect(cop.offenses).to be_empty
|
43
|
+
end
|
44
|
+
end
|
@@ -29,11 +29,9 @@ describe Rubocop::Cop::Style::EndOfLine do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
context 'when the default external encoding is US_ASCII' do
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
after(:each) { Encoding.default_external = @orig_encoding }
|
32
|
+
let(:orig_encoding) { Encoding.default_external }
|
33
|
+
before(:each) { Encoding.default_external = Encoding::US_ASCII }
|
34
|
+
after(:each) { Encoding.default_external = orig_encoding }
|
37
35
|
|
38
36
|
it 'does not crash on UTF-8 encoded non-ascii characters' do
|
39
37
|
inspect_source_file(cop,
|
@@ -2,76 +2,154 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe Rubocop::Cop::Style::GuardClause do
|
6
|
-
let(:cop) { described_class.new }
|
5
|
+
describe Rubocop::Cop::Style::GuardClause, :config do
|
6
|
+
let(:cop) { described_class.new(config) }
|
7
|
+
let(:cop_config) { {} }
|
7
8
|
|
8
|
-
it 'reports an offense if method body is if without else' do
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
9
|
+
it 'reports an offense if method body is if / unless without else' do
|
10
|
+
inspect_source(cop,
|
11
|
+
['def func',
|
12
|
+
' if something',
|
13
|
+
' work',
|
14
|
+
' end',
|
15
|
+
'end',
|
16
|
+
'',
|
17
|
+
'def func',
|
18
|
+
' unless something',
|
19
|
+
' work',
|
20
|
+
' end',
|
21
|
+
'end'])
|
22
|
+
expect(cop.offenses.size).to eq(2)
|
23
|
+
expect(cop.offenses.map(&:line).sort).to eq([2, 8])
|
24
|
+
expect(cop.messages)
|
25
|
+
.to eq(['Use a guard clause instead of wrapping ' \
|
26
|
+
'the code inside a conditional expression.'] * 2)
|
27
|
+
expect(cop.highlights).to eq(%w(if unless))
|
17
28
|
end
|
18
29
|
|
19
|
-
it 'reports an offense if method body ends with if without else' do
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
30
|
+
it 'reports an offense if method body ends with if / unless without else' do
|
31
|
+
inspect_source(cop,
|
32
|
+
['def func',
|
33
|
+
' test',
|
34
|
+
' if something',
|
35
|
+
' work',
|
36
|
+
' end',
|
37
|
+
'end',
|
38
|
+
'',
|
39
|
+
'def func',
|
40
|
+
' test',
|
41
|
+
' unless something',
|
42
|
+
' work',
|
43
|
+
' end',
|
44
|
+
'end'])
|
45
|
+
expect(cop.offenses.size).to eq(2)
|
46
|
+
expect(cop.offenses.map(&:line).sort).to eq([3, 10])
|
47
|
+
expect(cop.messages)
|
48
|
+
.to eq(['Use a guard clause instead of wrapping ' \
|
49
|
+
'the code inside a conditional expression.'] * 2)
|
50
|
+
expect(cop.highlights).to eq(%w(if unless))
|
29
51
|
end
|
30
52
|
|
31
|
-
it 'accepts a method which body is if with else' do
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
53
|
+
it 'accepts a method which body is if / unless with else' do
|
54
|
+
inspect_source(cop,
|
55
|
+
['def func',
|
56
|
+
' if something',
|
57
|
+
' work',
|
58
|
+
' else',
|
59
|
+
' test',
|
60
|
+
' end',
|
61
|
+
'end',
|
62
|
+
'',
|
63
|
+
'def func',
|
64
|
+
' unless something',
|
65
|
+
' work',
|
66
|
+
' else',
|
67
|
+
' test',
|
68
|
+
' end',
|
69
|
+
'end'])
|
41
70
|
expect(cop.offenses).to be_empty
|
42
71
|
end
|
43
72
|
|
44
|
-
it 'accepts a method which body does not end with if' do
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
73
|
+
it 'accepts a method which body does not end with if / unless' do
|
74
|
+
inspect_source(cop,
|
75
|
+
['def func',
|
76
|
+
' if something',
|
77
|
+
' work',
|
78
|
+
' end',
|
79
|
+
' test',
|
80
|
+
'end',
|
81
|
+
'',
|
82
|
+
'def func',
|
83
|
+
' unless something',
|
84
|
+
' work',
|
85
|
+
' end',
|
86
|
+
' test',
|
87
|
+
'end'])
|
53
88
|
expect(cop.offenses).to be_empty
|
54
89
|
end
|
55
90
|
|
56
|
-
it 'accepts a method
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
91
|
+
it 'accepts a method whose body is a modifier if / unless' do
|
92
|
+
inspect_source(cop,
|
93
|
+
['def func',
|
94
|
+
' work if something',
|
95
|
+
'end',
|
96
|
+
'',
|
97
|
+
'def func',
|
98
|
+
' work if something',
|
99
|
+
'end'])
|
65
100
|
expect(cop.offenses).to be_empty
|
66
101
|
end
|
67
102
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
103
|
+
context 'MinBodyLength: 1' do
|
104
|
+
let(:cop_config) do
|
105
|
+
{ 'EnforcedStyle' => 'MinBodyLength: 1' }
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'reports an offense for if whose body has 1 line' do
|
109
|
+
inspect_source(cop,
|
110
|
+
['def func',
|
111
|
+
' if something',
|
112
|
+
' work',
|
113
|
+
' end',
|
114
|
+
'end',
|
115
|
+
'',
|
116
|
+
'def func',
|
117
|
+
' unless something',
|
118
|
+
' work',
|
119
|
+
' end',
|
120
|
+
'end'])
|
121
|
+
expect(cop.offenses.size).to eq(2)
|
122
|
+
expect(cop.offenses.map(&:line).sort).to eq([2, 8])
|
123
|
+
expect(cop.messages)
|
124
|
+
.to eq(['Use a guard clause instead of wrapping ' \
|
125
|
+
'the code inside a conditional expression.'] * 2)
|
126
|
+
expect(cop.highlights).to eq(%w(if unless))
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context 'MinBodyLength: 4' do
|
131
|
+
let(:cop_config) do
|
132
|
+
{ 'MinBodyLength' => 4 }
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'accepts a method whose body has 3 line' do
|
136
|
+
inspect_source(cop,
|
137
|
+
['def func',
|
138
|
+
' if something',
|
139
|
+
' work',
|
140
|
+
' work',
|
141
|
+
' work',
|
142
|
+
' end',
|
143
|
+
'end',
|
144
|
+
'',
|
145
|
+
'def func',
|
146
|
+
' unless something',
|
147
|
+
' work',
|
148
|
+
' work',
|
149
|
+
' work',
|
150
|
+
' end',
|
151
|
+
'end'])
|
152
|
+
expect(cop.offenses).to be_empty
|
153
|
+
end
|
76
154
|
end
|
77
155
|
end
|