Skip to content

Commit

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

This PR fixes a false positive for `Migration/DepartmentName` cop
when a disable comment contains an unexpected character for department name.
  • Loading branch information
koic committed May 20, 2020
1 parent 3651d6b commit 179bb78
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@
* [#7972](https://github.com/rubocop-hq/rubocop/issues/7972): Fix an incorrect autocrrect for `Style/HashSyntax` when using a return value uses `return`. ([@koic][])
* [#7886](https://github.com/rubocop-hq/rubocop/issues/7886): Fix a bug in `AllowComments` logic in `Lint/SuppressedException`. ([@jonas054][])
* [#7991](https://github.com/rubocop-hq/rubocop/issues/7991): Fix an error for `Layout/EmptyLinesAroundAttributeAccessor` when attribute method is method chained. ([@koic][])
* [#7993](https://github.com/rubocop-hq/rubocop/issues/7993): Fix a false positive for `Migration/DepartmentName` when a disable comment contains an unexpected character for department name. ([@koic][])

### Changes

Expand Down
8 changes: 4 additions & 4 deletions lib/rubocop/cop/migration/department_name.rb
Expand Up @@ -27,10 +27,10 @@ def investigate(processed_source)
Regexp.last_match(4).scan(/[^,]+|[\W]+/) do |name|
trimmed_name = name.strip

break if contain_plain_comment?(trimmed_name)

check_cop_name(trimmed_name, comment, offset) unless valid_content_token?(trimmed_name)

break if contain_unexpected_character_for_department_name?(name)

offset += name.length
end
end
Expand Down Expand Up @@ -64,8 +64,8 @@ def valid_content_token?(content_token)
!DISABLING_COPS_CONTENT_TOKEN.match(content_token).nil?
end

def contain_plain_comment?(name)
name == '#'
def contain_unexpected_character_for_department_name?(name)
name.match?(%r{[^A-z/, ]})
end

def qualified_legacy_cop_name(cop_name)
Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/cop/migration/department_name_spec.rb
Expand Up @@ -90,6 +90,15 @@
end
end

context 'when a disable comment contains an unexpected character for department name' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
# rubocop:disable Style/Alias -- because something, something, and something
alias :ala :bala
RUBY
end
end

# `Migration/DepartmentName` cop's role is to complement a department name.
# The role would be simple if another feature could detect unexpected
# disable comment format.
Expand Down

0 comments on commit 179bb78

Please sign in to comment.