Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #7993] Fix a false positive for Migration/DepartmentName #7995

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
6 changes: 6 additions & 0 deletions manual/configuration.md
Expand Up @@ -629,6 +629,12 @@ Running `rubocop --[safe-]auto-correct --disable-uncorrectable` will
create comments to disable all offenses that can't be automatically
corrected.

Do not write anything other than cop name in the disabling comment. E.g.:

```ruby
# rubocop:disable Layout/LineLength --This is a bad comment that includes other than cop name.
```

## Setting the style guide URL

You can specify the base URL of the style guide using `StyleGuideBaseURL`.
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