Sha256: 99de666ffcaa09b6535c7ef6c98ae243c7d47ce457cef742bf4e09db9e61fb48

Contents?: true

Size: 1.35 KB

Versions: 11

Compression:

Stored size: 1.35 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

11 entries across 11 versions & 1 rubygems

Version Path
rubocop-1.12.1 lib/rubocop/cop/style/character_literal.rb
rubocop-1.12.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.11.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.10.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.9.1 lib/rubocop/cop/style/character_literal.rb
rubocop-1.9.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.8.1 lib/rubocop/cop/style/character_literal.rb
rubocop-1.8.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.7.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.6.1 lib/rubocop/cop/style/character_literal.rb
rubocop-1.6.0 lib/rubocop/cop/style/character_literal.rb