Sha256: 312eb4acc956f8caee9ecbe36977ca87c3ed9dc8ddb2bd35d87a461a67f87cd4

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # Checks if uses of quotes match the configured preference.
      class StringLiterals < Cop
        include ConfigurableEnforcedStyle
        include StringHelp

        private

        def message(*)
          if style == :single_quotes
            "Prefer single-quoted strings when you don't need string " \
            'interpolation or special symbols.'
          else
            'Prefer double-quoted strings unless you need single quotes to ' \
            'avoid extra backslashes for escaping.'
          end
        end

        def offense?(node)
          src = node.loc.expression.source
          return false if src =~ /^(%[qQ]?|\?|<<-)/i
          if style == :single_quotes
            src !~ /'/ && src !~ StringHelp::ESCAPED_CHAR_REGEXP
          else
            src !~ /" | \\/x
          end
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            replacement = node.loc.begin.is?('"') ? "'" : '"'
            corrector.replace(node.loc.begin, replacement)
            corrector.replace(node.loc.end, replacement)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/string_literals.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/string_literals.rb
rubocop-0.26.1 lib/rubocop/cop/style/string_literals.rb
rubocop-0.26.0 lib/rubocop/cop/style/string_literals.rb
rubocop-0.25.0 lib/rubocop/cop/style/string_literals.rb