Sha256: 1c23b7a56c090cd1bf6d3178b84e2ac97eb79779480ed0bb3f774ffc030190a0

Contents?: true

Size: 1.21 KB

Versions: 12

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, %("#{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

12 entries across 12 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/character_literal.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/character_literal.rb
rubocop-0.30.1 lib/rubocop/cop/style/character_literal.rb
rubocop-0.30.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.29.1 lib/rubocop/cop/style/character_literal.rb
rubocop-0.29.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.28.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.27.1 lib/rubocop/cop/style/character_literal.rb
rubocop-0.27.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.26.1 lib/rubocop/cop/style/character_literal.rb
rubocop-0.26.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.25.0 lib/rubocop/cop/style/character_literal.rb