Skip to content
Closed
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
148 changes: 148 additions & 0 deletions __tests__/extraction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3826,6 +3826,154 @@ local function run(y) return helper(y) end
});
});

// =============================================================================
// Julia (tree-sitter-julia WASM vendored; extends colbymchenry/codegraph#244)
// =============================================================================

describe('Julia Extraction', () => {
describe('Language detection', () => {
it('should detect Julia files', () => {
expect(detectLanguage('main.jl')).toBe('julia');
expect(detectLanguage('graph_utils.jl')).toBe('julia');
});

it('should report Julia as supported', () => {
expect(isLanguageSupported('julia')).toBe(true);
expect(getSupportedLanguages()).toContain('julia');
});
});

describe('Function extraction', () => {
it('should extract top-level function definitions', () => {
const code = `
function greet(name::String)
println("Hello")
end

function add(a::Int, b::Int)::Int
return a + b
end
`;
const result = extractFromSource('utils.jl', code);
const fns = result.nodes.filter((n) => n.kind === 'function');
expect(fns.find((f) => f.name === 'greet')).toBeDefined();
expect(fns.find((f) => f.name === 'add')).toBeDefined();
});

it('should extract function signature', () => {
const code = `
function process(x::Int, y::Float64)::String
return string(x + y)
end
`;
const result = extractFromSource('process.jl', code);
const fn = result.nodes.find((n) => n.kind === 'function' && n.name === 'process');
expect(fn).toBeDefined();
expect(fn?.signature).toContain('x::Int');
});

it('should extract macro definitions', () => {
const code = `
macro mytime(expr)
return :(0)
end
`;
const result = extractFromSource('macros.jl', code);
const macroFn = result.nodes.find((n) => n.kind === 'function' && n.name === 'mytime');
expect(macroFn).toBeDefined();
});

it('should extract one-line assignment functions', () => {
const code = 'has_key(d, k) = (k in keys(d))';
const result = extractFromSource('short.jl', code);
expect(result.nodes.find((n) => n.name === 'has_key' && n.kind === 'function')).toBeDefined();
});
});

describe('Struct and abstract extraction', () => {
it('should extract struct definitions without block wrapper', () => {
const code = `
struct Point
x::Float64
y::Float64
end

mutable struct Counter
value::Int
end
`;
const result = extractFromSource('types.jl', code);
const structs = result.nodes.filter((n) => n.kind === 'struct');
expect(structs.find((s) => s.name === 'Point')).toBeDefined();
expect(structs.find((s) => s.name === 'Counter')).toBeDefined();
});

it('should extract abstract type definitions', () => {
const code = `
abstract type Animal end
abstract type Shape end
`;
const result = extractFromSource('abstract.jl', code);
const abstracts = result.nodes.filter((n) => n.kind === 'interface');
expect(abstracts.find((a) => a.name === 'Animal')).toBeDefined();
expect(abstracts.find((a) => a.name === 'Shape')).toBeDefined();
});
});

describe('Module extraction', () => {
it('should extract module and nested definitions', () => {
const code = `
module SampleGraph
export greet

function greet(name::String)
println("Hello")
end
end
`;
const result = extractFromSource('mymodule.jl', code);
expect(result.nodes.find((n) => n.kind === 'module' && n.name === 'SampleGraph')).toBeDefined();
expect(
result.nodes.find(
(n) => (n.kind === 'function' || n.kind === 'method') && n.name === 'greet'
)
).toBeDefined();
});
});

describe('Import extraction', () => {
it('should extract import and using statements', () => {
const code = `
import LinearAlgebra
import Base.Math: sin, cos
using Statistics
using DataFrames: DataFrame
`;
const result = extractFromSource('imports.jl', code);
const imports = result.nodes.filter((n) => n.kind === 'import').map((n) => n.name);
expect(imports).toContain('LinearAlgebra');
expect(imports).toContain('Statistics');
});
});

describe('Call extraction', () => {
it('should extract function calls inside bodies without block', () => {
const code = `
function run(g)
out_neighbors(g, v)
sorted = topological_sort(cons)
end
`;
const result = extractFromSource('run.jl', code);
const calls = result.unresolvedReferences
.filter((r) => r.referenceKind === 'calls')
.map((r) => r.referenceName);
expect(calls).toContain('out_neighbors');
expect(calls).toContain('topological_sort');
});
});
});

// =============================================================================
// Luau (typed superset of Lua — https://luau.org)
// =============================================================================
Expand Down
44 changes: 43 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@types/better-sqlite3": "^7.6.0",
"@types/node": "^20.19.30",
"@types/picomatch": "^4.0.2",
"tree-sitter-julia": "^0.23.1",
"typescript": "^5.0.0",
"vitest": "^2.1.9"
},
Expand Down
5 changes: 4 additions & 1 deletion src/extraction/grammars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const WASM_GRAMMAR_FILES: Record<GrammarLanguage, string> = {
scala: 'tree-sitter-scala.wasm',
lua: 'tree-sitter-lua.wasm',
luau: 'tree-sitter-luau.wasm',
julia: 'tree-sitter-julia.wasm',
};

/**
Expand Down Expand Up @@ -92,6 +93,7 @@ export const EXTENSION_MAP: Record<string, Language> = {
'.sc': 'scala',
'.lua': 'lua',
'.luau': 'luau',
'.jl': 'julia',
};

/**
Expand Down Expand Up @@ -155,7 +157,7 @@ export async function loadGrammarsForLanguages(languages: Language[]): Promise<v
// ABI-13 build that corrupts the shared WASM heap under web-tree-sitter
// 0.25 (drops nested calls/imports on every file after the first); we
// vendor the upstream ABI-15 wasm instead.
const wasmPath = (lang === 'pascal' || lang === 'scala' || lang === 'lua' || lang === 'luau')
const wasmPath = (lang === 'pascal' || lang === 'scala' || lang === 'lua' || lang === 'luau' || lang === 'julia')
? path.join(__dirname, 'wasm', wasmFile)
: require.resolve(`tree-sitter-wasms/out/${wasmFile}`);
const language = await WasmLanguage.load(wasmPath);
Expand Down Expand Up @@ -325,6 +327,7 @@ export function getLanguageDisplayName(language: Language): string {
scala: 'Scala',
lua: 'Lua',
luau: 'Luau',
julia: 'Julia',
yaml: 'YAML',
twig: 'Twig',
unknown: 'Unknown',
Expand Down
2 changes: 2 additions & 0 deletions src/extraction/languages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { pascalExtractor } from './pascal';
import { scalaExtractor } from './scala';
import { luaExtractor } from './lua';
import { luauExtractor } from './luau';
import { juliaExtractor } from './julia';

export const EXTRACTORS: Partial<Record<Language, LanguageExtractor>> = {
typescript: typescriptExtractor,
Expand All @@ -47,4 +48,5 @@ export const EXTRACTORS: Partial<Record<Language, LanguageExtractor>> = {
scala: scalaExtractor,
lua: luaExtractor,
luau: luauExtractor,
julia: juliaExtractor,
};
Loading