Sha256: 8c817ee813139c33e27383d0ea29848ff3cd157f8e3ccc5890e75a549807e30c

Contents?: true

Size: 998 Bytes

Versions: 2

Compression:

Stored size: 998 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
            next if special_keyword?(final_node)
            next unless LITERALS.include?(final_node.type)

            add_offense(final_node, :expression)
          end
        end

        private

        def special_keyword?(node)
          # handle strings like __FILE__
          (node.type == :str && !node.loc.respond_to?(:begin)) ||
            node.loc.expression.is?('__LINE__')
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.31.0 lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.30.1 lib/rubocop/cop/lint/literal_in_interpolation.rb