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

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

Overview

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).

The cop supports autocorrection when there is only one suggestion. When there are multiple suggestions, the best suggestion cannot be identified and will not be autocorrected.

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 included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, 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?, #autocorrect_with_disable_uncorrectable?, #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.



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

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



93
94
95
96
# File 'lib/rubocop/cop/naming/inclusive_language.rb', line 93

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