Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Naming/InclusiveLanguage to be disabled by default. #10074

Merged
merged 1 commit into from Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/change_update_naminginclusivelanguage_to_be.md
@@ -0,0 +1 @@
* [#10074](https://github.com/rubocop/rubocop/pull/10074): Update `Naming/InclusiveLanguage` to be disabled by default. ([@dvandersluis][])
3 changes: 2 additions & 1 deletion config/default.yml
Expand Up @@ -2569,8 +2569,9 @@ Naming/HeredocDelimiterNaming:

Naming/InclusiveLanguage:
Description: 'Recommend the use of inclusive language instead of problematic terms.'
Enabled: pending
Enabled: false
VersionAdded: '1.18'
VersionChanged: '<<next>>'
CheckIdentifiers: true
CheckConstants: true
CheckVariables: true
Expand Down
18 changes: 9 additions & 9 deletions lib/rubocop/cop/naming/inclusive_language.rb
Expand Up @@ -70,6 +70,8 @@ class InclusiveLanguage < Base
include RangeHelp

EMPTY_ARRAY = [].freeze
MSG = "Consider replacing '%<term>s'%<suffix>s."
MSG_FOR_FILE_PATH = "Consider replacing '%<term>s' in file path%<suffix>s."

WordLocation = Struct.new(:word, :position)

Expand Down Expand Up @@ -197,12 +199,11 @@ def investigate_filepath
end

def create_single_word_message_for_file(word)
create_message(word).sub(/\.$/, ' in file path.')
create_message(word, MSG_FOR_FILE_PATH)
end

def create_multiple_word_message_for_file(words)
quoted_words = words.map { |word| "'#{word}'" }
"Consider replacing problematic terms #{quoted_words.join(', ')} in file path."
format(MSG_FOR_FILE_PATH, term: words.join("', '"), suffix: ' with other terms')
end

def scan_for_words(input)
Expand All @@ -223,9 +224,12 @@ def mask_input(str)
safe_str.gsub(@allowed_regex) { |match| '*' * match.size }
end

def create_message(word)
def create_message(word, message = MSG)
flagged_term = find_flagged_term(word)
"Consider replacing problematic term '#{word}'#{flagged_term['SuggestionString']}."
suggestions = flagged_term['SuggestionString']
suggestions = ' with another term' if suggestions.blank?

format(message, term: word, suffix: suggestions)
end

def find_flagged_term(word)
Expand All @@ -235,10 +239,6 @@ def find_flagged_term(word)
flagged_term
end

def create_message_for_file(word)
create_message(word).sub(/\.$/, ' in file path.')
end

def preprocess_suggestions(suggestions)
return '' if suggestions.nil? ||
(suggestions.is_a?(String) && suggestions.strip.empty?) || suggestions.empty?
Expand Down
63 changes: 31 additions & 32 deletions spec/rubocop/cop/naming/inclusive_language_spec.rb
Expand Up @@ -9,22 +9,22 @@
it 'registers an offense when using a flagged term' do
expect_offense(<<~RUBY)
whitelist = %w(user1 user2)
^^^^^^^^^ Consider replacing problematic term 'whitelist'.
^^^^^^^^^ Consider replacing 'whitelist' with another term.
RUBY
end

it 'registers an offense when using a flagged term with mixed case' do
expect_offense(<<~RUBY)
class WhiteList
^^^^^^^^^ Consider replacing problematic term 'WhiteList'.
^^^^^^^^^ Consider replacing 'WhiteList' with another term.
end
RUBY
end

it 'registers an offense for a partial word match' do
expect_offense(<<~RUBY)
class Nodewhitelist
^^^^^^^^^ Consider replacing problematic term 'whitelist'.
^^^^^^^^^ Consider replacing 'whitelist' with another term.
end
RUBY
end
Expand All @@ -37,7 +37,7 @@ class Nodewhitelist
it 'ignores flagged terms that are set to nil' do
expect_offense(<<~RUBY)
# working on replacing whitelist and blacklist
^^^^^^^^^ Consider replacing problematic term 'blacklist'.
^^^^^^^^^ Consider replacing 'blacklist' with another term.
RUBY
end
end
Expand All @@ -53,8 +53,8 @@ class Nodewhitelist
it 'registers an offense for each word' do
expect_offense(<<~RUBY)
master, slave = nodes
^^^^^ Consider replacing problematic term 'slave' with 'replica', 'secondary', or 'follower'.
^^^^^^ Consider replacing problematic term 'master' with 'main', 'primary', or 'leader'.
^^^^^ Consider replacing 'slave' with 'replica', 'secondary', or 'follower'.
^^^^^^ Consider replacing 'master' with 'main', 'primary', or 'leader'.
RUBY
end
end
Expand All @@ -67,7 +67,7 @@ class Nodewhitelist
it 'registers an offense for a flagged term matched with a regexp' do
expect_offense(<<~RUBY)
# white-list of IPs
^^^^^^^^^^ Consider replacing problematic term 'white-list'.
^^^^^^^^^^ Consider replacing 'white-list' with another term.
RUBY
end
end
Expand All @@ -92,11 +92,11 @@ class Nodewhitelist
# not allowed
Slave
^^^^^ Consider replacing problematic term 'Slave'.
^^^^^ Consider replacing 'Slave' with another term.
:database_slave
^^^^^ Consider replacing problematic term 'slave'.
^^^^^ Consider replacing 'slave' with another term.
'database@slave'
^^^^^ Consider replacing problematic term 'slave'.
^^^^^ Consider replacing 'slave' with another term.
RUBY
end
end
Expand Down Expand Up @@ -126,7 +126,7 @@ class Nodewhitelist
it 'registers an offense at the correct location' do
expect_offense(<<~RUBY)
barx, foo = method_call
^^^ Consider replacing problematic term 'foo'.
^^^ Consider replacing 'foo' with another term.
RUBY
end
end
Expand All @@ -143,7 +143,7 @@ class Nodewhitelist
it 'includes the suggestion in the offense message' do
expect_offense(<<~RUBY)
whitelist = %w(user1 user2)
^^^^^^^^^ Consider replacing problematic term 'whitelist' with 'allowlist'.
^^^^^^^^^ Consider replacing 'whitelist' with 'allowlist'.
RUBY
end
end
Expand All @@ -158,7 +158,7 @@ class Nodewhitelist
it 'includes both suggestions in the offense message' do
expect_offense(<<~RUBY)
whitelist = %w(user1 user2)
^^^^^^^^^ Consider replacing problematic term 'whitelist' with 'allowlist' or 'permit'.
^^^^^^^^^ Consider replacing 'whitelist' with 'allowlist' or 'permit'.
RUBY
end
end
Expand All @@ -176,7 +176,7 @@ class Nodewhitelist
it 'includes all suggestions in the message' do
expect_offense(<<~RUBY)
default_branch = 'master'
^^^^^^ Consider replacing problematic term 'master' with 'main', 'primary', or 'leader'.
^^^^^^ Consider replacing 'master' with 'main', 'primary', or 'leader'.
RUBY
end
end
Expand All @@ -193,7 +193,7 @@ class Nodewhitelist
it 'registers an offense' do
expect_offense(<<~RUBY)
whitelist = %w(user1 user2)
^^^^^^^^^ Consider replacing problematic term 'whitelist'.
^^^^^^^^^ Consider replacing 'whitelist' with another term.
RUBY
end
end
Expand All @@ -220,21 +220,21 @@ class Nodewhitelist
it 'registers offenses for instance variables' do
expect_offense(<<~RUBY)
@whitelist = %w(user1 user2)
^^^^^^^^^ Consider replacing problematic term 'whitelist'.
^^^^^^^^^ Consider replacing 'whitelist' with another term.
RUBY
end

it 'registers offenses for class variables' do
expect_offense(<<~RUBY)
@@whitelist = %w(user1 user2)
^^^^^^^^^ Consider replacing problematic term 'whitelist'.
^^^^^^^^^ Consider replacing 'whitelist' with another term.
RUBY
end

it 'registers offenses for global variables' do
expect_offense(<<~RUBY)
$whitelist = %w(user1 user2)
^^^^^^^^^ Consider replacing problematic term 'whitelist'.
^^^^^^^^^ Consider replacing 'whitelist' with another term.
RUBY
end
end
Expand All @@ -261,7 +261,7 @@ class Nodewhitelist
it 'registers offenses for constants' do
expect_offense(<<~RUBY)
WHITELIST = %w(user1 user2)
^^^^^^^^^ Consider replacing problematic term 'WHITELIST'.
^^^^^^^^^ Consider replacing 'WHITELIST' with another term.
RUBY
end
end
Expand All @@ -286,17 +286,17 @@ class Nodewhitelist
it 'registers an offense for an interpolated string' do
expect_offense(<<~RUBY)
puts "master node \#{node}"
^^^^^^ Consider replacing problematic term 'master'.
^^^^^^ Consider replacing 'master' with another term.
RUBY
end

it 'registers an offense for a multiline string' do
expect_offense(<<~RUBY)
node_types = "master
^^^^^^ Consider replacing problematic term 'master'.
^^^^^^ Consider replacing 'master' with another term.
slave
^^^^^ Consider replacing problematic term 'slave'.
^^^^^ Consider replacing 'slave' with another term.
primary
secondary"
RUBY
Expand All @@ -306,10 +306,10 @@ class Nodewhitelist
expect_offense(<<~RUBY)
node_text = <<~TEXT
master
^^^^^^ Consider replacing problematic term 'master'.
^^^^^^ Consider replacing 'master' with another term.
primary
slave
^^^^^ Consider replacing problematic term 'slave'.
^^^^^ Consider replacing 'slave' with another term.
secondary
TEXT
RUBY
Expand Down Expand Up @@ -337,7 +337,7 @@ class Nodewhitelist
it 'registers an offense' do
expect_offense(<<~RUBY)
config[:master] = {}
^^^^^^ Consider replacing problematic term 'master'.
^^^^^^ Consider replacing 'master' with another term.
RUBY
end
end
Expand All @@ -362,17 +362,17 @@ class Nodewhitelist
it 'registers an offense in a single line comment' do
expect_offense(<<~RUBY)
# is it a foo?
^^^ Consider replacing problematic term 'foo'.
^^^ Consider replacing 'foo' with another term.
bar = baz # it's a foo!
^^^ Consider replacing problematic term 'foo'.
^^^ Consider replacing 'foo' with another term.
RUBY
end

it 'registers an offense in a block comment' do
expect_offense(<<~RUBY)
=begin
foo
^^^ Consider replacing problematic term 'foo'.
^^^ Consider replacing 'foo' with another term.
bar
=end
RUBY
Expand Down Expand Up @@ -405,8 +405,7 @@ class Nodewhitelist

it 'registers an offense' do
expect(offenses.size).to eq(1)
expect(messages)
.to eq(["Consider replacing problematic term 'master' with 'main' in file path."])
expect(messages).to eq(["Consider replacing 'master' in file path with 'main'."])
end
end

Expand All @@ -419,7 +418,7 @@ class Nodewhitelist
it 'registers an offense with all problematic words' do
expect(offenses.size).to eq(1)
expect(messages)
.to eq(["Consider replacing problematic terms 'master', 'slave' in file path."])
.to eq(["Consider replacing 'master', 'slave' in file path with other terms."])
end
end

Expand All @@ -431,7 +430,7 @@ class Nodewhitelist

it 'registers an offense for a director' do
expect(offenses.size).to eq(1)
expect(messages).to eq(["Consider replacing problematic term 'master' in file path."])
expect(messages).to eq(["Consider replacing 'master' in file path with another term."])
end
end

Expand Down