Commit dc3a19c6 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Split up long detection method in Js::Ast class.

Significantly improve readability.
parent 287dff27
Loading
Loading
Loading
Loading
+40 −9
Original line number Diff line number Diff line
@@ -53,6 +53,22 @@ module JsDuck
        exp = ast.expression_statement? ? ast["expression"] : nil
        var = ast.variable_declaration? ? ast["declarations"][0] : nil

        if doc = detect_class(ast, exp, var)
          doc
        elsif doc = detect_method(ast, exp, var)
          doc
        elsif doc = detect_event(ast, exp, var)
          doc
        elsif doc = detect_property(ast, exp, var)
          doc
        else
          make_property()
        end
      end

      private

      def detect_class(ast, exp, var)
        # Ext.define("Class", {})
        if exp && exp.ext_define?
          make_class(exp["arguments"][0].to_value, exp)
@@ -85,8 +101,14 @@ module JsDuck
        elsif ast.object_expression?
          make_class("", ast)

        else
          nil
        end
      end

      def detect_method(ast, exp, var)
        # function foo() {}
        elsif ast.function?
        if ast.function?
          make_method(ast["id"].to_s || "", ast)

          # foo = function() {}
@@ -105,12 +127,23 @@ module JsDuck
        elsif ast.property? && ast["value"].function?
          make_method(ast["key"].key_value, ast["value"])

        else
          nil
        end
      end

      # this.fireEvent("foo", ...)
        elsif exp && exp.fire_event?
      def detect_event(ast, exp, var)
        if exp && exp.fire_event?
          make_event(exp["arguments"][0].to_value)
        else
          nil
        end
      end

      def detect_property(ast, exp, var)
        # foo = ...
        elsif exp && exp.assignment_expression?
        if exp && exp.assignment_expression?
          make_property(exp["left"].to_s, exp["right"])

          # var foo = ...
@@ -134,12 +167,10 @@ module JsDuck
          make_property(exp.to_value)

        else
          make_property()
          nil
        end
      end

      private

      # Class name begins with upcase char
      def class_name?(name)
        return name.split(/\./).last =~ /\A[A-Z]/