Commit 8fcc2b02 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Move all property auto-detection to Js::Property.

parent aa441e47
Loading
Loading
Loading
Loading
+6 −39
Original line number Diff line number Diff line
require "jsduck/js/method"
require "jsduck/js/event"
require "jsduck/js/property"
require "jsduck/js/node"
require "jsduck/tag_registry"

@@ -114,33 +115,7 @@ module JsDuck
      end

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

          # var foo = ...
        elsif var
          make_property(var["id"].to_s, var["init"])

          # foo: ...
        elsif ast.property?
          make_property(ast["key"].key_value, ast["value"])

          # foo;
        elsif exp && exp.identifier?
          make_property(exp.to_s)

          # "foo"  (inside some expression)
        elsif ast.string?
          make_property(ast.to_value)

          # "foo";  (as a statement of it's own)
        elsif exp && exp.string?
          make_property(exp.to_value)

        else
          nil
        end
        Js::Property.detect(ast, exp, var)
      end

      # Class name begins with upcase char
@@ -248,7 +223,8 @@ module JsDuck
        configs = []

        ast.each_property do |name, value, pair|
          cfg = make_property(name, value, :cfg)
          cfg = make_property(name, value)
          cfg[:tagname] = :cfg
          cfg.merge!(defaults)
          configs << cfg if apply_autodetected(cfg, pair)
        end
@@ -319,17 +295,8 @@ module JsDuck
        Js::Method.make(name, ast)
      end

      def make_property(name=nil, ast=nil, tagname=:property)
        return {
          :tagname => tagname,
          :name => name,
          :type => ast && ast.value_type,
          :default => ast && make_default(ast),
        }
      end

      def make_default(ast)
        ast.to_value != nil ? ast.to_s : nil
      def make_property(name=nil, ast=nil)
        Js::Property.make(name, ast)
      end

    end
+61 −0
Original line number Diff line number Diff line
require "jsduck/util/singleton"

module JsDuck
  module Js

    # Auto-detection of properties.
    class Property
      include Util::Singleton

      # Checks if AST node is a property, and if so, returns doc-hash
      # with property name and various auto-detected attributes.
      # When not a property returns nil.
      def detect(ast, exp, var)
        # foo = ...
        if exp && exp.assignment_expression?
          make(exp["left"].to_s, exp["right"])

          # var foo = ...
        elsif var
          make(var["id"].to_s, var["init"])

          # foo: ...
        elsif ast.property?
          make(ast["key"].key_value, ast["value"])

          # foo;
        elsif exp && exp.identifier?
          make(exp.to_s)

          # "foo"  (inside some expression)
        elsif ast.string?
          make(ast.to_value)

          # "foo";  (as a statement of it's own)
        elsif exp && exp.string?
          make(exp.to_value)

        else
          nil
        end
      end

      # Produces a doc-hash for a property.
      def make(name=nil, ast=nil)
        return {
          :tagname => :property,
          :name => name,
          :type => ast && ast.value_type,
          :default => ast && default(ast),
        }
      end

      private

      def default(ast)
        ast.to_value != nil ? ast.to_s : nil
      end

    end
  end
end