Sha256: 97fd42cca86de5b2caf9e79405f6c2a0987ea438428b92abd8cb71eadb9e85a5

Contents?: true

Size: 1.2 KB

Versions: 9

Compression:

Stored size: 1.2 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)
          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, %("#{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

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/style/character_literal.rb
rubocop-0.35.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.34.2 lib/rubocop/cop/style/character_literal.rb
rubocop-0.34.1 lib/rubocop/cop/style/character_literal.rb
rubocop-0.34.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.33.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.32.1 lib/rubocop/cop/style/character_literal.rb
rubocop-0.32.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.31.0 lib/rubocop/cop/style/character_literal.rb