diff --git a/pkg/domain/jmethod.go b/pkg/domain/jmethod.go index dd09b4133c70c96974583e2d8bd4f8e017aac686..e6be54cb0cb4a572a6a4e07ee6e1adfd41b60fd8 100644 --- a/pkg/domain/jmethod.go +++ b/pkg/domain/jmethod.go @@ -24,18 +24,7 @@ type JMethod struct { } func NewJMethod() JMethod { - return JMethod{ - Name: "", - Type: "", - Annotations: nil, - StartLine: 0, - StartLinePosition: 0, - StopLine: 0, - StopLinePosition: 0, - Parameters: nil, - MethodCalls: nil, - IsConstructor: false, - } + return JMethod{} } type JMethodInfo struct { diff --git a/pkg/domain/trial/code_function.go b/pkg/domain/trial/code_function.go index 18d073b12b7e8371313f841fd6335763ba5a9017..9b7822b52a758a2c1e746b8f20787b9396a55b1f 100644 --- a/pkg/domain/trial/code_function.go +++ b/pkg/domain/trial/code_function.go @@ -10,4 +10,5 @@ type CodeFunction struct { Modifiers []string Creators []CodeDataStruct InnerFunctions []CodeFunction + CodePosition CodePosition } diff --git a/trial/cocago/testdata/node_infos/hello_world.json b/trial/cocago/testdata/node_infos/hello_world.json index f35bb0d3220651ef3a30ba55003e96a283142a24..11c28c371e7144ec07bd6758102e64f6b66fb970 100644 --- a/trial/cocago/testdata/node_infos/hello_world.json +++ b/trial/cocago/testdata/node_infos/hello_world.json @@ -19,6 +19,12 @@ "FunctionNodes": [ { "Annotations": null, + "CodePosition": { + "StartLine": 0, + "StartLinePosition": 0, + "StopLine": 0, + "StopLinePosition": 0 + }, "Creators": null, "InnerFunctions": null, "MethodCalls": [ diff --git a/trial/cocago/testdata/node_infos/nested_method.json b/trial/cocago/testdata/node_infos/nested_method.json index bb0acd78becc16e23b52468ae5cd93379db56e7e..0af95e6108c16bcd1447f1f2a257b1588fa0f505 100644 --- a/trial/cocago/testdata/node_infos/nested_method.json +++ b/trial/cocago/testdata/node_infos/nested_method.json @@ -19,6 +19,12 @@ "FunctionNodes": [ { "Annotations": null, + "CodePosition": { + "StartLine": 0, + "StartLinePosition": 0, + "StopLine": 0, + "StopLinePosition": 0 + }, "Creators": null, "InnerFunctions": null, "MethodCalls": [ diff --git a/trial/cocago/testdata/node_infos/normal_method.json b/trial/cocago/testdata/node_infos/normal_method.json index 0249aa0b40ad61a7e4a9d686d17aa3a0ca9c00fb..a9da93d199acebb2d23193171a26081fddd698c5 100644 --- a/trial/cocago/testdata/node_infos/normal_method.json +++ b/trial/cocago/testdata/node_infos/normal_method.json @@ -26,6 +26,12 @@ "FunctionNodes": [ { "Annotations": null, + "CodePosition": { + "StartLine": 0, + "StartLinePosition": 0, + "StopLine": 0, + "StopLinePosition": 0 + }, "Creators": null, "InnerFunctions": null, "MethodCalls": null, @@ -63,6 +69,12 @@ }, { "Annotations": null, + "CodePosition": { + "StartLine": 0, + "StartLinePosition": 0, + "StopLine": 0, + "StopLinePosition": 0 + }, "Creators": null, "InnerFunctions": null, "MethodCalls": null, @@ -117,6 +129,12 @@ "FunctionNodes": [ { "Annotations": null, + "CodePosition": { + "StartLine": 0, + "StartLinePosition": 0, + "StopLine": 0, + "StopLinePosition": 0 + }, "Creators": null, "InnerFunctions": null, "MethodCalls": null, @@ -154,6 +172,12 @@ }, { "Annotations": null, + "CodePosition": { + "StartLine": 0, + "StartLinePosition": 0, + "StopLine": 0, + "StopLinePosition": 0 + }, "Creators": null, "InnerFunctions": null, "MethodCalls": null, diff --git a/trial/cocago/testdata/node_infos/struct_type_zero.json b/trial/cocago/testdata/node_infos/struct_type_zero.json index e7ee589cf367f78c4072417e293c199a0cdd9dd6..ca18509409f9c77f01412a235ceeab05c589b2b6 100644 --- a/trial/cocago/testdata/node_infos/struct_type_zero.json +++ b/trial/cocago/testdata/node_infos/struct_type_zero.json @@ -25,6 +25,12 @@ "FunctionNodes": [ { "Annotations": null, + "CodePosition": { + "StartLine": 0, + "StartLinePosition": 0, + "StopLine": 0, + "StopLinePosition": 0 + }, "Creators": null, "InnerFunctions": null, "MethodCalls": null, diff --git a/trial/cocago/testdata/node_infos/struct_with_func_decl.json b/trial/cocago/testdata/node_infos/struct_with_func_decl.json index 3ef834001ea1400e7262dd3fc14d6a64f4da07c1..e157328f4cccd06d8ce91d5fc99b17486794ab3f 100644 --- a/trial/cocago/testdata/node_infos/struct_with_func_decl.json +++ b/trial/cocago/testdata/node_infos/struct_with_func_decl.json @@ -58,6 +58,12 @@ "FunctionNodes": [ { "Annotations": null, + "CodePosition": { + "StartLine": 0, + "StartLinePosition": 0, + "StopLine": 0, + "StopLinePosition": 0 + }, "Creators": null, "InnerFunctions": null, "MethodCalls": [ diff --git a/trial/pkg/application/ts/ts_ident_app_test.go b/trial/pkg/application/ts/ts_ident_app_test.go index 524e38b98e3752a422b0d0e122d40479bc62c8e7..cf7d0b46471e1b65dec2ee6f10c53dd9e651b606 100644 --- a/trial/pkg/application/ts/ts_ident_app_test.go +++ b/trial/pkg/application/ts/ts_ident_app_test.go @@ -78,7 +78,7 @@ func Test_ShouldGetClassFromModule(t *testing.T) { for _, node := range results.ClassNodes { fmt.Println(node) } - g.Expect(len(results.ClassNodes)).To(Equal(2)) + g.Expect(len(results.ClassNodes)).To(Equal(1)) g.Expect(results.ClassNodes[0].Class).To(Equal("Employee")) } @@ -88,15 +88,16 @@ func Test_ShouldEnableGetClassMethod(t *testing.T) { app := new(TypeScriptApiApp) - results := app.Analysis(` + codefile := app.Analysis(` class Employee { displayName():void { console.log("hello, world"); } } -`, "").ClassNodes +`, "") - g.Expect(len(results[0].Methods)).To(Equal(1)) + g.Expect(len(codefile.DataStructures[0].Functions)).To(Equal(1)) + g.Expect(len(codefile.ClassNodes[0].Methods)).To(Equal(1)) } func Test_ShouldGetInterfaceImplements(t *testing.T) { diff --git a/trial/pkg/ast/ts/typescript_ident_converter.go b/trial/pkg/ast/ts/typescript_ident_converter.go index c0e8af94ccd61709b7abf4b90412a6afc7bba78a..b3b92e776d68db173b0f7fff8a883fa140e53fe1 100644 --- a/trial/pkg/ast/ts/typescript_ident_converter.go +++ b/trial/pkg/ast/ts/typescript_ident_converter.go @@ -4,12 +4,13 @@ import ( "github.com/antlr/antlr4/runtime/Go/antlr" "github.com/phodal/coca/languages/ts" "github.com/phodal/coca/pkg/domain" + "github.com/phodal/coca/pkg/domain/trial" ) func BuildArgExpressCall(memberDotExprCtx *parser.MemberDotExpressionContext) domain.JMethodCall { call := domain.NewJMethodCall() memberChild := memberDotExprCtx.GetChild(0) - switch x := memberChild.(type){ + switch x := memberChild.(type) { case *parser.IdentifierExpressionContext: call.Class = x.GetText() call.MethodName = memberDotExprCtx.IdentifierName().GetText() @@ -18,20 +19,27 @@ func BuildArgExpressCall(memberDotExprCtx *parser.MemberDotExpressionContext) do return call } -func BuildConstructorMethod(ctx *parser.ConstructorDeclarationContext) domain.JMethod { +func BuildConstructorMethod(ctx *parser.ConstructorDeclarationContext) (domain.JMethod, *trial.CodeFunction) { method := domain.NewJMethod() method.Name = "constructor" + function := &trial.CodeFunction{ + Name: "constructor", + } + method.AddPosition(ctx.GetChild(0).GetParent().(*antlr.BaseParserRuleContext)) if ctx.AccessibilityModifier() != nil { - method.Modifiers = append(method.Modifiers, ctx.AccessibilityModifier().GetText()) + modifier := ctx.AccessibilityModifier().GetText() + + method.Modifiers = append(method.Modifiers, modifier) + function.Modifiers = append(function.Modifiers, modifier) } - return method + return method, function } -func BuildMemberMethod(ctx *parser.PropertyMemberDeclarationContext) domain.JMethod { +func BuildMemberMethod(ctx *parser.PropertyMemberDeclarationContext) (domain.JMethod, *trial.CodeFunction) { method := domain.NewJMethod() method.Name = ctx.PropertyName().GetText() @@ -40,7 +48,15 @@ func BuildMemberMethod(ctx *parser.PropertyMemberDeclarationContext) domain.JMet method.StopLine = ctx.GetStop().GetLine() method.StopLinePosition = ctx.GetStop().GetColumn() - return method + function := &trial.CodeFunction{ + Name: ctx.PropertyName().GetText(), + } + function.CodePosition.StartLine = ctx.GetStart().GetLine() + function.CodePosition.StartLinePosition = ctx.GetStart().GetColumn() + function.CodePosition.StopLine = ctx.GetStop().GetLine() + function.CodePosition.StopLinePosition = ctx.GetStop().GetColumn() + + return method, function } func BuildImplements(typeList parser.IClassOrInterfaceTypeListContext) []string { @@ -64,7 +80,7 @@ func BuildMethodParameter(context *parser.ParameterListContext) []domain.JParame parameters = append(parameters, buildRequireParameterList(listContext)...) - if context.RestParameter() != nil { + if context.RestParameter() != nil { restParamCtx := context.RestParameter().(*parser.RestParameterContext) parameters = append(parameters, buildRestParameters(restParamCtx)) } diff --git a/trial/pkg/ast/ts/typescript_ident_listener.go b/trial/pkg/ast/ts/typescript_ident_listener.go index 494160e098ee0fc88a8c0ce5e18d52d1818710f5..76c763bbf1f4b44e1558cdb1d7fd6149d07df0ce 100644 --- a/trial/pkg/ast/ts/typescript_ident_listener.go +++ b/trial/pkg/ast/ts/typescript_ident_listener.go @@ -167,17 +167,23 @@ func (s *TypeScriptIdentListener) EnterClassDeclaration(ctx *parser.ClassDeclara heritageContext := ctx.ClassHeritage().(*parser.ClassHeritageContext) if heritageContext.ImplementsClause() != nil { typeList := heritageContext.ImplementsClause().(*parser.ImplementsClauseContext).ClassOrInterfaceTypeList() + currentNode.Implements = append(currentNode.Implements, BuildImplements(typeList)...) + currentDataStruct.Implements = append(currentNode.Implements, BuildImplements(typeList)...) } if heritageContext.ClassExtendsClause() != nil { referenceContext := heritageContext.ClassExtendsClause().(*parser.ClassExtendsClauseContext).TypeReference().(*parser.TypeReferenceContext) + currentNode.Extend = referenceContext.TypeName().GetText() + currentDataStruct.Extend = referenceContext.TypeName().GetText() } classTailContext := ctx.ClassTail().(*parser.ClassTailContext) handleClassBodyElements(classTailContext) + classNodeQueue = append(classNodeQueue, *currentNode) + dataStructQueue = append(dataStructQueue, *currentDataStruct) } func handleClassBodyElements(classTailContext *parser.ClassTailContext) { @@ -185,14 +191,17 @@ func handleClassBodyElements(classTailContext *parser.ClassTailContext) { elementChild := classElement.GetChild(0) switch x := elementChild.(type) { case *parser.ConstructorDeclarationContext: - currentNode.Methods = append(currentNode.Methods, BuildConstructorMethod(x)) + constructorMethod, codeFunction := BuildConstructorMethod(x) + + currentNode.Methods = append(currentNode.Methods, constructorMethod) + currentDataStruct.Functions = append(currentDataStruct.Functions, *codeFunction) case *parser.PropertyMemberDeclarationContext: - HandlePropertyMember(x, currentNode) + HandlePropertyMember(x, currentNode, currentDataStruct) } } } -func HandlePropertyMember(propertyMemberCtx *parser.PropertyMemberDeclarationContext, node *domain.JClassNode) { +func HandlePropertyMember(propertyMemberCtx *parser.PropertyMemberDeclarationContext, node *domain.JClassNode, dataStruct *trial.CodeDataStruct) { callSignatureSizePos := 3 if propertyMemberCtx.PropertyName() != nil { field := domain.JField{} @@ -208,8 +217,10 @@ func HandlePropertyMember(propertyMemberCtx *parser.PropertyMemberDeclarationCon callSignCtxPos := 2 switch propertyMemberCtx.GetChild(callSignCtxPos).(type) { case *parser.CallSignatureContext: - node.Methods = append(currentNode.Methods, BuildMemberMethod(propertyMemberCtx)) + memberMethod, memberFunction := BuildMemberMethod(propertyMemberCtx) + node.Methods = append(currentNode.Methods, memberMethod) + dataStruct.Functions = append(dataStruct.Functions, *memberFunction) } }