From 179bb78abc97ca3f91c83ce99ca94e8f4a0d5dde Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 19 May 2020 23:27:58 +0900 Subject: [PATCH] [Fix #7993] Fix a false positive for `Migration/DepartmentName` Fixes #7993. This PR fixes a false positive for `Migration/DepartmentName` cop when a disable comment contains an unexpected character for department name. --- CHANGELOG.md | 1 + lib/rubocop/cop/migration/department_name.rb | 8 ++++---- spec/rubocop/cop/migration/department_name_spec.rb | 9 +++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b35a6dfb0a8..917a0333036 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/migration/department_name.rb b/lib/rubocop/cop/migration/department_name.rb index a8689dbcebf..4374a226b5f 100644 --- a/lib/rubocop/cop/migration/department_name.rb +++ b/lib/rubocop/cop/migration/department_name.rb @@ -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 @@ -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) diff --git a/spec/rubocop/cop/migration/department_name_spec.rb b/spec/rubocop/cop/migration/department_name_spec.rb index ca05fcb230e..0dcaa1d82d8 100644 --- a/spec/rubocop/cop/migration/department_name_spec.rb +++ b/spec/rubocop/cop/migration/department_name_spec.rb @@ -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.