refactor: [ts] add first version data struct

上级 3e730b97
......@@ -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 {
......
......@@ -10,4 +10,5 @@ type CodeFunction struct {
Modifiers []string
Creators []CodeDataStruct
InnerFunctions []CodeFunction
CodePosition CodePosition
}
......@@ -19,6 +19,12 @@
"FunctionNodes": [
{
"Annotations": null,
"CodePosition": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Creators": null,
"InnerFunctions": null,
"MethodCalls": [
......
......@@ -19,6 +19,12 @@
"FunctionNodes": [
{
"Annotations": null,
"CodePosition": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Creators": null,
"InnerFunctions": null,
"MethodCalls": [
......
......@@ -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,
......
......@@ -25,6 +25,12 @@
"FunctionNodes": [
{
"Annotations": null,
"CodePosition": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Creators": null,
"InnerFunctions": null,
"MethodCalls": null,
......
......@@ -58,6 +58,12 @@
"FunctionNodes": [
{
"Annotations": null,
"CodePosition": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Creators": null,
"InnerFunctions": null,
"MethodCalls": [
......
......@@ -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) {
......
......@@ -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))
}
......
......@@ -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)
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册