Skip to content

Commit

Permalink
[Fix rubocop#7619] Support autocorrect of legacy names for `Migration…
Browse files Browse the repository at this point in the history
…/DepartmentName`

Fixes rubocop#7619.

This PR supports autocorrect of legacy cop names for
`Migration/DepartmentName`.

Auto-correction for legacy cop names complements legacy department names.
Migration to new cop names are expected to be modified using Gry gem.
https://github.com/pocke/gry
  • Loading branch information
koic committed Feb 4, 2020
1 parent ba59350 commit dd358d4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### New features

* [#7619](https://github.com/rubocop-hq/rubocop/issues/7619): Support autocorrect of legacy cop names for `Migration/DepartmentName`. ([@koic][])

### Bug fixes

* [#7639](https://github.com/rubocop-hq/rubocop/pull/7639): Fix logical operator edge case in `omit_parentheses` style of `Style/MethodCallWithArgsParentheses`. ([@gsamokovarov][])
Expand Down
15 changes: 14 additions & 1 deletion lib/rubocop/cop/migration/department_name.rb
Expand Up @@ -35,8 +35,13 @@ def investigate(processed_source)

def autocorrect(range)
shall_warn = false
qualified_cop_name = Cop.registry.qualified_cop_name(range.source,
cop_name = range.source
qualified_cop_name = Cop.registry.qualified_cop_name(cop_name,
nil, shall_warn)
unless qualified_cop_name.include?('/')
qualified_cop_name = qualified_legacy_cop_name(cop_name)
end

->(corrector) { corrector.replace(range, qualified_cop_name) }
end

Expand All @@ -53,6 +58,14 @@ def check_cop_name(name, comment, offset)
def valid_content_token?(content_token)
!DISABLING_COPS_CONTENT_TOKEN.match(content_token).nil?
end

def qualified_legacy_cop_name(cop_name)
legacy_cop_names = RuboCop::ConfigObsoletion::OBSOLETE_COPS.keys

legacy_cop_names.detect do |legacy_cop_name|
legacy_cop_name.split('/')[1] == cop_name
end
end
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/migration/department_name_spec.rb
Expand Up @@ -53,6 +53,23 @@
# rubocop : enable Style/Alias, Layout/LineLength
RUBY
end

it 'registers offenses and corrects when using a legacy cop name' do
expect_offense(<<~RUBY, 'file.rb')
# rubocop:disable SingleSpaceBeforeFirstArg, Layout/LineLength
^^^^^^^^^^^^^^^^^^^^^^^^^ Department name is missing.
name "apache_kafka"
RUBY

# `Style/SingleSpaceBeforeFirstArg` is a legacy name that has been
# renamed to `Layout/SpaceBeforeFirstArg`. In the autocorrection,
# the department name is complemented by the legacy cop name.
# Migration to the new name is expected to be modified using Gry gem.
expect_correction(<<~RUBY)
# rubocop:disable Style/SingleSpaceBeforeFirstArg, Layout/LineLength
name "apache_kafka"
RUBY
end
end

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

0 comments on commit dd358d4

Please sign in to comment.