Sha256: 81c2a585d765eac621a6d6c868b46f512ecee9f4476aec7c26fe5f4166c291d7

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # Checks for uses of double quotes where single quotes would do.
      class StringLiterals < Cop
        MSG = "Prefer single-quoted strings when you don't need " +
          'string interpolation or special symbols.'

        def on_str(node)
          # Constants like __FILE__ are handled as strings,
          # but don't respond to begin.
          return unless node.loc.respond_to?(:begin)
          return if part_of_ignored_node?(node)

          # regex matches IF there is a ' or there is a \\ in the string that
          # is not preceeded/followed by another \\ (e.g. "\\x34") but not
          # "\\\\"
          if node.loc.expression.source !~ /' | (?<! \\) \\{2}* \\ (?! \\)/x &&
              node.loc.begin && node.loc.begin.is?('"')
            convention(node, :expression)
          end
        end

        alias_method :on_dstr, :ignore_node
        alias_method :on_regexp, :ignore_node

        def autocorrect_action(node)
          @corrections << lambda do |corrector|
            corrector.replace(node.loc.begin, "'")
            corrector.replace(node.loc.end, "'")
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.13.0 lib/rubocop/cop/style/string_literals.rb