Skip to content

Commit

Permalink
[Fix #7627] Fix a false negative for Migration/DepartmentName
Browse files Browse the repository at this point in the history
Fixes #7627.

This PR fixes a false negative for `Migration/DepartmentName`
when there is space around `:`.

For example, `# rubocop : disable` is a valid syntax for
a disable comment.
  • Loading branch information
koic authored and bbatsov committed Jan 6, 2020
1 parent 3e1bf9d commit cc43348
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [#7602](https://github.com/rubocop-hq/rubocop/pull/7602): Ensure proper handling of Ruby 2.7 syntax. ([@drenmi][])
* [#7620](https://github.com/rubocop-hq/rubocop/issues/7620): Fix a false positive for `Migration/DepartmentName` when a disable comment contains a plain comment. ([@koic][])
* [#7616](https://github.com/rubocop-hq/rubocop/issues/7616): Fix an incorrect autocorrect for `Style/MultilineWhenThen` for when statement with then is an array or a hash. ([@koic][])
* [#7627](https://github.com/rubocop-hq/rubocop/issues/7627): Fix a false negative for `Migration/DepartmentName` when there is space around `:` (e.g. `# rubocop : disable`). ([@koic][])

### Changes

Expand Down
5 changes: 4 additions & 1 deletion lib/rubocop/cop/migration/department_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ class DepartmentName < Cop

MSG = 'Department name is missing.'

DISABLE_COMMENT_FORMAT =
/\A(# *rubocop *: *((dis|en)able|todo) +)(.*)/.freeze

# The token that makes up a disable comment.
# The token used after `# rubocop: disable` are `A-z`, `/`, and `,`.
# Also `A-z` includes `all`.
DISABLING_COPS_CONTENT_TOKEN = %r{[A-z/,]+}.freeze

def investigate(processed_source)
processed_source.each_comment do |comment|
next if comment.text !~ /\A(# *rubocop:((dis|en)able|todo) +)(.*)/
next if comment.text !~ DISABLE_COMMENT_FORMAT

offset = Regexp.last_match(1).length
Regexp.last_match(4).scan(%r{[\w/]+|\W+}) do |name|
Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cop/migration/department_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@
# rubocop:enable Style/Alias, Layout/LineLength
RUBY
end

it 'registers offenses and corrects when there is space around `:`' do
expect do
expect_offense(<<~RUBY, 'file.rb')
# rubocop : todo Alias, LineLength
^^^^^^^^^^ Department name is missing.
^^^^^ Department name is missing.
alias :ala :bala
# rubocop : enable Alias, LineLength
^^^^^^^^^^ Department name is missing.
^^^^^ Department name is missing.
RUBY
end.to output(warning).to_stderr

expect_correction(<<~RUBY)
# rubocop : todo Style/Alias, Layout/LineLength
alias :ala :bala
# rubocop : enable Style/Alias, Layout/LineLength
RUBY
end
end

context 'when a disable comment has cop names with departments' do
Expand Down

0 comments on commit cc43348

Please sign in to comment.