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

Improved default value detection.

Now also detecting regexes.
parent e7ffd854
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -219,10 +219,14 @@ module JsDuck
        :tagname => :property,
        :name => name,
        :type => make_value_type(ast),
        :default => ast ? to_s(ast) : nil,
        :default => make_default(ast),
      }
    end

    def make_default(ast)
      ast && to_value(ast) ? to_s(ast) : nil
    end

    def make_value_type(ast)
      if ast
        v = to_value(ast)
@@ -236,7 +240,7 @@ module JsDuck
          "Array"
        elsif v.is_a?(Hash)
          "Object"
        elsif v == nil && ast["type"] == "Literal" && ast["raw"] =~ /\A\//
        elsif v == :regexp
          "RegExp"
        else
          nil
+7 −2
Original line number Diff line number Diff line
@@ -6,7 +6,8 @@ module JsDuck
    # Converts AST node into a value.
    #
    # - String literals become Ruby strings
    # - Number literals become Ruby number
    # - Number literals become Ruby numbers
    # - Regex literals become :regexp symbols
    # - Array expressions become Ruby arrays
    # - etc
    #
@@ -26,7 +27,11 @@ module JsDuck
        end
        h
      when "Literal"
        if ast["value"] == nil && ast["raw"] =~ /\A\//
          :regexp
        else
          ast["value"]
        end
      else
        throw "Unknown node type: " + ast["type"]
      end
+18 −0
Original line number Diff line number Diff line
@@ -100,6 +100,10 @@ describe "JsDuck::Ast detects property with" do
      detect("/** */ foo = 15;")[:default].should == "15"
    end

    it "assignment with regex" do
      detect("/** */ foo = /abc/;")[:default].should == "/abc/"
    end

    it "assignment with object" do
      detect("/** */ foo = {bar: 5};")[:default].should == "{bar: 5}"
    end
@@ -109,4 +113,18 @@ describe "JsDuck::Ast detects property with" do
    end
  end

  describe "no default value in" do
    it "var without initialization" do
      detect("/** */ var foo;")[:default].should == nil
    end

    it "assignment of function call" do
      detect("/** */ foo = bar();")[:default].should == nil
    end

    it "object property with array containing function" do
      detect("X = { /** */ foo: [1, 2, function(){}] };")[:default].should == nil
    end
  end

end