From dd358d452fa0c4bed5476f07d355fe5f83abf60a Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 5 Feb 2020 01:07:11 +0900 Subject: [PATCH] [Fix #7619] Support autocorrect of legacy names for `Migration/DepartmentName` Fixes #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 --- CHANGELOG.md | 4 ++++ lib/rubocop/cop/migration/department_name.rb | 15 ++++++++++++++- .../cop/migration/department_name_spec.rb | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7142d2aff43..c259d9f562e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) diff --git a/lib/rubocop/cop/migration/department_name.rb b/lib/rubocop/cop/migration/department_name.rb index b3ff4c0f9f6..e79f69f03e3 100644 --- a/lib/rubocop/cop/migration/department_name.rb +++ b/lib/rubocop/cop/migration/department_name.rb @@ -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 @@ -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 diff --git a/spec/rubocop/cop/migration/department_name_spec.rb b/spec/rubocop/cop/migration/department_name_spec.rb index 257fec3de82..8c4fead6049 100644 --- a/spec/rubocop/cop/migration/department_name_spec.rb +++ b/spec/rubocop/cop/migration/department_name_spec.rb @@ -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