rubocop 0.24.1 → 0.25.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_todo.yml +12 -8
- data/.travis.yml +1 -1
- data/CHANGELOG.md +42 -0
- data/Gemfile +2 -0
- data/README.md +27 -6
- data/bin/rubocop +10 -15
- data/config/default.yml +72 -25
- data/config/enabled.yml +57 -25
- data/lib/rubocop.rb +15 -8
- data/lib/rubocop/config_loader.rb +11 -7
- data/lib/rubocop/cop/cop.rb +16 -16
- data/lib/rubocop/cop/ignored_node.rb +11 -4
- data/lib/rubocop/cop/lint/block_alignment.rb +29 -4
- data/lib/rubocop/cop/lint/debugger.rb +8 -1
- data/lib/rubocop/cop/lint/def_end_alignment.rb +5 -1
- data/lib/rubocop/cop/lint/end_alignment.rb +5 -5
- data/lib/rubocop/cop/lint/end_in_method.rb +2 -2
- data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -2
- data/lib/rubocop/cop/{style → metrics}/block_nesting.rb +1 -1
- data/lib/rubocop/cop/{style → metrics}/class_length.rb +4 -4
- data/lib/rubocop/cop/{style → metrics}/cyclomatic_complexity.rb +5 -16
- data/lib/rubocop/cop/{style → metrics}/line_length.rb +1 -1
- data/lib/rubocop/cop/{style → metrics}/method_length.rb +8 -4
- data/lib/rubocop/cop/{style → metrics}/parameter_lists.rb +1 -1
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +61 -0
- data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +10 -2
- data/lib/rubocop/cop/mixin/code_length.rb +2 -3
- data/lib/rubocop/cop/mixin/configurable_naming.rb +6 -20
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/if_node.rb +1 -1
- data/lib/rubocop/cop/mixin/method_complexity.rb +32 -0
- data/lib/rubocop/cop/mixin/negative_conditional.rb +1 -1
- data/lib/rubocop/cop/mixin/{check_methods.rb → on_method.rb} +3 -3
- data/lib/rubocop/cop/mixin/on_normal_if_unless.rb +24 -0
- data/lib/rubocop/cop/mixin/percent_literal.rb +2 -1
- data/lib/rubocop/cop/mixin/space_inside.rb +33 -4
- data/lib/rubocop/cop/mixin/statement_modifier.rb +14 -14
- data/lib/rubocop/cop/mixin/string_help.rb +4 -0
- data/lib/rubocop/cop/mixin/surrounding_space.rb +1 -0
- data/lib/rubocop/cop/rails/delegate.rb +2 -2
- data/lib/rubocop/cop/rails/output.rb +4 -2
- data/lib/rubocop/cop/style/accessor_method_name.rb +2 -2
- data/lib/rubocop/cop/style/align_hash.rb +9 -1
- data/lib/rubocop/cop/style/and_or.rb +37 -3
- data/lib/rubocop/cop/style/bare_percent_literals.rb +46 -0
- data/lib/rubocop/cop/style/block_end_newline.rb +56 -0
- data/lib/rubocop/cop/style/character_literal.rb +1 -1
- data/lib/rubocop/cop/style/def_with_parentheses.rb +2 -2
- data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +31 -3
- data/lib/rubocop/cop/style/empty_lines_around_body.rb +6 -2
- data/lib/rubocop/cop/style/end_of_line.rb +3 -14
- data/lib/rubocop/cop/style/if_unless_modifier.rb +3 -6
- data/lib/rubocop/cop/style/if_with_semicolon.rb +6 -7
- data/lib/rubocop/cop/style/indentation_width.rb +3 -8
- data/lib/rubocop/cop/style/method_def_parentheses.rb +2 -2
- data/lib/rubocop/cop/style/method_name.rb +4 -18
- data/lib/rubocop/cop/style/multiline_block_layout.rb +73 -0
- data/lib/rubocop/cop/style/multiline_if_then.rb +4 -4
- data/lib/rubocop/cop/style/negated_if.rb +1 -1
- data/lib/rubocop/cop/style/negated_while.rb +2 -2
- data/lib/rubocop/cop/style/next.rb +12 -0
- data/lib/rubocop/cop/style/one_line_conditional.rb +6 -6
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +22 -8
- data/lib/rubocop/cop/style/percent_q_literals.rb +53 -0
- data/lib/rubocop/cop/style/predicate_name.rb +2 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +2 -2
- data/lib/rubocop/cop/style/redundant_return.rb +2 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +7 -3
- data/lib/rubocop/cop/style/single_line_methods.rb +2 -2
- data/lib/rubocop/cop/style/space_after_method_name.rb +2 -2
- data/lib/rubocop/cop/style/space_inside_block_braces.rb +14 -10
- data/lib/rubocop/cop/style/space_inside_brackets.rb +5 -1
- data/lib/rubocop/cop/style/string_literals.rb +5 -8
- data/lib/rubocop/cop/style/trailing_comma.rb +15 -3
- data/lib/rubocop/cop/style/trivial_accessors.rb +2 -2
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +2 -4
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +13 -0
- data/lib/rubocop/cop/style/variable_interpolation.rb +14 -2
- data/lib/rubocop/cop/style/variable_name.rb +17 -17
- data/lib/rubocop/cop/style/while_until_modifier.rb +2 -5
- data/lib/rubocop/cop/util.rb +5 -0
- data/lib/rubocop/cop/variable_force.rb +7 -6
- data/lib/rubocop/formatter/base_formatter.rb +2 -2
- data/lib/rubocop/processed_source.rb +5 -22
- data/lib/rubocop/version.rb +1 -1
- data/relnotes/v0.25.0.md +91 -0
- data/rubocop.gemspec +2 -3
- data/spec/project_spec.rb +1 -1
- data/spec/rubocop/cli_spec.rb +70 -44
- data/spec/rubocop/comment_config_spec.rb +6 -6
- data/spec/rubocop/config_loader_spec.rb +19 -13
- data/spec/rubocop/config_spec.rb +3 -3
- data/spec/rubocop/cop/commissioner_spec.rb +1 -1
- data/spec/rubocop/cop/cop_spec.rb +3 -3
- data/spec/rubocop/cop/lint/block_alignment_spec.rb +113 -6
- data/spec/rubocop/cop/lint/debugger_spec.rb +10 -6
- data/spec/rubocop/cop/lint/useless_assignment_spec.rb +12 -0
- data/spec/rubocop/cop/{style → metrics}/block_nesting_spec.rb +1 -1
- data/spec/rubocop/cop/{style → metrics}/class_length_spec.rb +1 -1
- data/spec/rubocop/cop/{style → metrics}/cyclomatic_complexity_spec.rb +1 -1
- data/spec/rubocop/cop/{style → metrics}/if_unless_modifier_spec.rb +2 -2
- data/spec/rubocop/cop/{style → metrics}/line_length_spec.rb +7 -7
- data/spec/rubocop/cop/{style → metrics}/method_length_spec.rb +1 -1
- data/spec/rubocop/cop/{style → metrics}/parameter_lists_spec.rb +1 -1
- data/spec/rubocop/cop/metrics/perceived_complexity_spec.rb +222 -0
- data/spec/rubocop/cop/{style → metrics}/while_until_modifier_spec.rb +2 -2
- data/spec/rubocop/cop/rails/output_spec.rb +8 -2
- data/spec/rubocop/cop/style/align_hash_spec.rb +7 -0
- data/spec/rubocop/cop/style/and_or_spec.rb +245 -43
- data/spec/rubocop/cop/style/bare_percent_literals_spec.rb +132 -0
- data/spec/rubocop/cop/style/block_end_newline_spec.rb +61 -0
- data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +34 -0
- data/spec/rubocop/cop/style/end_of_line_spec.rb +8 -0
- data/spec/rubocop/cop/style/guard_clause_spec.rb +1 -1
- data/spec/rubocop/cop/style/multiline_block_layout_spec.rb +138 -0
- data/spec/rubocop/cop/style/next_spec.rb +32 -3
- data/spec/rubocop/cop/style/percent_literal_delimiters_spec.rb +34 -0
- data/spec/rubocop/cop/style/percent_q_literals_spec.rb +122 -0
- data/spec/rubocop/cop/style/space_inside_block_braces_spec.rb +20 -0
- data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +26 -2
- data/spec/rubocop/cop/style/trailing_comma_spec.rb +112 -0
- data/spec/rubocop/cop/style/unneeded_percent_q_spec.rb +86 -31
- data/spec/rubocop/cop/style/variable_interpolation_spec.rb +21 -1
- data/spec/rubocop/cop/team_spec.rb +14 -9
- data/spec/spec_helper.rb +1 -0
- metadata +47 -50
- data/lib/rubocop/cop/mixin/if_then_else.rb +0 -23
@@ -7,7 +7,7 @@ describe RuboCop::Cop::Style::WhileUntilModifier do
|
|
7
7
|
|
8
8
|
subject(:cop) { described_class.new(config) }
|
9
9
|
let(:config) do
|
10
|
-
hash = { '
|
10
|
+
hash = { 'Metrics/LineLength' => { 'Max' => 80 } }
|
11
11
|
RuboCop::Config.new(hash)
|
12
12
|
end
|
13
13
|
|
@@ -76,7 +76,7 @@ describe RuboCop::Cop::Style::WhileUntilModifier do
|
|
76
76
|
context 'when the maximum line length is specified by the cop itself' do
|
77
77
|
let(:config) do
|
78
78
|
hash = {
|
79
|
-
'
|
79
|
+
'Metrics/LineLength' => { 'Max' => 100 },
|
80
80
|
'Style/WhileUntilModifier' => { 'MaxLineLength' => 80 }
|
81
81
|
}
|
82
82
|
RuboCop::Config.new(hash)
|
@@ -5,7 +5,7 @@ require 'spec_helper'
|
|
5
5
|
describe RuboCop::Cop::Rails::Output do
|
6
6
|
subject(:cop) { described_class.new }
|
7
7
|
|
8
|
-
it 'records an offense for
|
8
|
+
it 'records an offense for methods without a receiver' do
|
9
9
|
source = ['p "edmond dantes"',
|
10
10
|
'puts "sinbad"',
|
11
11
|
'print "abbe busoni"',
|
@@ -14,7 +14,7 @@ describe RuboCop::Cop::Rails::Output do
|
|
14
14
|
expect(cop.offenses.size).to eq(4)
|
15
15
|
end
|
16
16
|
|
17
|
-
it 'does not record an offense for methods' do
|
17
|
+
it 'does not record an offense for methods with a receiver' do
|
18
18
|
source = ['obj.print',
|
19
19
|
'something.p',
|
20
20
|
'nothing.pp']
|
@@ -22,6 +22,12 @@ describe RuboCop::Cop::Rails::Output do
|
|
22
22
|
expect(cop.offenses).to be_empty
|
23
23
|
end
|
24
24
|
|
25
|
+
it 'does not record an offense for methods without arguments' do
|
26
|
+
source = %w(print pp puts)
|
27
|
+
inspect_source(cop, source)
|
28
|
+
expect(cop.offenses).to be_empty
|
29
|
+
end
|
30
|
+
|
25
31
|
it 'does not record an offense for comments' do
|
26
32
|
source = ['# print "test"',
|
27
33
|
'# p']
|
@@ -16,6 +16,13 @@ describe RuboCop::Cop::Style::AlignHash, :config do
|
|
16
16
|
' ccc: 3, dddd: 4)'])
|
17
17
|
expect(cop.offenses).to be_empty
|
18
18
|
end
|
19
|
+
|
20
|
+
it "does not auto-correct pairs that don't start a line" do
|
21
|
+
source = ['render :json => {:a => messages,',
|
22
|
+
' :b => :json}, :status => 404']
|
23
|
+
new_source = autocorrect_source(cop, source)
|
24
|
+
expect(new_source).to eq(source.join("\n"))
|
25
|
+
end
|
19
26
|
end
|
20
27
|
|
21
28
|
context 'always inspect last argument hash' do
|
@@ -2,56 +2,258 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe RuboCop::Cop::Style::AndOr do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
['test if a or b'])
|
11
|
-
expect(cop.offenses.size).to eq(1)
|
12
|
-
expect(cop.messages).to eq(['Use `||` instead of `or`.'])
|
13
|
-
end
|
5
|
+
describe RuboCop::Cop::Style::AndOr, :config do
|
6
|
+
context 'when style is conditionals' do
|
7
|
+
cop_config = {
|
8
|
+
'EnforcedStyle' => 'conditionals'
|
9
|
+
}
|
14
10
|
|
15
|
-
|
16
|
-
|
17
|
-
['test if a and b'])
|
18
|
-
expect(cop.offenses.size).to eq(1)
|
19
|
-
expect(cop.messages).to eq(['Use `&&` instead of `and`.'])
|
20
|
-
end
|
11
|
+
subject(:cop) { described_class.new(config) }
|
12
|
+
let(:cop_config) { cop_config }
|
21
13
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
14
|
+
it 'does not warn on short-circuit (and)' do
|
15
|
+
inspect_source(cop,
|
16
|
+
['x = a + b and return x'])
|
17
|
+
expect(cop.offenses.size).to eq(0)
|
18
|
+
end
|
27
19
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
20
|
+
it 'does not warn on short-circuit (or)' do
|
21
|
+
inspect_source(cop,
|
22
|
+
['x = a + b or return x'])
|
23
|
+
expect(cop.offenses.size).to eq(0)
|
24
|
+
end
|
33
25
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
26
|
+
it 'does warn on non short-circuit (and)' do
|
27
|
+
inspect_source(cop,
|
28
|
+
['x = a + b if a and b'])
|
29
|
+
expect(cop.offenses.size).to eq(1)
|
30
|
+
expect(cop.messages).to eq(['Use `&&` instead of `and`.'])
|
31
|
+
end
|
38
32
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
33
|
+
it 'does warn on non short-circuit (or)' do
|
34
|
+
inspect_source(cop,
|
35
|
+
['x = a + b if a or b'])
|
36
|
+
expect(cop.offenses.size).to eq(1)
|
37
|
+
expect(cop.messages).to eq(['Use `||` instead of `or`.'])
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'does warn on non short-circuit (and) (unless)' do
|
41
|
+
inspect_source(cop,
|
42
|
+
['x = a + b unless a and b'])
|
43
|
+
expect(cop.offenses.size).to eq(1)
|
44
|
+
expect(cop.messages).to eq(['Use `&&` instead of `and`.'])
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'does warn on non short-circuit (or) (unless)' do
|
48
|
+
inspect_source(cop,
|
49
|
+
['x = a + b unless a or b'])
|
50
|
+
expect(cop.offenses.size).to eq(1)
|
51
|
+
expect(cop.messages).to eq(['Use `||` instead of `or`.'])
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should handle boolean returning methods correctly' do
|
55
|
+
inspect_source(cop,
|
56
|
+
['1 if (not true) or false'])
|
57
|
+
expect(cop.offenses.size).to eq(1)
|
58
|
+
expect(cop.messages).to eq(['Use `||` instead of `or`.'])
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should handle recursion' do
|
62
|
+
inspect_source(cop,
|
63
|
+
['1 if (true and false) || (false or true)'])
|
64
|
+
expect(cop.offenses.size).to eq(2)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should handle recursion' do
|
68
|
+
inspect_source(cop,
|
69
|
+
['1 if (true or false) && (false and true)'])
|
70
|
+
expect(cop.offenses.size).to eq(2)
|
71
|
+
end
|
45
72
|
|
46
|
-
it 'leaves *or* as is if auto-correction changes the meaning' do
|
47
|
-
src = "teststring.include? 'a' or teststring.include? 'b'"
|
48
|
-
new_source = autocorrect_source(cop, src)
|
49
|
-
expect(new_source).to eq(src)
|
50
73
|
end
|
51
74
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
75
|
+
context 'when style is always' do
|
76
|
+
cop_config = {
|
77
|
+
'EnforcedStyle' => 'always'
|
78
|
+
}
|
79
|
+
|
80
|
+
subject(:cop) { described_class.new(config) }
|
81
|
+
let(:cop_config) { cop_config }
|
82
|
+
|
83
|
+
it 'registers an offense for OR' do
|
84
|
+
inspect_source(cop,
|
85
|
+
['test if a or b'])
|
86
|
+
expect(cop.offenses.size).to eq(1)
|
87
|
+
expect(cop.messages).to eq(['Use `||` instead of `or`.'])
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'registers an offense for AND' do
|
91
|
+
inspect_source(cop,
|
92
|
+
['test if a and b'])
|
93
|
+
expect(cop.offenses.size).to eq(1)
|
94
|
+
expect(cop.messages).to eq(['Use `&&` instead of `and`.'])
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'accepts ||' do
|
98
|
+
inspect_source(cop,
|
99
|
+
['test if a || b'])
|
100
|
+
expect(cop.offenses).to be_empty
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'accepts &&' do
|
104
|
+
inspect_source(cop,
|
105
|
+
['test if a && b'])
|
106
|
+
expect(cop.offenses).to be_empty
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'auto-corrects "and" with &&' do
|
110
|
+
new_source = autocorrect_source(cop, 'true and false')
|
111
|
+
expect(new_source).to eq('true && false')
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'auto-corrects "or" with ||' do
|
115
|
+
new_source = autocorrect_source(cop, ['x = 12345',
|
116
|
+
'true or false'])
|
117
|
+
expect(new_source).to eq(['x = 12345',
|
118
|
+
'true || false'].join("\n"))
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'auto-corrects "or" with || inside def' do
|
122
|
+
new_source = autocorrect_source(cop, ['def z(a, b)',
|
123
|
+
' return true if a or b',
|
124
|
+
'end'])
|
125
|
+
expect(new_source).to eq(['def z(a, b)',
|
126
|
+
' return true if a || b',
|
127
|
+
'end'].join("\n"))
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'leaves *or* as is if auto-correction changes the meaning' do
|
131
|
+
src = "x = y or teststring.include? 'b'"
|
132
|
+
new_source = autocorrect_source(cop, src)
|
133
|
+
expect(new_source).to eq(src)
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'leaves *and* as is if auto-correction changes the meaning' do
|
137
|
+
src = 'x = a + b and return x'
|
138
|
+
new_source = autocorrect_source(cop, src)
|
139
|
+
expect(new_source).to eq(src)
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'warns on short-circuit (and)' do
|
143
|
+
inspect_source(cop,
|
144
|
+
['x = a + b and return x'])
|
145
|
+
expect(cop.offenses.size).to eq(1)
|
146
|
+
expect(cop.messages).to eq(['Use `&&` instead of `and`.'])
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'also warns on non short-circuit (and)' do
|
150
|
+
inspect_source(cop,
|
151
|
+
['x = a + b if a and b'])
|
152
|
+
expect(cop.offenses.size).to eq(1)
|
153
|
+
expect(cop.messages).to eq(['Use `&&` instead of `and`.'])
|
154
|
+
end
|
155
|
+
|
156
|
+
it 'also warns on non short-circuit (and) (unless)' do
|
157
|
+
inspect_source(cop,
|
158
|
+
['x = a + b unless a and b'])
|
159
|
+
expect(cop.offenses.size).to eq(1)
|
160
|
+
expect(cop.messages).to eq(['Use `&&` instead of `and`.'])
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'warns on short-circuit (or)' do
|
164
|
+
inspect_source(cop,
|
165
|
+
['x = a + b or return x'])
|
166
|
+
expect(cop.offenses.size).to eq(1)
|
167
|
+
expect(cop.messages).to eq(['Use `||` instead of `or`.'])
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'also warns on non short-circuit (or)' do
|
171
|
+
inspect_source(cop,
|
172
|
+
['x = a + b if a or b'])
|
173
|
+
expect(cop.offenses.size).to eq(1)
|
174
|
+
expect(cop.messages).to eq(['Use `||` instead of `or`.'])
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'also warns on non short-circuit (or) (unless)' do
|
178
|
+
inspect_source(cop,
|
179
|
+
['x = a + b unless a or b'])
|
180
|
+
expect(cop.offenses.size).to eq(1)
|
181
|
+
expect(cop.messages).to eq(['Use `||` instead of `or`.'])
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'also warns on while (or)' do
|
185
|
+
inspect_source(cop,
|
186
|
+
['x = a + b while a or b'])
|
187
|
+
expect(cop.offenses.size).to eq(1)
|
188
|
+
expect(cop.messages).to eq(['Use `||` instead of `or`.'])
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'also warns on until (or)' do
|
192
|
+
inspect_source(cop,
|
193
|
+
['x = a + b until a or b'])
|
194
|
+
expect(cop.offenses.size).to eq(1)
|
195
|
+
expect(cop.messages).to eq(['Use `||` instead of `or`.'])
|
196
|
+
end
|
197
|
+
|
198
|
+
it 'auto-corrects "or" with || in method calls' do
|
199
|
+
new_source = autocorrect_source(cop, 'method a or b')
|
200
|
+
expect(new_source).to eq('method(a) || b')
|
201
|
+
end
|
202
|
+
|
203
|
+
it 'auto-corrects "or" with || in method calls (2)' do
|
204
|
+
new_source = autocorrect_source(cop, 'method a,b or b')
|
205
|
+
expect(new_source).to eq('method(a,b) || b')
|
206
|
+
end
|
207
|
+
|
208
|
+
it 'auto-corrects "or" with || in method calls (3)' do
|
209
|
+
new_source = autocorrect_source(cop, 'obj.method a or b')
|
210
|
+
expect(new_source).to eq('obj.method(a) || b')
|
211
|
+
end
|
212
|
+
|
213
|
+
it 'auto-corrects "or" with || in method calls (4)' do
|
214
|
+
new_source = autocorrect_source(cop, 'obj.method a,b or b')
|
215
|
+
expect(new_source).to eq('obj.method(a,b) || b')
|
216
|
+
end
|
217
|
+
|
218
|
+
it 'auto-corrects "or" with || and doesn\'t add extra parenthesis' do
|
219
|
+
new_source = autocorrect_source(cop, 'method(a, b) or b')
|
220
|
+
expect(new_source).to eq('method(a, b) || b')
|
221
|
+
end
|
222
|
+
|
223
|
+
it 'auto-corrects "or" with || and add parenthesis on left expr' do
|
224
|
+
new_source = autocorrect_source(cop, 'b or method a,b')
|
225
|
+
expect(new_source).to eq('b || method(a,b)')
|
226
|
+
end
|
227
|
+
|
228
|
+
it 'auto-corrects "and" with && in method calls' do
|
229
|
+
new_source = autocorrect_source(cop, 'method a and b')
|
230
|
+
expect(new_source).to eq('method(a) && b')
|
231
|
+
end
|
232
|
+
|
233
|
+
it 'auto-corrects "and" with && in method calls (2)' do
|
234
|
+
new_source = autocorrect_source(cop, 'method a,b and b')
|
235
|
+
expect(new_source).to eq('method(a,b) && b')
|
236
|
+
end
|
237
|
+
|
238
|
+
it 'auto-corrects "and" with && in method calls (3)' do
|
239
|
+
new_source = autocorrect_source(cop, 'obj.method a and b')
|
240
|
+
expect(new_source).to eq('obj.method(a) && b')
|
241
|
+
end
|
242
|
+
|
243
|
+
it 'auto-corrects "and" with && in method calls (4)' do
|
244
|
+
new_source = autocorrect_source(cop, 'obj.method a,b and b')
|
245
|
+
expect(new_source).to eq('obj.method(a,b) && b')
|
246
|
+
end
|
247
|
+
|
248
|
+
it 'auto-corrects "and" with && and doesn\'t add extra parenthesis' do
|
249
|
+
new_source = autocorrect_source(cop, 'method(a, b) and b')
|
250
|
+
expect(new_source).to eq('method(a, b) && b')
|
251
|
+
end
|
252
|
+
|
253
|
+
it 'auto-corrects "and" with && and add parenthesis on left expr' do
|
254
|
+
new_source = autocorrect_source(cop, 'b and method a,b')
|
255
|
+
expect(new_source).to eq('b && method(a,b)')
|
256
|
+
end
|
257
|
+
|
56
258
|
end
|
57
259
|
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe RuboCop::Cop::Style::BarePercentLiterals, :config do
|
6
|
+
subject(:cop) { described_class.new(config) }
|
7
|
+
|
8
|
+
shared_examples 'accepts other delimiters' do
|
9
|
+
it 'accepts __FILE__' do
|
10
|
+
inspect_source(cop, '__FILE__')
|
11
|
+
expect(cop.offenses).to be_empty
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'accepts regular expressions' do
|
15
|
+
inspect_source(cop, '/%Q?/')
|
16
|
+
expect(cop.offenses).to be_empty
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'accepts ""' do
|
20
|
+
inspect_source(cop, '""')
|
21
|
+
expect(cop.offenses).to be_empty
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'accepts "" string with interpolation' do
|
25
|
+
inspect_source(cop, '"#{file}hi"')
|
26
|
+
expect(cop.offenses).to be_empty
|
27
|
+
end
|
28
|
+
|
29
|
+
it "accepts ''" do
|
30
|
+
inspect_source(cop, "'hi'")
|
31
|
+
expect(cop.offenses).to be_empty
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'accepts %q' do
|
35
|
+
inspect_source(cop, '%q(hi)')
|
36
|
+
expect(cop.offenses).to be_empty
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'accepts heredoc' do
|
40
|
+
inspect_source(cop, ['func <<END',
|
41
|
+
'hi',
|
42
|
+
'END'])
|
43
|
+
expect(cop.offenses).to be_empty
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when EnforcedStyle is percent_q' do
|
48
|
+
let(:cop_config) { { 'EnforcedStyle' => 'percent_q' } }
|
49
|
+
|
50
|
+
context 'and strings are static' do
|
51
|
+
it 'registers an offense for %()' do
|
52
|
+
inspect_source(cop, '%(hi)')
|
53
|
+
expect(cop.messages).to eq(['Use `%Q` instead of `%`.'])
|
54
|
+
expect(cop.highlights).to eq(['%('])
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'auto-corrects' do
|
58
|
+
new_source = autocorrect_source(cop, '%(hi)')
|
59
|
+
expect(new_source).to eq('%Q(hi)')
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'accepts %Q()' do
|
63
|
+
inspect_source(cop, '%Q(hi)')
|
64
|
+
expect(cop.offenses).to be_empty
|
65
|
+
end
|
66
|
+
|
67
|
+
include_examples 'accepts other delimiters'
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'and strings are dynamic' do
|
71
|
+
it 'registers an offense for %()' do
|
72
|
+
inspect_source(cop, '%(#{x})')
|
73
|
+
expect(cop.messages).to eq(['Use `%Q` instead of `%`.'])
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'auto-corrects' do
|
77
|
+
new_source = autocorrect_source(cop, '%(#{x})')
|
78
|
+
expect(new_source).to eq('%Q(#{x})')
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'accepts %Q()' do
|
82
|
+
inspect_source(cop, '%Q(#{x})')
|
83
|
+
expect(cop.offenses).to be_empty
|
84
|
+
end
|
85
|
+
|
86
|
+
include_examples 'accepts other delimiters'
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context 'when EnforcedStyle is bare_percent' do
|
91
|
+
let(:cop_config) { { 'EnforcedStyle' => 'bare_percent' } }
|
92
|
+
|
93
|
+
context 'and strings are static' do
|
94
|
+
it 'registers an offense for %Q()' do
|
95
|
+
inspect_source(cop, '%Q(hi)')
|
96
|
+
expect(cop.messages).to eq(['Use `%` instead of `%Q`.'])
|
97
|
+
expect(cop.highlights).to eq(['%Q('])
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'auto-corrects' do
|
101
|
+
new_source = autocorrect_source(cop, '%Q(hi)')
|
102
|
+
expect(new_source).to eq('%(hi)')
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'accepts %()' do
|
106
|
+
inspect_source(cop, '%(hi)')
|
107
|
+
expect(cop.offenses).to be_empty
|
108
|
+
end
|
109
|
+
|
110
|
+
include_examples 'accepts other delimiters'
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'and strings are dynamic' do
|
114
|
+
it 'registers an offense for %Q()' do
|
115
|
+
inspect_source(cop, '%Q(#{x})')
|
116
|
+
expect(cop.messages).to eq(['Use `%` instead of `%Q`.'])
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'auto-corrects' do
|
120
|
+
new_source = autocorrect_source(cop, '%Q(#{x})')
|
121
|
+
expect(new_source).to eq('%(#{x})')
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'accepts %()' do
|
125
|
+
inspect_source(cop, '%(#{x})')
|
126
|
+
expect(cop.offenses).to be_empty
|
127
|
+
end
|
128
|
+
|
129
|
+
include_examples 'accepts other delimiters'
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|