Sha256: 814c88a26b38b8b6df631f577fc6fefb53c8d0234460325efadae3c200171bed

Contents?: true

Size: 1.3 KB

Versions: 14

Compression:

Stored size: 1.3 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

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.'.freeze

        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.source_range, %("#{string}"))
            elsif string.length == 1 # normal character
              corrector.replace(node.source_range, "'#{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

14 entries across 14 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/character_literal.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/character_literal.rb
rubocop-0.43.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.42.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.41.2 lib/rubocop/cop/style/character_literal.rb
rubocop-0.41.1 lib/rubocop/cop/style/character_literal.rb
rubocop-0.41.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.40.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.39.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.38.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.37.2 lib/rubocop/cop/style/character_literal.rb
rubocop-0.37.1 lib/rubocop/cop/style/character_literal.rb
rubocop-0.37.0 lib/rubocop/cop/style/character_literal.rb
rubocop-0.36.0 lib/rubocop/cop/style/character_literal.rb