Skip to content

armgit / Ix

Public Imported from GitHub
0

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

feat(ingest): parse PowerShell (.ps1/.psm1/.psd1) (#595)

KageBinary2cd76595be37
diff --git a/README.md b/README.md
index c96c3df8..c17c989b 100644
--- a/README.md
+++ b/README.md
@@ -293,10 +293,10 @@ Set `IX_DEBUG=1` to get full stack traces on any error.
## Supported Languages
-Ix parses and extracts symbols, calls, and imports across 26 languages, and recognizes several more config and data formats.
+Ix parses and extracts symbols, calls, and imports across 27 languages, and recognizes several more config and data formats.
**Languages:**
-JavaScript, TypeScript, Python, Java, C, C++, C#, Go, Ruby, Rust, PHP, Kotlin, Swift, Scala, R, SAS, Elixir, Haskell, Zig, Lua, Bash, HTML, XML, CSS, HCL / Terraform, Makefile
+JavaScript, TypeScript, Python, Java, C, C++, C#, Go, Ruby, Rust, PHP, Kotlin, Swift, Scala, R, SAS, Elixir, Haskell, Zig, Lua, Bash, PowerShell, HTML, XML, CSS, HCL / Terraform, Makefile
CUDA (`.cu` / `.cuh`) is parsed with the C++ grammar; kernel-launch syntax
(`kernel<<<grid, block>>>(args)`) is handled, so host-to-kernel calls appear in
diff --git a/core-ingestion/package-lock.json b/core-ingestion/package-lock.json
index 3bfccbb0..973c3ea6 100644
--- a/core-ingestion/package-lock.json
+++ b/core-ingestion/package-lock.json
@@ -16,6 +16,7 @@
"tree-sitter-java": "^0.23.5",
"tree-sitter-javascript": "^0.25.0",
"tree-sitter-php": "^0.24.2",
+ "tree-sitter-powershell": "^0.26.4",
"tree-sitter-python": "^0.25.0",
"tree-sitter-ruby": "^0.23.1",
"tree-sitter-rust": "^0.24.0",
@@ -2003,6 +2004,25 @@
}
}
},
+ "node_modules/tree-sitter-powershell": {
+ "version": "0.26.4",
+ "resolved": "https://registry.npmjs.org/tree-sitter-powershell/-/tree-sitter-powershell-0.26.4.tgz",
+ "integrity": "sha512-5RwNW/qN/cKArZY0znvTOG+OWSau7Agel5K0A8KQ5Nubw/4SyZ5KC02RKCaYcA2ur5UQ8esyxe9+6yXup2MXkw==",
+ "hasInstallScript": true,
+ "license": "MIT",
+ "dependencies": {
+ "node-addon-api": "^8.5.0",
+ "node-gyp-build": "^4.8.4"
+ },
+ "peerDependencies": {
+ "tree-sitter": "^0.25.0"
+ },
+ "peerDependenciesMeta": {
+ "tree-sitter": {
+ "optional": true
+ }
+ }
+ },
"node_modules/tree-sitter-python": {
"version": "0.25.0",
"resolved": "https://registry.npmjs.org/tree-sitter-python/-/tree-sitter-python-0.25.0.tgz",
diff --git a/core-ingestion/package.json b/core-ingestion/package.json
index e5c8834b..186d04fe 100644
--- a/core-ingestion/package.json
+++ b/core-ingestion/package.json
@@ -25,6 +25,7 @@
"tree-sitter-java": "^0.23.5",
"tree-sitter-javascript": "^0.25.0",
"tree-sitter-php": "^0.24.2",
+ "tree-sitter-powershell": "^0.26.4",
"tree-sitter-python": "^0.25.0",
"tree-sitter-ruby": "^0.23.1",
"tree-sitter-rust": "^0.24.0",
diff --git a/core-ingestion/src/__tests__/queries.powershell.test.ts b/core-ingestion/src/__tests__/queries.powershell.test.ts
new file mode 100644
index 00000000..a79686a9
--- /dev/null
+++ b/core-ingestion/src/__tests__/queries.powershell.test.ts
@@ -0,0 +1,59 @@
+import { describe, expect, it } from 'vitest';
+
+import { parseFile } from '../index.js';
+
+const names = (source: string) =>
+ parseFile('/repo/script.ps1', source)!.entities
+ .filter((entity) => entity.kind !== 'file')
+ .map((entity) => `${entity.kind}:${entity.name}`);
+
+const imports = (source: string, path = '/repo/script.ps1') =>
+ parseFile(path, source)!.relationships
+ .filter((relationship) => relationship.predicate === 'IMPORTS')
+ .map((relationship) => relationship.dstName);
+
+describe('PowerShell queries', () => {
+ it('captures function, filter and workflow, which share one node type', () => {
+ expect(names('function Get-Thing { }\nfilter Pick { $_ }\nworkflow Flow { }\n')).toEqual(
+ expect.arrayContaining(['function:Get-Thing', 'function:Pick', 'function:Flow']),
+ );
+ });
+
+ it('captures classes, their methods, and enums', () => {
+ expect(names('class Installer {\n [string]$Root\n [void] Run() { }\n}\nenum Mode { Fast; Slow }\n')).toEqual(
+ expect.arrayContaining(['class:Installer', 'method:Run', 'class:Mode']),
+ );
+ });
+
+ it('records calls, including calls to functions defined in the same file', () => {
+ const result = parseFile(
+ '/repo/script.ps1',
+ 'function Write-Ok($m) { Write-Host $m }\nWrite-Ok "done"\n',
+ );
+ expect(
+ result!.relationships
+ .filter((relationship) => relationship.predicate === 'CALLS')
+ .map((relationship) => relationship.dstName),
+ ).toEqual(expect.arrayContaining(['Write-Host', 'Write-Ok']));
+ });
+
+ it('resolves all three ways a script pulls in code', () => {
+ expect(imports('Import-Module Microsoft.PowerShell.Utility\n')).toEqual(['Microsoft.PowerShell.Utility']);
+ expect(imports('using module Ix.Common\n')).toEqual(['Ix.Common']);
+ expect(imports('using namespace System.IO\n')).toEqual(['System.IO']);
+ expect(imports('. "$PSScriptRoot/common.ps1"\n')).toEqual(['common.ps1']);
+ });
+
+ it('does not mistake Import-Module arguments for further modules', () => {
+ // `using module X` puts the scope keyword in a generic_token too, and
+ // -RequiredVersion's value is one as well; both produced junk imports
+ // ("module", "5.0") before the patterns were anchored.
+ expect(imports('Import-Module Pester -RequiredVersion 5.0 -Force\n')).toEqual(['Pester']);
+ });
+
+ it('parses .psm1 modules and .psd1 manifests', () => {
+ expect(names('function Get-Thing { }\n').length).toBeGreaterThan(0);
+ expect(parseFile('/repo/mod.psm1', 'function Get-Thing { }\n')).not.toBeNull();
+ expect(parseFile('/repo/mod.psd1', '@{ ModuleVersion = "1.0" }\n')).not.toBeNull();
+ });
+});
diff --git a/core-ingestion/src/index.ts b/core-ingestion/src/index.ts
index 8f1698ca..e1d63f08 100644
--- a/core-ingestion/src/index.ts
+++ b/core-ingestion/src/index.ts
@@ -115,6 +115,10 @@ const SAS = await tryImportGrammar('tree-sitter-sas');
// progress bar draws on. Pointing straight at the file skips that resolution.
const Lua = await tryImportGrammar('@tree-sitter-grammars/tree-sitter-lua/bindings/node/index.js');
const Css = await tryImportGrammar('tree-sitter-css/bindings/node/index.js');
+// tree-sitter-powershell is the same shape: ESM bindings with top-level await,
+// a directory `main` and no `"exports"`, so it needs the async helper and the
+// explicit file path for the same two reasons Lua and CSS do.
+const PowerShell = await tryImportGrammar('tree-sitter-powershell/bindings/node/index.js');
import { SupportedLanguages, languageFromPath } from './languages.js';
import { LANGUAGE_QUERIES } from './queries.js';
@@ -257,6 +261,7 @@ const GRAMMAR_MAP: Partial<Record<SupportedLanguages, any>> = {
...(Zig ? { [SupportedLanguages.Zig]: Zig } : {}),
...(Html ? { [SupportedLanguages.HTML]: Html } : {}),
...(Xml ? { [SupportedLanguages.XML]: Xml } : {}),
+ ...(PowerShell ? { [SupportedLanguages.PowerShell]: PowerShell } : {}),
...(Hcl ? { [SupportedLanguages.HCL]: Hcl } : {}),
...(Css ? { [SupportedLanguages.CSS]: Css } : {}),
};
diff --git a/core-ingestion/src/languages.ts b/core-ingestion/src/languages.ts
index f9fd2e6b..06314a4b 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',
+ PowerShell = 'powershell',
Haskell = 'haskell',
Zig = 'zig',
HTML = 'html',
@@ -86,6 +87,9 @@ const EXT_MAP: Record<string, SupportedLanguages> = {
'.bash': SupportedLanguages.Bash,
'.zsh': SupportedLanguages.Bash,
'.ksh': SupportedLanguages.Bash,
+ '.ps1': SupportedLanguages.PowerShell,
+ '.psm1': SupportedLanguages.PowerShell,
+ '.psd1': SupportedLanguages.PowerShell,
'.hs': SupportedLanguages.Haskell,
'.lhs': SupportedLanguages.Haskell,
'.zig': SupportedLanguages.Zig,
diff --git a/core-ingestion/src/queries.ts b/core-ingestion/src/queries.ts
index 8abdcf17..9f1a56b5 100644
--- a/core-ingestion/src/queries.ts
+++ b/core-ingestion/src/queries.ts
@@ -1786,6 +1786,45 @@ export const HASKELL_QUERIES = `
// 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.
+// PowerShell. `function`, `filter` and `workflow` all parse as
+// function_statement, so one pattern covers the three. Imports cover the three
+// ways a script pulls in code: Import-Module, `using module`/`using namespace`,
+// and dot-sourcing (`. ./common.ps1`), which is how most scripts actually share
+// helpers.
+//
+// Class properties are deliberately not captured: the `variable` node keeps its
+// `$` sigil, so `$Root` would never join to the `.Root` that references it, and
+// there is no name-normalisation hook to strip it. Better absent than wrong.
+export const POWERSHELL_QUERIES = `
+(function_statement (function_name) @name) @definition.function
+
+(class_statement (simple_name) @name) @definition.class
+
+(class_method_definition (simple_name) @name) @definition.method
+
+(enum_statement (simple_name) @name) @definition.enum
+
+(command (command_name) @call.name) @call
+
+(command
+ (command_name) @_cmd
+ (command_elements . (command_argument_sep) . (generic_token) @import.source)
+ (#eq? @_cmd "Import-Module")) @import
+
+(command
+ (command_name) @_using
+ (command_elements
+ (generic_token) @_scope
+ (generic_token) @import.source)
+ (#eq? @_using "using")
+ (#match? @_scope "^(module|namespace)$")) @import
+
+(command
+ (command_invokation_operator) @_op
+ (command_name_expr) @import.source
+ (#eq? @_op ".")) @import
+`;
+
export const BASH_QUERIES = `
(function_definition name: (word) @name) @definition.function
@@ -1861,6 +1900,7 @@ export const LANGUAGE_QUERIES: Record<SupportedLanguages, string> = {
[SupportedLanguages.Makefile]: MAKEFILE_QUERIES,
[SupportedLanguages.Lua]: LUA_QUERIES,
[SupportedLanguages.Bash]: BASH_QUERIES,
+ [SupportedLanguages.PowerShell]: POWERSHELL_QUERIES,
[SupportedLanguages.Haskell]: HASKELL_QUERIES,
[SupportedLanguages.Zig]: ZIG_QUERIES,
[SupportedLanguages.HTML]: HTML_QUERIES,
diff --git a/ix-cli/src/cli/supported-extensions.ts b/ix-cli/src/cli/supported-extensions.ts
index 457808f4..782f167a 100644
--- a/ix-cli/src/cli/supported-extensions.ts
+++ b/ix-cli/src/cli/supported-extensions.ts
@@ -24,6 +24,7 @@ export const SUPPORTED_EXTENSIONS = new Set<string>([
".mk", ".makefile",
".lua",
".sh", ".bash", ".zsh", ".ksh",
+ ".ps1", ".psm1", ".psd1",
".hs", ".lhs",
".zig",
".html", ".htm", ".xhtml",