Loading lib/jsduck/ast.rb +22 −0 Original line number Diff line number Diff line Loading @@ -210,9 +210,31 @@ module JsDuck return { :type => :property, :name => name, :value_type => make_value_type(ast) } end def make_value_type(ast) if ast v = to_value(ast) if v.is_a?(String) "String" elsif v.is_a?(Numeric) "Number" elsif v.is_a?(TrueClass) || v.is_a?(FalseClass) "Boolean" elsif v.is_a?(Array) "Array" elsif v.is_a?(Hash) "Object" else nil end else nil end end # -- various helper methods -- # Turns ObjectExpression into Ruby Hash for easy lookup. The keys Loading spec/ast_property_spec.rb +36 −0 Original line number Diff line number Diff line Loading @@ -51,4 +51,40 @@ describe "JsDuck::Ast detects property with" do end end describe "value type in var initialized with" do it "int" do detect("/** */ var foo = 5;")[:value_type].should == "Number" end it "float" do detect("/** */ var foo = 0.5;")[:value_type].should == "Number" end it "string" do detect("/** */ var foo = 'haa';")[:value_type].should == "String" end it "true" do detect("/** */ var foo = true;")[:value_type].should == "Boolean" end it "false" do detect("/** */ var foo = false;")[:value_type].should == "Boolean" end it "array" do detect("/** */ var foo = [];")[:value_type].should == "Array" end it "object" do detect("/** */ var foo = {};")[:value_type].should == "Object" end end describe "no value type in" do it "uninitialized var declaration" do detect("/** */ var foo;")[:value_type].should == nil end end end Loading
lib/jsduck/ast.rb +22 −0 Original line number Diff line number Diff line Loading @@ -210,9 +210,31 @@ module JsDuck return { :type => :property, :name => name, :value_type => make_value_type(ast) } end def make_value_type(ast) if ast v = to_value(ast) if v.is_a?(String) "String" elsif v.is_a?(Numeric) "Number" elsif v.is_a?(TrueClass) || v.is_a?(FalseClass) "Boolean" elsif v.is_a?(Array) "Array" elsif v.is_a?(Hash) "Object" else nil end else nil end end # -- various helper methods -- # Turns ObjectExpression into Ruby Hash for easy lookup. The keys Loading
spec/ast_property_spec.rb +36 −0 Original line number Diff line number Diff line Loading @@ -51,4 +51,40 @@ describe "JsDuck::Ast detects property with" do end end describe "value type in var initialized with" do it "int" do detect("/** */ var foo = 5;")[:value_type].should == "Number" end it "float" do detect("/** */ var foo = 0.5;")[:value_type].should == "Number" end it "string" do detect("/** */ var foo = 'haa';")[:value_type].should == "String" end it "true" do detect("/** */ var foo = true;")[:value_type].should == "Boolean" end it "false" do detect("/** */ var foo = false;")[:value_type].should == "Boolean" end it "array" do detect("/** */ var foo = [];")[:value_type].should == "Array" end it "object" do detect("/** */ var foo = {};")[:value_type].should == "Object" end end describe "no value type in" do it "uninitialized var declaration" do detect("/** */ var foo;")[:value_type].should == nil end end end