feat: [ts] add normal function decl

上级 2f90d4cf
namespace StringUtility
{
function ToCapital(str: string): string {
......
......@@ -49,6 +49,10 @@ func (j *JClassNode) BuildStringMethodMap(projectMethods map[string]string) {
}
}
func (j *JClassNode) IsNotEmpty() bool {
return len(j.Methods) > 0 || len(j.MethodCalls) > 0
}
func BuildCallMethodMap(deps []JClassNode) map[string]JMethod {
var callMethodMap = make(map[string]JMethod)
for _, clz := range deps {
......
......@@ -52,8 +52,9 @@ func Test_TypeScriptMultipleClass(t *testing.T) {
results := app.Analysis(string(code))
g.Expect(len(results)).To(Equal(3))
g.Expect(len(results)).To(Equal(4))
g.Expect(results[1].Implements[0]).To(Equal("IPerson"))
g.Expect(results[3].Class).To(Equal("default"))
}
func Test_ShouldEnableGetClassMethod(t *testing.T) {
......@@ -71,3 +72,19 @@ class Employee {
g.Expect(len(results[0].Methods)).To(Equal(1))
}
func Test_ShouldGetDefaultFunctionName(t *testing.T) {
g := NewGomegaWithT(t)
app := new(TypeScriptApiApp)
results := app.Analysis(`
function Sum(x: number, y: number) : void {
console.log('processNumKeyPairs: key = ' + key + ', value = ' + value)
return x + y;
}
`)
g.Expect(len(results[0].Methods)).To(Equal(1))
g.Expect(results[0].Methods[0].Name).To(Equal("Sum"))
}
......@@ -23,6 +23,15 @@ func NewTypeScriptIdentListener() *TypeScriptIdentListener {
return &TypeScriptIdentListener{}
}
func (s *TypeScriptIdentListener) GetNodeInfo() []domain.JClassNode {
if currentNode.IsNotEmpty() {
currentNode.Class = default_class
classNodes = append(classNodes, *currentNode)
currentNode = domain.NewClassNode()
}
return classNodes
}
func (s *TypeScriptIdentListener) EnterProgram(ctx *parser.ProgramContext) {
}
......@@ -123,10 +132,6 @@ func exitClass() {
}
func (s *TypeScriptIdentListener) EnterArgumentsExpression(ctx *parser.ArgumentsExpressionContext) {
if currentNode.Class == "" {
currentNode.Class = default_class
}
if reflect.TypeOf(ctx.GetChild(0)).String() == "*parser.MemberDotExpressionContext" {
memberDotExprCtx := ctx.GetChild(0).(*parser.MemberDotExpressionContext)
buildMemberDotExpr(memberDotExprCtx)
......@@ -145,10 +150,11 @@ func (s *TypeScriptIdentListener) EnterMemberDotExpression(ctx *parser.MemberDot
}
func (s *TypeScriptIdentListener) GetNodeInfo() []domain.JClassNode {
if currentNode.Class == default_class {
classNodes = append(classNodes, *currentNode)
currentNode = domain.NewClassNode()
}
return classNodes
}
func (s *TypeScriptIdentListener) EnterFunctionDeclaration(ctx *parser.FunctionDeclarationContext) {
method := domain.NewJMethod()
method.Name = ctx.Identifier().GetText()
method.AddPosition(ctx.GetChild(0).GetParent().(*antlr.BaseParserRuleContext))
currentNode.Methods = append(currentNode.Methods, method)
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册