Commit 85cc9795 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Move node body extraction to Node class.

parent 8a317cad
Loading
Loading
Loading
Loading
+1 −26
Original line number Diff line number Diff line
require "jsduck/util/singleton"
require "jsduck/js/node_array"

module JsDuck
  module Js
@@ -31,7 +30,7 @@ module JsDuck
            var_name = node["id"].to_s
            @this_map[var_name] = true
          else
            events.concat(detect_body(extract_body(node)))
            events.concat(detect_body(node.body))
          end
        end

@@ -54,30 +53,6 @@ module JsDuck
        node.type == "VariableDeclarator" && node["init"].type == "ThisExpression"
      end

      # Extracts all sub-statements and sub-expressions from AST node.
      # Without looking at the type of node, we just take all the
      # sub-hashes and -arrays.
      #
      # A downside of this simple algorithm is that the statements can
      # end up in different order than they are in source code.  For
      # example the IfStatement has three parts in the following
      # order: "test", "consequent", "alternate": But because we're
      # looping over a hash, they might end up in a totally different
      # order.
      def extract_body(node)
        body = []
        node.raw.each_pair do |key, value|
          if key == "type" || key == "range"
            # ignore
          elsif value.is_a?(Array)
            node[key].each {|n| body << n }
          elsif value.is_a?(Hash)
            body << node[key]
          end
        end
        body
      end

    end
  end
end
+1 −25
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ module JsDuck
            @this_map[var_name] = true
          end

          methods.concat(detect_body(extract_body(node)))
          methods.concat(detect_body(node.body))
        end

        methods
@@ -53,30 +53,6 @@ module JsDuck
        node.type == "VariableDeclarator" && node["init"].type == "ThisExpression"
      end

      # Extracts all sub-statements and sub-expressions from AST node.
      # Without looking at the type of node, we just take all the
      # sub-hashes and -arrays.
      #
      # A downside of this simple algorithm is that the statements can
      # end up in different order than they are in source code.  For
      # example the IfStatement has three parts in the following
      # order: "test", "consequent", "alternate": But because we're
      # looping over a hash, they might end up in a totally different
      # order.
      def extract_body(node)
        body = []
        node.raw.each_pair do |key, value|
          if key == "type" || key == "range"
            # ignore
          elsif value.is_a?(Array)
            node[key].each {|n| body << n }
          elsif value.is_a?(Hash)
            body << node[key]
          end
        end
        body
      end

    end
  end
end
+24 −0
Original line number Diff line number Diff line
@@ -85,6 +85,30 @@ module JsDuck
        @node["type"]
      end

      # Extracts all sub-statements and sub-expressions from AST node.
      # Without looking at the type of node, we just take all the
      # sub-hashes and -arrays.
      #
      # A downside of this simple algorithm is that the statements can
      # end up in different order than they are in source code.  For
      # example the IfStatement has three parts in the following
      # order: "test", "consequent", "alternate": But because we're
      # looping over a hash, they might end up in a totally different
      # order.
      def body
        body = []
        @node.each_pair do |key, value|
          if key == "type" || key == "range"
            # ignore
          elsif value.is_a?(Array)
            body.concat(value.map {|v| Js::Node.create(v) })
          elsif value.is_a?(Hash)
            body << Js::Node.create(value)
          end
        end
        body
      end

      # Iterates over keys and values in ObjectExpression.  The keys
      # are turned into strings, but values are left as is for further
      # processing.