From a4029e239994afea73f411ffa76bd56afc22b0d5 Mon Sep 17 00:00:00 2001 From: Egor Aristov Date: Thu, 16 Oct 2025 11:16:28 +0300 Subject: [PATCH] parse ts --- examples/simple/ts/index.ts | 5 ++- kitcom/goparser.go | 4 +++ kitcom/tsparser.go | 63 +++++++++++++++++++++++++++++++++---- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/examples/simple/ts/index.ts b/examples/simple/ts/index.ts index 477d85e..c83827a 100644 --- a/examples/simple/ts/index.ts +++ b/examples/simple/ts/index.ts @@ -1,5 +1,8 @@ /** - * @kittenipc:api + * kek + * @readonly + * lol + * @kittenipc api */ class IpcApi { Mul(a: number, b: number): number { diff --git a/kitcom/goparser.go b/kitcom/goparser.go index 096db88..a695a5d 100644 --- a/kitcom/goparser.go +++ b/kitcom/goparser.go @@ -68,6 +68,10 @@ func (g *GoApiParser) Parse(sourceFile string) (Api, error) { return Api{}, fmt.Errorf("no api struct found") } + if len(apiStructs) > 1 { + return Api{}, fmt.Errorf("multiple api struct found") + } + for _, decl := range astFile.Decls { funcDecl, ok := decl.(*ast.FuncDecl) if !ok { diff --git a/kitcom/tsparser.go b/kitcom/tsparser.go index 2cb8eaa..d9917b5 100644 --- a/kitcom/tsparser.go +++ b/kitcom/tsparser.go @@ -2,33 +2,84 @@ package main import ( "fmt" + "io" + "os" + "strings" "efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast" "efprojects.com/kitten-ipc/kitcom/internal/tsgo/core" "efprojects.com/kitten-ipc/kitcom/internal/tsgo/parser" + "efprojects.com/kitten-ipc/kitcom/internal/tsgo/tspath" ) +const TagName = "kittenipc" +const TagComment = "api" + type TypescriptApiParser struct { } +type apiClass struct { +} + func (t *TypescriptApiParser) Parse(sourceFilePath string) (Api, error) { + + f, err := os.Open(sourceFilePath) + if err != nil { + return Api{}, fmt.Errorf("failed to open file: %w", err) + } + defer f.Close() + + fileContents, err := io.ReadAll(f) + if err != nil { + return Api{}, fmt.Errorf("failed to read file: %w", err) + } + sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{ FileName: sourceFilePath, - Path: "", + Path: tspath.Path(sourceFilePath), CompilerOptions: core.SourceFileAffectingCompilerOptions{}, ExternalModuleIndicatorOptions: ast.ExternalModuleIndicatorOptions{}, JSDocParsingMode: ast.JSDocParsingModeParseAll, - }, "", core.ScriptKindTS) + }, string(fileContents), core.ScriptKindTS) _ = sourceFile + var apiClasses []apiClass + sourceFile.ForEachChild(func(node *ast.Node) bool { - if node.IsJSDoc() { - jsDoc := node.AsJSDoc() - _ = jsDoc - fmt.Println("a") + if node.Kind != ast.KindClassDeclaration { + return false } + cls := node.AsClassDeclaration() + + jsDocNodes := cls.JSDoc(nil) + if len(jsDocNodes) == 0 { + return false + } + + for _, jsDocNode := range jsDocNodes { + jsDoc := jsDocNode.AsJSDoc() + for _, tag := range jsDoc.Tags.Nodes { + if tag.TagName().Text() == TagName { + for _, com := range tag.Comments() { + if strings.TrimSpace(com.Text()) == TagComment { + apiClasses = append(apiClasses, apiClass{}) + return false + } + } + } + } + } + return false }) + if len(apiClasses) == 0 { + return Api{}, fmt.Errorf("no api class found") + } + + if len(apiClasses) > 1 { + return Api{}, fmt.Errorf("multiple api classes found") + } + return Api{}, nil }