refactor: [ts] make method num correct for interfacE

上级 5c7bb90b
package trial
type CodeDataStruct struct {
Name string
Type string
ID string
MemberIds []string
Extend string
Implements []string
Annotations interface{}
Properties []CodeProperty
Functions []CodeFunction
FunctionCalls []CodeCall
Extension interface{}
Name string
Type string
ID string
MemberIds []string
Extend string
Implements []string
Annotations interface{}
InOutProperties []CodeProperty
Functions []CodeFunction
FunctionCalls []CodeCall // for field call
Extension interface{}
Fields []CodeField
}
type CodeField struct {
CodeProperty
TypeValue string
}
type JavaExtension struct {
......
......@@ -12,3 +12,9 @@ type CodeFunction struct {
InnerFunctions []CodeFunction
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)
properties := BuildFieldToProperty(x.Methods.List)
dataStruct := trial.CodeDataStruct{
Name: ident,
ID: "",
MemberIds: nil,
Properties: properties,
Name: ident,
ID: "",
MemberIds: nil,
InOutProperties: properties,
}
member := trial.CodeMember{
......@@ -270,7 +270,7 @@ func AddStructType(currentStruct trial.CodeDataStruct, x *ast.StructType, curren
for _, field := range x.Fields.List {
property := BuildPropertyField(getFieldName(field), field)
member.FileID = currentFile.FullName
currentStruct.Properties = append(currentStruct.Properties, *property)
currentStruct.InOutProperties = append(currentStruct.InOutProperties, *property)
}
currentFile.Members = append(currentFile.Members, &member)
currentFile.DataStructures = append(currentFile.DataStructures, currentStruct)
......
......@@ -140,7 +140,7 @@ export interface IPerson {
g.Expect(len(results[0].Methods)).To(Equal(2))
//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(secondMethod.Name).To(Equal("getManagerName"))
......
......@@ -76,10 +76,10 @@ func (s *PythonIdentListener) EnterFrom_stmt(ctx *parser.From_stmtContext) {
func (s *PythonIdentListener) EnterClassdef(ctx *parser.ClassdefContext) {
hasEnterMember = true
dataStruct := &trial.CodeDataStruct{
Name: ctx.Name().GetText(),
ID: "",
MemberIds: nil,
Properties: nil,
Name: ctx.Name().GetText(),
ID: "",
MemberIds: nil,
InOutProperties: nil,
}
ctxIndex := ast_util.GetNodeIndex(ctx)
......
......@@ -72,9 +72,11 @@ func BuildImplements(typeList parser.IClassOrInterfaceTypeListContext) []string
return implements
}
func BuildMethodParameter(context *parser.ParameterListContext) []domain.JParameter {
func BuildMethodParameter(context *parser.ParameterListContext) ([]domain.JParameter, []trial.CodeProperty) {
childNode := context.GetChild(0)
var parameters []domain.JParameter = nil
var codeParameters []trial.CodeProperty = nil
switch x := childNode.(type) {
case *parser.RequiredParameterListContext:
listContext := x
......@@ -91,9 +93,15 @@ func BuildMethodParameter(context *parser.ParameterListContext) []domain.JParame
Name: "any",
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 {
......
......@@ -104,28 +104,34 @@ func (s *TypeScriptIdentListener) EnterInterfaceDeclaration(ctx *parser.Interfac
objectTypeCtx := ctx.ObjectType().(*parser.ObjectTypeContext)
if objectTypeCtx.TypeBody() != nil {
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() {
typeMemberCtx := typeMember.(*parser.TypeMemberContext)
memberChild := typeMemberCtx.GetChild(0)
switch x := memberChild.(type) {
case *parser.PropertySignatureContext:
BuildInterfacePropertySignature(x, classNode)
BuildInterfacePropertySignature(x, classNode, dataStruct)
case *parser.MethodSignatureContext:
method := domain.NewJMethod()
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)
}
}
}
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))
typeValue := signatureCtx.PropertyName().(*parser.PropertyNameContext).GetText()
......@@ -141,13 +147,34 @@ func BuildInterfacePropertySignature(signatureCtx *parser.PropertySignatureConte
method.Type = signatureCtx.Type_().GetText()
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 {
field := &domain.JField{
Type: typeType,
Value: typeValue,
}
codeField := &trial.CodeField{}
codeField.TypeType = typeType
codeField.TypeValue = typeValue
classNode.Fields = append(classNode.Fields, *field)
dataStruct.Fields = append(dataStruct.Fields, *codeField)
}
}
......@@ -257,7 +284,7 @@ func (s *TypeScriptIdentListener) EnterFunctionDeclaration(ctx *parser.FunctionD
ast_util.AddPosition(&method, ctx.GetChild(0).GetParent().(*antlr.BaseParserRuleContext))
callSignatureContext := ctx.CallSignature().(*parser.CallSignatureContext)
FillMethodFromCallSignature(callSignatureContext, &method)
FillMethodFromCallSignature(callSignatureContext, &method, nil)
function := &trial.CodeFunction{
Name: ctx.Identifier().GetText(),
......@@ -274,15 +301,19 @@ func (s *TypeScriptIdentListener) EnterFunctionDeclaration(ctx *parser.FunctionD
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 {
parameterListContext := callSignatureContext.ParameterList().(*parser.ParameterListContext)
methodParameters := BuildMethodParameter(parameterListContext)
methodParameters, _ := BuildMethodParameter(parameterListContext)
method.Parameters = append(method.Parameters, methodParameters...)
}
if callSignatureContext.TypeAnnotation() != nil {
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.
先完成此消息的编辑!
想要评论请 注册