Sha256: 87ae49cef3a6ca210e4b58ffc13d1c4088897fa6981ab4c58661c548e089e86f

Contents?: true

Size: 947 Bytes

Versions: 4

Compression:

Stored size: 947 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Lint
      # This cop checks for interpolated literals.
      #
      # @example
      #
      #   "result is #{10}"
      class LiteralInInterpolation < Cop
        LITERALS = [:str, :dstr, :int, :float, :array,
                    :hash, :regexp, :nil, :true, :false]

        MSG = 'Literal interpolation detected.'

        def on_dstr(node)
          node.children.select { |n| n.type == :begin }.each do |begin_node|
            final_node = begin_node.children.last
            next unless final_node
            # handle strings like __FILE__
            return if special_string?(final_node)
            next unless LITERALS.include?(final_node.type)

            add_offense(final_node, :expression)
          end
        end

        private

        def special_string?(node)
          node.type == :str && !node.loc.respond_to?(:begin)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.22.0 lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.21.0 lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.20.1 lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.20.0 lib/rubocop/cop/lint/literal_in_interpolation.rb