Sha256: ec4a9a866e752bef308f244a79139e2f569fefda4587b8916033536d746d4ff3

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require 'reek/core/code_context'
require 'reek/core/object_refs'

module Reek
  module Core
    #
    # The parameters in a method's definition.
    #
    module MethodParameters
      def default_assignments
        result = []
        self[1..-1].each do |exp|
          result << exp[1..2] if exp.is_a?(Sexp) && exp[0] == :lasgn
        end
        result
      end
    end

    #
    # A context wrapper for any method definition found in a syntax tree.
    #
    class MethodContext < CodeContext
      attr_reader :parameters
      attr_reader :refs
      attr_reader :num_statements

      def initialize(outer, exp)
        super(outer, exp)
        @parameters = exp[exp[0] == :defn ? 2 : 3]  # SMELL: SimulatedPolymorphism
        @parameters ||= []
        @parameters.extend(MethodParameters)
        @num_statements = 0
        @refs = ObjectRefs.new
      end

      def count_statements(num)
        @num_statements += num
      end

      def record_call_to(exp)
        receiver, meth = exp[1..2]
        receiver ||= [:self]
        case receiver[0]
        when :lvar
          @refs.record_reference_to(receiver) unless meth == :new
        when :self
          @refs.record_reference_to(:self)
        end
      end

      def record_use_of_self
        @refs.record_reference_to(:self)
      end

      def envious_receivers
        return [] if @refs.self_is_max?
        @refs.max_keys
      end

      def uses_param?(param)
        local_nodes(:lvar).include?(Sexp.new(:lvar, param.to_sym))
      end

      def unused_params
        exp.arguments.select do |param|
          next if param.anonymous_splat?
          next if param.marked_unused?
          !uses_param? param.plain_name
        end
      end

      def uses_super_with_implicit_arguments?
        exp.body.contains_nested_node? :zsuper
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reek-1.4.0 lib/reek/core/method_context.rb