Sha256: aaad060711669986b431aa2d1e0512be50f4eeb4eeb760d5e69949a16e644c96

Contents?: true

Size: 1.12 KB

Versions: 28

Compression:

Stored size: 1.12 KB

Contents

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

module JsDuck
  module Js

    # Looks the AST of a FunctionDeclaration or FunctionExpression for
    # uses of this.fireEvent().
    class Fires
      include Util::Singleton

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

        events = []
        @traverser.traverse(function_node["body"]) do |node|
          if fire_event?(node)
            events << node["arguments"][0].to_value
          end
        end

        events.sort.uniq
      end

      private

      # True when node is this.fireEvent("name") call.
      # Also true for me.fireEvent() when me == this.
      def fire_event?(node)
        node.call_expression? &&
          node["callee"].member_expression? &&
          @traverser.this?(node["callee"]["object"].to_s) &&
          node["callee"]["property"].to_s == "fireEvent" &&
          node["arguments"].length > 0 &&
          node["arguments"][0].value_type == "String"
      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/fires.rb
solvas-jsduck-6.0.0.9571 lib/jsduck/js/fires.rb
solvas-jsduck-6.0.0.6154 lib/jsduck/js/fires.rb
solvas-jsduck-6.0.0.4021 lib/jsduck/js/fires.rb
solvas-jsduck-6.0.0.2554 lib/jsduck/js/fires.rb
solvas-jsduck-6.0.0.1891 lib/jsduck/js/fires.rb
solvas-jsduck-6.0.0.beta.1888 lib/jsduck/js/fires.rb
jsduck-troopjs-0.0.10 lib/jsduck/js/fires.rb
jsduck-troopjs-0.0.9 lib/jsduck/js/fires.rb
jsduck-troopjs-0.0.8 lib/jsduck/js/fires.rb
jsduck-troopjs-0.0.7 lib/jsduck/js/fires.rb
jsduck-troopjs-0.0.5 lib/jsduck/js/fires.rb
jsduck-troopjs-0.0.4 lib/jsduck/js/fires.rb
jsduck-troopjs-0.0.3 lib/jsduck/js/fires.rb
jsduck-troopjs-0.0.1 lib/jsduck/js/fires.rb
jsduck-6.0.0beta lib/jsduck/js/fires.rb
jsduck-5.3.4 lib/jsduck/js/fires.rb
jsduck-5.3.3 lib/jsduck/js/fires.rb
jsduck-5.3.2 lib/jsduck/js/fires.rb
jsduck-5.3.1 lib/jsduck/js/fires.rb