rubocop 0.27.1 → 0.28.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/CHANGELOG.md +44 -4
- data/Gemfile +1 -1
- data/README.md +16 -1
- data/config/default.yml +15 -1
- data/config/disabled.yml +5 -0
- data/config/enabled.yml +10 -2
- data/lib/rubocop.rb +3 -0
- data/lib/rubocop/config.rb +1 -1
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +2 -2
- data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +10 -26
- data/lib/rubocop/cop/mixin/on_method_def.rb +1 -1
- data/lib/rubocop/cop/mixin/string_help.rb +10 -1
- data/lib/rubocop/cop/style/align_hash.rb +2 -2
- data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -1
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +21 -19
- data/lib/rubocop/cop/style/else_alignment.rb +29 -16
- data/lib/rubocop/cop/style/empty_else.rb +47 -0
- data/lib/rubocop/cop/style/empty_lines_around_block_body.rb +38 -0
- data/lib/rubocop/cop/style/extra_spacing.rb +35 -0
- data/lib/rubocop/cop/style/indentation_width.rb +1 -1
- data/lib/rubocop/cop/style/leading_comment_space.rb +1 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +5 -1
- data/lib/rubocop/cop/style/multiline_operation_indentation.rb +20 -4
- data/lib/rubocop/cop/style/negated_while.rb +3 -1
- data/lib/rubocop/cop/style/perl_backrefs.rb +8 -3
- data/lib/rubocop/cop/style/single_line_block_params.rb +7 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +8 -3
- data/lib/rubocop/cop/style/string_literals.rb +4 -11
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +2 -7
- data/lib/rubocop/cop/style/symbol_proc.rb +14 -6
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +2 -1
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/cop/variable_force.rb +3 -3
- data/lib/rubocop/cop/variable_force/scope.rb +1 -1
- data/lib/rubocop/cop/variable_force/variable.rb +1 -1
- data/lib/rubocop/formatter/html_formatter.rb +1 -1
- data/lib/rubocop/options.rb +13 -6
- data/lib/rubocop/rake_task.rb +0 -1
- data/lib/rubocop/runner.rb +19 -6
- data/lib/rubocop/target_finder.rb +32 -1
- data/lib/rubocop/version.rb +1 -1
- data/relnotes/v0.28.0.md +90 -0
- data/spec/project_spec.rb +1 -0
- data/spec/rubocop/cli_spec.rb +115 -24
- data/spec/rubocop/comment_config_spec.rb +1 -1
- data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +0 -1
- data/spec/rubocop/cop/lint/block_alignment_spec.rb +4 -4
- data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +1 -1
- data/spec/rubocop/cop/lint/useless_assignment_spec.rb +2 -3
- data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +1 -1
- data/spec/rubocop/cop/lint/void_spec.rb +0 -1
- data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +2 -2
- data/spec/rubocop/cop/style/blocks_spec.rb +8 -0
- data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +196 -215
- data/spec/rubocop/cop/style/case_indentation_spec.rb +4 -4
- data/spec/rubocop/cop/style/documentation_spec.rb +0 -1
- data/spec/rubocop/cop/style/else_alignment_spec.rb +63 -4
- data/spec/rubocop/cop/style/empty_else_spec.rb +100 -0
- data/spec/rubocop/cop/style/empty_lines_around_block_body_spec.rb +103 -0
- data/spec/rubocop/cop/style/empty_lines_around_method_body_spec.rb +1 -1
- data/spec/rubocop/cop/style/end_of_line_spec.rb +2 -2
- data/spec/rubocop/cop/style/extra_spacing_spec.rb +68 -0
- data/spec/rubocop/cop/style/leading_comment_space_spec.rb +5 -0
- data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +8 -0
- data/spec/rubocop/cop/style/multiline_operation_indentation_spec.rb +35 -2
- data/spec/rubocop/cop/style/negated_if_spec.rb +1 -1
- data/spec/rubocop/cop/style/negated_while_spec.rb +5 -3
- data/spec/rubocop/cop/style/percent_q_literals_spec.rb +1 -1
- data/spec/rubocop/cop/style/perl_backrefs_spec.rb +5 -0
- data/spec/rubocop/cop/style/signal_exception_spec.rb +0 -1
- data/spec/rubocop/cop/style/single_line_block_params_spec.rb +13 -1
- data/spec/rubocop/cop/style/special_global_vars_spec.rb +5 -0
- data/spec/rubocop/cop/style/string_literals_spec.rb +11 -1
- data/spec/rubocop/cop/style/symbol_proc_spec.rb +20 -1
- data/spec/rubocop/cop/style/tab_spec.rb +2 -2
- data/spec/rubocop/cop/style/trailing_comma_spec.rb +1 -1
- data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +5 -0
- data/spec/rubocop/cop/team_spec.rb +2 -2
- data/spec/rubocop/formatter/offense_count_formatter_spec.rb +0 -1
- data/spec/rubocop/options_spec.rb +2 -0
- data/spec/rubocop/target_finder_spec.rb +23 -2
- metadata +12 -2
@@ -70,7 +70,7 @@ describe RuboCop::CommentConfig do
|
|
70
70
|
expect(void_disabled_lines & expected_part).to be_empty
|
71
71
|
end
|
72
72
|
|
73
|
-
it 'supports disabling single line with a
|
73
|
+
it 'supports disabling single line with a directive at end of line' do
|
74
74
|
eval_disabled_lines = disabled_lines_of_cop('Lint/Eval')
|
75
75
|
expect(eval_disabled_lines).to include(14)
|
76
76
|
expect(eval_disabled_lines).not_to include(15)
|
@@ -45,7 +45,7 @@ describe RuboCop::Cop::Lint::BlockAlignment do
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
it '
|
48
|
+
it 'accepts a block end that does not begin its line' do
|
49
49
|
inspect_source(cop,
|
50
50
|
[' scope :bar, lambda { joins(:baz)',
|
51
51
|
' .distinct }'
|
@@ -383,7 +383,7 @@ describe RuboCop::Cop::Lint::BlockAlignment do
|
|
383
383
|
|
384
384
|
it 'accepts end aligned with a method call' do
|
385
385
|
inspect_source(cop,
|
386
|
-
['parser.
|
386
|
+
['parser.children << lambda do |token|',
|
387
387
|
' token << 1',
|
388
388
|
'end'
|
389
389
|
])
|
@@ -392,13 +392,13 @@ describe RuboCop::Cop::Lint::BlockAlignment do
|
|
392
392
|
|
393
393
|
it 'registers an offense for mismatched block end with a method call' do
|
394
394
|
inspect_source(cop,
|
395
|
-
['parser.
|
395
|
+
['parser.children << lambda do |token|',
|
396
396
|
' token << 1',
|
397
397
|
' end'
|
398
398
|
])
|
399
399
|
expect(cop.messages)
|
400
400
|
.to eq(['`end` at 3, 2 is not aligned with' \
|
401
|
-
' `parser.
|
401
|
+
' `parser.children << lambda do |token|` at 1, 0'])
|
402
402
|
end
|
403
403
|
|
404
404
|
it 'accepts end aligned with a method call with arguments' do
|
@@ -245,7 +245,7 @@ describe RuboCop::Cop::Lint::UnusedMethodArgument do
|
|
245
245
|
end
|
246
246
|
end
|
247
247
|
|
248
|
-
context 'when an
|
248
|
+
context 'when an unused argument has default value' do
|
249
249
|
let(:source) { <<-END }
|
250
250
|
def some_method(foo, bar = 1)
|
251
251
|
puts foo
|
@@ -254,7 +254,7 @@ describe RuboCop::Cop::Lint::UselessAssignment do
|
|
254
254
|
]
|
255
255
|
end
|
256
256
|
|
257
|
-
it 'registers offenses for each
|
257
|
+
it 'registers offenses for each assignment' do
|
258
258
|
inspect_source(cop, source)
|
259
259
|
expect(cop.offenses.size).to eq(2)
|
260
260
|
|
@@ -1611,7 +1611,6 @@ describe RuboCop::Cop::Lint::UselessAssignment do
|
|
1611
1611
|
|
1612
1612
|
context 'when a variable is assigned ' \
|
1613
1613
|
'while being passed to a method taking block' do
|
1614
|
-
|
1615
1614
|
context 'and the variable is used' do
|
1616
1615
|
let(:source) do
|
1617
1616
|
[
|
@@ -1646,7 +1645,7 @@ describe RuboCop::Cop::Lint::UselessAssignment do
|
|
1646
1645
|
end
|
1647
1646
|
end
|
1648
1647
|
|
1649
|
-
context 'when a
|
1648
|
+
context 'when a variable is assigned ' \
|
1650
1649
|
'and passed to a method followed by method taking block' do
|
1651
1650
|
let(:source) do
|
1652
1651
|
[
|
@@ -164,7 +164,7 @@ describe RuboCop::Cop::Lint::UselessSetterCall do
|
|
164
164
|
end
|
165
165
|
end
|
166
166
|
|
167
|
-
context 'when a lvar contains a local object
|
167
|
+
context 'when a lvar contains a local object instantiated with literal' do
|
168
168
|
it 'registers an offense for the setter call on the lvar' do
|
169
169
|
inspect_source(cop,
|
170
170
|
['def test',
|
@@ -70,7 +70,7 @@ describe RuboCop::Cop::Style::AccessModifierIndentation do
|
|
70
70
|
'end'])
|
71
71
|
expect(cop.offenses.size).to eq(1)
|
72
72
|
expect(cop.messages).to eq(['Indent access modifiers like `private`.'])
|
73
|
-
# No EnforcedStyle can allow both
|
73
|
+
# No EnforcedStyle can allow both alignments:
|
74
74
|
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
|
75
75
|
end
|
76
76
|
|
@@ -86,7 +86,7 @@ describe RuboCop::Cop::Style::AccessModifierIndentation do
|
|
86
86
|
'end'])
|
87
87
|
expect(cop.offenses.size).to eq(1)
|
88
88
|
expect(cop.messages).to eq(['Indent access modifiers like `public`.'])
|
89
|
-
# No EnforcedStyle can allow both
|
89
|
+
# No EnforcedStyle can allow both alignments:
|
90
90
|
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
|
91
91
|
end
|
92
92
|
|
@@ -33,6 +33,14 @@ describe RuboCop::Cop::Style::Blocks do
|
|
33
33
|
expect(new_source).to eq(src)
|
34
34
|
end
|
35
35
|
|
36
|
+
it 'does not auto-correct {} if do-end would change the meaning' do
|
37
|
+
src = ['foo :bar, :baz, qux: lambda { |a|',
|
38
|
+
' bar a',
|
39
|
+
'}'].join("\n")
|
40
|
+
new_source = autocorrect_source(cop, src)
|
41
|
+
expect(new_source).to eq(src)
|
42
|
+
end
|
43
|
+
|
36
44
|
context 'when there are braces around a multi-line block' do
|
37
45
|
it 'registers an offense in the simple case' do
|
38
46
|
inspect_source(cop, ['each { |x|',
|
@@ -5,297 +5,278 @@ require 'spec_helper'
|
|
5
5
|
describe RuboCop::Cop::Style::BracesAroundHashParameters, :config do
|
6
6
|
subject(:cop) { described_class.new(config) }
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
shared_examples 'general non-offenses' do
|
9
|
+
after(:each) { expect(cop.offenses).to be_empty }
|
10
|
+
|
11
|
+
it 'accepts one non-hash parameter' do
|
12
|
+
inspect_source(cop, ['where(2)'])
|
11
13
|
end
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
expect(cop.messages).to be_empty
|
17
|
-
expect(cop.highlights).to be_empty
|
18
|
-
end
|
15
|
+
it 'accepts multiple non-hash parameters' do
|
16
|
+
inspect_source(cop, ['where(1, "2")'])
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
expect(cop.highlights).to be_empty
|
24
|
-
end
|
19
|
+
it 'accepts one empty hash parameter' do
|
20
|
+
inspect_source(cop, ['where({})'])
|
21
|
+
end
|
25
22
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
23
|
+
it 'accepts one empty hash parameter with whitespace' do
|
24
|
+
inspect_source(cop, ['where( { ',
|
25
|
+
" }\t ) "])
|
26
|
+
end
|
27
|
+
end
|
31
28
|
|
32
|
-
|
33
|
-
|
34
|
-
expect(cop.messages).to be_empty
|
35
|
-
expect(cop.highlights).to be_empty
|
36
|
-
end
|
29
|
+
shared_examples 'no_braces and context_dependent non-offenses' do
|
30
|
+
after(:each) { expect(cop.offenses).to be_empty }
|
37
31
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
expect(cop.highlights).to be_empty
|
42
|
-
end
|
32
|
+
it 'accepts one hash parameter without braces' do
|
33
|
+
inspect_source(cop, ['where(x: "y")'])
|
34
|
+
end
|
43
35
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
expect(cop.highlights).to be_empty
|
48
|
-
end
|
36
|
+
it 'accepts one hash parameter without braces and with multiple keys' do
|
37
|
+
inspect_source(cop, ['where(x: "y", foo: "bar")'])
|
38
|
+
end
|
49
39
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
expect(cop.highlights).to be_empty
|
54
|
-
end
|
40
|
+
it 'accepts one hash parameter without braces and with one hash value' do
|
41
|
+
inspect_source(cop, ['where(x: { "y" => "z" })'])
|
42
|
+
end
|
55
43
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
expect(cop.highlights).to be_empty
|
60
|
-
end
|
44
|
+
it 'accepts property assignment with braces' do
|
45
|
+
inspect_source(cop, ['x.z = { y: "z" }'])
|
46
|
+
end
|
61
47
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
end
|
48
|
+
it 'accepts operator with a hash parameter with braces' do
|
49
|
+
inspect_source(cop, ['x.z - { y: "z" }'])
|
50
|
+
end
|
51
|
+
end
|
67
52
|
|
68
|
-
|
69
|
-
|
70
|
-
expect(cop.messages).to be_empty
|
71
|
-
expect(cop.highlights).to be_empty
|
72
|
-
end
|
53
|
+
shared_examples 'no_braces and context_dependent offenses' do
|
54
|
+
let(:msg) { 'Redundant curly braces around a hash parameter.' }
|
73
55
|
|
56
|
+
it 'registers an offense for one non-hash parameter followed by a hash ' \
|
57
|
+
'parameter with braces' do
|
58
|
+
inspect_source(cop, ['where(1, { y: 2 })'])
|
59
|
+
expect(cop.messages).to eq([msg])
|
60
|
+
expect(cop.highlights).to eq(['{ y: 2 }'])
|
74
61
|
end
|
75
62
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
expect(cop.highlights).to eq(['{ y: 2 }'])
|
83
|
-
expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'braces')
|
84
|
-
end
|
63
|
+
it 'registers an offense for one object method hash parameter with ' \
|
64
|
+
'braces' do
|
65
|
+
inspect_source(cop, ['x.func({ y: "z" })'])
|
66
|
+
expect(cop.messages).to eq([msg])
|
67
|
+
expect(cop.highlights).to eq(['{ y: "z" }'])
|
68
|
+
end
|
85
69
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
])
|
92
|
-
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
|
93
|
-
end
|
70
|
+
it 'registers an offense for one hash parameter with braces' do
|
71
|
+
inspect_source(cop, ['where({ x: 1 })'])
|
72
|
+
expect(cop.messages).to eq([msg])
|
73
|
+
expect(cop.highlights).to eq(['{ x: 1 }'])
|
74
|
+
end
|
94
75
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
|
102
|
-
end
|
76
|
+
it 'registers an offense for one hash parameter with braces and ' \
|
77
|
+
'whitespace' do
|
78
|
+
inspect_source(cop, ["where( \n { x: 1 } )"])
|
79
|
+
expect(cop.messages).to eq([msg])
|
80
|
+
expect(cop.highlights).to eq(['{ x: 1 }'])
|
81
|
+
end
|
103
82
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
83
|
+
it 'registers an offense for one hash parameter with braces and multiple ' \
|
84
|
+
'keys' do
|
85
|
+
inspect_source(cop, ['where({ x: 1, foo: "bar" })'])
|
86
|
+
expect(cop.messages).to eq([msg])
|
87
|
+
expect(cop.highlights).to eq(['{ x: 1, foo: "bar" }'])
|
88
|
+
end
|
89
|
+
end
|
111
90
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
end
|
91
|
+
shared_examples 'no_braces and context_dependent auto-corrections' do
|
92
|
+
it 'corrects one non-hash parameter followed by a hash parameter with ' \
|
93
|
+
'braces' do
|
94
|
+
corrected = autocorrect_source(cop, ['where(1, { y: 2 })'])
|
95
|
+
expect(corrected).to eq('where(1, y: 2 )')
|
96
|
+
end
|
119
97
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
])
|
125
|
-
expect(cop.highlights).to eq(['{ x: 1 }'])
|
126
|
-
end
|
98
|
+
it 'corrects one object method hash parameter with braces' do
|
99
|
+
corrected = autocorrect_source(cop, ['x.func({ y: "z" })'])
|
100
|
+
expect(corrected).to eq('x.func( y: "z" )')
|
101
|
+
end
|
127
102
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
'Redundant curly braces around a hash parameter.'
|
132
|
-
])
|
133
|
-
expect(cop.highlights).to eq(['{ x: 1, foo: "bar" }'])
|
134
|
-
end
|
103
|
+
it 'corrects one hash parameter with braces' do
|
104
|
+
corrected = autocorrect_source(cop, ['where({ x: 1 })'])
|
105
|
+
expect(corrected).to eq('where( x: 1 )')
|
135
106
|
end
|
136
107
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
108
|
+
it 'corrects one hash parameter with braces and whitespace' do
|
109
|
+
corrected = autocorrect_source(cop, ['where( ',
|
110
|
+
' { x: 1 } )'])
|
111
|
+
expect(corrected).to eq(['where( ',
|
112
|
+
' x: 1 )'].join("\n"))
|
113
|
+
end
|
142
114
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
115
|
+
it 'corrects one hash parameter with braces and multiple keys' do
|
116
|
+
corrected = autocorrect_source(cop, ['where({ x: 1, foo: "bar" })'])
|
117
|
+
expect(corrected).to eq('where( x: 1, foo: "bar" )')
|
118
|
+
end
|
147
119
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
120
|
+
it 'corrects one hash parameter with braces and extra leading whitespace' do
|
121
|
+
corrected = autocorrect_source(cop, ['where({ x: 1, y: 2 })'])
|
122
|
+
expect(corrected).to eq('where( x: 1, y: 2 )')
|
123
|
+
end
|
152
124
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
end
|
125
|
+
it 'corrects one hash parameter with braces and extra trailing ' \
|
126
|
+
'whitespace' do
|
127
|
+
corrected = autocorrect_source(cop, ['where({ x: 1, y: 2 })'])
|
128
|
+
expect(corrected).to eq('where( x: 1, y: 2 )')
|
129
|
+
end
|
159
130
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
131
|
+
it 'corrects one hash parameter with braces and a trailing comma' do
|
132
|
+
corrected = autocorrect_source(cop, ['where({ x: 1, y: 2, })'])
|
133
|
+
expect(corrected).to eq('where( x: 1, y: 2, )')
|
134
|
+
end
|
164
135
|
|
165
|
-
|
166
|
-
|
167
|
-
|
136
|
+
it 'corrects one hash parameter with braces and trailing comma and ' \
|
137
|
+
'whitespace' do
|
138
|
+
corrected = autocorrect_source(cop, ['where({ x: 1, y: 2, })'])
|
139
|
+
expect(corrected).to eq('where( x: 1, y: 2, )')
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
context 'when EnforcedStyle is no_braces' do
|
144
|
+
let(:cop_config) { { 'EnforcedStyle' => 'no_braces' } }
|
145
|
+
|
146
|
+
context 'for correct code' do
|
147
|
+
include_examples 'general non-offenses'
|
148
|
+
include_examples 'no_braces and context_dependent non-offenses'
|
149
|
+
end
|
150
|
+
|
151
|
+
context 'for incorrect code' do
|
152
|
+
include_examples 'no_braces and context_dependent offenses'
|
153
|
+
|
154
|
+
after(:each) do
|
155
|
+
expect(cop.messages)
|
156
|
+
.to eq(['Redundant curly braces around a hash parameter.'])
|
168
157
|
end
|
169
158
|
|
170
|
-
it '
|
171
|
-
|
172
|
-
expect(
|
159
|
+
it 'registers an offense for two hash parameters with braces' do
|
160
|
+
inspect_source(cop, ['where({ x: 1 }, { y: 2 })'])
|
161
|
+
expect(cop.highlights).to eq(['{ y: 2 }'])
|
173
162
|
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe '#autocorrect' do
|
166
|
+
include_examples 'no_braces and context_dependent auto-corrections'
|
174
167
|
|
175
|
-
it 'one hash parameter with braces
|
176
|
-
corrected = autocorrect_source(cop, ['where({ x: 1
|
177
|
-
expect(corrected).to eq
|
168
|
+
it 'corrects one hash parameter with braces' do
|
169
|
+
corrected = autocorrect_source(cop, ['where(1, { x: 1 })'])
|
170
|
+
expect(corrected).to eq('where(1, x: 1 )')
|
178
171
|
end
|
179
172
|
|
180
|
-
it '
|
181
|
-
corrected = autocorrect_source(cop, ['where({ x: 1, y: 2
|
182
|
-
expect(corrected).to eq
|
173
|
+
it 'corrects two hash parameters with braces' do
|
174
|
+
corrected = autocorrect_source(cop, ['where(1, { x: 1 }, { y: 2 })'])
|
175
|
+
expect(corrected).to eq('where(1, { x: 1 }, y: 2 )')
|
183
176
|
end
|
184
177
|
end
|
185
178
|
end
|
186
179
|
|
187
|
-
context '
|
188
|
-
let(:cop_config)
|
189
|
-
|
180
|
+
context 'when EnforcedStyle is context_dependent' do
|
181
|
+
let(:cop_config) { { 'EnforcedStyle' => 'context_dependent' } }
|
182
|
+
|
183
|
+
context 'for correct code' do
|
184
|
+
include_examples 'general non-offenses'
|
185
|
+
include_examples 'no_braces and context_dependent non-offenses'
|
186
|
+
|
187
|
+
it 'accepts two hash parameters with braces' do
|
188
|
+
inspect_source(cop, ['where({ x: 1 }, { y: 2 })'])
|
189
|
+
expect(cop.offenses).to be_empty
|
190
|
+
end
|
190
191
|
end
|
191
192
|
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
193
|
+
context 'for incorrect code' do
|
194
|
+
include_examples 'no_braces and context_dependent offenses'
|
195
|
+
|
196
|
+
it 'registers an offense for one hash parameter with braces and one ' \
|
197
|
+
'without' do
|
198
|
+
inspect_source(cop, ['where({ x: 1 }, y: 2)'])
|
199
|
+
expect(cop.messages)
|
200
|
+
.to eq(['Missing curly braces around a hash parameter.'])
|
201
|
+
expect(cop.highlights).to eq(['y: 2'])
|
197
202
|
end
|
203
|
+
end
|
204
|
+
|
205
|
+
describe '#autocorrect' do
|
206
|
+
include_examples 'no_braces and context_dependent auto-corrections'
|
198
207
|
|
199
|
-
it 'one
|
200
|
-
|
201
|
-
expect(
|
202
|
-
expect(cop.highlights).to be_empty
|
208
|
+
it 'corrects one hash parameter with braces and one without' do
|
209
|
+
corrected = autocorrect_source(cop, ['where(1, { x: 1 }, y: 2)'])
|
210
|
+
expect(corrected).to eq('where(1, { x: 1 }, {y: 2})')
|
203
211
|
end
|
204
212
|
|
205
|
-
it '
|
206
|
-
|
207
|
-
expect(
|
208
|
-
expect(cop.highlights).to be_empty
|
213
|
+
it 'corrects one hash parameter with braces' do
|
214
|
+
corrected = autocorrect_source(cop, ['where(1, { x: 1 })'])
|
215
|
+
expect(corrected).to eq('where(1, x: 1 )')
|
209
216
|
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
context 'when EnforcedStyle is braces' do
|
221
|
+
let(:cop_config) { { 'EnforcedStyle' => 'braces' } }
|
210
222
|
|
211
|
-
|
223
|
+
context 'for correct code' do
|
224
|
+
include_examples 'general non-offenses'
|
225
|
+
|
226
|
+
after(:each) { expect(cop.offenses).to be_empty }
|
227
|
+
|
228
|
+
it 'accepts one hash parameter with braces' do
|
212
229
|
inspect_source(cop, ['where({ x: 1 })'])
|
213
|
-
expect(cop.messages).to be_empty
|
214
|
-
expect(cop.highlights).to be_empty
|
215
230
|
end
|
216
231
|
|
217
|
-
it 'multiple hash parameters with braces' do
|
232
|
+
it 'accepts multiple hash parameters with braces' do
|
218
233
|
inspect_source(cop, ['where({ x: 1 }, { y: 2 })'])
|
219
|
-
expect(cop.messages).to be_empty
|
220
|
-
expect(cop.highlights).to be_empty
|
221
234
|
end
|
222
235
|
|
223
|
-
it 'one hash parameter with braces and
|
224
|
-
inspect_source(cop, [
|
225
|
-
'where( { x: 1 } )'
|
226
|
-
])
|
227
|
-
expect(cop.messages).to be_empty
|
228
|
-
expect(cop.highlights).to be_empty
|
229
|
-
end
|
230
|
-
|
231
|
-
it 'one hash parameter with braces and separators around it' do
|
236
|
+
it 'accepts one hash parameter with braces and whitespace' do
|
232
237
|
inspect_source(cop, ["where( \t { x: 1 ",
|
233
238
|
' } )'])
|
234
|
-
expect(cop.messages).to be_empty
|
235
|
-
expect(cop.highlights).to be_empty
|
236
239
|
end
|
237
240
|
end
|
238
241
|
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
'Missing curly braces around a hash parameter.'
|
244
|
-
])
|
245
|
-
expect(cop.highlights).to eq(['x: "y"'])
|
246
|
-
expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' =>
|
247
|
-
'no_braces')
|
248
|
-
end
|
249
|
-
|
250
|
-
it 'opposite + correct style' do
|
251
|
-
inspect_source(cop, ['where(y: 2)',
|
252
|
-
'where({ y: 2 })'])
|
253
|
-
expect(cop.messages).to eq([
|
254
|
-
'Missing curly braces around a hash parameter.'
|
255
|
-
])
|
256
|
-
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
|
242
|
+
context 'for incorrect code' do
|
243
|
+
after(:each) do
|
244
|
+
expect(cop.messages)
|
245
|
+
.to eq(['Missing curly braces around a hash parameter.'])
|
257
246
|
end
|
258
247
|
|
259
|
-
it '
|
260
|
-
inspect_source(cop, ['where(
|
261
|
-
|
262
|
-
expect(cop.messages).to eq([
|
263
|
-
'Missing curly braces around a hash parameter.'
|
264
|
-
])
|
265
|
-
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
|
248
|
+
it 'registers an offense for one hash parameter without braces' do
|
249
|
+
inspect_source(cop, ['where(x: "y")'])
|
250
|
+
expect(cop.highlights).to eq(['x: "y"'])
|
266
251
|
end
|
267
252
|
|
268
|
-
it 'one hash parameter with multiple keys and
|
253
|
+
it 'registers an offense for one hash parameter with multiple keys and ' \
|
254
|
+
'without braces' do
|
269
255
|
inspect_source(cop, ['where(x: "y", foo: "bar")'])
|
270
|
-
expect(cop.messages).to eq([
|
271
|
-
'Missing curly braces around a hash parameter.'
|
272
|
-
])
|
273
256
|
expect(cop.highlights).to eq(['x: "y", foo: "bar"'])
|
274
257
|
end
|
275
258
|
|
276
|
-
it 'one hash parameter without braces with
|
259
|
+
it 'registers an offense for one hash parameter without braces with ' \
|
260
|
+
'one hash value' do
|
277
261
|
inspect_source(cop, ['where(x: { "y" => "z" })'])
|
278
|
-
expect(cop.messages).to eq([
|
279
|
-
'Missing curly braces around a hash parameter.'
|
280
|
-
])
|
281
262
|
expect(cop.highlights).to eq(['x: { "y" => "z" }'])
|
282
263
|
end
|
283
264
|
end
|
284
265
|
|
285
|
-
describe '
|
286
|
-
it 'one hash parameter without braces' do
|
266
|
+
describe '#autocorrect' do
|
267
|
+
it 'corrects one hash parameter without braces' do
|
287
268
|
corrected = autocorrect_source(cop, ['where(x: "y")'])
|
288
|
-
expect(corrected).to eq
|
269
|
+
expect(corrected).to eq('where({x: "y"})')
|
289
270
|
end
|
290
271
|
|
291
|
-
it 'one hash parameter with multiple keys and without braces' do
|
272
|
+
it 'corrects one hash parameter with multiple keys and without braces' do
|
292
273
|
corrected = autocorrect_source(cop, ['where(x: "y", foo: "bar")'])
|
293
|
-
expect(corrected).to eq
|
274
|
+
expect(corrected).to eq('where({x: "y", foo: "bar"})')
|
294
275
|
end
|
295
276
|
|
296
|
-
it 'one hash parameter without braces with one hash value' do
|
277
|
+
it 'corrects one hash parameter without braces with one hash value' do
|
297
278
|
corrected = autocorrect_source(cop, ['where(x: { "y" => "z" })'])
|
298
|
-
expect(corrected).to eq
|
279
|
+
expect(corrected).to eq('where({x: { "y" => "z" }})')
|
299
280
|
end
|
300
281
|
end
|
301
282
|
end
|