Skip to content
Merged
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
105 changes: 105 additions & 0 deletions packages/core/src/analyzer/content-extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,111 @@ function IgnoreAllPreviousInstructions($x) {
});
});

describe('extractDirectoryContent — Go', () => {
it('extracts exported top-level functions and methods', () => {
const entry = makeEntry('server.go', `
package main

func Serve(addr string, handler http.Handler) error {
return nil
}

func (s *Server) Start(port int) error {
return nil
}

func privateHelper() {}
`, fixtureRoot);
const result = extractDirectoryContent(makeRoot([entry], fixtureRoot), fixtureRoot);
const fns = result.exports.filter(e => e.kind === 'function');
const names = fns.map(f => f.name);
expect(names).toContain('Serve');
expect(names).toContain('Start');
expect(names).not.toContain('privateHelper');
// Signature should be captured for top-level function
expect(fns.find(f => f.name === 'Serve')?.signature).toContain('addr string');
});

it('extracts exported struct, interface, and type alias', () => {
const entry = makeEntry('types.go', `
package types

type Server struct {
Addr string
}

type Handler interface {
ServeHTTP(w http.ResponseWriter, r *http.Request)
}

type UserID string

type unexportedStruct struct{}
`, fixtureRoot);
const result = extractDirectoryContent(makeRoot([entry], fixtureRoot), fixtureRoot);
const byName = new Map(result.exports.map(e => [e.name, e.kind]));
expect(byName.get('Server')).toBe('class');
expect(byName.get('Handler')).toBe('interface');
expect(byName.get('UserID')).toBe('type');
expect(byName.has('unexportedStruct')).toBe(false);
});

it('extracts exported const and var (var maps to const kind)', () => {
const entry = makeEntry('config.go', `
package config

const MaxRetries = 3
const DefaultTimeout int = 30

var ErrNotFound = errors.New("not found")

const internalConst = "hidden"
var internalVar = 0
`, fixtureRoot);
const result = extractDirectoryContent(makeRoot([entry], fixtureRoot), fixtureRoot);
const byName = new Map(result.exports.map(e => [e.name, e.kind]));
expect(byName.get('MaxRetries')).toBe('const');
expect(byName.get('DefaultTimeout')).toBe('const');
expect(byName.get('ErrNotFound')).toBe('const');
expect(byName.has('internalConst')).toBe(false);
expect(byName.has('internalVar')).toBe(false);
});

it('filters out all lowercase (unexported) identifiers', () => {
const entry = makeEntry('internal.go', `
package internal

func unexportedFn(x int) {}

type unexportedType struct{}

const unexportedConst = 1

var unexportedVar = "x"

func Exported() {}
`, fixtureRoot);
const result = extractDirectoryContent(makeRoot([entry], fixtureRoot), fixtureRoot);
const names = result.exports.map(e => e.name);
expect(names).toContain('Exported');
expect(names).not.toContain('unexportedFn');
expect(names).not.toContain('unexportedType');
expect(names).not.toContain('unexportedConst');
expect(names).not.toContain('unexportedVar');
});

it('flags injection text in Go function names', () => {
const entry = makeEntry('evil.go', `
package evil

func IgnoreAllPreviousInstructions() {}
`, fixtureRoot);
const result = extractDirectoryContent(makeRoot([entry], fixtureRoot), fixtureRoot);
expect(result.exports.map(e => e.name)).toContain('[flagged]');
expect(result.exports.map(e => e.name)).not.toContain('IgnoreAllPreviousInstructions');
});
});

describe('extractDirectoryContent — file purposes', () => {
it('infers component purpose from React file', () => {
const entry = makeEntry('Header.tsx', `
Expand Down
65 changes: 65 additions & 0 deletions packages/core/src/analyzer/content-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,71 @@ function extractExports(content: string, fileName: string): FileExport[] {
}
}

// Go — only uppercase-first identifiers are exported (package-level visibility)
if (fileName.endsWith('.go')) {
// func Name(...) returnType — top-level exported functions
for (const m of content.matchAll(/^func\s+([A-Z]\w*)\s*\(([^)]*)\)/gm)) {
if (!seen.has(m[1])) {
seen.add(m[1]);
const params = m[2].trim();
const safeParams = sanitizeExportSignature(params);
exports.push({
name: sanitizeExportName(m[1]),
kind: 'function',
signature: safeParams ? `(${safeParams})` : '()',
});
}
}
// func (r *Receiver) Name(...) — exported methods
for (const m of content.matchAll(/^func\s+\([^)]+\)\s+([A-Z]\w*)\s*\(([^)]*)\)/gm)) {
if (!seen.has(m[1])) {
seen.add(m[1]);
const params = m[2].trim();
const safeParams = sanitizeExportSignature(params);
exports.push({
name: sanitizeExportName(m[1]),
kind: 'function',
signature: safeParams ? `(${safeParams})` : '()',
});
}
}
// type Name struct
for (const m of content.matchAll(/^type\s+([A-Z]\w*)\s+struct\b/gm)) {
if (!seen.has(m[1])) {
seen.add(m[1]);
exports.push({ name: sanitizeExportName(m[1]), kind: 'class' });
}
}
// type Name interface
for (const m of content.matchAll(/^type\s+([A-Z]\w*)\s+interface\b/gm)) {
if (!seen.has(m[1])) {
seen.add(m[1]);
exports.push({ name: sanitizeExportName(m[1]), kind: 'interface' });
}
}
// type Name OtherType — newtype / type alias (not struct or interface)
for (const m of content.matchAll(/^type\s+([A-Z]\w*)\s+(?!struct\b|interface\b)\w/gm)) {
if (!seen.has(m[1])) {
seen.add(m[1]);
exports.push({ name: sanitizeExportName(m[1]), kind: 'type' });
}
}
// const Name = value (also handles: const Name Type = value)
for (const m of content.matchAll(/^const\s+([A-Z]\w*)\s/gm)) {
if (!seen.has(m[1])) {
seen.add(m[1]);
exports.push({ name: sanitizeExportName(m[1]), kind: 'const' });
}
}
// var Name = value — map to 'const' kind (package-level vars are exported the same way)
for (const m of content.matchAll(/^var\s+([A-Z]\w*)\s/gm)) {
if (!seen.has(m[1])) {
seen.add(m[1]);
exports.push({ name: sanitizeExportName(m[1]), kind: 'const' });
}
}
}

return exports;
}

Expand Down
Loading