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.

Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +44 -4
  3. data/Gemfile +1 -1
  4. data/README.md +16 -1
  5. data/config/default.yml +15 -1
  6. data/config/disabled.yml +5 -0
  7. data/config/enabled.yml +10 -2
  8. data/lib/rubocop.rb +3 -0
  9. data/lib/rubocop/config.rb +1 -1
  10. data/lib/rubocop/cop/mixin/access_modifier_node.rb +2 -2
  11. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +10 -26
  12. data/lib/rubocop/cop/mixin/on_method_def.rb +1 -1
  13. data/lib/rubocop/cop/mixin/string_help.rb +10 -1
  14. data/lib/rubocop/cop/style/align_hash.rb +2 -2
  15. data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -1
  16. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +21 -19
  17. data/lib/rubocop/cop/style/else_alignment.rb +29 -16
  18. data/lib/rubocop/cop/style/empty_else.rb +47 -0
  19. data/lib/rubocop/cop/style/empty_lines_around_block_body.rb +38 -0
  20. data/lib/rubocop/cop/style/extra_spacing.rb +35 -0
  21. data/lib/rubocop/cop/style/indentation_width.rb +1 -1
  22. data/lib/rubocop/cop/style/leading_comment_space.rb +1 -1
  23. data/lib/rubocop/cop/style/line_end_concatenation.rb +5 -1
  24. data/lib/rubocop/cop/style/multiline_operation_indentation.rb +20 -4
  25. data/lib/rubocop/cop/style/negated_while.rb +3 -1
  26. data/lib/rubocop/cop/style/perl_backrefs.rb +8 -3
  27. data/lib/rubocop/cop/style/single_line_block_params.rb +7 -1
  28. data/lib/rubocop/cop/style/special_global_vars.rb +8 -3
  29. data/lib/rubocop/cop/style/string_literals.rb +4 -11
  30. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +2 -7
  31. data/lib/rubocop/cop/style/symbol_proc.rb +14 -6
  32. data/lib/rubocop/cop/style/unneeded_capital_w.rb +2 -1
  33. data/lib/rubocop/cop/util.rb +1 -1
  34. data/lib/rubocop/cop/variable_force.rb +3 -3
  35. data/lib/rubocop/cop/variable_force/scope.rb +1 -1
  36. data/lib/rubocop/cop/variable_force/variable.rb +1 -1
  37. data/lib/rubocop/formatter/html_formatter.rb +1 -1
  38. data/lib/rubocop/options.rb +13 -6
  39. data/lib/rubocop/rake_task.rb +0 -1
  40. data/lib/rubocop/runner.rb +19 -6
  41. data/lib/rubocop/target_finder.rb +32 -1
  42. data/lib/rubocop/version.rb +1 -1
  43. data/relnotes/v0.28.0.md +90 -0
  44. data/spec/project_spec.rb +1 -0
  45. data/spec/rubocop/cli_spec.rb +115 -24
  46. data/spec/rubocop/comment_config_spec.rb +1 -1
  47. data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +0 -1
  48. data/spec/rubocop/cop/lint/block_alignment_spec.rb +4 -4
  49. data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +1 -1
  50. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +2 -3
  51. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +1 -1
  52. data/spec/rubocop/cop/lint/void_spec.rb +0 -1
  53. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +2 -2
  54. data/spec/rubocop/cop/style/blocks_spec.rb +8 -0
  55. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +196 -215
  56. data/spec/rubocop/cop/style/case_indentation_spec.rb +4 -4
  57. data/spec/rubocop/cop/style/documentation_spec.rb +0 -1
  58. data/spec/rubocop/cop/style/else_alignment_spec.rb +63 -4
  59. data/spec/rubocop/cop/style/empty_else_spec.rb +100 -0
  60. data/spec/rubocop/cop/style/empty_lines_around_block_body_spec.rb +103 -0
  61. data/spec/rubocop/cop/style/empty_lines_around_method_body_spec.rb +1 -1
  62. data/spec/rubocop/cop/style/end_of_line_spec.rb +2 -2
  63. data/spec/rubocop/cop/style/extra_spacing_spec.rb +68 -0
  64. data/spec/rubocop/cop/style/leading_comment_space_spec.rb +5 -0
  65. data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +8 -0
  66. data/spec/rubocop/cop/style/multiline_operation_indentation_spec.rb +35 -2
  67. data/spec/rubocop/cop/style/negated_if_spec.rb +1 -1
  68. data/spec/rubocop/cop/style/negated_while_spec.rb +5 -3
  69. data/spec/rubocop/cop/style/percent_q_literals_spec.rb +1 -1
  70. data/spec/rubocop/cop/style/perl_backrefs_spec.rb +5 -0
  71. data/spec/rubocop/cop/style/signal_exception_spec.rb +0 -1
  72. data/spec/rubocop/cop/style/single_line_block_params_spec.rb +13 -1
  73. data/spec/rubocop/cop/style/special_global_vars_spec.rb +5 -0
  74. data/spec/rubocop/cop/style/string_literals_spec.rb +11 -1
  75. data/spec/rubocop/cop/style/symbol_proc_spec.rb +20 -1
  76. data/spec/rubocop/cop/style/tab_spec.rb +2 -2
  77. data/spec/rubocop/cop/style/trailing_comma_spec.rb +1 -1
  78. data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +5 -0
  79. data/spec/rubocop/cop/team_spec.rb +2 -2
  80. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +0 -1
  81. data/spec/rubocop/options_spec.rb +2 -0
  82. data/spec/rubocop/target_finder_spec.rb +23 -2
  83. 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 direcive at end of line' do
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)
@@ -90,7 +90,6 @@ describe RuboCop::Cop::Lint::AssignmentInCondition, :config do
90
90
  ])
91
91
  expect(cop.offenses).to be_empty
92
92
  end
93
-
94
93
  end
95
94
 
96
95
  context 'safe assignment is not allowed' do
@@ -45,7 +45,7 @@ describe RuboCop::Cop::Lint::BlockAlignment do
45
45
  end
46
46
  end
47
47
 
48
- it 'acepts a block end that does not begin its line' do
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.childs << lambda do |token|',
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.childs << lambda do |token|',
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.childs << lambda do |token|` at 1, 0'])
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 unsed argument has default value' do
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 asignment' do
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 variabled is assigned ' \
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 instanciated with literal' do
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',
@@ -61,5 +61,4 @@ describe RuboCop::Cop::Lint::Void do
61
61
  ])
62
62
  expect(cop.offenses).to be_empty
63
63
  end
64
-
65
64
  end
@@ -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 aligments:
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 aligments:
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
- context 'no_braces' do
9
- let(:cop_config) do
10
- { 'EnforcedStyle' => 'no_braces' }
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
- describe 'accepts' do
14
- it 'one non-hash parameter' do
15
- inspect_source(cop, ['where(2)'])
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
- it 'one empty hash parameter' do
21
- inspect_source(cop, ['where({})'])
22
- expect(cop.messages).to be_empty
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
- it 'one hash parameter with separators' do
27
- inspect_source(cop, ["where( { \n }\t ) "])
28
- expect(cop.messages).to be_empty
29
- expect(cop.highlights).to be_empty
30
- end
23
+ it 'accepts one empty hash parameter with whitespace' do
24
+ inspect_source(cop, ['where( { ',
25
+ " }\t ) "])
26
+ end
27
+ end
31
28
 
32
- it 'multiple non-hash parameters' do
33
- inspect_source(cop, ['where(1, "2")'])
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
- it 'one hash parameter without braces' do
39
- inspect_source(cop, ['where(x: "y")'])
40
- expect(cop.messages).to be_empty
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
- it 'one hash parameter without braces and multiple keys' do
45
- inspect_source(cop, ['where(x: "y", foo: "bar")'])
46
- expect(cop.messages).to be_empty
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
- it 'one hash parameter without braces and one hash value' do
51
- inspect_source(cop, ['where(x: { "y" => "z" })'])
52
- expect(cop.messages).to be_empty
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
- it 'multiple hash parameters with braces' do
57
- inspect_source(cop, ['where({ x: 1 }, { y: 2 })'])
58
- expect(cop.messages).to be_empty
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
- it 'property assignment with braces' do
63
- inspect_source(cop, ['x.z = { y: "z" }'])
64
- expect(cop.messages).to be_empty
65
- expect(cop.highlights).to be_empty
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
- it 'operator with a hash parameter with braces' do
69
- inspect_source(cop, ['x.z - { y: "z" }'])
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
- describe 'registers an offense for' do
77
- it 'one non-hash parameter followed by a hash parameter with braces' do
78
- inspect_source(cop, ['where(1, { y: 2 })'])
79
- expect(cop.messages).to eq([
80
- 'Redundant curly braces around a hash parameter.'
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
- it 'correct + opposite style' do
87
- inspect_source(cop, ['where(1, y: 2)',
88
- 'where(1, { y: 2 })'])
89
- expect(cop.messages).to eq([
90
- 'Redundant curly braces around a hash parameter.'
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
- it 'opposite + correct style' do
96
- inspect_source(cop, ['where(1, { y: 2 })',
97
- 'where(1, y: 2)'])
98
- expect(cop.messages).to eq([
99
- 'Redundant curly braces around a hash parameter.'
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
- it 'one object method hash parameter with braces' do
105
- inspect_source(cop, ['x.func({ y: "z" })'])
106
- expect(cop.messages).to eq([
107
- 'Redundant curly braces around a hash parameter.'
108
- ])
109
- expect(cop.highlights).to eq(['{ y: "z" }'])
110
- end
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
- it 'one hash parameter with braces' do
113
- inspect_source(cop, ['where({ x: 1 })'])
114
- expect(cop.messages).to eq([
115
- 'Redundant curly braces around a hash parameter.'
116
- ])
117
- expect(cop.highlights).to eq(['{ x: 1 }'])
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
- it 'one hash parameter with braces and separators' do
121
- inspect_source(cop, ["where( \n { x: 1 } )"])
122
- expect(cop.messages).to eq([
123
- 'Redundant curly braces around a hash parameter.'
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
- it 'one hash parameter with braces and multiple keys' do
129
- inspect_source(cop, ['where({ x: 1, foo: "bar" })'])
130
- expect(cop.messages).to eq([
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
- describe 'auto-corrects' do
138
- it 'one non-hash parameter followed by a hash parameter with braces' do
139
- corrected = autocorrect_source(cop, ['where(1, { y: 2 })'])
140
- expect(corrected).to eq 'where(1, y: 2 )'
141
- end
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
- it 'one object method hash parameter with braces' do
144
- corrected = autocorrect_source(cop, ['x.func({ y: "z" })'])
145
- expect(corrected).to eq 'x.func( y: "z" )'
146
- end
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
- it 'one hash parameter with braces' do
149
- corrected = autocorrect_source(cop, ['where({ x: 1 })'])
150
- expect(corrected).to eq 'where( x: 1 )'
151
- end
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
- it 'one hash parameter with braces and separators' do
154
- corrected = autocorrect_source(cop, ['where( ',
155
- ' { x: 1 } )'])
156
- expect(corrected).to eq(['where( ',
157
- ' x: 1 )'].join("\n"))
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
- it 'one hash parameter with braces and multiple keys' do
161
- corrected = autocorrect_source(cop, ['where({ x: 1, foo: "bar" })'])
162
- expect(corrected).to eq 'where( x: 1, foo: "bar" )'
163
- end
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
- it 'one hash parameter with braces and extra leading whitespace' do
166
- corrected = autocorrect_source(cop, ['where({ x: 1, y: 2 })'])
167
- expect(corrected).to eq 'where( x: 1, y: 2 )'
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 'one hash parameter with braces and extra trailing whitespace' do
171
- corrected = autocorrect_source(cop, ['where({ x: 1, y: 2 })'])
172
- expect(corrected).to eq 'where( x: 1, y: 2 )'
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 and a trailing comma' do
176
- corrected = autocorrect_source(cop, ['where({ x: 1, y: 2, })'])
177
- expect(corrected).to eq 'where( x: 1, y: 2, )'
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 'one hash parameter with braces and trailing comma and whitespace' do
181
- corrected = autocorrect_source(cop, ['where({ x: 1, y: 2, })'])
182
- expect(corrected).to eq 'where( x: 1, y: 2, )'
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 'braces' do
188
- let(:cop_config) do
189
- { 'EnforcedStyle' => 'braces' }
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
- describe 'accepts' do
193
- it 'an empty hash parameter' do
194
- inspect_source(cop, ['where({})'])
195
- expect(cop.messages).to be_empty
196
- expect(cop.highlights).to be_empty
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 non-hash parameter' do
200
- inspect_source(cop, ['where(2)'])
201
- expect(cop.messages).to be_empty
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 'multiple non-hash parameters' do
206
- inspect_source(cop, ['where(1, "2")'])
207
- expect(cop.messages).to be_empty
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
- it 'one hash parameter with braces' do
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 spaces around it' do
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
- describe 'registers an offense for' do
240
- it 'one hash parameter without braces' do
241
- inspect_source(cop, ['where(x: "y")'])
242
- expect(cop.messages).to eq([
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 'correct + opposite style' do
260
- inspect_source(cop, ['where({ y: 2 })',
261
- 'where(y: 2)'])
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 without braces' do
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 one hash value' do
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 'auto-corrects' do
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 'where({x: "y"})'
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 'where({x: "y", foo: "bar"})'
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 'where({x: { "y" => "z" }})'
279
+ expect(corrected).to eq('where({x: { "y" => "z" }})')
299
280
  end
300
281
  end
301
282
  end