Loading lib/jsduck/ast.rb +6 −2 Original line number Diff line number Diff line Loading @@ -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) Loading @@ -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 Loading lib/jsduck/evaluator.rb +7 −2 Original line number Diff line number Diff line Loading @@ -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 # Loading @@ -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 Loading spec/ast_property_spec.rb +18 −0 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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 Loading
lib/jsduck/ast.rb +6 −2 Original line number Diff line number Diff line Loading @@ -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) Loading @@ -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 Loading
lib/jsduck/evaluator.rb +7 −2 Original line number Diff line number Diff line Loading @@ -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 # Loading @@ -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 Loading
spec/ast_property_spec.rb +18 −0 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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