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.

Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +12 -8
  3. data/.travis.yml +1 -1
  4. data/CHANGELOG.md +42 -0
  5. data/Gemfile +2 -0
  6. data/README.md +27 -6
  7. data/bin/rubocop +10 -15
  8. data/config/default.yml +72 -25
  9. data/config/enabled.yml +57 -25
  10. data/lib/rubocop.rb +15 -8
  11. data/lib/rubocop/config_loader.rb +11 -7
  12. data/lib/rubocop/cop/cop.rb +16 -16
  13. data/lib/rubocop/cop/ignored_node.rb +11 -4
  14. data/lib/rubocop/cop/lint/block_alignment.rb +29 -4
  15. data/lib/rubocop/cop/lint/debugger.rb +8 -1
  16. data/lib/rubocop/cop/lint/def_end_alignment.rb +5 -1
  17. data/lib/rubocop/cop/lint/end_alignment.rb +5 -5
  18. data/lib/rubocop/cop/lint/end_in_method.rb +2 -2
  19. data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -2
  20. data/lib/rubocop/cop/{style → metrics}/block_nesting.rb +1 -1
  21. data/lib/rubocop/cop/{style → metrics}/class_length.rb +4 -4
  22. data/lib/rubocop/cop/{style → metrics}/cyclomatic_complexity.rb +5 -16
  23. data/lib/rubocop/cop/{style → metrics}/line_length.rb +1 -1
  24. data/lib/rubocop/cop/{style → metrics}/method_length.rb +8 -4
  25. data/lib/rubocop/cop/{style → metrics}/parameter_lists.rb +1 -1
  26. data/lib/rubocop/cop/metrics/perceived_complexity.rb +61 -0
  27. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +10 -2
  28. data/lib/rubocop/cop/mixin/code_length.rb +2 -3
  29. data/lib/rubocop/cop/mixin/configurable_naming.rb +6 -20
  30. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  31. data/lib/rubocop/cop/mixin/if_node.rb +1 -1
  32. data/lib/rubocop/cop/mixin/method_complexity.rb +32 -0
  33. data/lib/rubocop/cop/mixin/negative_conditional.rb +1 -1
  34. data/lib/rubocop/cop/mixin/{check_methods.rb → on_method.rb} +3 -3
  35. data/lib/rubocop/cop/mixin/on_normal_if_unless.rb +24 -0
  36. data/lib/rubocop/cop/mixin/percent_literal.rb +2 -1
  37. data/lib/rubocop/cop/mixin/space_inside.rb +33 -4
  38. data/lib/rubocop/cop/mixin/statement_modifier.rb +14 -14
  39. data/lib/rubocop/cop/mixin/string_help.rb +4 -0
  40. data/lib/rubocop/cop/mixin/surrounding_space.rb +1 -0
  41. data/lib/rubocop/cop/rails/delegate.rb +2 -2
  42. data/lib/rubocop/cop/rails/output.rb +4 -2
  43. data/lib/rubocop/cop/style/accessor_method_name.rb +2 -2
  44. data/lib/rubocop/cop/style/align_hash.rb +9 -1
  45. data/lib/rubocop/cop/style/and_or.rb +37 -3
  46. data/lib/rubocop/cop/style/bare_percent_literals.rb +46 -0
  47. data/lib/rubocop/cop/style/block_end_newline.rb +56 -0
  48. data/lib/rubocop/cop/style/character_literal.rb +1 -1
  49. data/lib/rubocop/cop/style/def_with_parentheses.rb +2 -2
  50. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +31 -3
  51. data/lib/rubocop/cop/style/empty_lines_around_body.rb +6 -2
  52. data/lib/rubocop/cop/style/end_of_line.rb +3 -14
  53. data/lib/rubocop/cop/style/if_unless_modifier.rb +3 -6
  54. data/lib/rubocop/cop/style/if_with_semicolon.rb +6 -7
  55. data/lib/rubocop/cop/style/indentation_width.rb +3 -8
  56. data/lib/rubocop/cop/style/method_def_parentheses.rb +2 -2
  57. data/lib/rubocop/cop/style/method_name.rb +4 -18
  58. data/lib/rubocop/cop/style/multiline_block_layout.rb +73 -0
  59. data/lib/rubocop/cop/style/multiline_if_then.rb +4 -4
  60. data/lib/rubocop/cop/style/negated_if.rb +1 -1
  61. data/lib/rubocop/cop/style/negated_while.rb +2 -2
  62. data/lib/rubocop/cop/style/next.rb +12 -0
  63. data/lib/rubocop/cop/style/one_line_conditional.rb +6 -6
  64. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +22 -8
  65. data/lib/rubocop/cop/style/percent_q_literals.rb +53 -0
  66. data/lib/rubocop/cop/style/predicate_name.rb +2 -2
  67. data/lib/rubocop/cop/style/redundant_begin.rb +2 -2
  68. data/lib/rubocop/cop/style/redundant_return.rb +2 -2
  69. data/lib/rubocop/cop/style/rescue_modifier.rb +7 -3
  70. data/lib/rubocop/cop/style/single_line_methods.rb +2 -2
  71. data/lib/rubocop/cop/style/space_after_method_name.rb +2 -2
  72. data/lib/rubocop/cop/style/space_inside_block_braces.rb +14 -10
  73. data/lib/rubocop/cop/style/space_inside_brackets.rb +5 -1
  74. data/lib/rubocop/cop/style/string_literals.rb +5 -8
  75. data/lib/rubocop/cop/style/trailing_comma.rb +15 -3
  76. data/lib/rubocop/cop/style/trivial_accessors.rb +2 -2
  77. data/lib/rubocop/cop/style/unneeded_capital_w.rb +2 -4
  78. data/lib/rubocop/cop/style/unneeded_percent_q.rb +13 -0
  79. data/lib/rubocop/cop/style/variable_interpolation.rb +14 -2
  80. data/lib/rubocop/cop/style/variable_name.rb +17 -17
  81. data/lib/rubocop/cop/style/while_until_modifier.rb +2 -5
  82. data/lib/rubocop/cop/util.rb +5 -0
  83. data/lib/rubocop/cop/variable_force.rb +7 -6
  84. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  85. data/lib/rubocop/processed_source.rb +5 -22
  86. data/lib/rubocop/version.rb +1 -1
  87. data/relnotes/v0.25.0.md +91 -0
  88. data/rubocop.gemspec +2 -3
  89. data/spec/project_spec.rb +1 -1
  90. data/spec/rubocop/cli_spec.rb +70 -44
  91. data/spec/rubocop/comment_config_spec.rb +6 -6
  92. data/spec/rubocop/config_loader_spec.rb +19 -13
  93. data/spec/rubocop/config_spec.rb +3 -3
  94. data/spec/rubocop/cop/commissioner_spec.rb +1 -1
  95. data/spec/rubocop/cop/cop_spec.rb +3 -3
  96. data/spec/rubocop/cop/lint/block_alignment_spec.rb +113 -6
  97. data/spec/rubocop/cop/lint/debugger_spec.rb +10 -6
  98. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +12 -0
  99. data/spec/rubocop/cop/{style → metrics}/block_nesting_spec.rb +1 -1
  100. data/spec/rubocop/cop/{style → metrics}/class_length_spec.rb +1 -1
  101. data/spec/rubocop/cop/{style → metrics}/cyclomatic_complexity_spec.rb +1 -1
  102. data/spec/rubocop/cop/{style → metrics}/if_unless_modifier_spec.rb +2 -2
  103. data/spec/rubocop/cop/{style → metrics}/line_length_spec.rb +7 -7
  104. data/spec/rubocop/cop/{style → metrics}/method_length_spec.rb +1 -1
  105. data/spec/rubocop/cop/{style → metrics}/parameter_lists_spec.rb +1 -1
  106. data/spec/rubocop/cop/metrics/perceived_complexity_spec.rb +222 -0
  107. data/spec/rubocop/cop/{style → metrics}/while_until_modifier_spec.rb +2 -2
  108. data/spec/rubocop/cop/rails/output_spec.rb +8 -2
  109. data/spec/rubocop/cop/style/align_hash_spec.rb +7 -0
  110. data/spec/rubocop/cop/style/and_or_spec.rb +245 -43
  111. data/spec/rubocop/cop/style/bare_percent_literals_spec.rb +132 -0
  112. data/spec/rubocop/cop/style/block_end_newline_spec.rb +61 -0
  113. data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +34 -0
  114. data/spec/rubocop/cop/style/end_of_line_spec.rb +8 -0
  115. data/spec/rubocop/cop/style/guard_clause_spec.rb +1 -1
  116. data/spec/rubocop/cop/style/multiline_block_layout_spec.rb +138 -0
  117. data/spec/rubocop/cop/style/next_spec.rb +32 -3
  118. data/spec/rubocop/cop/style/percent_literal_delimiters_spec.rb +34 -0
  119. data/spec/rubocop/cop/style/percent_q_literals_spec.rb +122 -0
  120. data/spec/rubocop/cop/style/space_inside_block_braces_spec.rb +20 -0
  121. data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +26 -2
  122. data/spec/rubocop/cop/style/trailing_comma_spec.rb +112 -0
  123. data/spec/rubocop/cop/style/unneeded_percent_q_spec.rb +86 -31
  124. data/spec/rubocop/cop/style/variable_interpolation_spec.rb +21 -1
  125. data/spec/rubocop/cop/team_spec.rb +14 -9
  126. data/spec/spec_helper.rb +1 -0
  127. metadata +47 -50
  128. data/lib/rubocop/cop/mixin/if_then_else.rb +0 -23
@@ -140,6 +140,11 @@ describe RuboCop::Cop::Style::SpaceInsideBlockBraces do
140
140
  expect(cop.highlights).to eq(['{|'])
141
141
  end
142
142
 
143
+ it 'accepts new lambda syntax' do
144
+ inspect_source(cop, ['->(x) { x }'])
145
+ expect(cop.messages).to be_empty
146
+ end
147
+
143
148
  it 'auto-corrects missing space' do
144
149
  new_source = autocorrect_source(cop, 'each {|x| puts }')
145
150
  expect(new_source).to eq('each { |x| puts }')
@@ -182,6 +187,11 @@ describe RuboCop::Cop::Style::SpaceInsideBlockBraces do
182
187
  expect(cop.highlights).to eq([' '])
183
188
  end
184
189
 
190
+ it 'accepts new lambda syntax' do
191
+ inspect_source(cop, ['->(x) { x }'])
192
+ expect(cop.messages).to be_empty
193
+ end
194
+
185
195
  it 'auto-corrects unwanted space' do
186
196
  new_source = autocorrect_source(cop, 'each { |x| puts }')
187
197
  expect(new_source).to eq('each {|x| puts }')
@@ -256,6 +266,11 @@ describe RuboCop::Cop::Style::SpaceInsideBlockBraces do
256
266
  expect(cop.highlights).to eq(['{|'])
257
267
  end
258
268
 
269
+ it 'accepts new lambda syntax' do
270
+ inspect_source(cop, ['->(x) {x}'])
271
+ expect(cop.messages).to be_empty
272
+ end
273
+
259
274
  it 'auto-corrects missing space' do
260
275
  new_source = autocorrect_source(cop, 'each {|x| puts}')
261
276
  expect(new_source).to eq('each { |x| puts}')
@@ -277,6 +292,11 @@ describe RuboCop::Cop::Style::SpaceInsideBlockBraces do
277
292
  expect(cop.highlights).to eq([' '])
278
293
  end
279
294
 
295
+ it 'accepts new lambda syntax' do
296
+ inspect_source(cop, ['->(x) {x}'])
297
+ expect(cop.messages).to be_empty
298
+ end
299
+
280
300
  it 'auto-corrects unwanted space' do
281
301
  new_source = autocorrect_source(cop, 'each { |x| puts}')
282
302
  expect(new_source).to eq('each {|x| puts}')
@@ -13,6 +13,22 @@ describe RuboCop::Cop::Style::SpaceInsideBrackets do
13
13
  'Space inside square brackets detected.'])
14
14
  end
15
15
 
16
+ it 'registers an offense for Hash#[] with symbol key and spaces inside' do
17
+ inspect_source(cop, ['a[ :key]',
18
+ 'b[:key ]'])
19
+ expect(cop.messages).to eq(
20
+ ['Space inside square brackets detected.',
21
+ 'Space inside square brackets detected.'])
22
+ end
23
+
24
+ it 'registers an offense for Hash#[] with string key and spaces inside' do
25
+ inspect_source(cop, ['a[\'key\' ]',
26
+ 'b[ \'key\']'])
27
+ expect(cop.messages).to eq(
28
+ ['Space inside square brackets detected.',
29
+ 'Space inside square brackets detected.'])
30
+ end
31
+
16
32
  it 'accepts space inside strings within square brackets' do
17
33
  inspect_source(cop, ["['Encoding:',",
18
34
  " ' Enabled: false']"])
@@ -52,8 +68,16 @@ describe RuboCop::Cop::Style::SpaceInsideBrackets do
52
68
 
53
69
  it 'auto-corrects unwanted space' do
54
70
  new_source = autocorrect_source(cop, ['a = [1, 2 ]',
55
- 'b = [ 1, 2]'])
71
+ 'b = [ 1, 2]',
72
+ 'c[ :key]',
73
+ 'd[:key ]',
74
+ 'e["key" ]',
75
+ 'f[ "key"]'])
56
76
  expect(new_source).to eq(['a = [1, 2]',
57
- 'b = [1, 2]'].join("\n"))
77
+ 'b = [1, 2]',
78
+ 'c[:key]',
79
+ 'd[:key]',
80
+ 'e["key"]',
81
+ 'f["key"]'].join("\n"))
58
82
  end
59
83
  end
@@ -75,6 +75,28 @@ describe RuboCop::Cop::Style::TrailingComma, :config do
75
75
  inspect_source(cop, 'some_method')
76
76
  expect(cop.offenses).to be_empty
77
77
  end
78
+
79
+ it 'auto-corrects unwanted comma in an Array literal' do
80
+ new_source = autocorrect_source(cop, 'VALUES = [1001, 2020, 3333, ]')
81
+ expect(new_source).to eq('VALUES = [1001, 2020, 3333 ]')
82
+ end
83
+
84
+ it 'auto-corrects unwanted comma in a Hash literal' do
85
+ new_source = autocorrect_source(cop,
86
+ 'MAP = { a: 1001, b: 2020, c: 3333, }')
87
+ expect(new_source).to eq('MAP = { a: 1001, b: 2020, c: 3333 }')
88
+ end
89
+
90
+ it 'auto-corrects unwanted comma in a method call' do
91
+ new_source = autocorrect_source(cop, 'some_method(a, b, c, )')
92
+ expect(new_source).to eq('some_method(a, b, c )')
93
+ end
94
+
95
+ it 'auto-corrects unwanted comma in a method call with hash parameters at' \
96
+ ' the end' do
97
+ new_source = autocorrect_source(cop, 'some_method(a, b, c: 0, d: 1, )')
98
+ expect(new_source).to eq('some_method(a, b, c: 0, d: 1 )')
99
+ end
78
100
  end
79
101
 
80
102
  context 'with single line list of values' do
@@ -155,6 +177,44 @@ describe RuboCop::Cop::Style::TrailingComma, :config do
155
177
  '})'])
156
178
  expect(cop.offenses).to be_empty
157
179
  end
180
+
181
+ it 'auto-corrects unwanted comma in an Array literal' do
182
+ new_source = autocorrect_source(cop, ['VALUES = [',
183
+ ' 1001,',
184
+ ' 2020,',
185
+ ' 3333,',
186
+ ' ]'])
187
+ expect(new_source).to eq(['VALUES = [',
188
+ ' 1001,',
189
+ ' 2020,',
190
+ ' 3333',
191
+ ' ]'].join("\n"))
192
+ end
193
+
194
+ it 'auto-corrects unwanted comma in a Hash literal' do
195
+ new_source = autocorrect_source(cop, ['MAP = { a: 1001,',
196
+ ' b: 2020,',
197
+ ' c: 3333,',
198
+ ' }'])
199
+ expect(new_source).to eq(['MAP = { a: 1001,',
200
+ ' b: 2020,',
201
+ ' c: 3333',
202
+ ' }'].join("\n"))
203
+ end
204
+
205
+ it 'auto-corrects unwanted comma in a method call with hash parameters' \
206
+ ' at the end' do
207
+ new_source = autocorrect_source(cop, ['some_method(',
208
+ ' a,',
209
+ ' b,',
210
+ ' c: 0,',
211
+ ' d: 1,)'])
212
+ expect(new_source).to eq(['some_method(',
213
+ ' a,',
214
+ ' b,',
215
+ ' c: 0,',
216
+ ' d: 1)'].join("\n"))
217
+ end
158
218
  end
159
219
 
160
220
  context 'when EnforcedStyleForMultiline is comma' do
@@ -254,6 +314,19 @@ describe RuboCop::Cop::Style::TrailingComma, :config do
254
314
  expect(cop.offenses).to be_empty
255
315
  end
256
316
 
317
+ # this is a sad parse error
318
+ it 'accepts no trailing comma in a method call with a block' \
319
+ ' paramter at the end' do
320
+ inspect_source(cop, ['some_method(',
321
+ ' a,',
322
+ ' b,',
323
+ ' c: 0,',
324
+ ' d: 1,',
325
+ ' &block',
326
+ ' )'])
327
+ expect(cop.offenses).to be_empty
328
+ end
329
+
257
330
  it 'accepts a multiline word array' do
258
331
  inspect_source(cop, ['ingredients = %w(',
259
332
  ' sausage',
@@ -273,6 +346,45 @@ describe RuboCop::Cop::Style::TrailingComma, :config do
273
346
  '})'])
274
347
  expect(cop.offenses).to be_empty
275
348
  end
349
+
350
+ it 'auto-corrects an Array literal with two of the values on the same' \
351
+ ' line and a trailing comma' do
352
+ new_source = autocorrect_source(cop, ['VALUES = [',
353
+ ' 1001, 2020,',
354
+ ' 3333,',
355
+ ' ]'])
356
+ expect(new_source).to eq(['VALUES = [',
357
+ ' 1001, 2020,',
358
+ ' 3333',
359
+ ' ]'].join("\n"))
360
+ end
361
+
362
+ it 'auto-corrects missing comma in a Hash literal' do
363
+ new_source = autocorrect_source(cop, ['MAP = { a: 1001,',
364
+ ' b: 2020,',
365
+ ' c: 3333',
366
+ '}'])
367
+ expect(new_source).to eq(['MAP = { a: 1001,',
368
+ ' b: 2020,',
369
+ ' c: 3333,',
370
+ '}'].join("\n"))
371
+ end
372
+
373
+ it 'auto-corrects missing comma in a method call with hash parameters' \
374
+ ' at the end' do
375
+ new_source = autocorrect_source(cop, ['some_method(',
376
+ ' a,',
377
+ ' b,',
378
+ ' c: 0,',
379
+ ' d: 1',
380
+ ' )'])
381
+ expect(new_source).to eq(['some_method(',
382
+ ' a,',
383
+ ' b,',
384
+ ' c: 0,',
385
+ ' d: 1,',
386
+ ' )'].join("\n"))
387
+ end
276
388
  end
277
389
  end
278
390
  end
@@ -6,67 +6,122 @@ describe RuboCop::Cop::Style::UnneededPercentQ do
6
6
  subject(:cop) { described_class.new }
7
7
 
8
8
  context 'with %q strings' do
9
+ let(:source) do
10
+ <<-END.strip_indent
11
+ %q('hi') # line 1
12
+ %q("hi")
13
+ %q(hi)
14
+ %q('"hi"')
15
+ %q('hi\\t') # line 5
16
+ END
17
+ end
18
+ let(:corrected) do
19
+ <<-END.strip_indent
20
+ "'hi'" # line 1
21
+ '"hi"'
22
+ 'hi'
23
+ %q('"hi"')
24
+ %q('hi\\t') # line 5
25
+ END
26
+ end
27
+ before { inspect_source(cop, source) }
28
+
9
29
  it 'registers an offense for only single quotes' do
10
- inspect_source(cop, "%q('hi')")
30
+ expect(cop.offenses.map(&:line)).to include(1)
11
31
  expect(cop.messages).to eq(['Use `%q` only for strings that contain ' \
12
- 'both single quotes and double quotes.'])
32
+ 'both single quotes and double quotes.'] * 3)
13
33
  end
14
34
 
15
35
  it 'registers an offense for only double quotes' do
16
- inspect_source(cop, '%q("hi")')
17
- expect(cop.offenses.size).to eq(1)
36
+ expect(cop.offenses.map(&:line)).to include(2)
37
+ end
38
+
39
+ it 'registers an offense for no quotes' do
40
+ expect(cop.offenses.map(&:line)).to include(3)
18
41
  end
19
42
 
20
43
  it 'accepts a string with single quotes and double quotes' do
21
- inspect_source(cop, %Q(%q('"hi"')))
22
- expect(cop.offenses).to be_empty
44
+ expect(cop.offenses.map(&:line)).not_to include(4)
23
45
  end
24
46
 
25
- it 'normally auto-corrects %q to single quotes' do
26
- new_source = autocorrect_source(cop, '%q(hi)')
27
- expect(new_source).to eq("'hi'")
47
+ it 'accepts a string with a tab character' do
48
+ expect(cop.offenses.map(&:line)).not_to include(5)
28
49
  end
29
50
 
30
- it 'auto-corrects %q to double quotes if necessary' do
31
- new_source = autocorrect_source(cop, "%q('hi')")
32
- expect(new_source).to eq(%q("'hi'"))
51
+ it 'auto-corrects' do
52
+ new_source = autocorrect_source(cop, source)
53
+ expect(new_source).to eq(corrected)
33
54
  end
34
55
  end
35
56
 
36
57
  context 'with %Q strings' do
37
- it 'registers an offense for static string with only double quotes' do
38
- inspect_source(cop, '%Q("hi")')
58
+ let(:source) do
59
+ <<-END.strip_indent
60
+ %Q(hi) # line 1
61
+ %Q("hi")
62
+ %Q(hi\#{4})
63
+ %Q('"hi"')
64
+ %Q("\\thi")
65
+ %Q("hi\#{4}")
66
+ /%Q?/ # line 7
67
+ END
68
+ end
69
+ let(:corrected) do
70
+ <<-END.strip_indent
71
+ "hi" # line 1
72
+ '"hi"'
73
+ "hi\#{4}"
74
+ %Q('"hi"')
75
+ %Q("\\thi")
76
+ %Q("hi\#{4}")
77
+ /%Q?/ # line 7
78
+ END
79
+ end
80
+ before { inspect_source(cop, source) }
81
+
82
+ it 'registers an offense for static string without quotes' do
83
+ expect(cop.offenses.map(&:line)).to include(1)
39
84
  expect(cop.messages).to eq(['Use `%Q` only for strings that contain ' \
40
85
  'both single quotes and double quotes, or ' \
41
86
  'for dynamic strings that contain double ' \
42
- 'quotes.'])
87
+ 'quotes.'] * 3)
88
+ end
89
+
90
+ it 'registers an offense for static string with only double quotes' do
91
+ expect(cop.offenses.map(&:line)).to include(2)
92
+ end
93
+
94
+ it 'registers an offense for dynamic string without quotes' do
95
+ expect(cop.offenses.map(&:line)).to include(3)
43
96
  end
44
97
 
45
98
  it 'accepts a string with single quotes and double quotes' do
46
- inspect_source(cop, %Q(%Q('"hi"')))
47
- expect(cop.offenses).to be_empty
99
+ expect(cop.offenses.map(&:line)).not_to include(4)
48
100
  end
49
101
 
50
- it 'accepts a dynamic %Q string with double quotes' do
51
- inspect_source(cop, '%Q("hi#{4}")')
52
- expect(cop.offenses).to be_empty
102
+ it 'accepts a string with double quotes and tab character' do
103
+ expect(cop.offenses.map(&:line)).not_to include(5)
53
104
  end
54
105
 
55
- it 'auto-corrects static %Q to double quotes' do
56
- new_source = autocorrect_source(cop, '%Q(hi)')
57
- # One could argue that the double quotes are not necessary for a static
58
- # string, but that's the job of the StringLiterals cop to check.
59
- expect(new_source).to eq('"hi"')
106
+ it 'accepts a dynamic %Q string with double quotes' do
107
+ expect(cop.offenses.map(&:line)).not_to include(6)
60
108
  end
61
109
 
62
- it 'auto-corrects static %Q with inner double quotes to single quotes' do
63
- new_source = autocorrect_source(cop, '%Q("hi")')
64
- expect(new_source).to eq(%q('"hi"'))
110
+ it 'accepts regular expressions starting with %Q' do
111
+ expect(cop.offenses.map(&:line)).not_to include(7)
65
112
  end
66
113
 
67
- it 'auto-corrects dynamic %Q to double quotes' do
68
- new_source = autocorrect_source(cop, '%Q(hi #{func})')
69
- expect(new_source).to eq('"hi #{func}"')
114
+ it 'auto-corrects' do
115
+ new_source = autocorrect_source(cop, source)
116
+ expect(new_source).to eq(corrected)
70
117
  end
71
118
  end
119
+
120
+ it 'accepts a heredoc string that contains %q' do
121
+ inspect_source(cop, [' s = <<END',
122
+ "%q('hi') # line 1",
123
+ '%q("hi")',
124
+ 'END'])
125
+ expect(cop.offenses).to be_empty
126
+ end
72
127
  end
@@ -5,7 +5,7 @@ require 'spec_helper'
5
5
  describe RuboCop::Cop::Style::VariableInterpolation do
6
6
  subject(:cop) { described_class.new }
7
7
 
8
- it 'registers an offense for interpolated global variables' do
8
+ it 'registers an offense for interpolated global variables in string' do
9
9
  inspect_source(cop,
10
10
  ['puts "this is a #$test"'])
11
11
  expect(cop.offenses.size).to eq(1)
@@ -15,6 +15,26 @@ describe RuboCop::Cop::Style::VariableInterpolation do
15
15
  ' with expression `#{$test}`.'])
16
16
  end
17
17
 
18
+ it 'registers an offense for interpolated global variables in regexp' do
19
+ inspect_source(cop,
20
+ ['puts /this is a #$test/'])
21
+ expect(cop.offenses.size).to eq(1)
22
+ expect(cop.highlights).to eq(['$test'])
23
+ expect(cop.messages)
24
+ .to eq(['Replace interpolated variable `$test`' \
25
+ ' with expression `#{$test}`.'])
26
+ end
27
+
28
+ it 'registers an offense for interpolated global variables in regexp' do
29
+ inspect_source(cop,
30
+ ['puts `this is a #$test`'])
31
+ expect(cop.offenses.size).to eq(1)
32
+ expect(cop.highlights).to eq(['$test'])
33
+ expect(cop.messages)
34
+ .to eq(['Replace interpolated variable `$test`' \
35
+ ' with expression `#{$test}`.'])
36
+ end
37
+
18
38
  it 'registers an offense for interpolated regexp back references' do
19
39
  inspect_source(cop,
20
40
  ['puts "this is a #$1"'])
@@ -72,7 +72,7 @@ describe RuboCop::Cop::Team do
72
72
  end
73
73
 
74
74
  it 'returns offenses from cops' do
75
- expect(cop_names).to include('Style/LineLength')
75
+ expect(cop_names).to include('Metrics/LineLength')
76
76
  end
77
77
  end
78
78
 
@@ -112,30 +112,35 @@ describe RuboCop::Cop::Team do
112
112
 
113
113
  context 'when only some cop classes are passed to .new' do
114
114
  let(:cop_classes) do
115
- [RuboCop::Cop::Lint::Void, RuboCop::Cop::Style::LineLength]
115
+ [RuboCop::Cop::Lint::Void, RuboCop::Cop::Metrics::LineLength]
116
116
  end
117
117
 
118
118
  it 'returns only intances of the classes' do
119
119
  expect(cops.size).to eq(2)
120
120
  cops.sort! { |a, b| a.name <=> b.name }
121
121
  expect(cops[0].name).to eq('Lint/Void')
122
- expect(cops[1].name).to eq('Style/LineLength')
122
+ expect(cops[1].name).to eq('Metrics/LineLength')
123
123
  end
124
124
  end
125
125
 
126
126
  context 'when some classes are disabled with config' do
127
- before do
128
- %w(Lint/Void Style/LineLength).each do |cop_name|
129
- config.for_cop(cop_name)['Enabled'] = false
127
+ let(:disabled_config) do
128
+ %w(
129
+ Lint/Void
130
+ Metrics/LineLength
131
+ ).each_with_object({}) do |cop_name, accum|
132
+ accum[cop_name] = { 'Enabled' => false }
130
133
  end
131
134
  end
132
-
135
+ let(:config) do
136
+ RuboCop::ConfigLoader.merge_with_default(disabled_config, '')
137
+ end
133
138
  let(:cop_names) { cops.map(&:name) }
134
139
 
135
140
  it 'does not return intances of the classes' do
136
141
  expect(cops).not_to be_empty
137
- expect(cop_names).not_to include('Style/Void')
138
- expect(cop_names).not_to include('Style/LineLength')
142
+ expect(cop_names).not_to include('Lint/Void')
143
+ expect(cop_names).not_to include('Metrics/LineLength')
139
144
  end
140
145
  end
141
146
  end