Sha256: 68cf862573317047b5126a6c18c0a5c057863a97494efa7fa64bc2cd170aef73

Contents?: true

Size: 1.21 KB

Versions: 6

Compression:

Stored size: 1.21 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # Checks for uses of the character literal ?x.
      class CharacterLiteral < Cop
        include StringHelp

        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.loc.expression.source.size.between?(2, 3)
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            string = node.loc.expression.source[1..-1]

            if string.length == 1 # normal character
              corrector.replace(node.loc.expression, "'#{string}'")
            elsif string.length == 2 # special character like \n
              corrector.replace(node.loc.expression, %Q("#{string}"))
            end
          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

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-0.22.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.21.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.20.1 lib/rubocop/cop/style/character_literal.rb
rubocop-0.20.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.19.1 lib/rubocop/cop/style/character_literal.rb
rubocop-0.19.0 lib/rubocop/cop/style/character_literal.rb