refactor: [ts] make method num correct for interfacE

上级 5c7bb90b
package trial package trial
type CodeDataStruct struct { type CodeDataStruct struct {
Name string Name string
Type string Type string
ID string ID string
MemberIds []string MemberIds []string
Extend string Extend string
Implements []string Implements []string
Annotations interface{}
Properties []CodeProperty Annotations interface{}
Functions []CodeFunction InOutProperties []CodeProperty
FunctionCalls []CodeCall Functions []CodeFunction
Extension interface{} FunctionCalls []CodeCall // for field call
Extension interface{}
Fields []CodeField
}
type CodeField struct {
CodeProperty
TypeValue string
} }
type JavaExtension struct { type JavaExtension struct {
......
...@@ -12,3 +12,9 @@ type CodeFunction struct { ...@@ -12,3 +12,9 @@ type CodeFunction struct {
InnerFunctions []CodeFunction InnerFunctions []CodeFunction
CodePosition CodePosition CodePosition CodePosition
} }
func (c *CodeFunction) BuildSingleReturnType(typeType string) *CodeProperty {
return &CodeProperty{
TypeType: typeType,
}
}
...@@ -118,10 +118,10 @@ func AddInterface(x *ast.InterfaceType, ident string, codeFile *trial.CodeFile) ...@@ -118,10 +118,10 @@ func AddInterface(x *ast.InterfaceType, ident string, codeFile *trial.CodeFile)
properties := BuildFieldToProperty(x.Methods.List) properties := BuildFieldToProperty(x.Methods.List)
dataStruct := trial.CodeDataStruct{ dataStruct := trial.CodeDataStruct{
Name: ident, Name: ident,
ID: "", ID: "",
MemberIds: nil, MemberIds: nil,
Properties: properties, InOutProperties: properties,
} }
member := trial.CodeMember{ member := trial.CodeMember{
...@@ -270,7 +270,7 @@ func AddStructType(currentStruct trial.CodeDataStruct, x *ast.StructType, curren ...@@ -270,7 +270,7 @@ func AddStructType(currentStruct trial.CodeDataStruct, x *ast.StructType, curren
for _, field := range x.Fields.List { for _, field := range x.Fields.List {
property := BuildPropertyField(getFieldName(field), field) property := BuildPropertyField(getFieldName(field), field)
member.FileID = currentFile.FullName member.FileID = currentFile.FullName
currentStruct.Properties = append(currentStruct.Properties, *property) currentStruct.InOutProperties = append(currentStruct.InOutProperties, *property)
} }
currentFile.Members = append(currentFile.Members, &member) currentFile.Members = append(currentFile.Members, &member)
currentFile.DataStructures = append(currentFile.DataStructures, currentStruct) currentFile.DataStructures = append(currentFile.DataStructures, currentStruct)
......
...@@ -140,7 +140,7 @@ export interface IPerson { ...@@ -140,7 +140,7 @@ export interface IPerson {
g.Expect(len(results[0].Methods)).To(Equal(2)) g.Expect(len(results[0].Methods)).To(Equal(2))
//fmt.Println(codeFile.DataStructures) //fmt.Println(codeFile.DataStructures)
//g.Expect(len(codeFile.DataStructures[0].Functions)).To(Equal(2)) g.Expect(len(codeFile.DataStructures[0].Functions)).To(Equal(2))
g.Expect(firstMethod.Name).To(Equal("getSalary")) g.Expect(firstMethod.Name).To(Equal("getSalary"))
g.Expect(secondMethod.Name).To(Equal("getManagerName")) g.Expect(secondMethod.Name).To(Equal("getManagerName"))
......
...@@ -76,10 +76,10 @@ func (s *PythonIdentListener) EnterFrom_stmt(ctx *parser.From_stmtContext) { ...@@ -76,10 +76,10 @@ func (s *PythonIdentListener) EnterFrom_stmt(ctx *parser.From_stmtContext) {
func (s *PythonIdentListener) EnterClassdef(ctx *parser.ClassdefContext) { func (s *PythonIdentListener) EnterClassdef(ctx *parser.ClassdefContext) {
hasEnterMember = true hasEnterMember = true
dataStruct := &trial.CodeDataStruct{ dataStruct := &trial.CodeDataStruct{
Name: ctx.Name().GetText(), Name: ctx.Name().GetText(),
ID: "", ID: "",
MemberIds: nil, MemberIds: nil,
Properties: nil, InOutProperties: nil,
} }
ctxIndex := ast_util.GetNodeIndex(ctx) ctxIndex := ast_util.GetNodeIndex(ctx)
......
...@@ -72,9 +72,11 @@ func BuildImplements(typeList parser.IClassOrInterfaceTypeListContext) []string ...@@ -72,9 +72,11 @@ func BuildImplements(typeList parser.IClassOrInterfaceTypeListContext) []string
return implements return implements
} }
func BuildMethodParameter(context *parser.ParameterListContext) []domain.JParameter { func BuildMethodParameter(context *parser.ParameterListContext) ([]domain.JParameter, []trial.CodeProperty) {
childNode := context.GetChild(0) childNode := context.GetChild(0)
var parameters []domain.JParameter = nil var parameters []domain.JParameter = nil
var codeParameters []trial.CodeProperty = nil
switch x := childNode.(type) { switch x := childNode.(type) {
case *parser.RequiredParameterListContext: case *parser.RequiredParameterListContext:
listContext := x listContext := x
...@@ -91,9 +93,15 @@ func BuildMethodParameter(context *parser.ParameterListContext) []domain.JParame ...@@ -91,9 +93,15 @@ func BuildMethodParameter(context *parser.ParameterListContext) []domain.JParame
Name: "any", Name: "any",
Type: predefinedTypeContext.GetText(), Type: predefinedTypeContext.GetText(),
}) })
parameter := trial.CodeProperty{
TypeName: "any",
TypeType: predefinedTypeContext.GetText(),
}
codeParameters = append(codeParameters, parameter)
} }
return parameters return parameters, codeParameters
} }
func buildRestParameters(ctx *parser.RestParameterContext) domain.JParameter { func buildRestParameters(ctx *parser.RestParameterContext) domain.JParameter {
......
...@@ -104,28 +104,34 @@ func (s *TypeScriptIdentListener) EnterInterfaceDeclaration(ctx *parser.Interfac ...@@ -104,28 +104,34 @@ func (s *TypeScriptIdentListener) EnterInterfaceDeclaration(ctx *parser.Interfac
objectTypeCtx := ctx.ObjectType().(*parser.ObjectTypeContext) objectTypeCtx := ctx.ObjectType().(*parser.ObjectTypeContext)
if objectTypeCtx.TypeBody() != nil { if objectTypeCtx.TypeBody() != nil {
typeMemberListCtx := objectTypeCtx.TypeBody().(*parser.TypeBodyContext).TypeMemberList().(*parser.TypeMemberListContext) typeMemberListCtx := objectTypeCtx.TypeBody().(*parser.TypeBodyContext).TypeMemberList().(*parser.TypeMemberListContext)
BuildInterfaceTypeBody(typeMemberListCtx, s.currentNode) BuildInterfaceTypeBody(typeMemberListCtx, s.currentNode, s.currentDataStruct)
} }
} }
func BuildInterfaceTypeBody(ctx *parser.TypeMemberListContext, classNode *domain.JClassNode) { func BuildInterfaceTypeBody(ctx *parser.TypeMemberListContext, classNode *domain.JClassNode, dataStruct *trial.CodeDataStruct) {
for _, typeMember := range ctx.AllTypeMember() { for _, typeMember := range ctx.AllTypeMember() {
typeMemberCtx := typeMember.(*parser.TypeMemberContext) typeMemberCtx := typeMember.(*parser.TypeMemberContext)
memberChild := typeMemberCtx.GetChild(0) memberChild := typeMemberCtx.GetChild(0)
switch x := memberChild.(type) { switch x := memberChild.(type) {
case *parser.PropertySignatureContext: case *parser.PropertySignatureContext:
BuildInterfacePropertySignature(x, classNode) BuildInterfacePropertySignature(x, classNode, dataStruct)
case *parser.MethodSignatureContext: case *parser.MethodSignatureContext:
method := domain.NewJMethod() method := domain.NewJMethod()
method.Name = x.PropertyName().GetText() method.Name = x.PropertyName().GetText()
FillMethodFromCallSignature(x.CallSignature().(*parser.CallSignatureContext), &method)
function := trial.CodeFunction{
Name: x.PropertyName().GetText(),
}
FillMethodFromCallSignature(x.CallSignature().(*parser.CallSignatureContext), &method, &function)
dataStruct.Functions = append(dataStruct.Functions, function)
classNode.Methods = append(classNode.Methods, method) classNode.Methods = append(classNode.Methods, method)
} }
} }
} }
func BuildInterfacePropertySignature(signatureCtx *parser.PropertySignatureContext, classNode *domain.JClassNode) { func BuildInterfacePropertySignature(signatureCtx *parser.PropertySignatureContext, classNode *domain.JClassNode, dataStruct *trial.CodeDataStruct) {
typeType := BuildTypeAnnotation(signatureCtx.TypeAnnotation().(*parser.TypeAnnotationContext)) typeType := BuildTypeAnnotation(signatureCtx.TypeAnnotation().(*parser.TypeAnnotationContext))
typeValue := signatureCtx.PropertyName().(*parser.PropertyNameContext).GetText() typeValue := signatureCtx.PropertyName().(*parser.PropertyNameContext).GetText()
...@@ -141,13 +147,34 @@ func BuildInterfacePropertySignature(signatureCtx *parser.PropertySignatureConte ...@@ -141,13 +147,34 @@ func BuildInterfacePropertySignature(signatureCtx *parser.PropertySignatureConte
method.Type = signatureCtx.Type_().GetText() method.Type = signatureCtx.Type_().GetText()
classNode.Methods = append(classNode.Methods, method) classNode.Methods = append(classNode.Methods, method)
function := &trial.CodeFunction{
Name: typeValue,
}
param := trial.CodeProperty{
Name: "any",
TypeType: typeType,
}
returnType := trial.CodeProperty{
TypeType: signatureCtx.Type_().GetText(),
}
function.Parameters = append(function.Parameters, param)
function.ReturnTypes = append(function.ReturnTypes, returnType)
dataStruct.Functions = append(dataStruct.Functions, *function)
} else { } else {
field := &domain.JField{ field := &domain.JField{
Type: typeType, Type: typeType,
Value: typeValue, Value: typeValue,
} }
codeField := &trial.CodeField{}
codeField.TypeType = typeType
codeField.TypeValue = typeValue
classNode.Fields = append(classNode.Fields, *field) classNode.Fields = append(classNode.Fields, *field)
dataStruct.Fields = append(dataStruct.Fields, *codeField)
} }
} }
...@@ -257,7 +284,7 @@ func (s *TypeScriptIdentListener) EnterFunctionDeclaration(ctx *parser.FunctionD ...@@ -257,7 +284,7 @@ func (s *TypeScriptIdentListener) EnterFunctionDeclaration(ctx *parser.FunctionD
ast_util.AddPosition(&method, ctx.GetChild(0).GetParent().(*antlr.BaseParserRuleContext)) ast_util.AddPosition(&method, ctx.GetChild(0).GetParent().(*antlr.BaseParserRuleContext))
callSignatureContext := ctx.CallSignature().(*parser.CallSignatureContext) callSignatureContext := ctx.CallSignature().(*parser.CallSignatureContext)
FillMethodFromCallSignature(callSignatureContext, &method) FillMethodFromCallSignature(callSignatureContext, &method, nil)
function := &trial.CodeFunction{ function := &trial.CodeFunction{
Name: ctx.Identifier().GetText(), Name: ctx.Identifier().GetText(),
...@@ -274,15 +301,19 @@ func (s *TypeScriptIdentListener) EnterFunctionDeclaration(ctx *parser.FunctionD ...@@ -274,15 +301,19 @@ func (s *TypeScriptIdentListener) EnterFunctionDeclaration(ctx *parser.FunctionD
s.currentDataStruct.Functions = append(s.currentDataStruct.Functions, *function) s.currentDataStruct.Functions = append(s.currentDataStruct.Functions, *function)
} }
func FillMethodFromCallSignature(callSignatureContext *parser.CallSignatureContext, method *domain.JMethod) { func FillMethodFromCallSignature(callSignatureContext *parser.CallSignatureContext, method *domain.JMethod, function *trial.CodeFunction) {
if callSignatureContext.ParameterList() != nil { if callSignatureContext.ParameterList() != nil {
parameterListContext := callSignatureContext.ParameterList().(*parser.ParameterListContext) parameterListContext := callSignatureContext.ParameterList().(*parser.ParameterListContext)
methodParameters := BuildMethodParameter(parameterListContext) methodParameters, _ := BuildMethodParameter(parameterListContext)
method.Parameters = append(method.Parameters, methodParameters...) method.Parameters = append(method.Parameters, methodParameters...)
} }
if callSignatureContext.TypeAnnotation() != nil { if callSignatureContext.TypeAnnotation() != nil {
annotationContext := callSignatureContext.TypeAnnotation().(*parser.TypeAnnotationContext) annotationContext := callSignatureContext.TypeAnnotation().(*parser.TypeAnnotationContext)
method.Type = BuildTypeAnnotation(annotationContext) typeAnnotation := BuildTypeAnnotation(annotationContext)
method.Type = typeAnnotation
function.ReturnTypes = append(function.ReturnTypes, *function.BuildSingleReturnType(typeAnnotation))
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册