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

Detection of :mixins by Ast class.

parent 17f3e477
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -126,6 +126,7 @@ module JsDuck
          cls[:extends] = make_extends(cfg["extend"])
          cls[:requires] = make_requires(cfg["requires"])
          cls[:uses] = make_requires(cfg["uses"])
          cls[:mixins] = make_mixins(cfg["mixins"])
        end
      end

@@ -148,6 +149,15 @@ module JsDuck
      return classes.all? {|c| c.is_a? String } ? classes : []
    end

    def make_mixins(cfg_value)
      return [] unless cfg_value

      v = to_value(cfg_value)
      classes = v.is_a?(Hash) ? v.values : Array(v)

      return classes.all? {|c| c.is_a? String } ? classes : []
    end

    def object_expression_to_hash(ast)
      h = {}
      if ast && ast["type"] == "ObjectExpression"
+8 −0
Original line number Diff line number Diff line
@@ -17,6 +17,14 @@ module JsDuck
      case ast["type"]
      when "ArrayExpression"
        ast["elements"].map {|e| to_value(e) }
      when "ObjectExpression"
        h = {}
        ast["properties"].each do |p|
          key = p["key"]["type"] == "Identifier" ? p["key"]["name"] : to_value(p["key"])
          value = to_value(p["value"])
          h[key] = value
        end
        h
      when "Identifier"
        ast["name"]
      when "Literal"
+51 −0
Original line number Diff line number Diff line
@@ -158,4 +158,55 @@ describe "JsDuck::Ast detects class with" do
      EOS
    end
  end

  describe "mixins in" do
    it "Ext.define() with mixins as string" do
      detect(<<-EOS)[:mixins].should == ["Some.Class", "Other.Class"]
        /** */
        Ext.define('MyClass', {
            mixins: ["Some.Class", "Other.Class"]
        });
      EOS
    end

    it "Ext.define() with mixins as array of strings" do
      detect(<<-EOS)[:mixins].should == ["Other.Class"]
        /** */
        Ext.define('MyClass', {
            mixins: "Other.Class"
        });
      EOS
    end

    it "Ext.define() with mixins as object" do
      detect(<<-EOS)[:mixins].should == ["Some.Class", "Other.Class"]
        /** */
        Ext.define('MyClass', {
            mixins: {
                some: "Some.Class",
                other: "Other.Class"
            }
        });
      EOS
    end
  end

  describe "no mixins in" do
    it "Ext.define() without mixins" do
      detect(<<-EOS)[:mixins].should == []
        /** */
        Ext.define('MyClass', {
        });
      EOS
    end

    it "Ext.define() with mixins as nested object" do
      detect(<<-EOS)[:mixins].should == []
        /** */
        Ext.define('MyClass', {
            mixins: {foo: {bar: "foo"}}
        });
      EOS
    end
  end
end