Sha256: 1b1ee099c9dee95e47d6ebd3634d26bff5b70353cd2d9f3768bb8bb5d114b24b

Contents?: true

Size: 1.04 KB

Versions: 28

Compression:

Stored size: 1.04 KB

Contents

require "jsduck/util/singleton"
require "jsduck/js/scoped_traverser"

module JsDuck
  module Js

    # Looks the AST of a FunctionDeclaration or FunctionExpression for
    # calls to methods of the owner class.
    class MethodCalls
      include Util::Singleton

      # Returns array of method names called by the given function.
      # When no methods called, empty array is returned.
      def detect(function_node)
        @traverser = Js::ScopedTraverser.new

        methods = []
        @traverser.traverse(function_node["body"]) do |node|
          if method_call?(node)
            methods << node["callee"]["property"].to_s
          end
        end

        methods.sort.uniq
      end

      private

      # True when node is this.someMethod() call.
      # Also true for me.someMethod() when me == this.
      def method_call?(node)
        node.call_expression? &&
          node["callee"].member_expression? &&
          node["callee"].raw["computed"] == false &&
          @traverser.this?(node["callee"]["object"].to_s)
      end

    end
  end
end

Version data entries

28 entries across 28 versions & 3 rubygems

Version Path
solvas-jsduck-6.0.0.30539 lib/jsduck/js/method_calls.rb
solvas-jsduck-6.0.0.9571 lib/jsduck/js/method_calls.rb
solvas-jsduck-6.0.0.6154 lib/jsduck/js/method_calls.rb
solvas-jsduck-6.0.0.4021 lib/jsduck/js/method_calls.rb
solvas-jsduck-6.0.0.2554 lib/jsduck/js/method_calls.rb
solvas-jsduck-6.0.0.1891 lib/jsduck/js/method_calls.rb
solvas-jsduck-6.0.0.beta.1888 lib/jsduck/js/method_calls.rb
jsduck-troopjs-0.0.10 lib/jsduck/js/method_calls.rb
jsduck-troopjs-0.0.9 lib/jsduck/js/method_calls.rb
jsduck-troopjs-0.0.8 lib/jsduck/js/method_calls.rb
jsduck-troopjs-0.0.7 lib/jsduck/js/method_calls.rb
jsduck-troopjs-0.0.5 lib/jsduck/js/method_calls.rb
jsduck-troopjs-0.0.4 lib/jsduck/js/method_calls.rb
jsduck-troopjs-0.0.3 lib/jsduck/js/method_calls.rb
jsduck-troopjs-0.0.1 lib/jsduck/js/method_calls.rb
jsduck-6.0.0beta lib/jsduck/js/method_calls.rb
jsduck-5.3.4 lib/jsduck/js/method_calls.rb
jsduck-5.3.3 lib/jsduck/js/method_calls.rb
jsduck-5.3.2 lib/jsduck/js/method_calls.rb
jsduck-5.3.1 lib/jsduck/js/method_calls.rb