Loading lib/jsduck/ast.rb +38 −3 Original line number Diff line number Diff line Loading @@ -34,7 +34,7 @@ module JsDuck make_class(to_s(exp["left"])) # var foo = Ext.extend("Parent", {}) elsif var && ext_extend?(var["init"]) elsif var && var["init"] && ext_extend?(var["init"]) make_class(to_s(var["id"]), var["init"]) # var Foo = ... Loading @@ -54,15 +54,35 @@ module JsDuck make_method(to_s(exp["left"]), exp["right"]) # var foo = function() {} elsif var && function?(var["init"]) elsif var && var["init"] && function?(var["init"]) make_method(to_s(var["id"]), var["init"]) # foo: function() {} elsif property?(ast) && function?(ast["value"]) make_method(key_value(ast["key"]), ast["value"]) # foo = ... elsif exp && assignment?(exp) make_property(to_s(exp["left"]), exp["right"]) # var foo = ... elsif var make_property(to_s(var["id"]), var["init"]) # foo: ... elsif property?(ast) make_property(key_value(ast["key"]), ast["value"]) # foo; elsif exp && ident?(exp) make_property(to_s(exp)) # "foo"; elsif exp && string?(exp) make_property(to_value(exp)) else {:type => :property} make_property() end end Loading Loading @@ -104,6 +124,14 @@ module JsDuck ast["type"] == "Property" end def ident?(ast) ast["type"] == "Identifier" end def string?(ast) ast["type"] == "Literal" && ast["value"].is_a?(String) end # Class name begins with upcase char def class_name?(name) return name.split(/\./).last =~ /\A[A-Z]/ Loading Loading @@ -166,6 +194,13 @@ module JsDuck } end def make_property(name=nil, ast=nil) return { :type => :property, :name => name, } end # -- various helper methods -- # Turns ObjectExpression into Ruby Hash for easy lookup. The keys Loading spec/ast_property_spec.rb 0 → 100644 +54 −0 Original line number Diff line number Diff line require "jsduck/ast" require "jsduck/esprima_parser" describe "JsDuck::Ast detects property with" do def detect(string) node = JsDuck::EsprimaParser.new(string).parse[0] return JsDuck::Ast.new.detect(node[:code]) end describe "name in" do it "var declaration" do detect("/** */ var foo;")[:name].should == "foo" end it "var declaration with initialization" do detect("/** */ foo = 5;")[:name].should == "foo" end it "assignment to var" do detect("/** */ foo = 5;")[:name].should == "foo" end it "assignment to object property" do detect("/** */ foo.bar.baz = 5;")[:name].should == "foo.bar.baz" end it "object property" do detect(<<-EOS)[:name].should == "foo" Foo = { /** */ foo: 5 }; EOS end it "object with string key" do detect(<<-EOS)[:name].should == "foo" Foo = { /** */ "foo": 5 }; EOS end it "lonely identifier" do detect("/** */ foo;")[:name].should == "foo" end it "lonely string" do detect("/** */ 'foo';")[:name].should == "foo" end end end Loading
lib/jsduck/ast.rb +38 −3 Original line number Diff line number Diff line Loading @@ -34,7 +34,7 @@ module JsDuck make_class(to_s(exp["left"])) # var foo = Ext.extend("Parent", {}) elsif var && ext_extend?(var["init"]) elsif var && var["init"] && ext_extend?(var["init"]) make_class(to_s(var["id"]), var["init"]) # var Foo = ... Loading @@ -54,15 +54,35 @@ module JsDuck make_method(to_s(exp["left"]), exp["right"]) # var foo = function() {} elsif var && function?(var["init"]) elsif var && var["init"] && function?(var["init"]) make_method(to_s(var["id"]), var["init"]) # foo: function() {} elsif property?(ast) && function?(ast["value"]) make_method(key_value(ast["key"]), ast["value"]) # foo = ... elsif exp && assignment?(exp) make_property(to_s(exp["left"]), exp["right"]) # var foo = ... elsif var make_property(to_s(var["id"]), var["init"]) # foo: ... elsif property?(ast) make_property(key_value(ast["key"]), ast["value"]) # foo; elsif exp && ident?(exp) make_property(to_s(exp)) # "foo"; elsif exp && string?(exp) make_property(to_value(exp)) else {:type => :property} make_property() end end Loading Loading @@ -104,6 +124,14 @@ module JsDuck ast["type"] == "Property" end def ident?(ast) ast["type"] == "Identifier" end def string?(ast) ast["type"] == "Literal" && ast["value"].is_a?(String) end # Class name begins with upcase char def class_name?(name) return name.split(/\./).last =~ /\A[A-Z]/ Loading Loading @@ -166,6 +194,13 @@ module JsDuck } end def make_property(name=nil, ast=nil) return { :type => :property, :name => name, } end # -- various helper methods -- # Turns ObjectExpression into Ruby Hash for easy lookup. The keys Loading
spec/ast_property_spec.rb 0 → 100644 +54 −0 Original line number Diff line number Diff line require "jsduck/ast" require "jsduck/esprima_parser" describe "JsDuck::Ast detects property with" do def detect(string) node = JsDuck::EsprimaParser.new(string).parse[0] return JsDuck::Ast.new.detect(node[:code]) end describe "name in" do it "var declaration" do detect("/** */ var foo;")[:name].should == "foo" end it "var declaration with initialization" do detect("/** */ foo = 5;")[:name].should == "foo" end it "assignment to var" do detect("/** */ foo = 5;")[:name].should == "foo" end it "assignment to object property" do detect("/** */ foo.bar.baz = 5;")[:name].should == "foo.bar.baz" end it "object property" do detect(<<-EOS)[:name].should == "foo" Foo = { /** */ foo: 5 }; EOS end it "object with string key" do detect(<<-EOS)[:name].should == "foo" Foo = { /** */ "foo": 5 }; EOS end it "lonely identifier" do detect("/** */ foo;")[:name].should == "foo" end it "lonely string" do detect("/** */ 'foo';")[:name].should == "foo" end end end