Skip to content

feat(ast/powershell): Support powershell for codegraph#913

Open
weburnit wants to merge 1 commit into
colbymchenry:mainfrom
igot-ai:main
Open

feat(ast/powershell): Support powershell for codegraph#913
weburnit wants to merge 1 commit into
colbymchenry:mainfrom
igot-ai:main

Conversation

@weburnit

Copy link
Copy Markdown

Summary

Adds first-class PowerShell indexing to CodeGraph for scripts and modules:

  • Language support (powershell) — .ps1, .psm1, and .psd1 are detected and parsed.
  • Tree-sitter grammar — vendors tree-sitter-powershell.wasm from tree-sitter-powershell@0.26.4 after ABI/runtime health check.
  • Symbol extraction — functions, classes, class methods, class properties, enums, enum members, and module-level variables.
  • Flow extraction — command calls, case-insensitive call resolution, using module, Import-Module, and dot-sourced script imports.

What Changed

12 files touched, including 1 vendored WASM grammar:

  • src/types.ts — adds powershell to the Language union.
  • src/extraction/grammars.ts — maps .ps1 / .psm1 / .psd1, loads the vendored PowerShell WASM, and adds the display name.
  • src/extraction/languages/powershell.ts (new) — PowerShell extractor for functions, classes, members, enums, imports, command calls, and signatures.
  • src/extraction/languages/index.ts — registers the PowerShell extractor.
  • src/extraction/wasm/tree-sitter-powershell.wasm (new) — vendored ABI-15 tree-sitter grammar.
  • src/extraction/tree-sitter.ts — handles PowerShell enum-member names, module-level assignments, class inheritance, and command/import extraction inside function bodies.
  • src/resolution/import-resolver.ts — resolves PowerShell module/script imports to file nodes.
  • src/resolution/name-matcher.ts — adds case-insensitive exact-name fallback for PowerShell commands and symbols.
  • src/resolution/index.ts — lets the resolver pre-filter accept case-insensitive PowerShell matches.
  • __tests__/extraction.test.ts — adds language detection, symbol extraction, and cross-file resolution coverage.
  • README.md — documents PowerShell in supported languages.
  • CHANGELOG.md — adds an [Unreleased] user-facing entry.

Why

PowerShell automation repos are often split across .psm1 modules, dot-sourced .ps1 helper scripts, and command-style function calls. Before this change, CodeGraph treated those files as unsupported, so agents had to fall back to file reads and grep to understand script/module flows.

This completes the static-processing path for common PowerShell projects: CodeGraph can now find PowerShell entry points, trace calls between functions, and surface module/script dependencies without reading every script by hand.

Design Decisions

Decision Rationale
Vendor tree-sitter-powershell.wasm tree-sitter-wasms does not ship PowerShell; the npm grammar ships a healthy ABI-15 WASM.
Support .ps1, .psm1, and .psd1 These are the core script, module, and manifest/data extensions PowerShell projects rely on.
Treat PowerShell calls as command AST nodes PowerShell function invocation is command-shaped (Get-Thing -Name x), not conventional call_expression syntax.
Resolve command names case-insensitively PowerShell command and function names are case-insensitive in normal execution.
Model using module, Import-Module, and dot-sourcing as imports These are the static module/script loading forms that create cross-file dependencies.
Skip dynamic invocation guesses Shapes like & $cmd or wildcard dot-sourcing are runtime-dynamic; missing an edge is safer than creating a wrong one.

Verification

✓ tree-sitter grammar health check → ABI 15, 20/20 clean parses
✓ npm run build → TypeScript build and asset copy pass
✓ npx vitest run __tests__/extraction.test.ts -t "PowerShell" → 3/3 PowerShell tests pass
✓ npx vitest run __tests__/extraction.test.ts → 375/375 extraction tests pass
✓ npm test → 1579 passed, 2 skipped
✓ sample PowerShell project verification → PASS, 2 files indexed, 13 nodes, 14 edges

Test Coverage

Test Asserts
Language detection .ps1, .psm1, and .psd1 detect as powershell; .ps1 is accepted as a source file.
Language support list powershell appears in getSupportedLanguages().
Symbol extraction Extracts function, class, method, property, enum, enum member, and module-level variable nodes.
Import extraction Emits import references for using module / Import-Module static paths.
Command call extraction Emits call references for command-style function invocations and nested script-block commands.
Inheritance extraction Extracts PowerShell base classes from class Child : Base.
Cross-file resolution Resolves using module ./Private/Helpers.psm1 to the helper module file.
Case-insensitive call resolution Resolves find-widget to function Find-Widget.

Migration

None. PowerShell support is additive. No schema changes and no breaking changes.

Existing projects should re-index to pick up PowerShell files:

codegraph index -f

For a new PowerShell project:

codegraph init

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant