rubocop 0.23.0 → 0.24.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 (115) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/CHANGELOG.md +35 -1
  4. data/CONTRIBUTING.md +1 -1
  5. data/README.md +6 -6
  6. data/config/default.yml +25 -6
  7. data/config/enabled.yml +20 -0
  8. data/lib/rubocop.rb +10 -13
  9. data/lib/rubocop/cli.rb +23 -20
  10. data/lib/rubocop/cop/lint/def_end_alignment.rb +47 -0
  11. data/lib/rubocop/cop/lint/end_alignment.rb +18 -65
  12. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  13. data/lib/rubocop/cop/lint/syntax.rb +28 -4
  14. data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +1 -1
  15. data/lib/rubocop/cop/lint/unused_block_argument.rb +13 -1
  16. data/lib/rubocop/cop/lint/useless_access_modifier.rb +3 -2
  17. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -9
  18. data/lib/rubocop/cop/lint/useless_setter_call.rb +28 -20
  19. data/lib/rubocop/cop/mixin/access_modifier_node.rb +18 -0
  20. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +4 -2
  21. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +42 -0
  22. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +32 -0
  23. data/lib/rubocop/cop/mixin/surrounding_space.rb +7 -8
  24. data/lib/rubocop/cop/mixin/unused_argument.rb +1 -1
  25. data/lib/rubocop/cop/offense.rb +27 -14
  26. data/lib/rubocop/cop/style/access_modifier_indentation.rb +2 -9
  27. data/lib/rubocop/cop/style/attr.rb +3 -1
  28. data/lib/rubocop/cop/style/class_check.rb +42 -0
  29. data/lib/rubocop/cop/style/each_with_object.rb +5 -1
  30. data/lib/rubocop/cop/style/empty_lines.rb +1 -4
  31. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +2 -8
  32. data/lib/rubocop/cop/style/empty_lines_around_body.rb +1 -4
  33. data/lib/rubocop/cop/style/encoding.rb +3 -5
  34. data/lib/rubocop/cop/style/end_of_line.rb +2 -5
  35. data/lib/rubocop/cop/style/file_name.rb +2 -4
  36. data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -1
  37. data/lib/rubocop/cop/style/indentation_consistency.rb +2 -1
  38. data/lib/rubocop/cop/style/indentation_width.rb +25 -5
  39. data/lib/rubocop/cop/style/line_length.rb +59 -5
  40. data/lib/rubocop/cop/style/next.rb +18 -18
  41. data/lib/rubocop/cop/style/numeric_literals.rb +22 -9
  42. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
  43. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  44. data/lib/rubocop/cop/style/semicolon.rb +1 -3
  45. data/lib/rubocop/cop/style/space_after_colon.rb +7 -3
  46. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +3 -1
  47. data/lib/rubocop/cop/style/space_before_comma.rb +16 -0
  48. data/lib/rubocop/cop/style/space_before_semicolon.rb +16 -0
  49. data/lib/rubocop/cop/style/tab.rb +6 -5
  50. data/lib/rubocop/cop/style/trailing_comma.rb +33 -6
  51. data/lib/rubocop/cop/style/trailing_whitespace.rb +4 -3
  52. data/lib/rubocop/cop/style/unneeded_capital_w.rb +6 -0
  53. data/lib/rubocop/cop/style/unneeded_percent_q.rb +45 -0
  54. data/lib/rubocop/cop/style/unneeded_percent_x.rb +2 -3
  55. data/lib/rubocop/cop/style/word_array.rb +1 -1
  56. data/lib/rubocop/cop/team.rb +6 -12
  57. data/lib/rubocop/cop/util.rb +26 -8
  58. data/lib/rubocop/cop/variable_force.rb +3 -6
  59. data/lib/rubocop/cop/variable_force/variable.rb +7 -3
  60. data/lib/rubocop/processed_source.rb +52 -12
  61. data/lib/rubocop/{file_inspector.rb → runner.rb} +50 -59
  62. data/lib/rubocop/version.rb +1 -1
  63. data/relnotes/v0.24.0.md +77 -0
  64. data/rubocop.gemspec +4 -4
  65. data/spec/rubocop/cli_spec.rb +38 -21
  66. data/spec/rubocop/config_loader_spec.rb +7 -6
  67. data/spec/rubocop/config_spec.rb +8 -8
  68. data/spec/rubocop/cop/cop_spec.rb +1 -1
  69. data/spec/rubocop/cop/lint/def_end_alignment_spec.rb +108 -0
  70. data/spec/rubocop/cop/lint/end_alignment_spec.rb +0 -47
  71. data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +6 -7
  72. data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +19 -0
  73. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +8 -18
  74. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +99 -51
  75. data/spec/rubocop/cop/offense_spec.rb +3 -3
  76. data/spec/rubocop/cop/rails/delegate_spec.rb +1 -1
  77. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +12 -0
  78. data/spec/rubocop/cop/style/align_hash_spec.rb +4 -4
  79. data/spec/rubocop/cop/style/align_parameters_spec.rb +1 -1
  80. data/spec/rubocop/cop/style/attr_spec.rb +12 -2
  81. data/spec/rubocop/cop/style/class_check_spec.rb +41 -0
  82. data/spec/rubocop/cop/style/each_with_object_spec.rb +5 -0
  83. data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +5 -0
  84. data/spec/rubocop/cop/style/indentation_width_spec.rb +95 -0
  85. data/spec/rubocop/cop/style/line_length_spec.rb +75 -0
  86. data/spec/rubocop/cop/style/next_spec.rb +28 -0
  87. data/spec/rubocop/cop/style/numeric_literals_spec.rb +10 -0
  88. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +5 -0
  89. data/spec/rubocop/cop/style/space_after_colon_spec.rb +17 -0
  90. data/spec/rubocop/cop/style/space_around_equals_in_parameter_default_spec.rb +11 -0
  91. data/spec/rubocop/cop/style/space_around_operators_spec.rb +1 -0
  92. data/spec/rubocop/cop/style/space_before_comma_spec.rb +42 -0
  93. data/spec/rubocop/cop/style/space_before_semicolon_spec.rb +28 -0
  94. data/spec/rubocop/cop/style/trailing_comma_spec.rb +37 -15
  95. data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +8 -10
  96. data/spec/rubocop/cop/style/unneeded_percent_q_spec.rb +72 -0
  97. data/spec/rubocop/cop/style/word_array_spec.rb +6 -0
  98. data/spec/rubocop/cop/team_spec.rb +8 -8
  99. data/spec/rubocop/cop/util_spec.rb +10 -0
  100. data/spec/rubocop/cop/variable_force/assignment_spec.rb +1 -1
  101. data/spec/rubocop/cop/variable_force/locatable_spec.rb +1 -1
  102. data/spec/rubocop/cop/variable_force/scope_spec.rb +1 -1
  103. data/spec/rubocop/cop/variable_force/variable_spec.rb +4 -4
  104. data/spec/rubocop/formatter/base_formatter_spec.rb +5 -5
  105. data/spec/rubocop/formatter/colorizable_spec.rb +2 -2
  106. data/spec/rubocop/formatter/json_formatter_spec.rb +1 -1
  107. data/spec/rubocop/path_util_spec.rb +15 -15
  108. data/spec/rubocop/processed_source_spec.rb +104 -50
  109. data/spec/rubocop/runner_spec.rb +64 -0
  110. data/spec/spec_helper.rb +8 -10
  111. data/spec/support/shared_examples.rb +22 -0
  112. metadata +39 -15
  113. data/lib/rubocop/source_parser.rb +0 -47
  114. data/spec/rubocop/file_inspector_spec.rb +0 -84
  115. data/spec/rubocop/source_parser_spec.rb +0 -85
@@ -559,6 +559,7 @@ describe RuboCop::CLI, :isolated_environment do
559
559
  expect(cli.run(['--auto-gen-config'])).to eq(1)
560
560
  expect(IO.readlines('.rubocop_todo.yml')[7..-1].map(&:chomp))
561
561
  .to eq(['# Offense count: 1',
562
+ '# Configuration parameters: AllowURI.',
562
563
  'Style/LineLength:',
563
564
  ' Max: 85',
564
565
  '',
@@ -635,6 +636,7 @@ describe RuboCop::CLI, :isolated_environment do
635
636
  ' Enabled: false',
636
637
  '',
637
638
  '# Offense count: 2',
639
+ '# Configuration parameters: AllowURI.',
638
640
  'Style/LineLength:',
639
641
  ' Max: 90',
640
642
  '',
@@ -977,13 +979,13 @@ describe RuboCop::CLI, :isolated_environment do
977
979
  current = types.delete(slice.shift[/Type '(?<c>[^'']+)'/, 'c'])
978
980
  # all cops in their type listing
979
981
  cops.with_type(current).each do |cop|
980
- expect(slice.any? { |l| l.include? cop.cop_name }).to be_true
982
+ expect(slice.any? { |l| l.include? cop.cop_name }).to be_truthy
981
983
  end
982
984
 
983
985
  # no cop in wrong type listing
984
986
  types.each do |type|
985
987
  cops.with_type(type).each do |cop|
986
- expect(slice.any? { |l| l.include? cop.cop_name }).to be_false
988
+ expect(slice.any? { |l| l.include? cop.cop_name }).to be_falsey
987
989
  end
988
990
  end
989
991
  end
@@ -1271,22 +1273,19 @@ describe RuboCop::CLI, :isolated_environment do
1271
1273
  end
1272
1274
  end
1273
1275
 
1274
- describe '#wants_to_quit?' do
1275
- it 'is initially false' do
1276
- expect(cli.wants_to_quit?).to be_false
1277
- end
1278
-
1279
- context 'when true' do
1280
- it 'returns 1' do
1281
- create_file('example.rb', '# encoding: utf-8')
1282
- cli.wants_to_quit = true
1283
- expect(cli.run(['example.rb'])).to eq(1)
1284
- end
1276
+ context 'when interrupted' do
1277
+ it 'returns 1' do
1278
+ allow_any_instance_of(RuboCop::Runner)
1279
+ .to receive(:aborting?).and_return(true)
1280
+ create_file('example.rb', '# encoding: utf-8')
1281
+ expect(cli.run(['example.rb'])).to eq(1)
1285
1282
  end
1286
1283
  end
1287
1284
 
1288
1285
  describe '#trap_interrupt' do
1286
+ let(:runner) { RuboCop::Runner.new({}, RuboCop::ConfigStore.new) }
1289
1287
  let(:interrupt_handlers) { [] }
1288
+
1290
1289
  before do
1291
1290
  allow(Signal).to receive(:trap).with('INT') do |&block|
1292
1291
  interrupt_handlers << block
@@ -1299,30 +1298,29 @@ describe RuboCop::CLI, :isolated_environment do
1299
1298
 
1300
1299
  it 'adds a handler for SIGINT' do
1301
1300
  expect(interrupt_handlers).to be_empty
1302
- cli.trap_interrupt
1301
+ cli.trap_interrupt(runner)
1303
1302
  expect(interrupt_handlers.size).to eq(1)
1304
1303
  end
1305
1304
 
1306
1305
  context 'with SIGINT once' do
1307
- it 'sets #wants_to_quit? to true' do
1308
- cli.trap_interrupt
1309
- expect(cli.wants_to_quit?).to be_false
1306
+ it 'aborts processing' do
1307
+ cli.trap_interrupt(runner)
1308
+ expect(runner).to receive(:abort)
1310
1309
  interrupt
1311
- expect(cli.wants_to_quit?).to be_true
1312
1310
  end
1313
1311
 
1314
1312
  it 'does not exit immediately' do
1313
+ cli.trap_interrupt(runner)
1315
1314
  expect_any_instance_of(Object).not_to receive(:exit)
1316
1315
  expect_any_instance_of(Object).not_to receive(:exit!)
1317
- cli.trap_interrupt
1318
1316
  interrupt
1319
1317
  end
1320
1318
  end
1321
1319
 
1322
1320
  context 'with SIGINT twice' do
1323
1321
  it 'exits immediately' do
1322
+ cli.trap_interrupt(runner)
1324
1323
  expect_any_instance_of(Object).to receive(:exit!).with(1)
1325
- cli.trap_interrupt
1326
1324
  interrupt
1327
1325
  interrupt
1328
1326
  end
@@ -1387,6 +1385,23 @@ describe RuboCop::CLI, :isolated_environment do
1387
1385
  ''].join("\n"))
1388
1386
  end
1389
1387
 
1388
+ context 'when errors are raised while processing files due to bugs' do
1389
+ let(:errors) do
1390
+ ['An error occurred while Encoding cop was inspecting file.rb.']
1391
+ end
1392
+
1393
+ before do
1394
+ allow_any_instance_of(RuboCop::Runner)
1395
+ .to receive(:errors).and_return(errors)
1396
+ end
1397
+
1398
+ it 'displays an error message to stderr' do
1399
+ cli.run([])
1400
+ expect($stderr.string)
1401
+ .to include('1 error occurred:').and include(errors.first)
1402
+ end
1403
+ end
1404
+
1390
1405
  describe 'rubocop:disable comment' do
1391
1406
  it 'can disable all cops in a code section' do
1392
1407
  create_file('example.rb',
@@ -1796,8 +1811,10 @@ describe RuboCop::CLI, :isolated_environment do
1796
1811
  .to eq(['== example1.rb ==',
1797
1812
  'C: 2: 6: %w-literals should be delimited by [ and ]',
1798
1813
  'C: 3: 6: %q-literals should be delimited by ( and )',
1814
+ 'C: 3: 6: Use %q only for strings that contain both single ' \
1815
+ 'quotes and double quotes.',
1799
1816
  '',
1800
- '1 file inspected, 2 offenses detected',
1817
+ '1 file inspected, 3 offenses detected',
1801
1818
  ''].join("\n"))
1802
1819
  end
1803
1820
 
@@ -192,7 +192,8 @@ describe RuboCop::ConfigLoader do
192
192
  'Description' =>
193
193
  default_config['Style/LineLength']['Description'],
194
194
  'Enabled' => true,
195
- 'Max' => 77
195
+ 'Max' => 77,
196
+ 'AllowURI' => true
196
197
  },
197
198
  'Style/MethodLength' => {
198
199
  'Description' =>
@@ -292,7 +293,7 @@ describe RuboCop::ConfigLoader do
292
293
 
293
294
  context 'when no config file exists for the target file' do
294
295
  it 'is disabled' do
295
- expect(config.cop_enabled?('Style/SymbolArray')).to be_false
296
+ expect(config.cop_enabled?('Style/SymbolArray')).to be_falsey
296
297
  end
297
298
  end
298
299
 
@@ -302,7 +303,7 @@ describe RuboCop::ConfigLoader do
302
303
  'Style/LineLength:',
303
304
  ' Max: 80'
304
305
  ])
305
- expect(config.cop_enabled?('Style/SymbolArray')).to be_false
306
+ expect(config.cop_enabled?('Style/SymbolArray')).to be_falsey
306
307
  end
307
308
  end
308
309
 
@@ -312,7 +313,7 @@ describe RuboCop::ConfigLoader do
312
313
  'Style/SymbolArray:',
313
314
  ' Enabled: true'
314
315
  ])
315
- expect(config.cop_enabled?('Style/SymbolArray')).to be_true
316
+ expect(config.cop_enabled?('Style/SymbolArray')).to be_truthy
316
317
  end
317
318
  end
318
319
  end
@@ -323,7 +324,7 @@ describe RuboCop::ConfigLoader do
323
324
  default_config = described_class.default_configuration
324
325
  symbol_name_config =
325
326
  default_config.for_cop('Lint/AssignmentInCondition')
326
- expect(symbol_name_config['AllowSafeAssignment']).to be_true
327
+ expect(symbol_name_config['AllowSafeAssignment']).to be_truthy
327
328
  end
328
329
  end
329
330
  end
@@ -339,7 +340,7 @@ describe RuboCop::ConfigLoader do
339
340
  it 'requires the passed path' do
340
341
  config_path = described_class.configuration_file_for('.')
341
342
  described_class.configuration_from_file(config_path)
342
- expect(defined?(MyClass)).to be_true
343
+ expect(defined?(MyClass)).to be_truthy
343
344
  end
344
345
  end
345
346
  end
@@ -88,14 +88,14 @@ describe RuboCop::Config do
88
88
  context 'when the passed path matches any of patterns to include' do
89
89
  it 'returns true' do
90
90
  file_path = '/home/foo/project/Gemfile'
91
- expect(configuration.file_to_include?(file_path)).to be_true
91
+ expect(configuration.file_to_include?(file_path)).to be_truthy
92
92
  end
93
93
  end
94
94
 
95
95
  context 'when the passed path does not match any of patterns to include' do
96
96
  it 'returns false' do
97
97
  file_path = '/home/foo/project/Gemfile.lock'
98
- expect(configuration.file_to_include?(file_path)).to be_false
98
+ expect(configuration.file_to_include?(file_path)).to be_falsey
99
99
  end
100
100
  end
101
101
  end
@@ -120,22 +120,22 @@ describe RuboCop::Config do
120
120
  context 'when the passed path matches any of patterns to exclude' do
121
121
  it 'returns true' do
122
122
  file_path = "#{Dir.pwd}/log/foo.rb"
123
- expect(configuration.file_to_exclude?(file_path)).to be_true
123
+ expect(configuration.file_to_exclude?(file_path)).to be_truthy
124
124
 
125
- expect(configuration.file_to_exclude?('log/foo.rb')).to be_true
125
+ expect(configuration.file_to_exclude?('log/foo.rb')).to be_truthy
126
126
 
127
- expect(configuration.file_to_exclude?('bar.rb')).to be_true
127
+ expect(configuration.file_to_exclude?('bar.rb')).to be_truthy
128
128
  end
129
129
  end
130
130
 
131
131
  context 'when the passed path does not match any of patterns to exclude' do
132
132
  it 'returns false' do
133
133
  file_path = "#{Dir.pwd}/log_file.rb"
134
- expect(configuration.file_to_exclude?(file_path)).to be_false
134
+ expect(configuration.file_to_exclude?(file_path)).to be_falsey
135
135
 
136
- expect(configuration.file_to_exclude?('app/controller.rb')).to be_false
136
+ expect(configuration.file_to_exclude?('app/controller.rb')).to be_falsey
137
137
 
138
- expect(configuration.file_to_exclude?('baz.rb')).to be_false
138
+ expect(configuration.file_to_exclude?('baz.rb')).to be_falsey
139
139
  end
140
140
  end
141
141
  end
@@ -31,7 +31,7 @@ describe RuboCop::Cop::Cop do
31
31
  end
32
32
 
33
33
  it 'raises an error if the cop name is in more than one namespace' do
34
- pending 'Example needs a cop with same name in two namespaces'
34
+ skip 'Example needs a cop with same name in two namespaces'
35
35
  expect { described_class.qualified_cop_name('ExampleCop', '--only') }
36
36
  .to raise_error(RuboCop::Cop::AmbiguousCopName)
37
37
  end
@@ -0,0 +1,108 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe RuboCop::Cop::Lint::DefEndAlignment, :config do
6
+ subject(:cop) { described_class.new(config) }
7
+ let(:opposite) do
8
+ cop_config['AlignWith'] == 'def' ? 'start_of_line' : 'def'
9
+ end
10
+
11
+ context 'when AlignWith is start_of_line' do
12
+ let(:cop_config) { { 'AlignWith' => 'start_of_line' } }
13
+
14
+ include_examples 'misaligned', '', 'def', 'test', ' end'
15
+ include_examples 'misaligned', '', 'def', 'Test.test', ' end', 'defs'
16
+
17
+ include_examples 'aligned', 'def', 'test', 'end'
18
+ include_examples 'aligned', 'def', 'Test.test', 'end', 'defs'
19
+
20
+ context 'in ruby 2.1 or later' do
21
+ include_examples 'aligned', 'public def', 'test', 'end'
22
+ include_examples 'aligned', 'protected def', 'test', 'end'
23
+ include_examples 'aligned', 'private def', 'test', 'end'
24
+ include_examples 'aligned', 'module_function def', 'test', 'end'
25
+
26
+ include_examples('misaligned', '',
27
+ 'public def', 'test',
28
+ ' end')
29
+ include_examples('misaligned', '',
30
+ 'protected def', 'test',
31
+ ' end')
32
+ include_examples('misaligned', '',
33
+ 'private def', 'test',
34
+ ' end')
35
+ include_examples('misaligned', '',
36
+ 'module_function def', 'test',
37
+ ' end')
38
+ end
39
+
40
+ it 'registers an offense for correct + opposite' do
41
+ inspect_source(cop, ['private def a',
42
+ ' a1',
43
+ 'end',
44
+ '',
45
+ 'private def b',
46
+ ' b1',
47
+ ' end'])
48
+ expect(cop.offenses.size).to eq(1)
49
+ expect(cop.messages.first)
50
+ .to eq('`end` at 7, 8 is not aligned with `private def` at 5, 8')
51
+ expect(cop.highlights.first).to eq('end')
52
+ expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
53
+ end
54
+ end
55
+
56
+ context 'when AlignWith is def' do
57
+ let(:cop_config) { { 'AlignWith' => 'def' } }
58
+
59
+ include_examples 'misaligned', '', 'def', 'test', ' end'
60
+ include_examples 'misaligned', '', 'def', 'Test.test', ' end', 'defs'
61
+
62
+ include_examples 'aligned', 'def', 'test', 'end'
63
+ include_examples 'aligned', 'def', 'Test.test', 'end', 'defs'
64
+
65
+ context 'in ruby 2.1 or later' do
66
+ include_examples('aligned',
67
+ 'public def', 'test',
68
+ ' end')
69
+ include_examples('aligned',
70
+ 'protected def', 'test',
71
+ ' end')
72
+ include_examples('aligned',
73
+ 'private def', 'test',
74
+ ' end')
75
+ include_examples('aligned',
76
+ 'module_function def', 'test',
77
+ ' end')
78
+
79
+ include_examples('misaligned',
80
+ 'public ', 'def', 'test',
81
+ 'end')
82
+ include_examples('misaligned',
83
+ 'protected ', 'def', 'test',
84
+ 'end')
85
+ include_examples('misaligned',
86
+ 'private ', 'def', 'test',
87
+ 'end')
88
+ include_examples('misaligned',
89
+ 'module_function ', 'def', 'test',
90
+ 'end')
91
+
92
+ it 'registers an offense for correct + opposite' do
93
+ inspect_source(cop, ['private def a',
94
+ ' a1',
95
+ 'end',
96
+ '',
97
+ 'private def b',
98
+ ' b1',
99
+ ' end'])
100
+ expect(cop.offenses.size).to eq(1)
101
+ expect(cop.messages.first)
102
+ .to eq('`end` at 3, 0 is not aligned with `def` at 1, 8')
103
+ expect(cop.highlights.first).to eq('end')
104
+ expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
105
+ end
106
+ end
107
+ end
108
+ end
@@ -9,33 +9,8 @@ describe RuboCop::Cop::Lint::EndAlignment, :config do
9
9
  cop_config['AlignWith'] == 'keyword' ? 'variable' : 'keyword'
10
10
  end
11
11
 
12
- shared_examples 'misaligned' do |prefix, alignment_base, arg, end_kw, name|
13
- name ||= alignment_base
14
- it "registers an offense for mismatched #{name} ... end" do
15
- inspect_source(cop, ["#{prefix}#{alignment_base} #{arg}",
16
- end_kw])
17
- expect(cop.offenses.size).to eq(1)
18
- expect(cop.messages.first).to match(
19
- /`end` at 2, \d+ is not aligned with `#{alignment_base}` at 1,/
20
- )
21
- expect(cop.highlights.first).to eq('end')
22
- expect(cop.config_to_allow_offenses).to eq('AlignWith' => opposite)
23
- end
24
- end
25
-
26
- shared_examples 'aligned' do |alignment_base, arg, end_kw, name|
27
- name ||= alignment_base
28
- it "accepts matching #{name} ... end" do
29
- inspect_source(cop, ["#{alignment_base} #{arg}",
30
- end_kw])
31
- expect(cop.offenses).to be_empty
32
- end
33
- end
34
-
35
12
  include_examples 'misaligned', '', 'class', 'Test', ' end'
36
13
  include_examples 'misaligned', '', 'module', 'Test', ' end'
37
- include_examples 'misaligned', '', 'def', 'test', ' end'
38
- include_examples 'misaligned', '', 'def', 'Test.test', ' end', 'defs'
39
14
  include_examples 'misaligned', '', 'if', 'test', ' end'
40
15
  include_examples 'misaligned', '', 'unless', 'test', ' end'
41
16
  include_examples 'misaligned', '', 'while', 'test', ' end'
@@ -43,33 +18,11 @@ describe RuboCop::Cop::Lint::EndAlignment, :config do
43
18
 
44
19
  include_examples 'aligned', 'class', 'Test', 'end'
45
20
  include_examples 'aligned', 'module', 'Test', 'end'
46
- include_examples 'aligned', 'def', 'test', 'end'
47
- include_examples 'aligned', 'def', 'Test.test', 'end', 'defs'
48
21
  include_examples 'aligned', 'if', 'test', 'end'
49
22
  include_examples 'aligned', 'unless', 'test', 'end'
50
23
  include_examples 'aligned', 'while', 'test', 'end'
51
24
  include_examples 'aligned', 'until', 'test', 'end'
52
25
 
53
- context 'in ruby 2.1 or later' do
54
- include_examples 'aligned', 'public def', 'test', 'end'
55
- include_examples 'aligned', 'protected def', 'test', 'end'
56
- include_examples 'aligned', 'private def', 'test', 'end'
57
- include_examples 'aligned', 'module_function def', 'test', 'end'
58
-
59
- include_examples('misaligned', '',
60
- 'public def', 'test',
61
- ' end')
62
- include_examples('misaligned', '',
63
- 'protected def', 'test',
64
- ' end')
65
- include_examples('misaligned', '',
66
- 'private def', 'test',
67
- ' end')
68
- include_examples('misaligned', '',
69
- 'module_function def', 'test',
70
- ' end')
71
- end
72
-
73
26
  it 'can handle ternary if' do
74
27
  inspect_source(cop, 'a = cond ? x : y')
75
28
  expect(cop.offenses).to be_empty
@@ -20,15 +20,14 @@ describe RuboCop::Cop::Lint::InvalidCharacterLiteral do
20
20
  let(:source) { 'p(? )' }
21
21
 
22
22
  it 'registers an offense' do
23
- pending 'Is there a way to emit this warning without syntax errors?' do
23
+ pending 'Is there a way to emit this warning without syntax errors?'
24
24
 
25
- inspect_source(cop, source)
25
+ inspect_source(cop, source)
26
26
 
27
- expect(cop.offenses.size).to eq(1)
28
- expect(cop.offenses.first.message)
29
- .to eq('Invalid character literal; use ?\s')
30
- expect(cop.highlights).to eq([' '])
31
- end
27
+ expect(cop.offenses.size).to eq(1)
28
+ expect(cop.offenses.first.message)
29
+ .to eq('Invalid character literal; use ?\s')
30
+ expect(cop.highlights).to eq([' '])
32
31
  end
33
32
  end
34
33
  end
@@ -72,6 +72,25 @@ describe RuboCop::Cop::Lint::UnusedBlockArgument do
72
72
  end
73
73
  end
74
74
 
75
+ context 'when a block have a block local variable' do
76
+ context 'and the variable is unused' do
77
+ let(:source) { <<-END }
78
+ 1.times do |index; block_local_variable|
79
+ puts index
80
+ end
81
+ END
82
+
83
+ it 'registers an offense' do
84
+ expect(cop.offenses.size).to eq(1)
85
+ expect(cop.offenses.first.message).to eq(
86
+ 'Unused block local variable - `block_local_variable`.'
87
+ )
88
+ expect(cop.offenses.first.line).to eq(1)
89
+ expect(cop.highlights).to eq(['block_local_variable'])
90
+ end
91
+ end
92
+ end
93
+
75
94
  context 'when a lambda block takes arguments' do
76
95
  context 'and all the arguments are unused' do
77
96
  let(:source) { <<-END }