Skip to content

Commit

Permalink
[Fix rubocop#9957] Add WholeWord configuration to `Naming/Inclusive…
Browse files Browse the repository at this point in the history
…Language`'s `FlaggedTerms` config.
  • Loading branch information
dvandersluis committed Jul 30, 2021
1 parent 39fcf1c commit 91a3a22
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions 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][])
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -2557,6 +2557,7 @@ Naming/InclusiveLanguage:
- denylist
- block
slave:
WholeWord: true
Suggestions: ['replica', 'secondary', 'follower']

Naming/MemoizedInstanceVariableName:
Expand Down
19 changes: 18 additions & 1 deletion lib/rubocop/cop/naming/inclusive_language.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions spec/rubocop/cop/naming/inclusive_language_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 91a3a22

Please sign in to comment.