Skip to content

Commit

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

This PR fixes the following false positive for `Migration/DepartmentName`
when a disable comment contains a plain comment.

```console
% cat example.rb
# rubocop:disable Style/SafeNavigation # support Ruby < 2.3.0

% rubocop --only Migration/DepartmentName
Inspecting 1 file
C

Offenses:

example.rb:1:50: C: Migration/DepartmentName: Department name is missing.
# rubocop:disable Style/SafeNavigation # support Ruby < 2.3.0
                                                 ^^^^

1 file inspected, 1 offense detected
```

The following is an example disable comment.

```ruby
# rubocop:disable Style/FrozenStringLiteralComment, Style/StringLiterals
```

The token used after `# rubocop: disable` are `A-z`, `/`, or `,`.
Also `A-z` includes `all`.

```ruby
# rubocop:disable all
```

This PR will change the processing of `Migration/DepartmentName` cop to
finish when an invalid token is used.
  • Loading branch information
koic authored and bbatsov committed Jan 4, 2020
1 parent 019fe5e commit 6d0907b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* [#7595](https://github.com/rubocop-hq/rubocop/issues/7595): Make `Style/NumericPredicate` aware of ignored methods when specifying ignored methods. ([@koic][])
* [#7607](https://github.com/rubocop-hq/rubocop/issues/7607): Fix `Style/FrozenStringLiteralComment` infinite loop when magic comments are newline-separated. ([@pirj][])
* [#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][])

### Changes

Expand Down
12 changes: 12 additions & 0 deletions lib/rubocop/cop/migration/department_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ class DepartmentName < Cop

MSG = 'Department name is missing.'

# 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) +)(.*)/

offset = Regexp.last_match(1).length
Regexp.last_match(4).scan(%r{[\w/]+|\W+}) do |name|
break unless valid_content_token?(name.strip)

check_cop_name(name, comment, offset)

offset += name.length
end
end
Expand All @@ -38,6 +46,10 @@ def check_cop_name(name, comment, offset)
range = range_between(start, start + name.length)
add_offense(range, location: range)
end

def valid_content_token?(content_token)
!DISABLING_COPS_CONTENT_TOKEN.match(content_token).nil?
end
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/cop/migration/department_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@
RUBY
end
end

context 'when a disable comment contains a plain comment' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
# rubocop:disable Style/Alias # Plain code comment
alias :ala :bala
RUBY
end
end
end

0 comments on commit 6d0907b

Please sign in to comment.