Sha256: 8bf64e43f785d4855af05f7a845739de2435d75701b5ad03f508561ca51918f9

Contents?: true

Size: 947 Bytes

Versions: 13

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

13 entries across 13 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/lint/literal_in_interpolation.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.29.1 lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.29.0 lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.28.0 lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.27.1 lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.27.0 lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.26.1 lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.26.0 lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.25.0 lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.24.1 lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.24.0 lib/rubocop/cop/lint/literal_in_interpolation.rb
rubocop-0.23.0 lib/rubocop/cop/lint/literal_in_interpolation.rb