Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ testdata/jsons

src/lang/testdata
*.json
!ts-parser/**/*.json

tools
abcoder

!testdata/asts/*.json
!testdata/asts/*.json
11 changes: 11 additions & 0 deletions lang/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ type ParseOptions struct {
collect.CollectOption
// specify the repo id
RepoID string

// TS options
// tsconfig string
TSParseOptions
}

type TSParseOptions struct {
// tsconfig path
TSConfig string
// srcDir path
TSSrcDir []string
}

func Parse(ctx context.Context, uri string, args ParseOptions) ([]byte, error) {
Expand Down
13 changes: 8 additions & 5 deletions lang/uniast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ import (
type Language string

const (
Golang Language = "go"
Rust Language = "rust"
Cxx Language = "cxx"
Python Language = "python"
Unknown Language = ""
Golang Language = "go"
Rust Language = "rust"
Cxx Language = "cxx"
Python Language = "python"
TypeScript Language = "typescript"
Unknown Language = ""
)

func (l Language) String() string {
Expand Down Expand Up @@ -64,6 +65,8 @@ func NewLanguage(lang string) (l Language) {
return Cxx
case "python":
return Python
case "ts", "typescript", "javascript", "js":
return TypeScript
default:
return Unknown
}
Expand Down
53 changes: 53 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

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

var wopts lang.WriteOptions
flags.StringVar(&wopts.Compiler, "compiler", "", "destination compiler path.")
Expand Down Expand Up @@ -110,6 +113,15 @@ func main() {
}

opts.Language = language

if language == uniast.TypeScript {
if err := parseTSProject(context.Background(), uri, opts, flagOutput); err != nil {
log.Error("Failed to parse: %v\n", err)
os.Exit(1)
}
return
}

if flagLsp != nil {
opts.LSP = *flagLsp
}
Expand Down Expand Up @@ -252,3 +264,44 @@ func (s *StringArray) Set(value string) error {
func (s *StringArray) String() string {
return strings.Join(*s, ",")
}

func parseTSProject(ctx context.Context, repoPath string, opts lang.ParseOptions, outputFlag *string) error {
if outputFlag == nil {
return fmt.Errorf("output path is required")
}

parserPath, err := exec.LookPath("abcoder-ts-parser")
if err != nil {
log.Info("abcoder-ts-parser not found, installing...")
cmd := exec.Command("npm", "install", "-g", "abcoder-ts-parser")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to install abcoder-ts-parser: %v", err)
}
parserPath, err = exec.LookPath("abcoder-ts-parser")
if err != nil {
return fmt.Errorf("failed to find abcoder-ts-parser after installation: %v", err)
}
}

args := []string{"parse", repoPath}
if len(opts.TSSrcDir) > 0 {
args = append(args, "--src", strings.Join(opts.TSSrcDir, ","))
}
if opts.TSConfig != "" {
args = append(args, "--tsconfig", opts.TSConfig)
}
if *outputFlag != "" {
args = append(args, "--output", *outputFlag)
}

cmd := exec.CommandContext(ctx, parserPath, args...)
cmd.Env = append(os.Environ(), "NODE_OPTIONS=--max-old-space-size=65536")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

log.Info("Running abcoder-ts-parser with args: %v", args)

return cmd.Run()
}
34 changes: 34 additions & 0 deletions ts-parser/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"parserOptions": {
"ecmaVersion": 2022,
"sourceType": "module"
},
"rules": {
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
"@typescript-eslint/no-explicit-any": "warn",
"prefer-const": "error",
"no-var": "error",
"object-shorthand": "error",
"prefer-arrow-callback": "error",
"@typescript-eslint/ban-types": "off"
},
"env": {
"node": true,
"es2022": true
},
"ignorePatterns": [
"dist/",
"node_modules/",
"**/*.js",
"**/*.d.ts",
"coverage/"
]
}
9 changes: 9 additions & 0 deletions ts-parser/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules/

dist/

.vscode/

temp/

output.json
38 changes: 38 additions & 0 deletions ts-parser/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Dependencies
node_modules/

# Build outputs
dist/

# Test files and directories
**/*.test.ts
**/*.test.js
**/*.spec.ts
**/*.spec.js
**/test/
**/tests/
test-repo/

# Logs
*.log

# Environment files
.env*

# Package files
package-lock.json
yarn.lock

# Generated files
*.d.ts
*.js.map
*.ts.map

# Temporary files
*.tmp
*.temp
.DS_Store

# IDE
.vscode/
.idea/
10 changes: 10 additions & 0 deletions ts-parser/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"semi": true,
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"bracketSpacing": true,
"arrowParens": "avoid"
}
46 changes: 46 additions & 0 deletions ts-parser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# TypeScript Parser for ABCoder

A TypeScript AST parser that extracts method calls, variable references, and dependencies.

Usage:

Build: `npm run build`

Run: `node dist/index.js parse [options] <directory>`

Parse a TypeScript repository and generate UNIAST JSON

Arguments:
directory Directory to parse


| Option | Description |
|--------|-------------|
| -o, --output <file> | Output file path (default: "output.json") |
| -t, --tsconfig <file> | Path to tsconfig.json file, if you provide a relative path, it will be relative to **the directory of the input file** (default: "tsconfig.json") |
| --no-dist | Ignore dist folder and its contents |
| --pretty | Pretty print JSON output |
| --src <dirs> | Directory paths to include (comma-separated) |
| -h, --help | display help for command |


See `./index.ts` for more information.


## Notes

1. MUST correctly specify the location of the current project's `tsconfig.json`.

2. If you provide a relative path to argument `--tsconfig`, it will be relative to **the directory of the input file**.

3. Before usage, please configure the dependencies for your TypeScript project, such as running npm install and setting up cross-package dependencies in monorepo.

4. If the repository you're analyzing is too large, you may need to adjust Node.js's maximum memory allocation.


## Some known issues

- When there is a circular dependency, the parser will choose one of the dependencies as the main dependency.
- The parser does not handle dynamic imports.
- The parser does not handle TypeScript decorators.
- For external symbol which has no `.d.ts` declaration file, the parser will not be able to resolve the symbol.
Loading
Loading