Sha256: 23efd417242d755ba343feea16f1fc8221e443f22201c06e5e05b0a98cf7cdce

Contents?: true

Size: 1.7 KB

Versions: 181

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for uses of the character literal ?x.
      # Starting with Ruby 1.9 character literals are
      # essentially one-character strings, so this syntax
      # is mostly redundant at this point.
      #
      # ? character literal can be used to express meta and control character.
      # That's a good use case of ? literal so it doesn't count it as an offense.
      #
      # @example
      #   # bad
      #   ?x
      #
      #   # good
      #   'x'
      #
      #   # good - control & meta escapes
      #   ?\C-\M-d
      #   "\C-\M-d" # same as above
      class CharacterLiteral < Base
        include StringHelp
        extend AutoCorrector

        MSG = 'Do not use the character literal - use string literal instead.'

        def offense?(node)
          # we don't register an offense for things like ?\C-\M-d
          node.character_literal? && node.source.size.between?(2, 3)
        end

        def autocorrect(corrector, node)
          string = node.source[1..]

          # special character like \n
          # or ' which needs to use "" or be escaped.
          if string.length == 2 || string == "'"
            corrector.replace(node, %("#{string}"))
          elsif string.length == 1 # normal character
            corrector.replace(node, "'#{string}'")
          end
        end

        # Dummy implementation of method in ConfigurableEnforcedStyle that is
        # called from StringHelp.
        def opposite_style_detected; end

        # Dummy implementation of method in ConfigurableEnforcedStyle that is
        # called from StringHelp.
        def correct_style_detected; end
      end
    end
  end
end

Version data entries

181 entries across 180 versions & 19 rubygems

Version Path
rubocop-1.74.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.73.2 lib/rubocop/cop/style/character_literal.rb
siteimprove_api_client-1.0.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.73.1/lib/rubocop/cop/style/character_literal.rb
rubocop-1.73.1 lib/rubocop/cop/style/character_literal.rb
rubocop-1.73.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.72.2 lib/rubocop/cop/style/character_literal.rb
rubocop-1.72.1 lib/rubocop/cop/style/character_literal.rb
rubocop-1.72.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.71.2 lib/rubocop/cop/style/character_literal.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-1.71.1/lib/rubocop/cop/style/character_literal.rb
rubocop-1.71.1 lib/rubocop/cop/style/character_literal.rb
rubocop-1.71.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.70.0 lib/rubocop/cop/style/character_literal.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/character_literal.rb
rubocop-1.69.2 lib/rubocop/cop/style/character_literal.rb
rubocop-1.69.1 lib/rubocop/cop/style/character_literal.rb
rubocop-1.69.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.68.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.67.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.66.1 lib/rubocop/cop/style/character_literal.rb