Sha256: a70dac29f0f8a2f5d4714910878642a9e96ae448ab1967c0a51f8d6e9eefa697

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Inclusivity
      # 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.
      #
      # @example Using insensitive language
      #   # bad
      #   blacklist = 1
      #
      #   # good
      #   banlist = 1
      #
      class Race < Cop
        MSG = "`%s` may be insensitive. Consider alternatives: %s"

        def on_lvasgn(node)
          name, = *node
          return unless name

          check_name(node, name, node.loc.name)
        end
        alias on_ivasgn on_lvasgn
        alias on_cvasgn on_lvasgn
        alias on_arg on_lvasgn
        alias on_optarg on_lvasgn
        alias on_restarg on_lvasgn
        alias on_kwoptarg on_lvasgn
        alias on_kwarg on_lvasgn
        alias on_kwrestarg on_lvasgn
        alias on_blockarg on_lvasgn
        alias on_lvar on_lvasgn

        private

        def check_name(node, name, name_range)
          if (alternatives = preferred_language(name))
            msg = message(name, alternatives)
            add_offense(node, location: name_range, message: msg)
          end
        end

        def preferred_language(word)
          cop_config["Offenses"][word.to_s.downcase]
        end

        def message(insensitive, alternatives)
          format(MSG, insensitive, alternatives.join(", "))
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-inclusivity-1.0.0 lib/rubocop/cop/inclusivity/race.rb