Class: RuboCop::Cop::Inclusivity::Race

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ActiveSupport::Inflector
Defined in:
lib/rubocop/cop/inclusivity/race.rb

Overview

Detects potentially insensitive langugae used in variable names and suggests alternatives that promote inclusivity.

The Offenses config parameter can be used to configure the cop with your list of insensitive words.

Examples:

Using insensitive language

# bad
blacklist = 1

# good
banlist = 1

Constant Summary collapse

ALLOWLIST =
"Allowlist"
OFFENSES =
"Offenses"
DOUBLE_QUOTE =
"\""
MSG =
"`%s` may be insensitive. Consider alternatives: %s"
PARTIAL =
"partial"
SINGLE_QUOTE =
"'"
SYMBOL =
":"
UTF_8 =
"UTF-8"

Instance Method Summary collapse

Instance Method Details

#on_casgn(node) ⇒ Object



53
54
55
56
# File 'lib/rubocop/cop/inclusivity/race.rb', line 53

def on_casgn(node)
  _parent, constant_name, _value = *node
  check(node.loc.name, constant_name) { |alternative| alternative.upcase }
end

#on_const(node) ⇒ Object



82
83
84
85
86
# File 'lib/rubocop/cop/inclusivity/race.rb', line 82

def on_const(node)
  match_consts(node) do |const_name|
    check(node.loc.name, const_name)
  end
end

#on_def(node) ⇒ Object



71
72
73
74
# File 'lib/rubocop/cop/inclusivity/race.rb', line 71

def on_def(node)
  name, _args, _forward_args = *node
  check(node.loc.name, name)
end

#on_send(node) ⇒ Object



76
77
78
79
80
# File 'lib/rubocop/cop/inclusivity/race.rb', line 76

def on_send(node)
  match_methods_and_variables(node) do |variable_name|
    check(node.loc.selector, variable_name.to_s.delete_suffix("="))
  end
end

#on_str(node) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/rubocop/cop/inclusivity/race.rb', line 63

def on_str(node)
  name, = *node
  check(node.source_range, name) do |replacement|
    quote = determine_quote(node.source)
    "#{quote}#{replacement.downcase}#{quote}"
  end
end

#on_sym(node) ⇒ Object



58
59
60
61
# File 'lib/rubocop/cop/inclusivity/race.rb', line 58

def on_sym(node)
  name, = *node
  check(node.source_range, name) { |replacement| node.source[0] == SYMBOL ? ":#{replacement}" : replacement }
end

#simple_substitution(node) ⇒ Object Also known as: on_lvasgn, on_ivasgn, on_cvasgn, on_arg, on_optarg, on_restarg, on_kwoptarg, on_kwarg, on_kwrestarg, on_blockarg, on_lvar, on_cvar



36
37
38
39
# File 'lib/rubocop/cop/inclusivity/race.rb', line 36

def simple_substitution(node)
  name, = *node
  name && check(node.loc.name, name)
end