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
@@ -363,16 +363,7 @@ describe RuboCop::Cop::Lint::UselessAssignment do
363
363
  ]
364
364
  end
365
365
 
366
- it 'registers an offense for the declaration' do
367
- inspect_source(cop, source)
368
- expect(cop.offenses.size).to eq(1)
369
- expect(cop.offenses.first.message)
370
- .to eq('Useless assignment to variable - `foo`.')
371
- expect(cop.offenses.first.line).to eq(1)
372
- expect(cop.highlights).to eq(['foo'])
373
- end
374
-
375
- include_examples 'mimics MRI 2.1'
366
+ include_examples 'accepts'
376
367
  end
377
368
 
378
369
  context 'when a block local variable is assigned and unreferenced' do
@@ -498,14 +489,13 @@ describe RuboCop::Cop::Lint::UselessAssignment do
498
489
 
499
490
  it 'registers an offense' do
500
491
  pending 'Requires an advanced logic that checks whether the return ' \
501
- 'value of an operator assignment is used or not.' do
502
- inspect_source(cop, source)
503
- expect(cop.offenses.size).to eq(1)
504
- expect(cop.offenses.first.message)
505
- .to eq('Useless assignment to variable - `foo`.')
506
- expect(cop.offenses.first.line).to eq(7)
507
- expect(cop.highlights).to eq(['foo'])
508
- end
492
+ 'value of an operator assignment is used or not.'
493
+ inspect_source(cop, source)
494
+ expect(cop.offenses.size).to eq(1)
495
+ expect(cop.offenses.first.message)
496
+ .to eq('Useless assignment to variable - `foo`.')
497
+ expect(cop.offenses.first.line).to eq(7)
498
+ expect(cop.highlights).to eq(['foo'])
509
499
  end
510
500
  end
511
501
 
@@ -5,61 +5,85 @@ require 'spec_helper'
5
5
  describe RuboCop::Cop::Lint::UselessSetterCall do
6
6
  subject(:cop) { described_class.new }
7
7
 
8
- it 'registers an offense for def ending with lvar attr assignment' do
9
- inspect_source(cop,
10
- ['def test',
11
- ' top = Top.new',
12
- ' top.attr = 5',
13
- 'end'
14
- ])
15
- expect(cop.offenses.size).to eq(1)
16
- expect(cop.messages)
17
- .to eq(['Useless setter call to local variable `top`.'])
8
+ context 'with method ending with setter call on local object' do
9
+ it 'registers an offense' do
10
+ inspect_source(cop,
11
+ ['def test',
12
+ ' top = Top.new',
13
+ ' top.attr = 5',
14
+ 'end'
15
+ ])
16
+ expect(cop.offenses.size).to eq(1)
17
+ expect(cop.messages)
18
+ .to eq(['Useless setter call to local variable `top`.'])
19
+ end
18
20
  end
19
21
 
20
- it 'registers an offense for defs ending with lvar attr assignment' do
21
- inspect_source(cop,
22
- ['def Top.test',
23
- ' top = Top.new',
24
- ' top.attr = 5',
25
- 'end'
26
- ])
27
- expect(cop.offenses.size).to eq(1)
22
+ context 'with singleton method ending with setter call on local object' do
23
+ it 'registers an offense' do
24
+ inspect_source(cop,
25
+ ['def Top.test',
26
+ ' top = Top.new',
27
+ ' top.attr = 5',
28
+ 'end'
29
+ ])
30
+ expect(cop.offenses.size).to eq(1)
31
+ end
28
32
  end
29
33
 
30
- it 'accepts def ending with ivar assignment' do
31
- inspect_source(cop,
32
- ['def test',
33
- ' something',
34
- ' @top = 5',
35
- 'end'
36
- ])
37
- expect(cop.offenses).to be_empty
34
+ context 'with method ending with square bracket setter on local object' do
35
+ it 'registers an offense' do
36
+ inspect_source(cop,
37
+ ['def test',
38
+ ' top = Top.new',
39
+ ' top[:attr] = 5',
40
+ 'end'
41
+ ])
42
+ expect(cop.offenses.size).to eq(1)
43
+ expect(cop.messages)
44
+ .to eq(['Useless setter call to local variable `top`.'])
45
+ end
38
46
  end
39
47
 
40
- it 'accepts def ending ivar attr assignment' do
41
- inspect_source(cop,
42
- ['def test',
43
- ' something',
44
- ' @top.attr = 5',
45
- 'end'
46
- ])
47
- expect(cop.offenses).to be_empty
48
+ context 'with method ending with ivar assignment' do
49
+ it 'accepts' do
50
+ inspect_source(cop,
51
+ ['def test',
52
+ ' something',
53
+ ' @top = 5',
54
+ 'end'
55
+ ])
56
+ expect(cop.offenses).to be_empty
57
+ end
48
58
  end
49
59
 
50
- it 'accepts def ending with argument attr assignment' do
51
- inspect_source(cop,
52
- ['def test(some_arg)',
53
- ' unrelated_local_variable = Top.new',
54
- ' some_arg.attr = 5',
55
- 'end'
56
- ])
57
- expect(cop.offenses).to be_empty
60
+ context 'with method ending with setter call on ivar' do
61
+ it 'accepts' do
62
+ inspect_source(cop,
63
+ ['def test',
64
+ ' something',
65
+ ' @top.attr = 5',
66
+ 'end'
67
+ ])
68
+ expect(cop.offenses).to be_empty
69
+ end
70
+ end
71
+
72
+ context 'with method ending with setter call on argument' do
73
+ it 'accepts' do
74
+ inspect_source(cop,
75
+ ['def test(some_arg)',
76
+ ' unrelated_local_variable = Top.new',
77
+ ' some_arg.attr = 5',
78
+ 'end'
79
+ ])
80
+ expect(cop.offenses).to be_empty
81
+ end
58
82
  end
59
83
 
60
- context 'when a lvar has an object passed as argument ' \
84
+ context 'when a lvar contains an object passed as argument ' \
61
85
  'at the end of the method' do
62
- it 'accepts the lvar attr assignment' do
86
+ it 'accepts the setter call on the lvar' do
63
87
  inspect_source(cop,
64
88
  ['def test(some_arg)',
65
89
  ' @some_ivar = some_arg',
@@ -73,9 +97,9 @@ describe RuboCop::Cop::Lint::UselessSetterCall do
73
97
  end
74
98
  end
75
99
 
76
- context 'when a lvar has an object passed as argument ' \
100
+ context 'when a lvar contains an object passed as argument ' \
77
101
  'by multiple-assignment at the end of the method' do
78
- it 'accepts the lvar attr assignment' do
102
+ it 'accepts the setter call on the lvar' do
79
103
  inspect_source(cop,
80
104
  ['def test(some_arg)',
81
105
  ' _first, some_lvar, _third = 1, some_arg, 3',
@@ -86,7 +110,7 @@ describe RuboCop::Cop::Lint::UselessSetterCall do
86
110
  end
87
111
  end
88
112
 
89
- context 'when a lvar does not have any object passed as argument ' \
113
+ context 'when a lvar does not contain any object passed as argument ' \
90
114
  'with multiple-assignment at the end of the method' do
91
115
  it 'registers an offense' do
92
116
  inspect_source(cop,
@@ -99,9 +123,9 @@ describe RuboCop::Cop::Lint::UselessSetterCall do
99
123
  end
100
124
  end
101
125
 
102
- context 'when a lvar possibly has an object passed as argument ' \
126
+ context 'when a lvar possibly contains an object passed as argument ' \
103
127
  'by logical-operator-assignment at the end of the method' do
104
- it 'accepts the lvar attr assignment' do
128
+ it 'accepts the setter call on the lvar' do
105
129
  inspect_source(cop,
106
130
  ['def test(some_arg)',
107
131
  ' some_lvar = nil',
@@ -113,7 +137,7 @@ describe RuboCop::Cop::Lint::UselessSetterCall do
113
137
  end
114
138
  end
115
139
 
116
- context 'when a lvar does not have any object passed as argument ' \
140
+ context 'when a lvar does not contain any object passed as argument ' \
117
141
  'by binary-operator-assignment at the end of the method' do
118
142
  it 'registers an offense' do
119
143
  inspect_source(cop,
@@ -129,7 +153,7 @@ describe RuboCop::Cop::Lint::UselessSetterCall do
129
153
 
130
154
  context 'when a lvar declared as an argument ' \
131
155
  'is no longer the passed object at the end of the method' do
132
- it 'registers an offense for the lvar attr assignment' do
156
+ it 'registers an offense for the setter call on the lvar' do
133
157
  inspect_source(cop,
134
158
  ['def test(some_arg)',
135
159
  ' some_arg = Top.new',
@@ -140,6 +164,30 @@ describe RuboCop::Cop::Lint::UselessSetterCall do
140
164
  end
141
165
  end
142
166
 
167
+ context 'when a lvar contains a local object instanciated with literal' do
168
+ it 'registers an offense for the setter call on the lvar' do
169
+ inspect_source(cop,
170
+ ['def test',
171
+ ' some_arg = {}',
172
+ ' some_arg[:attr] = 1',
173
+ 'end'
174
+ ])
175
+ expect(cop.offenses.size).to eq(1)
176
+ end
177
+ end
178
+
179
+ context 'when a lvar contains a non-local object returned by a method' do
180
+ it 'accepts' do
181
+ inspect_source(cop,
182
+ ['def test',
183
+ ' some_lvar = Foo.shared_object',
184
+ ' some_lvar[:attr] = 1',
185
+ 'end'
186
+ ])
187
+ expect(cop.offenses).to be_empty
188
+ end
189
+ end
190
+
143
191
  it 'is not confused by operators ending with =' do
144
192
  inspect_source(cop,
145
193
  ['def test',
@@ -17,7 +17,7 @@ describe RuboCop::Cop::Offense do
17
17
  expect(offense.line).to eq(1)
18
18
  expect(offense.message).to eq('message')
19
19
  expect(offense.cop_name).to eq('CopName')
20
- expect(offense.corrected?).to be_true
20
+ expect(offense.corrected?).to be_truthy
21
21
  end
22
22
 
23
23
  it 'overrides #to_s' do
@@ -35,14 +35,14 @@ describe RuboCop::Cop::Offense do
35
35
  o1 = described_class.new(:convention, location, 'message', 'CopName')
36
36
  o2 = described_class.new(:convention, location, 'message', 'CopName')
37
37
 
38
- expect(o1 == o2).to be_true
38
+ expect(o1 == o2).to be_truthy
39
39
  end
40
40
 
41
41
  it 'is frozen' do
42
42
  expect(offense).to be_frozen
43
43
  end
44
44
 
45
- [:severity, :location, :line, :column, :message, :cop_name].each do |a|
45
+ [:severity, :location, :message, :cop_name].each do |a|
46
46
  describe "##{a}" do
47
47
  it 'is frozen' do
48
48
  expect(offense.send(a)).to be_frozen
@@ -113,7 +113,7 @@ describe RuboCop::Cop::Rails::Delegate do
113
113
  ' end',
114
114
  ' ',
115
115
  ' protected',
116
- ' ' ,
116
+ ' ',
117
117
  ' def fox',
118
118
  ' bar.fox',
119
119
  ' end'])
@@ -95,6 +95,18 @@ describe RuboCop::Cop::Style::AccessModifierIndentation, :config do
95
95
  .to eq(['Indent access modifiers like `private`.'])
96
96
  end
97
97
 
98
+ it 'accepts misaligned private in blocks that are not recognized as ' \
99
+ 'class/module definitions' do
100
+ inspect_source(cop,
101
+ ['Test = func do',
102
+ '',
103
+ 'private',
104
+ '',
105
+ ' def test; end',
106
+ 'end'])
107
+ expect(cop.offenses).to be_empty
108
+ end
109
+
98
110
  it 'registers an offense for misaligned private in module ' \
99
111
  'defined with Module.new' do
100
112
  inspect_source(cop,
@@ -25,13 +25,13 @@ describe RuboCop::Cop::Style::AlignHash, :config do
25
25
  }
26
26
  end
27
27
 
28
- it 'registers offence for misaligned keys in implicit hash' do
28
+ it 'registers offense for misaligned keys in implicit hash' do
29
29
  inspect_source(cop, ['func(a: 0,',
30
30
  ' b: 1)'])
31
31
  expect(cop.offenses.size).to eq(1)
32
32
  end
33
33
 
34
- it 'registers offence for misaligned keys in explicit hash' do
34
+ it 'registers offense for misaligned keys in explicit hash' do
35
35
  inspect_source(cop, ['func({a: 0,',
36
36
  ' b: 1})'])
37
37
  expect(cop.offenses.size).to eq(1)
@@ -71,7 +71,7 @@ describe RuboCop::Cop::Style::AlignHash, :config do
71
71
  expect(cop.offenses).to be_empty
72
72
  end
73
73
 
74
- it 'registers offence for misaligned keys in explicit hash' do
74
+ it 'registers offense for misaligned keys in explicit hash' do
75
75
  inspect_source(cop, ['func({a: 0,',
76
76
  ' b: 1})'])
77
77
  expect(cop.offenses.size).to eq(1)
@@ -85,7 +85,7 @@ describe RuboCop::Cop::Style::AlignHash, :config do
85
85
  }
86
86
  end
87
87
 
88
- it 'registers offence for misaligned keys in implicit hash' do
88
+ it 'registers offense for misaligned keys in implicit hash' do
89
89
  inspect_source(cop, ['func(a: 0,',
90
90
  ' b: 1)'])
91
91
  expect(cop.offenses.size).to eq(1)
@@ -322,7 +322,7 @@ describe RuboCop::Cop::Style::AlignParameters, :config do
322
322
  expect(cop.offenses).to be_empty
323
323
  end
324
324
 
325
- it 'registers offences for double indentation from relevant method' do
325
+ it 'registers offenses for double indentation from relevant method' do
326
326
  inspect_source(cop, [' something',
327
327
  ' .method_name(',
328
328
  ' a,',
@@ -12,8 +12,18 @@ describe RuboCop::Cop::Style::Attr do
12
12
  expect(cop.offenses.size).to eq(1)
13
13
  end
14
14
 
15
+ it 'accepts attr when it does not take arguments' do
16
+ inspect_source(cop, 'func(attr)')
17
+ expect(cop.offenses).to be_empty
18
+ end
19
+
20
+ it 'accepts attr when it has a receiver' do
21
+ inspect_source(cop, 'x.attr arg')
22
+ expect(cop.offenses).to be_empty
23
+ end
24
+
15
25
  it 'auto-corrects attr to attr_reader' do
16
- new_source = autocorrect_source(cop, 'attr')
17
- expect(new_source).to eq('attr_reader')
26
+ new_source = autocorrect_source(cop, 'attr :name')
27
+ expect(new_source).to eq('attr_reader :name')
18
28
  end
19
29
  end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe RuboCop::Cop::Style::ClassCheck, :config do
6
+ subject(:cop) { described_class.new(config) }
7
+
8
+ context 'when enforced style is is_a?' do
9
+ let(:cop_config) { { 'EnforcedStyle' => 'is_a?' } }
10
+
11
+ it 'registers an offense for kind_of?' do
12
+ inspect_source(cop, 'x.kind_of? y')
13
+ expect(cop.offenses.size).to eq(1)
14
+ expect(cop.highlights).to eq(['kind_of?'])
15
+ expect(cop.messages)
16
+ .to eq(['Prefer `Object#is_a?` over `Object#kind_of?`.'])
17
+ end
18
+
19
+ it 'auto-corrects kind_of? to is_a?' do
20
+ corrected = autocorrect_source(cop, ['x.kind_of? y'])
21
+ expect(corrected).to eq 'x.is_a? y'
22
+ end
23
+ end
24
+
25
+ context 'when enforced style is kind_of?' do
26
+ let(:cop_config) { { 'EnforcedStyle' => 'kind_of?' } }
27
+
28
+ it 'registers an offense for is_a?' do
29
+ inspect_source(cop, 'x.is_a? y')
30
+ expect(cop.offenses.size).to eq(1)
31
+ expect(cop.highlights).to eq(['is_a?'])
32
+ expect(cop.messages)
33
+ .to eq(['Prefer `Object#kind_of?` over `Object#is_a?`.'])
34
+ end
35
+
36
+ it 'auto-corrects is_a? to kind_of?' do
37
+ corrected = autocorrect_source(cop, ['x.is_a? y'])
38
+ expect(corrected).to eq 'x.kind_of? y'
39
+ end
40
+ end
41
+ end
@@ -65,4 +65,9 @@ describe RuboCop::Cop::Style::EachWithObject do
65
65
  inspect_source(cop, ['[].inject(:+)', '[].reduce(:+)'])
66
66
  expect(cop.offenses).to be_empty
67
67
  end
68
+
69
+ it 'does not blow up for reduce with no arguments' do
70
+ inspect_source(cop, ['[1, 2, 3].inject { |a, e| a + e }'])
71
+ expect(cop.offenses).to be_empty
72
+ end
68
73
  end
@@ -11,6 +11,11 @@ describe RuboCop::Cop::Style::IfWithSemicolon do
11
11
  ['Never use if x; Use the ternary operator instead.'])
12
12
  end
13
13
 
14
+ it 'accepts one line if/then/end' do
15
+ inspect_source(cop, ['if cond then run else dont end'])
16
+ expect(cop.messages).to be_empty
17
+ end
18
+
14
19
  it 'can handle modifier conditionals' do
15
20
  inspect_source(cop, ['class Hash',
16
21
  'end if RUBY_VERSION < "1.8.7"'])
@@ -149,6 +149,47 @@ describe RuboCop::Cop::Style::IndentationWidth do
149
149
  ' end',
150
150
  'end'].join("\n")
151
151
  end
152
+
153
+ it 'indents parenthesized expressions' do
154
+ src = ['var1 = nil',
155
+ 'array_list = []',
156
+ 'if var1.attr1 != 0 || array_list.select{ |w|',
157
+ ' (w.attr2 == var1.attr2)',
158
+ ' }.blank?',
159
+ ' array_list << var1',
160
+ 'end']
161
+ corrected = autocorrect_source(cop, src)
162
+ expect(corrected)
163
+ .to eq ['var1 = nil',
164
+ 'array_list = []',
165
+ 'if var1.attr1 != 0 || array_list.select{ |w|',
166
+ ' (w.attr2 == var1.attr2)',
167
+ ' }.blank?',
168
+ ' array_list << var1',
169
+ 'end'].join("\n")
170
+ end
171
+
172
+ it 'leaves rescue ; end unchanged' do
173
+ src = ['if variable',
174
+ ' begin',
175
+ ' do_something',
176
+ ' rescue ; end # consume any exception',
177
+ 'end']
178
+ corrected = autocorrect_source(cop, src)
179
+ expect(corrected).to eq src.join("\n")
180
+ end
181
+
182
+ it 'leaves block unchanged if block end is not on its own line' do
183
+ src = ['a_function {',
184
+ ' # a comment',
185
+ ' result = AObject.find_by_attr(attr) if attr',
186
+ ' result || AObject.make(',
187
+ ' :attr => attr,',
188
+ ' :attr2 => Other.get_value(),',
189
+ ' :attr3 => Another.get_value()) }']
190
+ corrected = autocorrect_source(cop, src)
191
+ expect(corrected).to eq src.join("\n")
192
+ end
152
193
  end
153
194
 
154
195
  it 'accepts a one line if statement' do
@@ -505,6 +546,20 @@ describe RuboCop::Cop::Style::IndentationWidth do
505
546
  expect(cop.offenses).to be_empty
506
547
  end
507
548
 
549
+ it 'accepts aligned values in when clause' do
550
+ inspect_source(cop,
551
+ ['case superclass',
552
+ 'when /\A(#{NAMESPACEMATCH})(?:\s|\Z)/,',
553
+ ' /\A(Struct|OStruct)\.new/,',
554
+ ' /\ADelegateClass\((.+?)\)\s*\Z/,',
555
+ ' /\A(#{NAMESPACEMATCH})\(/',
556
+ ' $1',
557
+ 'when "self"',
558
+ ' namespace.path',
559
+ 'end'])
560
+ expect(cop.offenses).to be_empty
561
+ end
562
+
508
563
  it 'accepts case/when/else laid out as a table' do
509
564
  inspect_source(cop,
510
565
  ['case sexp.loc.keyword.source',
@@ -703,6 +758,46 @@ describe RuboCop::Cop::Style::IndentationWidth do
703
758
  end
704
759
  end
705
760
 
761
+ context 'with begin/rescue/else/ensure/end' do
762
+ it 'registers an offense for bad indentation of bodies' do
763
+ inspect_source(cop,
764
+ ['def my_func',
765
+ " puts 'do something outside block'",
766
+ ' begin',
767
+ " puts 'do something error prone'",
768
+ ' rescue SomeException, SomeOther => e',
769
+ " puts 'wrongly intended error handling'",
770
+ ' rescue',
771
+ " puts 'wrongly intended error handling'",
772
+ ' else',
773
+ " puts 'wrongly intended normal case handling'",
774
+ ' ensure',
775
+ " puts 'wrongly intended common handling'",
776
+ ' end',
777
+ 'end'])
778
+ expect(cop.messages).to eq(['Use 2 (not 0) spaces for indentation.',
779
+ 'Use 2 (not 1) spaces for indentation.',
780
+ 'Use 2 (not 1) spaces for indentation.',
781
+ 'Use 2 (not 3) spaces for indentation.',
782
+ 'Use 2 (not 4) spaces for indentation.'])
783
+ end
784
+ end
785
+
786
+ context 'with def/rescue/end' do
787
+ it 'registers an offense for bad indentation of bodies' do
788
+ inspect_source(cop,
789
+ ['def my_func',
790
+ " puts 'do something error prone'",
791
+ 'rescue SomeException',
792
+ " puts 'wrongly intended error handling'",
793
+ 'rescue',
794
+ " puts 'wrongly intended error handling'",
795
+ 'end'])
796
+ expect(cop.messages).to eq(['Use 2 (not 1) spaces for indentation.',
797
+ 'Use 2 (not 1) spaces for indentation.'])
798
+ end
799
+ end
800
+
706
801
  context 'with block' do
707
802
  it 'registers an offense for bad indentation of a do/end body' do
708
803
  inspect_source(cop,