Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat(parser): add Haskell parser support (.hs, .lhs) (closes #141) (#250)

Joseph Mikhail5b097a5bf8b2
diff --git a/core-ingestion/package-lock.json b/core-ingestion/package-lock.json
index 08751e05..d3bf2b88 100644
--- a/core-ingestion/package-lock.json
+++ b/core-ingestion/package-lock.json
@@ -35,6 +35,7 @@
"@tree-sitter-grammars/tree-sitter-lua": "^0.2.0",
"tree-sitter-bash": "^0.21.0",
"tree-sitter-elixir": "^0.3.5",
+ "tree-sitter-haskell": "^0.23.1",
"tree-sitter-kotlin": "^0.3.8",
"tree-sitter-make": "^1.1.1",
"tree-sitter-sas": "^0.4.2",
@@ -1360,6 +1361,26 @@
}
}
},
+ "node_modules/tree-sitter-haskell": {
+ "version": "0.23.1",
+ "resolved": "https://registry.npmjs.org/tree-sitter-haskell/-/tree-sitter-haskell-0.23.1.tgz",
+ "integrity": "sha512-qG4CYhejveu9DLMLEGBz/n9/TTeGSFLC6wniwOgG6m8/v7Dng8qR0ob0EVG7+XH+9WiOxohpGA23EhceWuxY4w==",
+ "hasInstallScript": true,
+ "license": "MIT",
+ "optional": true,
+ "dependencies": {
+ "node-addon-api": "^8.2.2",
+ "node-gyp-build": "^4.8.2"
+ },
+ "peerDependencies": {
+ "tree-sitter": "^0.21.1"
+ },
+ "peerDependenciesMeta": {
+ "tree-sitter": {
+ "optional": true
+ }
+ }
+ },
"node_modules/tree-sitter-java": {
"version": "0.23.5",
"resolved": "https://registry.npmjs.org/tree-sitter-java/-/tree-sitter-java-0.23.5.tgz",
diff --git a/core-ingestion/package.json b/core-ingestion/package.json
index 13edfc05..02c728ef 100644
--- a/core-ingestion/package.json
+++ b/core-ingestion/package.json
@@ -36,6 +36,7 @@
"@tree-sitter-grammars/tree-sitter-lua": "^0.2.0",
"tree-sitter-bash": "^0.21.0",
"tree-sitter-elixir": "^0.3.5",
+ "tree-sitter-haskell": "^0.23.1",
"tree-sitter-kotlin": "^0.3.8",
"tree-sitter-make": "^1.1.1",
"tree-sitter-sas": "^0.4.2",
diff --git a/core-ingestion/src/__tests__/queries.haskell.test.ts b/core-ingestion/src/__tests__/queries.haskell.test.ts
new file mode 100644
index 00000000..9e361235
--- /dev/null
+++ b/core-ingestion/src/__tests__/queries.haskell.test.ts
@@ -0,0 +1,55 @@
+import { describe, it, expect } from 'vitest';
+import { parseFile } from '../index.js';
+import { SupportedLanguages } from '../languages.js';
+
+// tree-sitter-haskell ships no win32/node26 prebuild, so these run green in CI
+// (Linux/node24, where it builds) and are skipped locally if the grammar is absent.
+const hs = parseFile('/repo/M.hs', 'main = pure ()\n');
+const describeFn = hs && hs.language === SupportedLanguages.Haskell ? describe : describe.skip;
+
+describeFn('Haskell queries', () => {
+ it('detects language as Haskell', () => {
+ expect(parseFile('/repo/M.hs', 'x = 1\n')!.language).toBe(SupportedLanguages.Haskell);
+ });
+
+ it('captures functions, top-level binds, data types and classes', () => {
+ const result = parseFile('/repo/Service.hs', `
+module Data.Service where
+getUser :: Int -> IO User
+getUser uid = fetchUser uid
+config = defaultConfig
+data Status = Active | Inactive
+class Loggable a where
+ logMsg :: a -> String
+`);
+ expect(result).not.toBeNull();
+ const names = result!.entities.map(e => e.name);
+ expect(names).toEqual(expect.arrayContaining(['getUser', 'config', 'Status']));
+ expect(result!.entities).toContainEqual(
+ expect.objectContaining({ name: 'Loggable', kind: 'interface' }),
+ );
+ });
+
+ it('emits CALLS for bare and module-qualified application', () => {
+ const result = parseFile('/repo/Run.hs', `
+run xs = process (Map.lookup key xs)
+`);
+ expect(result).not.toBeNull();
+ const calls = result!.relationships
+ .filter(r => r.predicate === 'CALLS')
+ .map(r => r.dstName);
+ expect(calls).toEqual(expect.arrayContaining(['process', 'lookup']));
+ });
+
+ it('emits IMPORTS for module imports', () => {
+ const result = parseFile('/repo/M.hs', `
+import Data.Maybe (fromMaybe)
+import qualified Data.Map as Map
+`);
+ expect(result).not.toBeNull();
+ const imports = result!.relationships
+ .filter(r => r.predicate === 'IMPORTS')
+ .map(r => r.dstName);
+ expect(imports).toEqual(expect.arrayContaining(['Data.Maybe', 'Data.Map']));
+ });
+});
diff --git a/core-ingestion/src/index.ts b/core-ingestion/src/index.ts
index 208f90d9..52dd3b41 100644
--- a/core-ingestion/src/index.ts
+++ b/core-ingestion/src/index.ts
@@ -37,6 +37,7 @@ const R = tryLoadGrammar('@davisvaughan/tree-sitter-r');
const Elixir = tryLoadGrammar('tree-sitter-elixir');
const Make = tryLoadGrammar('tree-sitter-make');
const Bash = tryLoadGrammar('tree-sitter-bash');
+const Haskell = tryLoadGrammar('tree-sitter-haskell');
// tree-sitter-sas uses ESM bindings with top-level await — incompatible with tryLoadGrammar (CJS require)
let SAS: any = null;
// @ts-ignore
@@ -179,6 +180,7 @@ const GRAMMAR_MAP: Partial<Record<SupportedLanguages, any>> = {
...(Make ? { [SupportedLanguages.Makefile]: Make } : {}),
...(Lua ? { [SupportedLanguages.Lua]: Lua } : {}),
...(Bash ? { [SupportedLanguages.Bash]: Bash } : {}),
+ ...(Haskell ? { [SupportedLanguages.Haskell]: Haskell } : {}),
};
// Capture key prefix → NodeKind string
diff --git a/core-ingestion/src/languages.ts b/core-ingestion/src/languages.ts
index 01dbb2b5..62beeabe 100644
--- a/core-ingestion/src/languages.ts
+++ b/core-ingestion/src/languages.ts
@@ -25,6 +25,7 @@ export enum SupportedLanguages {
Makefile = 'makefile',
Lua = 'lua',
Bash = 'bash',
+ Haskell = 'haskell',
}
const EXT_MAP: Record<string, SupportedLanguages> = {
@@ -71,6 +72,8 @@ const EXT_MAP: Record<string, SupportedLanguages> = {
'.bash': SupportedLanguages.Bash,
'.zsh': SupportedLanguages.Bash,
'.ksh': SupportedLanguages.Bash,
+ '.hs': SupportedLanguages.Haskell,
+ '.lhs': SupportedLanguages.Haskell,
};
export function languageFromPath(filePath: string): SupportedLanguages | null {
diff --git a/core-ingestion/src/queries.ts b/core-ingestion/src/queries.ts
index 2b1addc3..5eda9918 100644
--- a/core-ingestion/src/queries.ts
+++ b/core-ingestion/src/queries.ts
@@ -1605,6 +1605,23 @@ export const R_QUERIES = `
(#eq? @_src "source")) @import
`;
+// Haskell. Function definitions (with args) and top-level binds (no args), data
+// types / newtypes / type synonyms, type classes, the module header, function
+// application as calls (bare and module-qualified), and module imports.
+// NOTE: `type_synomym` is the grammar's actual (misspelled) node name.
+export const HASKELL_QUERIES = `
+(function name: (variable) @name) @definition.function
+(bind name: (variable) @name) @definition.function
+(data_type name: (name) @name) @definition.type
+(newtype name: (name) @name) @definition.type
+(type_synomym (name) @name) @definition.type
+(class name: (name) @name) @definition.interface
+(header module: (module) @name) @definition.module
+(apply function: (variable) @call.name) @call
+(apply function: (qualified (variable) @call.name)) @call
+(import module: (module) @import.source) @import
+`;
+
// Bash / shell. Functions (both `foo() {}` and `function foo {}`), command
// invocations as calls (system commands dangle at resolution; calls to defined
// functions resolve), and `source FILE` / `. FILE` as imports.
@@ -1683,5 +1700,6 @@ export const LANGUAGE_QUERIES: Record<SupportedLanguages, string> = {
[SupportedLanguages.Makefile]: MAKEFILE_QUERIES,
[SupportedLanguages.Lua]: LUA_QUERIES,
[SupportedLanguages.Bash]: BASH_QUERIES,
+ [SupportedLanguages.Haskell]: HASKELL_QUERIES,
};