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 Bash/Shell parser support (.sh, .bash, .zsh) (closes #135) (#249)

Joseph Mikhail21fa75d94788
diff --git a/core-ingestion/package-lock.json b/core-ingestion/package-lock.json
index e5b7ddab..08751e05 100644
--- a/core-ingestion/package-lock.json
+++ b/core-ingestion/package-lock.json
@@ -33,6 +33,7 @@
"optionalDependencies": {
"@davisvaughan/tree-sitter-r": "^1.2.0",
"@tree-sitter-grammars/tree-sitter-lua": "^0.2.0",
+ "tree-sitter-bash": "^0.21.0",
"tree-sitter-elixir": "^0.3.5",
"tree-sitter-kotlin": "^0.3.8",
"tree-sitter-make": "^1.1.1",
@@ -1219,6 +1220,34 @@
"node-gyp-build": "^4.8.4"
}
},
+ "node_modules/tree-sitter-bash": {
+ "version": "0.21.0",
+ "resolved": "https://registry.npmjs.org/tree-sitter-bash/-/tree-sitter-bash-0.21.0.tgz",
+ "integrity": "sha512-UuXf+wliu1mmS/O2Iz7OQghExM4a+lk+GaVPndZVpAJnFuzanaN33UcHOsrmngHxaOXHz5JSZrwp6i2qM/PKag==",
+ "hasInstallScript": true,
+ "license": "MIT",
+ "optional": true,
+ "dependencies": {
+ "node-addon-api": "^7.1.0",
+ "node-gyp-build": "^4.8.0",
+ "web-tree-sitter": "^0.21.0"
+ },
+ "peerDependencies": {
+ "tree-sitter": "^0.21.0"
+ },
+ "peerDependenciesMeta": {
+ "tree_sitter": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/tree-sitter-bash/node_modules/node-addon-api": {
+ "version": "7.1.1",
+ "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-7.1.1.tgz",
+ "integrity": "sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==",
+ "license": "MIT",
+ "optional": true
+ },
"node_modules/tree-sitter-c": {
"version": "0.21.4",
"resolved": "https://registry.npmjs.org/tree-sitter-c/-/tree-sitter-c-0.21.4.tgz",
@@ -1780,6 +1809,13 @@
}
}
},
+ "node_modules/web-tree-sitter": {
+ "version": "0.21.0",
+ "resolved": "https://registry.npmjs.org/web-tree-sitter/-/web-tree-sitter-0.21.0.tgz",
+ "integrity": "sha512-iJ+QJ6ikN9D9cG7Kh6q3KtAstYFUQbYZ8OjuPEJYWfj2kLrmp5I3C2n6WjE1Y3jvj7nJbkcrJytJGWUEhCxn+g==",
+ "license": "MIT",
+ "optional": true
+ },
"node_modules/which": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
diff --git a/core-ingestion/package.json b/core-ingestion/package.json
index 5ff212f6..13edfc05 100644
--- a/core-ingestion/package.json
+++ b/core-ingestion/package.json
@@ -34,6 +34,7 @@
"optionalDependencies": {
"@davisvaughan/tree-sitter-r": "^1.2.0",
"@tree-sitter-grammars/tree-sitter-lua": "^0.2.0",
+ "tree-sitter-bash": "^0.21.0",
"tree-sitter-elixir": "^0.3.5",
"tree-sitter-kotlin": "^0.3.8",
"tree-sitter-make": "^1.1.1",
diff --git a/core-ingestion/src/__tests__/queries.bash.test.ts b/core-ingestion/src/__tests__/queries.bash.test.ts
new file mode 100644
index 00000000..0d46df01
--- /dev/null
+++ b/core-ingestion/src/__tests__/queries.bash.test.ts
@@ -0,0 +1,55 @@
+import { describe, it, expect } from 'vitest';
+import { parseFile } from '../index.js';
+import { SupportedLanguages } from '../languages.js';
+
+describe('Bash queries', () => {
+ it('detects shell by extension and common dotfiles', () => {
+ expect(parseFile('/x/deploy.sh', 'echo hi\n')!.language).toBe(SupportedLanguages.Bash);
+ expect(parseFile('/x/run.zsh', 'echo hi\n')!.language).toBe(SupportedLanguages.Bash);
+ expect(parseFile('/home/.bashrc', 'alias l=ls\n')!.language).toBe(SupportedLanguages.Bash);
+ });
+
+ it('captures both function definition forms', () => {
+ const result = parseFile('/x/lib.sh', `
+build() { make all; }
+function deploy {
+ echo deploying
+}
+`);
+ expect(result).not.toBeNull();
+ expect(result!.entities.map(e => e.name)).toEqual(
+ expect.arrayContaining(['build', 'deploy']),
+ );
+ expect(result!.entities).toContainEqual(
+ expect.objectContaining({ name: 'build', kind: 'function' }),
+ );
+ });
+
+ it('emits CALLS for command invocations (incl. calls to defined functions)', () => {
+ const result = parseFile('/x/run.sh', `
+deploy() {
+ build
+ notify_team
+}
+`);
+ expect(result).not.toBeNull();
+ const calls = result!.relationships
+ .filter(r => r.predicate === 'CALLS')
+ .map(r => r.dstName);
+ expect(calls).toEqual(expect.arrayContaining(['build', 'notify_team']));
+ });
+
+ it('emits IMPORTS for source and dot includes', () => {
+ const result = parseFile('/x/main.sh', `
+source ./lib/common.sh
+. /etc/profile.d/app.sh
+`);
+ expect(result).not.toBeNull();
+ const raws = result!.relationships
+ .filter(r => r.predicate === 'IMPORTS')
+ .map(r => r.importRaw);
+ expect(raws).toEqual(
+ expect.arrayContaining(['./lib/common.sh', '/etc/profile.d/app.sh']),
+ );
+ });
+});
diff --git a/core-ingestion/src/index.ts b/core-ingestion/src/index.ts
index 1b31b5bd..208f90d9 100644
--- a/core-ingestion/src/index.ts
+++ b/core-ingestion/src/index.ts
@@ -36,6 +36,7 @@ const Swift = tryLoadGrammar('tree-sitter-swift');
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');
// tree-sitter-sas uses ESM bindings with top-level await — incompatible with tryLoadGrammar (CJS require)
let SAS: any = null;
// @ts-ignore
@@ -177,6 +178,7 @@ const GRAMMAR_MAP: Partial<Record<SupportedLanguages, any>> = {
...(Elixir ? { [SupportedLanguages.Elixir]: Elixir } : {}),
...(Make ? { [SupportedLanguages.Makefile]: Make } : {}),
...(Lua ? { [SupportedLanguages.Lua]: Lua } : {}),
+ ...(Bash ? { [SupportedLanguages.Bash]: Bash } : {}),
};
// Capture key prefix → NodeKind string
diff --git a/core-ingestion/src/languages.ts b/core-ingestion/src/languages.ts
index e43f7f89..01dbb2b5 100644
--- a/core-ingestion/src/languages.ts
+++ b/core-ingestion/src/languages.ts
@@ -24,6 +24,7 @@ export enum SupportedLanguages {
Elixir = 'elixir',
Makefile = 'makefile',
Lua = 'lua',
+ Bash = 'bash',
}
const EXT_MAP: Record<string, SupportedLanguages> = {
@@ -66,6 +67,10 @@ const EXT_MAP: Record<string, SupportedLanguages> = {
'.lua': SupportedLanguages.Lua,
'.mk': SupportedLanguages.Makefile,
'.makefile': SupportedLanguages.Makefile,
+ '.sh': SupportedLanguages.Bash,
+ '.bash': SupportedLanguages.Bash,
+ '.zsh': SupportedLanguages.Bash,
+ '.ksh': SupportedLanguages.Bash,
};
export function languageFromPath(filePath: string): SupportedLanguages | null {
@@ -78,6 +83,12 @@ export function languageFromPath(filePath: string): SupportedLanguages | null {
if(lowerFileName === 'makefile' || lowerFileName === 'makefile.mk' || lowerFileName.endsWith('.makefile') || lowerFileName.endsWith('.mk') || lowerFileName === 'gnumakefile') {
return SupportedLanguages.Makefile;
}
+ // Common extensionless shell config scripts (dotfiles have no real extension).
+ if (lowerFileName === '.bashrc' || lowerFileName === '.bash_profile' || lowerFileName === '.bash_aliases'
+ || lowerFileName === '.zshrc' || lowerFileName === '.zprofile' || lowerFileName === '.profile'
+ || lowerFileName === '.zshenv' || lowerFileName === '.bash_logout') {
+ return SupportedLanguages.Bash;
+ }
const dotIndex = lowerFileName.lastIndexOf('.');
if (dotIndex === -1) return null;
const ext = lowerFileName.slice(dotIndex);
diff --git a/core-ingestion/src/queries.ts b/core-ingestion/src/queries.ts
index 7a7e5f79..2b1addc3 100644
--- a/core-ingestion/src/queries.ts
+++ b/core-ingestion/src/queries.ts
@@ -1605,6 +1605,20 @@ export const R_QUERIES = `
(#eq? @_src "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.
+export const BASH_QUERIES = `
+(function_definition name: (word) @name) @definition.function
+
+(command name: (command_name (word) @call.name)) @call
+
+(command
+ name: (command_name (word) @_src)
+ argument: [(word) (string) (concatenation)] @import.source
+ (#match? @_src "^(source|\\.)$")) @import
+`;
+
// Lua — mirrors the grammar's bundled tags.scm, mapped to Ix capture names.
// Covers: function declarations (plain / local / table-dot / method-colon),
// function expressions assigned to a name or table field, calls (bare / dotted /
@@ -1668,5 +1682,6 @@ export const LANGUAGE_QUERIES: Record<SupportedLanguages, string> = {
[SupportedLanguages.Elixir]: ELIXIR_QUERIES,
[SupportedLanguages.Makefile]: MAKEFILE_QUERIES,
[SupportedLanguages.Lua]: LUA_QUERIES,
+ [SupportedLanguages.Bash]: BASH_QUERIES,
};