Sha256: 232af51ff47a657c8537e414389fdb3aea32c37ca2da288a0c8dc725d0a223d0

Contents?: true

Size: 1.06 KB

Versions: 11

Compression:

Stored size: 1.06 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    class StringLiterals < Cop
      ERROR_MESSAGE = "Prefer single-quoted strings when you don't need " +
        'string interpolation or special symbols.'

      def inspect(file, source, tokens, sexp)
        state = :outside
        tokens.each do |t|
          state = case [state, t.type]
                  when [:outside, :on_tstring_beg]
                    :double_quote if t.text == '"'

                  when [:double_quote, :on_tstring_content]
                    :valid_double_quote if t.text =~ /'|\\[ntrx]/

                  when [:double_quote, :on_embexpr_beg]
                    :embedded_expression

                  when [:double_quote, :on_tstring_end]
                    add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
                    :outside

                  when [:embedded_expression, :on_rbrace]
                    :valid_double_quote

                  when [:valid_double_quote, :on_tstring_end]
                    :outside
            end || state
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rubocop-0.4.6 lib/rubocop/cop/string_literals.rb
rubocop-0.4.5 lib/rubocop/cop/string_literals.rb
rubocop-0.4.4 lib/rubocop/cop/string_literals.rb
rubocop-0.4.3 lib/rubocop/cop/string_literals.rb
rubocop-0.4.2 lib/rubocop/cop/string_literals.rb
rubocop-0.4.1 lib/rubocop/cop/string_literals.rb
rubocop-0.4.0 lib/rubocop/cop/string_literals.rb
rubocop-0.3.2 lib/rubocop/cop/string_literals.rb
rubocop-0.3.1 lib/rubocop/cop/string_literals.rb
rubocop-0.3.0 lib/rubocop/cop/string_literals.rb
rubocop-0.2.1 lib/rubocop/cop/string_literals.rb