Loading lib/jsduck/function_ast.rb +18 −0 Original line number Diff line number Diff line Loading @@ -75,6 +75,8 @@ module JsDuck :this elsif boolean?(ast) "Boolean" elsif string?(ast) "String" else :other end Loading Loading @@ -112,6 +114,22 @@ module JsDuck ast["type"] == "Literal" && (ast["value"] == true || ast["value"] == false) end def string?(ast) if string_literal?(ast) true elsif ast["type"] == "BinaryExpression" && ast["operator"] == "+" string?(ast["left"]) || string?(ast["right"]) elsif ast["type"] == "UnaryExpression" && ast["operator"] == "typeof" true else false end end def string_literal?(ast) ast["type"] == "Literal" && ast["value"].is_a?(String) end def control_flow?(ast) CONTROL_FLOW[ast["type"]] end Loading spec/function_ast_spec.rb +22 −0 Original line number Diff line number Diff line Loading @@ -285,4 +285,26 @@ describe "JsDuck::FunctionAst#return_types" do end end describe "returns ['String'] when function body" do it "returns a string literal" do returns("/** */ function foo() { return 'foo'; }").should == ["String"] end it "returns a string concatenation" do returns("/** */ function foo() { return 'foo' + 'bar'; }").should == ["String"] end it "returns a string concatenated with number" do returns("/** */ function foo() { return 'foo' + 7; }").should == ["String"] end it "returns a number concatenated with string" do returns("/** */ function foo() { return 8 + 'foo'; }").should == ["String"] end it "returns a typeof expression" do returns("/** */ function foo() { return typeof 8; }").should == ["String"] end end end Loading
lib/jsduck/function_ast.rb +18 −0 Original line number Diff line number Diff line Loading @@ -75,6 +75,8 @@ module JsDuck :this elsif boolean?(ast) "Boolean" elsif string?(ast) "String" else :other end Loading Loading @@ -112,6 +114,22 @@ module JsDuck ast["type"] == "Literal" && (ast["value"] == true || ast["value"] == false) end def string?(ast) if string_literal?(ast) true elsif ast["type"] == "BinaryExpression" && ast["operator"] == "+" string?(ast["left"]) || string?(ast["right"]) elsif ast["type"] == "UnaryExpression" && ast["operator"] == "typeof" true else false end end def string_literal?(ast) ast["type"] == "Literal" && ast["value"].is_a?(String) end def control_flow?(ast) CONTROL_FLOW[ast["type"]] end Loading
spec/function_ast_spec.rb +22 −0 Original line number Diff line number Diff line Loading @@ -285,4 +285,26 @@ describe "JsDuck::FunctionAst#return_types" do end end describe "returns ['String'] when function body" do it "returns a string literal" do returns("/** */ function foo() { return 'foo'; }").should == ["String"] end it "returns a string concatenation" do returns("/** */ function foo() { return 'foo' + 'bar'; }").should == ["String"] end it "returns a string concatenated with number" do returns("/** */ function foo() { return 'foo' + 7; }").should == ["String"] end it "returns a number concatenated with string" do returns("/** */ function foo() { return 8 + 'foo'; }").should == ["String"] end it "returns a typeof expression" do returns("/** */ function foo() { return typeof 8; }").should == ["String"] end end end