Class: RuboCop::Cop::Naming::InclusiveLanguage

Inherits:
Base
  • Object
show all
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/naming/inclusive_language.rb

Overview

This cops recommends the use of inclusive language instead of problematic terms. The cop can check the following locations for offenses:

  • identifiers

  • constants

  • variables

  • strings

  • symbols

  • comments

  • file paths

Each of these locations can be individually enabled/disabled via configuration, for example CheckIdentifiers = true/false.

Flagged terms are configurable for the cop. For each flagged term an optional 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).

Examples:

FlaggedTerms: { whitelist: { Suggestions: ['allowlist'] } }

# Suggest replacing identifier whitelist with allowlist

# bad
whitelist_users = %w(user1 user1)

# good
allowlist_users = %w(user1 user2)

FlaggedTerms: { master: { Suggestions: ['main', 'primary', 'leader'] } }

# Suggest replacing master in an instance variable name with main, primary, or leader

# bad
@master_node = 'node1.example.com'

# good
@primary_node = 'node1.example.com'

FlaggedTerms: { whitelist: { Regex: !ruby/regexp '/white?list' } }

# Identify problematic terms using a Regexp

# bad
white_list = %w(user1 user2)

# good
allow_list = %w(user1 user2)

FlaggedTerms: { master: { AllowedRegex: 'master'?s degree' } }

# Specify allowed uses of the flagged term as a string or regexp.

# bad
# They had a masters

# good
# They had a master's degree

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

Defined Under Namespace

Classes: WordLocation

Constant Summary collapse

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

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

#initialize(config = nil, options = nil) ⇒ InclusiveLanguage

Returns a new instance of InclusiveLanguage.


78
79
80
81
82
83
84
85
# File 'lib/rubocop/cop/naming/inclusive_language.rb', line 78

def initialize(config = nil, options = nil)
  super
  @flagged_term_hash = {}
  @flagged_terms_regex = nil
  @allowed_regex = nil
  @check_token = preprocess_check_config
  preprocess_flagged_terms
end

Instance Method Details

#on_new_investigationObject


87
88
89
90
# File 'lib/rubocop/cop/naming/inclusive_language.rb', line 87

def on_new_investigation
  investigate_filepath if cop_config['CheckFilepaths']
  investigate_tokens
end