Sha256: 9496861b551a28e806740ebfd4346f6ca7413609879dfe26e985eb8b7dc4da2c

Contents?: true

Size: 1.32 KB

Versions: 22

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for uses of the character literal ?x.
      #
      # @example
      #   # bad
      #   ?x
      #
      #   # good
      #   'x'
      #
      #   # good
      #   ?\C-\M-d
      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.loc.begin.is?('?') && node.source.size.between?(2, 3)
        end

        def autocorrect(corrector, node)
          string = node.source[1..-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

22 entries across 22 versions & 3 rubygems

Version Path
rubocop-1.23.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.22.3 lib/rubocop/cop/style/character_literal.rb
rubocop-1.22.2 lib/rubocop/cop/style/character_literal.rb
rubocop-1.22.1 lib/rubocop/cop/style/character_literal.rb
rubocop-1.22.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.21.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.20.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.19.1 lib/rubocop/cop/style/character_literal.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/style/character_literal.rb
rubocop-1.19.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.18.4 lib/rubocop/cop/style/character_literal.rb
rubocop-1.18.3 lib/rubocop/cop/style/character_literal.rb
rubocop-1.18.2 lib/rubocop/cop/style/character_literal.rb
rubocop-1.18.1 lib/rubocop/cop/style/character_literal.rb
rubocop-1.18.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.17.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.16.1 lib/rubocop/cop/style/character_literal.rb
rubocop-1.16.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.15.0 lib/rubocop/cop/style/character_literal.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/style/character_literal.rb