From 7fcdd43ddc55ee9b9f02e06b87ffd2e6892fab97 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 21 Sep 2024 21:22:39 +0900 Subject: [PATCH] Enhance the autocorrect for `Naming/InclusiveLanguage` This PR enhances the autocorrect for `Naming/InclusiveLanguage` when a sole suggestion is set. The value of `Suggestions` is an array `['an offense']`, but since it can be determined as a sole term, it is autocorrectable. ```yaml Naming/InclusiveLanguage: FlaggedTerms: ' a offense': Suggestions: - an offense ``` --- ...tocorrect_for_naming_inclusive_language.md | 1 + lib/rubocop/cop/naming/inclusive_language.rb | 15 ++++++++++++--- .../cop/naming/inclusive_language_spec.rb | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 changelog/change_enhance_autocorrect_for_naming_inclusive_language.md diff --git a/changelog/change_enhance_autocorrect_for_naming_inclusive_language.md b/changelog/change_enhance_autocorrect_for_naming_inclusive_language.md new file mode 100644 index 000000000000..cf8554be2162 --- /dev/null +++ b/changelog/change_enhance_autocorrect_for_naming_inclusive_language.md @@ -0,0 +1 @@ +* [#13254](https://github.com/rubocop/rubocop/pull/13254): Enhance the autocorrect for `Naming/InclusiveLanguage` when a sole suggestion is set. ([@koic][]) diff --git a/lib/rubocop/cop/naming/inclusive_language.rb b/lib/rubocop/cop/naming/inclusive_language.rb index 686d97ef0562..f879439797c1 100644 --- a/lib/rubocop/cop/naming/inclusive_language.rb +++ b/lib/rubocop/cop/naming/inclusive_language.rb @@ -116,9 +116,9 @@ def add_offenses_for_token(token, word_locations) add_offense(range, message: create_message(word)) do |corrector| suggestions = find_flagged_term(word)['Suggestions'] - next unless suggestions.is_a?(String) - - corrector.replace(range, suggestions) + if (preferred_term = preferred_sole_term(suggestions)) + corrector.replace(range, preferred_term) + end end end end @@ -157,6 +157,15 @@ def preprocess_flagged_terms set_regexes(flagged_term_strings, allowed_strings) end + def preferred_sole_term(suggestions) + case suggestions + when Array + suggestions.one? && preferred_sole_term(suggestions.first) + when String + suggestions + end + end + def extract_regexp(term, term_definition) return term_definition['Regex'] if term_definition['Regex'] return /(?:\b|(?<=[\W_]))#{term}(?:\b|(?=[\W_]))/ if term_definition['WholeWord'] diff --git a/spec/rubocop/cop/naming/inclusive_language_spec.rb b/spec/rubocop/cop/naming/inclusive_language_spec.rb index 33950708c44a..c7c5fa7eedfd 100644 --- a/spec/rubocop/cop/naming/inclusive_language_spec.rb +++ b/spec/rubocop/cop/naming/inclusive_language_spec.rb @@ -168,6 +168,25 @@ class Nodewhitelist end end + context 'flagged term with one suggestion in array' do + let(:cop_config) do + { 'FlaggedTerms' => { + 'whitelist' => { 'Suggestions' => %w[allowlist] } + } } + end + + it 'includes both suggestions in the offense message' do + expect_offense(<<~RUBY) + whitelist = %w(user1 user2) + ^^^^^^^^^ Consider replacing 'whitelist' with 'allowlist'. + RUBY + + expect_correction(<<~RUBY) + allowlist = %w(user1 user2) + RUBY + end + end + context 'flagged term with two suggestions' do let(:cop_config) do { 'FlaggedTerms' => {