Skip to content

Commit 56ff6e8

Browse files
committed
feat(core): add Go language scanner with tree-sitter
Implements issue #129: Go scanner using tree-sitter WASM for parsing. - Add web-tree-sitter and tree-sitter-wasms dependencies - Add tree-sitter utility module (reusable for future languages) - Implement GoScanner with query-based extraction - Extract: functions, methods, structs, interfaces, types, constants - Skip generated files (// Code generated) - 30 tests with Go fixtures Closes #129
1 parent 1d81ba8 commit 56ff6e8

File tree

9 files changed

+1240
-2
lines changed

9 files changed

+1240
-2
lines changed

packages/core/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
"remark": "^15.0.1",
4343
"remark-parse": "^11.0.0",
4444
"remark-stringify": "^11.0.0",
45+
"tree-sitter-wasms": "^0.1.13",
4546
"ts-morph": "^27.0.2",
46-
"unified": "^11.0.5"
47+
"unified": "^11.0.5",
48+
"web-tree-sitter": "^0.25.10"
4749
}
4850
}

packages/core/src/scanner/__tests__/fixtures/go/generated.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Package example demonstrates methods with receivers.
2+
package example
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"time"
8+
)
9+
10+
// ExpBackoff helps implement exponential backoff for retries.
11+
// It is useful in distributed systems for retrying operations.
12+
type ExpBackoff struct {
13+
initialWait time.Duration
14+
maxWait time.Duration
15+
multiplier float64
16+
numFailures int
17+
}
18+
19+
// NewExpBackoff creates a new ExpBackoff instance.
20+
func NewExpBackoff(initial, max time.Duration, mult float64) *ExpBackoff {
21+
return &ExpBackoff{
22+
initialWait: initial,
23+
maxWait: max,
24+
multiplier: mult,
25+
}
26+
}
27+
28+
// Success resets the backoff state after a successful operation.
29+
func (e *ExpBackoff) Success() {
30+
e.numFailures = 0
31+
}
32+
33+
// MarkFailAndGetWait increments failure count and returns wait duration.
34+
// This uses a pointer receiver because it modifies state.
35+
func (e *ExpBackoff) MarkFailAndGetWait() time.Duration {
36+
e.numFailures++
37+
return e.calculateWait()
38+
}
39+
40+
// calculateWait computes the wait time (unexported method).
41+
func (e *ExpBackoff) calculateWait() time.Duration {
42+
// Implementation details...
43+
return e.initialWait
44+
}
45+
46+
// String returns a string representation (value receiver).
47+
func (e ExpBackoff) String() string {
48+
return fmt.Sprintf("ExpBackoff{failures: %d}", e.numFailures)
49+
}
50+
51+
// Connection represents a network connection.
52+
type Connection struct {
53+
host string
54+
port int
55+
timeout time.Duration
56+
isActive bool
57+
}
58+
59+
// Connect establishes a connection to the remote host.
60+
func (c *Connection) Connect(ctx context.Context) error {
61+
c.isActive = true
62+
return nil
63+
}
64+
65+
// Close terminates the connection.
66+
func (c *Connection) Close() error {
67+
c.isActive = false
68+
return nil
69+
}
70+
71+
// IsActive returns whether the connection is currently active.
72+
// Uses value receiver since it doesn't modify state.
73+
func (c Connection) IsActive() bool {
74+
return c.isActive
75+
}
76+
77+
// Host returns the connection host.
78+
func (c Connection) Host() string {
79+
return c.host
80+
}
81+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Package example provides example Go code for scanner testing.
2+
package example
3+
4+
import (
5+
"context"
6+
"fmt"
7+
)
8+
9+
// MaxRetries is the maximum number of retry attempts.
10+
const MaxRetries = 3
11+
12+
// DefaultTimeout is the default timeout duration.
13+
const DefaultTimeout = 30
14+
15+
// privateConst should not be extracted (unexported).
16+
const privateConst = "hidden"
17+
18+
// Config holds application configuration.
19+
type Config struct {
20+
Host string
21+
Port int
22+
Timeout int
23+
}
24+
25+
// Server represents a server instance.
26+
// It handles incoming requests and manages connections.
27+
type Server struct {
28+
config *Config
29+
running bool
30+
}
31+
32+
// Reader defines the interface for reading data.
33+
type Reader interface {
34+
// Read reads data into the provided buffer.
35+
Read(p []byte) (n int, err error)
36+
}
37+
38+
// Writer defines the interface for writing data.
39+
type Writer interface {
40+
Write(p []byte) (n int, err error)
41+
}
42+
43+
// ReadWriter combines Reader and Writer interfaces.
44+
type ReadWriter interface {
45+
Reader
46+
Writer
47+
}
48+
49+
// ID is a type alias for string identifiers.
50+
type ID string
51+
52+
// Handler is a function type for request handlers.
53+
type Handler func(ctx context.Context, req Request) Response
54+
55+
// Request represents an incoming request.
56+
type Request struct {
57+
ID ID
58+
Payload []byte
59+
}
60+
61+
// Response represents an outgoing response.
62+
type Response struct {
63+
ID ID
64+
Status int
65+
Body []byte
66+
}
67+
68+
// NewServer creates a new server with the given configuration.
69+
// It initializes all internal state and validates the config.
70+
func NewServer(cfg *Config) *Server {
71+
return &Server{
72+
config: cfg,
73+
running: false,
74+
}
75+
}
76+
77+
// Start begins the server and starts accepting connections.
78+
func Start(ctx context.Context) error {
79+
fmt.Println("Starting server...")
80+
return nil
81+
}
82+
83+
// processRequest handles a single request.
84+
// This is an unexported function.
85+
func processRequest(req Request) Response {
86+
return Response{
87+
ID: req.ID,
88+
Status: 200,
89+
Body: []byte("OK"),
90+
}
91+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Package example contains tests for the example package.
2+
package example
3+
4+
import (
5+
"testing"
6+
)
7+
8+
// TestNewServer tests server creation.
9+
func TestNewServer(t *testing.T) {
10+
cfg := &Config{Host: "localhost", Port: 8080}
11+
server := NewServer(cfg)
12+
if server == nil {
13+
t.Error("expected server to be created")
14+
}
15+
}
16+
17+
// TestProcessRequest tests request processing.
18+
func TestProcessRequest(t *testing.T) {
19+
req := Request{ID: "test-1", Payload: []byte("hello")}
20+
resp := processRequest(req)
21+
if resp.Status != 200 {
22+
t.Errorf("expected status 200, got %d", resp.Status)
23+
}
24+
}
25+
26+
// helperFunction is a test helper (unexported).
27+
func helperFunction() string {
28+
return "helper"
29+
}

0 commit comments

Comments
 (0)