Skip to content

Commit

Permalink
feat: [ts] add parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 10, 2020
1 parent 4cef4d1 commit 5e9f8e8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
6 changes: 4 additions & 2 deletions trial/pkg/application/ts/ts_ident_app_test.go
Expand Up @@ -85,6 +85,8 @@ function Sum(x: number, y: number) : void {
}
`)

g.Expect(len(results[0].Methods)).To(Equal(1))
g.Expect(results[0].Methods[0].Name).To(Equal("Sum"))
parameters := results[0].Methods[0].Parameters
g.Expect(len(parameters)).To(Equal(2))
g.Expect(parameters[0].Name).To(Equal("x"))
g.Expect(parameters[0].Type).To(Equal("number"))
}
27 changes: 27 additions & 0 deletions trial/pkg/ast/typescript_ident_converter.go
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/languages/ts"
"github.com/phodal/coca/pkg/domain"
"reflect"
)

func BuildArgExpressCall(memberDotExprCtx *parser.MemberDotExpressionContext) domain.JMethodCall {
Expand Down Expand Up @@ -51,3 +52,29 @@ func BuildImplements(typeList parser.IClassOrInterfaceTypeListContext) []string
return implements
}

func BuildMethodParameter(context *parser.ParameterListContext) []domain.JParameter {
childNode := context.GetChild(0)
childType := reflect.TypeOf(childNode).String()
var parameters []domain.JParameter = nil
switch childType {
case "*parser.RequiredParameterListContext":
var requireParamsList []domain.JParameter = nil
for _, requiredParameter := range childNode.(*parser.RequiredParameterListContext).AllRequiredParameter() {
paramCtx := requiredParameter.(*parser.RequiredParameterContext)
name := paramCtx.IdentifierOrPattern().GetText()
paramType := ""
if paramCtx.TypeAnnotation() != nil {
paramType = paramCtx.TypeAnnotation().(*parser.TypeAnnotationContext).Type_().GetText()
}
parameter := domain.JParameter{
Name: name,
Type: paramType,
}
requireParamsList = append(requireParamsList, parameter)
}

parameters = append(parameters, requireParamsList...)
}

return parameters
}
11 changes: 9 additions & 2 deletions trial/pkg/ast/typescript_ident_listener.go
Expand Up @@ -69,7 +69,7 @@ func (s *TypeScriptIdentListener) EnterClassDeclaration(ctx *parser.ClassDeclara
if reflect.TypeOf(elementChild).String() == "*parser.ConstructorDeclarationContext" {
constructorDeclCtx := elementChild.(*parser.ConstructorDeclarationContext)
currentNode.Methods = append(currentNode.Methods, BuildConstructorMethod(constructorDeclCtx))
} else if reflect.TypeOf(elementChild).String() == "*parser.PropertyMemberDeclarationContext"{
} else if reflect.TypeOf(elementChild).String() == "*parser.PropertyMemberDeclarationContext" {
s.handlePropertyMember(elementChild)
}
}
Expand Down Expand Up @@ -121,5 +121,12 @@ func (s *TypeScriptIdentListener) EnterFunctionDeclaration(ctx *parser.FunctionD
method.Name = ctx.Identifier().GetText()
method.AddPosition(ctx.GetChild(0).GetParent().(*antlr.BaseParserRuleContext))

callSignatureContext := ctx.CallSignature().(*parser.CallSignatureContext)
if callSignatureContext.ParameterList() != nil {
parameterListContext := callSignatureContext.ParameterList().(*parser.ParameterListContext)
methodParameters := BuildMethodParameter(parameterListContext)
method.Parameters = append(method.Parameters, methodParameters...)
}

currentNode.Methods = append(currentNode.Methods, method)
}
}

0 comments on commit 5e9f8e8

Please sign in to comment.