Skip to content

Commit eb42226

Browse files
koicbbatsov
authored andcommitted
Fix rake new_cop problem that doesn't add require line (#4722)
1 parent 04087f9 commit eb42226

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
* [#4643](https://github.com/bbatsov/rubocop/issues/4643): Modify `Style/InverseMethods` to not register a separate offense for an inverse method nested inside of the block of an inverse method offense. ([@rrosenblum][])
6868
* [#4593](https://github.com/bbatsov/rubocop/issues/4593): Fix false positive in `Rails/SaveBang` when `save/update_attribute` is used with a `case` statement. ([@theRealNG][])
6969
* [#4322](https://github.com/bbatsov/rubocop/issues/4322): Fix Style/MultilineMemoization from autocorrecting to invalid ruby. ([@dpostorivo][])
70+
* [#4722](https://github.com/bbatsov/rubocop/pull/4722): Fix `rake new_cop` problem that doesn't add `require` line. ([@koic][])
7071

7172
### Changes
7273

lib/rubocop/cop/generator.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,19 @@ def updated_directives
204204

205205
def target_line
206206
@target_line ||= begin
207+
in_the_same_department = false
207208
inject_parts = require_path_fragments(injectable_require_directive)
208209

209210
require_entries.find.with_index do |entry, index|
210211
current_entry_parts = require_path_fragments(entry)
211212

212-
next unless inject_parts[0..-2] == current_entry_parts[0..-2]
213+
if inject_parts[0..-2] == current_entry_parts[0..-2]
214+
in_the_same_department = true
213215

214-
break index if inject_parts.last < current_entry_parts.last
216+
break index if inject_parts.last < current_entry_parts.last
217+
elsif in_the_same_department
218+
break index
219+
end
215220
end
216221
end
217222
end

spec/rubocop/cop/generator_spec.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,65 @@ def on_send(node)
194194
end
195195
end
196196

197+
context 'when a cop of style department already exists' do
198+
let(:cop_identifier) { 'Style/TheEndOfStyle' }
199+
200+
before do
201+
allow(File)
202+
.to receive(:readlines).with('lib/rubocop.rb')
203+
.and_return(<<-RUBY.strip_indent.lines)
204+
# frozen_string_literal: true
205+
206+
require 'parser'
207+
require 'rainbow'
208+
209+
require 'English'
210+
require 'set'
211+
require 'forwardable'
212+
213+
require 'rubocop/version'
214+
215+
require 'rubocop/cop/style/end_block'
216+
require 'rubocop/cop/style/even_odd'
217+
require 'rubocop/cop/style/file_name'
218+
require 'rubocop/cop/style/flip_flop'
219+
220+
require 'rubocop/cop/rails/action_filter'
221+
222+
require 'rubocop/cop/team'
223+
RUBY
224+
end
225+
226+
it 'injects a `require` statement on the end of style department' do
227+
generated_source = <<-RUBY.strip_indent
228+
# frozen_string_literal: true
229+
230+
require 'parser'
231+
require 'rainbow'
232+
233+
require 'English'
234+
require 'set'
235+
require 'forwardable'
236+
237+
require 'rubocop/version'
238+
239+
require 'rubocop/cop/style/end_block'
240+
require 'rubocop/cop/style/even_odd'
241+
require 'rubocop/cop/style/file_name'
242+
require 'rubocop/cop/style/flip_flop'
243+
require 'rubocop/cop/style/the_end_of_style'
244+
245+
require 'rubocop/cop/rails/action_filter'
246+
247+
require 'rubocop/cop/team'
248+
RUBY
249+
250+
generator.inject_require
251+
expect(File)
252+
.to have_received(:write).with('lib/rubocop.rb', generated_source)
253+
end
254+
end
255+
197256
context 'when a `require` entry already exists' do
198257
before do
199258
allow(File)

0 commit comments

Comments
 (0)