Commit 31bf26a2 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Integrate FunctionAst to detect chainable from code.

parent 4a1c740c
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
require "jsduck/serializer"
require "jsduck/evaluator"
require "jsduck/function_ast"

module JsDuck

@@ -392,7 +393,8 @@ module JsDuck
      return {
        :tagname => :method,
        :name => name,
        :params => make_params(ast)
        :params => make_params(ast),
        :chainable => chainable?(ast),
      }
    end

@@ -404,6 +406,10 @@ module JsDuck
      end
    end

    def chainable?(ast)
      FunctionAst.new.returns(ast) == "this"
    end

    def make_property(name=nil, ast=nil, tagname=:property)
      return {
        :tagname => tagname,
+1 −0
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ module JsDuck
    def merge_like_method(docs, code)
      h = do_merge(docs, code)
      h[:params] = merge_params(docs, code)
      h[:meta][:chainable] = code[:chainable] if code[:chainable]
      h
    end

+21 −0
Original line number Diff line number Diff line
@@ -159,4 +159,25 @@ describe JsDuck::Aggregator do
    end
  end

  describe "function with 'return this;' in code" do
    let(:cls) do
      parse(<<-EOS)["MyClass"]
        /** */
        Ext.define("MyClass", {
            /** */
            bar: function() { return this; }
        });
      EOS
    end

    it "adds @chainable tag" do
      cls[:members][0][:meta][:chainable].should == true
    end

    it "adds @return {MyClass} this" do
      cls[:members][0][:return][:type].should == "MyClass"
      cls[:members][0][:return][:doc].should == "this"
    end
  end

end