Sha256: c96448edb59a7ddd600e51fc6db6fa5983a083d727eee009f4c039fb502ef8ab

Contents?: true

Size: 1.37 KB

Versions: 40

Compression:

Stored size: 1.37 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 < 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.source.size.between?(2, 3)
        end

        def autocorrect(node)
          lambda do |corrector|
            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
        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

40 entries across 40 versions & 5 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/character_literal.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/character_literal.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/character_literal.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/character_literal.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/character_literal.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/character_literal.rb
rubocop-1.5.2 lib/rubocop/cop/style/character_literal.rb
rubocop-1.5.1 lib/rubocop/cop/style/character_literal.rb
rubocop-1.5.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.4.2 lib/rubocop/cop/style/character_literal.rb
rubocop-1.4.1 lib/rubocop/cop/style/character_literal.rb
rubocop-1.4.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.3.1 lib/rubocop/cop/style/character_literal.rb
rubocop-1.3.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.2.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.1.0 lib/rubocop/cop/style/character_literal.rb
rubocop-1.0.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.93.1 lib/rubocop/cop/style/character_literal.rb
rubocop-0.93.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.92.0 lib/rubocop/cop/style/character_literal.rb