From 91a3a22392fb2c4dfdd69854e1b728989a8f5471 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Fri, 30 Jul 2021 15:16:17 -0400 Subject: [PATCH] [Fix #9957] Add `WholeWord` configuration to `Naming/InclusiveLanguage`'s `FlaggedTerms` config. --- .../fix_add_wholeword_configuration_to.md | 1 + config/default.yml | 1 + lib/rubocop/cop/naming/inclusive_language.rb | 19 +++++++++++- .../cop/naming/inclusive_language_spec.rb | 29 +++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_add_wholeword_configuration_to.md diff --git a/changelog/fix_add_wholeword_configuration_to.md b/changelog/fix_add_wholeword_configuration_to.md new file mode 100644 index 00000000000..b04ae9fb7be --- /dev/null +++ b/changelog/fix_add_wholeword_configuration_to.md @@ -0,0 +1 @@ +* [#9957](https://github.com/rubocop/rubocop/issues/9957): Add `WholeWord` configuration to `Naming/InclusiveLanguage`'s `FlaggedTerms` config. ([@dvandersluis][]) diff --git a/config/default.yml b/config/default.yml index 5fbba4556d7..7fd0449bfab 100644 --- a/config/default.yml +++ b/config/default.yml @@ -2557,6 +2557,7 @@ Naming/InclusiveLanguage: - denylist - block slave: + WholeWord: true Suggestions: ['replica', 'secondary', 'follower'] Naming/MemoizedInstanceVariableName: diff --git a/lib/rubocop/cop/naming/inclusive_language.rb b/lib/rubocop/cop/naming/inclusive_language.rb index 04683b8a0d2..0b5a546c763 100644 --- a/lib/rubocop/cop/naming/inclusive_language.rb +++ b/lib/rubocop/cop/naming/inclusive_language.rb @@ -19,6 +19,8 @@ module Naming # Regex can be specified to identify offenses. Suggestions for replacing a flagged term can # be configured and will be displayed as part of the offense message. # An AllowedRegex can be specified for a flagged term to exempt allowed uses of the term. + # `WholeWord: true` can be set on a flagged term to indicate the cop should only match when + # a term matches the whole word (partial matches will not be offenses). # # @example FlaggedTerms: { whitelist: { Suggestions: ['allowlist'] } } # # Suggest replacing identifier whitelist with allowlist @@ -56,6 +58,14 @@ module Naming # # good # # They had a master's degree # + # @example FlaggedTerms: { slave: { WholeWord: true } } + # # Specify that only terms that are full matches will be flagged. + # + # # bad + # Slave + # + # # good (won't be flagged despite containing `slave`) + # TeslaVehicle class InclusiveLanguage < Base include RangeHelp @@ -123,7 +133,7 @@ def preprocess_flagged_terms next if term_definition.nil? allowed_strings.concat(process_allowed_regex(term_definition['AllowedRegex'])) - regex_string = ensure_regex_string(term_definition['Regex'] || term) + regex_string = ensure_regex_string(extract_regexp(term, term_definition)) flagged_term_strings << regex_string add_to_flagged_term_hash(regex_string, term, term_definition) @@ -132,6 +142,13 @@ def preprocess_flagged_terms set_regexes(flagged_term_strings, allowed_strings) 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'] + + term + end + def add_to_flagged_term_hash(regex_string, term, term_definition) @flagged_term_hash[Regexp.new(regex_string, Regexp::IGNORECASE)] = term_definition.merge('Term' => term, diff --git a/spec/rubocop/cop/naming/inclusive_language_spec.rb b/spec/rubocop/cop/naming/inclusive_language_spec.rb index fbcc9370155..d811fe1c629 100644 --- a/spec/rubocop/cop/naming/inclusive_language_spec.rb +++ b/spec/rubocop/cop/naming/inclusive_language_spec.rb @@ -71,6 +71,35 @@ class Nodewhitelist RUBY end end + + context 'WholeWord: true' do + let(:cop_config) do + { 'CheckStrings' => true, 'FlaggedTerms' => { 'slave' => { 'WholeWord' => true } } } + end + + it 'only flags when the term is a whole word' do + expect_offense(<<~RUBY) + # infix allowed + TeslaVehicle + SLAVersion + :teslavehicle + + # prefix allowed + DatabaseSlave + + # suffix allowed + Slave1 + + # not allowed + Slave + ^^^^^ Consider replacing problematic term 'Slave'. + :database_slave + ^^^^^ Consider replacing problematic term 'slave'. + 'database@slave' + ^^^^^ Consider replacing problematic term 'slave'. + RUBY + end + end end context 'allowed use' do