Skip to content

Commit d1bf614

Browse files
angrychowHoblovski
authored andcommitted
add parse logic for abcoder parser, depend on ts-parser package
1 parent d1fecd9 commit d1bf614

File tree

4 files changed

+78
-8
lines changed

4 files changed

+78
-8
lines changed

lang/parse.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ type ParseOptions struct {
4444
collect.CollectOption
4545
// specify the repo id
4646
RepoID string
47+
48+
// TS options
49+
// tsconfig string
50+
TSParseOptions
51+
}
52+
53+
type TSParseOptions struct {
54+
// tsconfig path
55+
TSConfig string
56+
// srcDir path
57+
TSSrcDir []string
4758
}
4859

4960
func Parse(ctx context.Context, uri string, args ParseOptions) ([]byte, error) {

lang/uniast/ast.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ import (
2727
type Language string
2828

2929
const (
30-
Golang Language = "go"
31-
Rust Language = "rust"
32-
Cxx Language = "cxx"
33-
Python Language = "python"
34-
Unknown Language = ""
30+
Golang Language = "go"
31+
Rust Language = "rust"
32+
Cxx Language = "cxx"
33+
Python Language = "python"
34+
TypeScript Language = "typescript"
35+
Unknown Language = ""
3536
)
3637

3738
func (l Language) String() string {
@@ -64,6 +65,8 @@ func NewLanguage(lang string) (l Language) {
6465
return Cxx
6566
case "python":
6667
return Python
68+
case "ts", "typescript", "javascript", "js":
69+
return TypeScript
6770
default:
6871
return Unknown
6972
}

main.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"flag"
3636
"fmt"
3737
"os"
38+
"os/exec"
3839
"path/filepath"
3940
"strings"
4041

@@ -77,6 +78,8 @@ func main() {
7778
flags.BoolVar(&opts.LoadByPackages, "load-by-packages", false, "load by packages (only works for Go now)")
7879
flags.Var((*StringArray)(&opts.Excludes), "exclude", "exclude files or directories, support multiple values")
7980
flags.StringVar(&opts.RepoID, "repo-id", "", "specify the repo id")
81+
flags.StringVar(&opts.TSConfig, "tsconfig", "", "tsconfig path (only works for TS now)")
82+
flags.Var((*StringArray)(&opts.TSSrcDir), "ts-src-dir", "src-dir path (only works for TS now)")
8083

8184
var wopts lang.WriteOptions
8285
flags.StringVar(&wopts.Compiler, "compiler", "", "destination compiler path.")
@@ -110,6 +113,15 @@ func main() {
110113
}
111114

112115
opts.Language = language
116+
117+
if language == uniast.TypeScript {
118+
if err := parseTSProject(context.Background(), uri, opts, flagOutput); err != nil {
119+
log.Error("Failed to parse: %v\n", err)
120+
os.Exit(1)
121+
}
122+
return
123+
}
124+
113125
if flagLsp != nil {
114126
opts.LSP = *flagLsp
115127
}
@@ -252,3 +264,44 @@ func (s *StringArray) Set(value string) error {
252264
func (s *StringArray) String() string {
253265
return strings.Join(*s, ",")
254266
}
267+
268+
func parseTSProject(ctx context.Context, repoPath string, opts lang.ParseOptions, outputFlag *string) error {
269+
if outputFlag == nil {
270+
return fmt.Errorf("output path is required")
271+
}
272+
273+
parserPath, err := exec.LookPath("abcoder-ts-parser")
274+
if err != nil {
275+
log.Info("abcoder-ts-parser not found, installing...")
276+
cmd := exec.Command("npm", "install", "-g", "abcoder-ts-parser")
277+
cmd.Stdout = os.Stdout
278+
cmd.Stderr = os.Stderr
279+
if err := cmd.Run(); err != nil {
280+
return fmt.Errorf("failed to install abcoder-ts-parser: %v", err)
281+
}
282+
parserPath, err = exec.LookPath("abcoder-ts-parser")
283+
if err != nil {
284+
return fmt.Errorf("failed to find abcoder-ts-parser after installation: %v", err)
285+
}
286+
}
287+
288+
args := []string{"parse", repoPath}
289+
if len(opts.TSSrcDir) > 0 {
290+
args = append(args, "--src", strings.Join(opts.TSSrcDir, ","))
291+
}
292+
if opts.TSConfig != "" {
293+
args = append(args, "--tsconfig", opts.TSConfig)
294+
}
295+
if *outputFlag != "" {
296+
args = append(args, "--output", *outputFlag)
297+
}
298+
299+
cmd := exec.CommandContext(ctx, parserPath, args...)
300+
cmd.Env = append(os.Environ(), "NODE_OPTIONS=--max-old-space-size=65536")
301+
cmd.Stdout = os.Stdout
302+
cmd.Stderr = os.Stderr
303+
304+
log.Info("Running abcoder-ts-parser with args: %v", args)
305+
306+
return cmd.Run()
307+
}

ts-parser/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
{
22
"name": "abcoder-ts-parser",
3-
"version": "1.0.0",
3+
"version": "0.0.2",
44
"description": "TypeScript AST parser for UNIAST v0.1.3 specification",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
7+
"bin": {
8+
"abcoder-ts-parser": "dist/index.js"
9+
},
710
"scripts": {
811
"build": "tsc",
912
"dev": "ts-node src/index.ts",
@@ -20,8 +23,8 @@
2023
"parser",
2124
"uniast"
2225
],
23-
"author": "",
24-
"license": "MIT",
26+
"author": "angrychow",
27+
"license": "ALv2",
2528
"dependencies": {
2629
"commander": "^11.0.0",
2730
"json5": "^2.2.3",

0 commit comments

Comments
 (0)