* feat(resolution): close dynamic-dispatch coverage holes (callback synthesis + django ORM)
Static tree-sitter extraction misses calls whose target is computed or indirect,
so flows through callbacks, observers, and descriptors were absent from the graph.
- callback-synthesizer.ts: whole-graph pass after base resolution. Detects
registrar/dispatcher channels (field-backed observers + string-keyed
EventEmitters), correlates registration sites, and synthesizes
dispatcher->callback `calls` edges (provenance:'heuristic'). Records the
registration site (registeredAt) in edge metadata. Precision guards: named
handlers only, registrar-name match, event fan-out cap.
- frameworks/python.ts + resolution/{index,types}.ts: claimsReference hook +
django ORM resolver (_iterable_class -> ModelIterable.__iter__).
- extraction/tree-sitter.ts: extract named nested functions so inline named
handlers become linkable nodes.
trace(mutateElement, triggerRender) and trace(_fetch_all, execute_sql) now
connect; node count stable (no explosion).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(mcp): self-sufficient flow output + fix explore budget regression
- Surface synthesized-edge evidence in trace, the node trail, and context call
paths: a dynamic-dispatch hop now shows "callback via onUpdate @App.tsx:3148"
with the registration site inline (and trace inlines each hop's call-site
source line) -- the exact glue agents previously Read/Grep'd to reconstruct.
- Fix non-monotonic explore output budget: the 500-5000 file tier capped
maxCharsPerFile at 2500, BELOW the <500 tier's 3800, so on god-file projects
(excalidraw's 415 KB App.tsx) one explore returned <1% of the file and forced
a Read. Raised to 6500/file, 28000 total.
- Stop explore from inviting Read: truncation/trim notes said "use Read for
more"; they now steer to another codegraph_explore and treat returned source
as already Read.
Measured on excalidraw: best-case flow answer went from 5 reads / 131s to
0 reads / 73s with ~3-4 codegraph calls.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* chore(agent-eval): coverage probes, block-read hook, and design docs
Dev-only validation harness for the dynamic-dispatch coverage work:
- probe-{trace,node,context,explore}.mjs: drive MCP tools against a built index
without a full agent run.
- block-read-hook.sh + hook-settings.json: PreToolUse experiment that denies
source Reads to measure codegraph sufficiency (forced Read-0).
- docs/design/: callback-edge-synthesis + dynamic-dispatch-coverage playbook.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): bridge React boundaries — re-render + JSX child synthesis
Closes the two dynamic-dispatch hops that broke "state mutation -> on-screen
render" flows in React apps. Both are call-invisible (React-internal) but the
code between them is fully call-connected, so one synthesized edge each makes the
whole flow trace end-to-end.
- reactRenderEdges: setState(...) re-runs the component's render(). For each
class with a render method, link sibling methods calling this.setState ->
render. The setState gate keeps it to React class components.
- reactJsxChildEdges: a component that returns <Child .../> mounts Child. Link
parent -> each capitalized JSX child, resolved to a component/function/class
node (the resolution gate drops TS generics like Array<Foo>). File-oriented,
capped per parent.
- Surface both in synthEdgeNote (trace + node trail) and context call-paths.
Validated on excalidraw: trace(mutateElement, renderStaticScene) now connects in
6 hops across callback -> react-render -> jsx-child; 1 + 46 + 280 synthesized
edges, node count stable (no explosion). Partial coverage is worse than none:
react-render alone raised agent reads (revealed a hop it then drilled); adding
the jsx hop closed the flow and dropped reads to 0-1.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(claude): retrieval performance contract + coverage validation methodology
Add a "Retrieval performance & dynamic-dispatch coverage" section so future
changes/PRs don't silently regress agent retrieval:
- the explore call+output budget table by repo size, with the monotonic-per-file
invariant (the bug that started this: <5000 tier's 2500 < <500 tier's 3800).
- the "partial coverage is worse than none" principle.
- the required validation methodology (small/medium/large x >=3 prompts per
language x framework; deterministic probes + agent A/B; pass bar).
- the Excalidraw worked example (before/after numbers) as the template to
replicate for every language/framework.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(claude): use full n=4 measured range in Excalidraw worked example
Best run 0 Read/3 cg/76s; typical ~1 Read/~4 cg; occasional over-drill outlier.
Report the range, not a single run — run-to-run variance is large.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(mcp): steer flow questions to codegraph_trace first (tightens variance)
codegraph_trace was absent from every steering intent map — all three guidance
files routed "how does X reach Y" to context+explore, never to the trace tool.
So agents used trace only by chance; when one didn't, it floundered
reconstructing the path with search+callers (an 18-call run vs ~6 for trace-users).
Add codegraph_trace to the intent map + a "flow" common chain (trace from->to
FIRST = the whole path in one call, then ONE explore for bodies) across all three
synced files (server-instructions, instructions-template, .cursor rule).
Validated on excalidraw (hard "to the screen" Q, n=4 before/after):
- call count 3-10 -> 3-4 (over-drill outlier gone)
- duration 64-112s -> 51-74s
- trace adoption 3/4 -> 4/4; search+callers path-reconstruction -> 0
- fully-clean runs (0 Read, 0 Grep) 0/4 -> 2/4; best 3 cg / 0 / 0 / 51s
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): Vue SFC template coverage (events + kebab components)
The .vue extractor only parses <script>, so template usage is invisible —
handlers and kebab child components used only in <template> have no edge. Add a
vueTemplateEdges channel (scoped to the <template> block of .vue files):
- event bindings: @click="onClick" / v-on:submit="save" -> handler method/function
(skips inline arrows and $emit; resolves same-file first to avoid cross-app
mis-match in monorepos).
- kebab child components: <el-button> -> ElButton (PascalCase children like
<VPNav/> are already caught by the JSX channel via the SFC component node).
Surface vue-handler in synthEdgeNote (trace/node trail) + context call-paths.
Validated on vue repos (reindex, no node explosion):
- vue-handler edges: vitepress 15, vben 404, element-plus 603 — all precise
(code-login @submit -> handleLogin, register @submit -> handleSubmit, ...).
- callers(handleLogin) now includes the login component (was 0); each monorepo
app's login resolves to its own same-file handler.
- composition: PascalCase + kebab work; element-plus's el-/filename naming
(el-button -> button.vue) is a known library-prefix limitation.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record Vue validation in coverage matrix + limits
Vue / Nuxt row → ✅ template events + composition (vitepress S / vben M /
element-plus L); 🔬 reactive→render (vue-core Proxy runtime, deferred).
§7: Vue results + the two real limits — composable-destructure handlers
(@click="closeSidebar" from useSidebarControl, a data-flow frontier) and
prefix-convention kebab (el-button→button.vue). Agent reads dropped in every
size; strongest where handlers are local functions.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): resolve Vue composable-destructure template handlers
@click="closeSidebar" where `const { close: closeSidebar } = useSidebarControl()`
previously didn't resolve — the handler is a destructured composable return, not a
local fn node. Now: parse the SFC's `use*()` destructures into alias→{composable,
key}, and for an unresolved template handler follow alias → composable → the
returned member (`close`) defined in the composable's file. Precise-only: no
fallback to the composable itself (the component already has a static useX() call
edge), so we add an edge only when the specific returned fn is found.
Validated: vitepress Layout @click→close / @open-menu→open (in composables/
sidebar.ts); sidebar-flow agent run dropped 6→0 reads (best case). element-plus's
fallback-only matches correctly drop to 0; node counts stable; direct handlers
(vben handleLogin) unaffected.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): composable-destructure handlers now resolved (Vue)
@click="closeSidebar" → composable returned fn; vitepress sidebar 6→0 reads.
Remaining Vue limits: prefix-convention kebab + reactive→render frontier.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(extraction): extract function-valued properties of exported-const objects
`export const actions = { default: async () => {...} }` (SvelteKit form actions,
and general JS handler/route/reducer maps) left the arrow functions unextracted —
the walker skips object-literal functions (deliberately, to avoid inline-object
noise like `ctx.set({...})`). So an action's body (and its calls) was invisible.
Now: for an EXPORTED const whose initializer is an object literal, extract each
function-valued property (arrow / function expression) as a function named by its
key and walk its body. extractFunction gains a nameOverride so ONLY this explicit
path names pair-arrows — inline-object arrows reached by the general walker still
fall through to the <anonymous> skip, so no noise returns. JS/TS-gated.
Validated: fixtures extract the actions + walk bodies (default→helper, default→
api.post resolve); SvelteKit detection doesn't break it. Blast radius tiny:
excalidraw +1 node, Python (django) +0, Vue repos +0, realworld +11 (the actions).
Known residual: a `$lib`-alias namespace-member call (`api.post`) from an extracted
action node doesn't resolve even though the same alias resolves for `load` — a
deeper resolver interaction, separate from this extraction change. Local/relative
calls from actions connect fine.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record Svelte validation (already well-covered) + actions fix
Svelte/SvelteKit row → already strong (template calls/composition/namespace/load);
+ exported-const object-of-functions extraction. Lesson: measure before assuming
a hole — modern Svelte barely uses on:click={fn}; Svelte needed far less than Vue.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): connect Express inline arrow route handlers to their services
The Express resolver created route nodes but linked handlers via a single regex
whose `[^)]+` broke on inline arrows — so `router.post('/x', async (req,res) =>
{...})` (the dominant modern pattern) connected to NOTHING, and the anonymous
handler's body (the actual request→service flow) was lost. The whole inline-handler
API was unreachable: e.g. realworld's `POST /users/login` route → 0 edges.
Now: match the route head, span the full call with a string-aware balanced-paren
scan, and for an inline arrow handler extract its body's calls (string-aware brace
scan) and attribute them to the route node as `calls` edges. A RESERVED denylist
drops res/req/builtin methods (json, next, status, ...) to keep only business calls.
Named-handler routes keep the existing reference behavior.
Validated: realworld POST /users/login → login (auth.service); 19 precise
route→service edges (was 0) — POST /articles→createArticle, .../favorite→
favoriteArticle, etc., no json/next noise. ghost +65 inline-handler edges. No node
explosion (ghost 40767, parse 3394 unchanged). Framework-scoped: zero blast radius
off Express.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record Express validation (inline-handler fix)
Express/Koa row → resolver already handled named handlers; the real hole was
inline arrow route handlers (router.post('/x', async (req,res)=>{...})) — fixed:
route→service body calls (realworld 19 / ghost 65 edges, no explosion). Agent A/B
muddied by repo size (realworld tiny) / complexity (ghost layered API). Lesson
inverse of Svelte: Express's dominant pattern WAS the uncovered one.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record NestJS validation (already well-covered)
NestJS row → resolver handles @decorator routes; DI controller→service
(this.svc.method) resolves correctly at scale (immich: addUsersToAlbum→addUsers,
etc.). Agent A/B: codegraph eliminated Grep (0 vs 3). No dynamic-dispatch hole.
Surfaced a general hygiene gap (not NestJS): committed dist/ build output gets
indexed (no default build-dir ignore) — narrow (real apps gitignore dist/),
deferred as a core-indexer follow-up.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): Rails RESTful resources routing → controller#action
The rails resolver only saw explicit `get '/x' => 'c#a'` routes, so apps using
the dominant `resources :articles` / `resource :user` RESTful routing had ZERO
route nodes (realworld + spree: 0 routes despite full routes.rb files). The whole
request→controller flow was disconnected.
Fix (frameworks/ruby.ts):
- extract: expand `resources`/`resource` into their REST actions (only/except
filters; pluralize the singular `resource :user` → users_controller), emit a
precise `controller#action` ref per action. Explicit routes now also reference
`controller#action` instead of a bare ambiguous `action`.
- resolve: new `controller#action` pattern → the action method in
<ctrl>_controller.rb (file convention + controller-class fallback).
- claimsReference: claim `controller#action` refs so resolveOne's pre-filter
doesn't drop them before resolve() runs (same hook the django ORM work needed —
these refs name no declared symbol).
Validated: realworld 0→16, forem 0→635 precise route→action edges (GET /articles→
index, resource :user→users#show, etc.), pluralization correct, no node explosion
(route nodes proportional to resources). Agent A/B (forem, large): with codegraph
1-4 reads / 0 grep / 47-53s vs without 4-5 reads / 2-3 grep / 66-85s. Framework-
scoped (zero blast radius off Rails). Residuals: Rails Engine routing (spree
mounts an engine), ActiveRecord dynamic finders (metaprogramming frontier).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): Spring bare + class-prefixed route mappings → controller method
The Spring resolver required a string path in the mapping regex, so BARE method
mappings (`@PostMapping` with the path on the class-level `@RequestMapping`) were
missed — the dominant multi-method-controller pattern. realworld's two-action
ArticleFavoriteApi only linked one method; halo had 28 routes for 2444 files.
Fix (frameworks/java.ts):
- Treat class-level `@RequestMapping` as a PREFIX (not a bogus route) and join it
onto each method's path.
- Match verb-specific mappings (@GetMapping/@PostMapping/...) BARE or with a path.
- Also handle method-level `@RequestMapping(value=..., method=RequestMethod.X)`
(older style) — restored after an initial cut dropped it (mall regressed 292→1;
caught by the regression check).
Validated: realworld 13→19, mall 246 (all precise, class prefix joined:
GET /subject/listAll→listAll, POST /articles/{slug}/favorite→favoriteArticle +
DELETE→unfavoriteArticle), no node explosion. DI controller→service resolves
(article→findBySlug, updateArticle→canWriteArticle). Agent A/B (mall cart flow):
with codegraph 0 reads/0 grep vs without 2/2. Residuals: halo's complex custom
patterns (9/29 resolve); Spring Data JPA derived queries (metaprogramming frontier).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record Spring validation (bare-mapping routing fix)
Spring row → bare @GetMapping/@PostMapping + class @RequestMapping prefix join →
route→method (realworld 13→19, mall →246); DI controller→service resolves. A
first cut regressed mall 292→1 (dropped @RequestMapping-on-method), caught by the
route-count regression check. Residuals: halo custom patterns, JPA derived queries.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): Django DRF router.register → ViewSet
Django's ORM (_iterable_class, prior work) and URL routing (path/url/as_view→view)
were already covered. The remaining hole: DRF `router.register(r'articles',
ArticleViewSet)` — the core CRUD endpoints — wasn't extracted (only path()/url()),
so a DRF API's main resources connected to nothing (realworld's ArticleViewSet:
0 callers).
Fix (frameworks/python.ts): match `.register(r'prefix', XViewSet)` → route→ViewSet
class. The STRING first arg distinguishes DRF router.register from
`admin.site.register(Model, Admin)` (model class first arg); View/ViewSet suffix
keeps it to viewsets. The ViewSet class resolves via the existing View/ViewSet
pattern.
Validated: realworld VIEWSET /articles → ArticleViewSet (was 0). Narrow in corpus
(realworld 1 router; wagtail=path, saleor=GraphQL) but real for DRF-router APIs.
Agent A/B (wagtail Page flow, medium): with codegraph 4-7 reads / 1-4 grep / 58-81s
vs without 7-9 reads / 6 grep / 82-86s. No regression (wagtail/saleor route counts
unchanged — purely additive). Residuals: signals, DRF inherited viewset actions,
GraphQL resolvers.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): Laravel route → precise Controller@method (not bare action)
extractLaravelHandler discarded the controller: `Route::get([UserController::class,
'index'])` and `'UserController@index'` both emitted a BARE `index` ref. With the
route in routes/api.php (not the controller file), name-matching mis-resolved every
common action to the WRONG controller — realworld's GET user → ArticleController.index
(should be UserController), GET articles/feed → ArticleController (should be
FeedController), etc. The routes existed but pointed at the wrong handler.
Fix (frameworks/laravel.ts): emit precise `Controller@method` (array + string
syntax, namespace-stripped) and `claimsReference` it so resolveOne's pre-filter
doesn't drop it before Pattern-4 resolveControllerMethod runs (the recurring hook,
also needed by django ORM + Rails routing).
Validated: realworld all routes now resolve to the correct controller; bookstack
267/332 precise (GET pages → PageApiController.list, array syntax). No node
explosion. Agent A/B (bookstack page-view, large): with codegraph 2-3 reads / 1-2
grep / 51-60s vs without 4-6 / 3-5 / 60-74s. Residuals: firefly's fluent
->uses()/['uses'=>...] handler format (3/568 resolve), Eloquent dynamic finders.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): Gin/chi routes on group vars (any receiver, not just r/router)
The route regex matched only `(router|r|mux|app|e).METHOD(...)`, but real Gin/chi
apps route on GROUP variables — `v1.GET`, `PublicGroup.GET`, `userRouter.POST` —
so group-routed apps connected almost nothing: gin-vue-admin had 4 routes for 625
files. Broaden the receiver to ANY identifier; the verb + string-path + handler-arg
gates keep it route-specific (e.g. `http.Get(url)` has no handler arg, so it's
excluded).
Validated: gin-vue-admin 4→259 routes, 257 resolve precisely (POST createInfo→
CreateInfo, GET getInfoList→GetInfoList); realworld stable 24→25 (no regression);
no garbage (257/259 resolve, not false positives), node count proportional. gitness
(chi, custom handlers) is a residual (26/321). Inline `func(c *gin.Context){...}`
handlers still lose their body (anonymous, like Express was) — separate residual.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record Gin validation (group-var routing fix)
Gin/chi row → routes on ANY group var (v1.GET/PublicGroup.GET), not just r/router
(gin-vue-admin 4→259 routes). Agent A/B: 0 reads/0 grep/26-30s vs 3/3/52-53s —
cleanest backend win yet. Residuals: inline func handlers, gitness chi custom.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): ASP.NET feature-folder detection + bare attribute routes
Two holes left ASP.NET apps disconnected:
1. detect() only fired on a /Controllers/ dir, root Program.cs/Startup.cs, or a
.csproj (which often isn't in the indexed source set). Feature-folder apps
(realworld: Features/*/FooController.cs, subdir Program.cs) were never detected
→ 0 routes despite a full set of controllers. Broaden: scan Controller/Program/
Startup .cs source for ASP.NET signatures ([ApiController]/[Route]/[Http*],
ControllerBase, MapControllers, WebApplication, Microsoft.AspNetCore).
2. The attribute regex required a string path, so BARE [HttpGet] (route on the
class [Route("[controller]")]) was missed — eShopOnWeb was 24 bare / 2 string.
Match bare-or-with-path + join the class [Route] prefix (like the Spring fix).
No claimsReference needed: ASP.NET attribute routes are co-located IN the controller
with the action, so the bare method-name ref resolves same-file.
Validated: realworld 0→19 routes (all precise: GET /articles→Get, POST /articles→
Create, class prefix joined), eShopOnWeb 9→33. Route→action correct + co-located.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record ASP.NET validation (detection + bare-attribute fix)
ASP.NET Core row → feature-folder detection (realworld 0→19, was undetected) +
bare [HttpGet] / class [Route] prefix (eShopOnWeb 9→33, jellyfin 362→399). No
claimsReference needed (routes co-located in controller). Agent A/B (eShop): 1-2
reads/0 grep vs 6-7/1-6. Residual: EF Core LINQ.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): Flask/FastAPI route holes + Python builtin-name handler guard
Three fixes that connect the request→route→handler flow for Flask and
FastAPI. Validated S/L: fastapi-realworld 12→20, flask-microblog 6→27,
Netflix dispatch 290/290 (100%), redash decorator routes 6/6; canonical
flows trace end-to-end (login→get_user_by_email, create_user→from_dict).
- Flask: the route regex required `def` immediately after `@x.route(...)`,
so an intervening decorator (@login_required, @cache.cached) or stacked
@x.route lines (one view bound to several URLs) dropped the route.
Switch to the findHandler scan (match the decorator, then find the next
def) like FastAPI — skips intervening decorators.
- FastAPI: the path regex `[^'"]+` rejected the empty path `@router.get("")`
(router/prefix-root routes, frequently multi-line). Allow empty path +
guard the route name against a trailing space.
- Python builtin-name guard (src/resolution/index.ts): a handler named
after a Python builtin method (index/get/update/count…) was filtered by
isBuiltInOrExternal and lost its route→handler edge. Mirror the
dotted-method branch's knownNames guard onto the bare branch — a bare
name a declared symbol owns is a real target, not a builtin call.
+2 legit edges on realworld, 0 change on the django control (precision held).
Tests: new Flask (intervening/stacked decorator) and FastAPI (empty-path,
multi-line) extractor cases + a Flask end-to-end integration test (a view
named `index` behind @login_required). Also corrects 6 pre-existing stale
Laravel/Rails route-ref assertions surfaced by the suite — they expected
the old bare action name, but the resolvers now emit precise
controller@action / controller#action (from earlier precision commits).
Full suite green (781 passed).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record Flask/FastAPI validation (decorator + builtin-name fixes)
Matrix row Python/Flask+FastAPI 🔬→✅ and a §7 note: Flask intervening/
stacked decorators, FastAPI empty-path routes, the Python builtin-name
handler guard, S/L numbers, the login-auth A/B (0–1 read/0 grep with vs
3 read/2 grep without), and residuals (Flask-RESTful class-based
add_resource; redash JS file-route false-positives).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): Drupal route-handler resolution (claimsReference, single-colon controllers, contrib detection)
The *.routing.yml extractor and _controller/_form resolver existed but two
gaps left most routes unlinked. Validated S/M/L: admin_toolbar 0→14 (14/14),
webform 144/208, drupal-core 536→731/836 (87%); canonical flow traverses
(getAnnouncements ← /admin/announcements_feed); node count unchanged.
- claimsReference: Drupal handler refs are FQCNs (\Drupal\…\Class::method),
bare form classes (\…\SettingsForm), or single-colon controller-services
(\…\Controller:method). Only the ::method shape survived resolveOne's
pre-filter (its member is a known method name); the bare-FQCN forms and
single-colon controllers were dropped before resolve() ran. Claim FQCN /
Class:method / hook_* refs (same pattern as Rails controller#action).
- Single-colon controller match: broaden the controller regex from :: to
:{1,2} and tighten the _form branch to !name.includes(':').
- Detection: detect() only checked composer `require` for a drupal/* dep, but
a contrib module often has an empty require and is identified only by
"name":"drupal/<m>" + "type":"drupal-module" (admin_toolbar → 0 routes).
Broaden to composer name/type + a *.info.yml fallback.
Remaining unresolved is the entity-annotation handler frontier
(_entity_form: type.op) and OOP #[Hook] attributes (Drupal 11 moved ~all
procedural hooks to attribute methods — out of scope here). Tests: contrib
detection, *.info.yml fallback, claimsReference, single-colon controller.
Full suite green (787 passed).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record Drupal validation (claimsReference + contrib detection)
Add the PHP/Drupal matrix row (✅) and a §7 note: the claimsReference
pre-filter fix for FQCN/single-colon handlers, broadened contrib detection,
S/M/L numbers (admin_toolbar 0→14, webform 144/208, core 536→731), the
route→controller A/B (0 read/1 grep with vs 1 read/2 grep+glob without), and
the frontier residuals (entity-annotation handlers, OOP #[Hook] attributes).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): Axum chained methods + namespaced handlers
The Axum route extractor used a flat regex that captured only the first
method(handler) of a .route() call and only a bare \w+ handler, so two
dominant Axum idioms broke:
- method chains: .route("/user", get(get_current_user).put(update_user))
emitted no node for the .put arm — half the API was missing.
- namespaced handlers: get(listing::feed_articles) captured `listing`
(the module), so the route resolved to nothing.
Rewrite with a balanced-paren scan of each .route(...) call, a route node
per chained method, and last-::-segment handler names. realworld-axum
12→19 routes, 19/19 resolved (every chained PUT/DELETE/POST now present,
feed_articles resolves). Rocket needed nothing (550/556, 99%, attribute
macros); crates.io confirms namespaced axum handlers resolve.
Residual frontier: actix runtime routing web::get().to(handler) (the
dominant actix style, unextracted; attribute macros 35/51). Fix is
Axum-scoped — the attribute/actix/Rocket path is untouched. Tests: chained
methods + multi-line namespaced handler. Full suite green (789 passed).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record Rust/Axum validation (chained methods + namespaced handlers)
Update the Rust matrix row 🔬→✅ and add a §7 note: the Axum chained-method
+ namespaced-handler fix (realworld-axum 12→19, 19/19), Rocket already 99%,
crates.io (utoipa routes! macro frontier + SvelteKit frontend routes), the
update-user A/B, and the actix runtime-routing frontier.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): Vapor grouped/RouteCollection routing (was 0 routes on real apps)
The Vapor extractor only matched (app|router|routes).METHOD("path", use:
handler), but real Vapor apps route on a grouped builder inside
RouteCollection.boot(routes:): `let todos = routes.grouped("todos");
todos.get(use: index)` — any var receiver, no path arg (the path is the
group prefix). Every real app tested extracted 0 routes (template,
SteamPress, SwiftPackageIndex-Server, penny-bot, Feather).
Rewrite the extractor:
- any receiver (\w+), not just app/router/routes;
- optional path segments that may be non-string (User.parameter, :id, a
path constant) — the `use:` keyword discriminates a route from
Environment.get("X") / req.parameters.get("X");
- a group-prefix map from `let X = Y.grouped("a")` and
`Y.group("a") { X in }` so a grouped/nested route gets its full path
(todo.delete(use: delete) -> DELETE /todos/:todoID).
Result: vapor-template 0→3 (3/3, nested path exact), SteamPress 0→27
(27/27), SwiftPackageIndex-Server 0→14 (14/14 handler resolution).
Canonical flow traverses (createPostHandler <- GET /createPost ->
createPostView). Route names now carry a leading slash (GET /users),
consistent with the other frameworks.
Frontier: typed-route enums (SPI's SiteURL.x.pathComponents — handler
resolves, path label only) and closure handlers (app.get("x"){ } —
anonymous). Tests: grouped RouteCollection, self.handler + non-string
segments, use:-discriminator. Full suite green (792 passed).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record Vapor validation (grouped RouteCollection routing)
Update the Swift/Vapor matrix row ⬜→✅ and add a §7 note: the extractor was
dead on real apps (0 routes everywhere); rewrote for any receiver, optional
non-string paths, .grouped/.group{} prefix tracking, and the use:
discriminator. S/M/L all 100% handler resolution (template 0→3, SteamPress
0→27, SPI 0→14), the create-post A/B (0 read/0 grep with vs 1–4 read
without), and frontiers (typed-route enums, closure handlers).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): React Router <Route> JSX route extraction
react.ts extracted components/hooks and Next.js file routes but returned
references: [], so React Router <Route> declarations produced no route
nodes or route→component edges. Add <Route> JSX extraction: scan a window
after each <Route (so the nested > in element={<Comp/>} doesn't truncate
the match), pull path="…" + component={C} (v5) or element={<C/>} (v6) in
any attribute order, emit a route node + component reference (resolved by
the existing PascalCase resolveComponent). The <Routes> container is
excluded via the \b boundary.
react-realworld 0→10 routes, 10/10 resolved (/login→Login,
/editor/:slug→Editor, /@:username→Profile). No regression on excalidraw
(9,290 nodes, 46 react-render synth edges intact, 0 false routes). Tests:
v5 component=, v6 element=, <Routes>-container guard. Suite green (794).
Frontier: object data-router createBrowserRouter([{path,element}]) (modern
v6) is object-based not JSX — not covered.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record React Router routing (the React row's routing half)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): actix-web builder-API routing (web::resource / .to(handler))
Actix's attribute macros were covered, but the dominant actix style is the
builder API — web::resource("/path").route(web::get().to(handler)),
web::resource("/").to(handler) (all methods), and App .route("/path",
web::get().to(handler)). The handler is in .to(handler), not get(handler),
so the Axum .route scan extracted nothing — actix-examples had 80
web::resource calls all unlinked.
Add an actix block: scan each web::resource("/path") (bounding its method
chain at the next resource) for web::METHOD().to(h) pairs, fall back to a
direct .to(h) (method ANY), plus the App-level .route("/x",
web::METHOD().to(h)) form. actix-examples 51→128 routes, 35→112 resolved
(GET /user/{name}→with_param, POST /user→add_user). No regression on Axum
(realworld-axum still 19/19). Tests: resource+route, resource direct .to,
App-level route. Suite green (797).
Frontier: web::scope("/api") prefixes not prepended; anonymous .to(|req|…)
closures have no named target.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record actix builder-API routing validation
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(extraction): Flutter setState→build synthesis + Dart method body ranges
Two changes that connect Flutter's reactive dispatch:
- Dart method ranges (foundational): Dart models a method body as a SIBLING
of the method_signature node, so every Dart method node had endLine ==
startLine (signature only) — body-level analysis (callees, context slices,
the synthesizer's body scan) saw only `void f() {`. Extend endLine to the
resolved body in the shared createNode, guarded to only ever extend
(child-body grammars are a no-op; controls excalidraw 9,290 / django 302
unchanged).
- Flutter setState→build synthesizer channel (the Dart analog of react-render):
for each Dart class with a `build` method, link sibling methods whose body
calls setState( → build. setState re-runs build (Flutter-internal, no static
edge), so "tap → handler → setState → rebuilt UI" dead-ended at setState.
counter initState→build, books build→BookDetail/BookForm. Widget composition
needs no synthesis — Dart widgets are explicit constructor calls, already
static (compass_app build→ErrorIndicator/HomeButton). Tests: Dart method
spans its body; Flutter handler→build synthesis end-to-end. Suite green (798).
Frontier: MVVM Command/ChangeNotifier dispatch (no setState) + Navigator.push
route-as-widget navigation.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record Dart/Flutter validation (setState→build + method ranges)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): Spring Boot Kotlin routing (.kt + fun handlers)
Kotlin had zero framework coverage — no resolver listed kotlin, and the
Spring resolver was languages:['java'] with a .java-only extract gate and a
Java-syntax handler regex (public X name()). Spring Boot Kotlin apps (same
@GetMapping/@RestController annotations, .kt files) extracted 0 routes.
Extend the Spring resolver: languages ['java','kotlin'], accept .kt, and add
a Kotlin `fun name(` alternative to the handler-method regex (Kotlin has no
access modifier; the return type follows the name). Also allow Kotlin class
modifiers (open/data/sealed) in the class @RequestMapping-prefix detection,
and tag route/ref language per file.
spring-petclinic-kotlin 0→18 routes, 18/18 resolved; class @RequestMapping
prefixes join, stacked annotations skipped, DI controller→repo resolves
(showOwner ← GET /owners/{ownerId} → OwnerRepository.findById). Java Spring
unchanged (realworld 19/19 — the Kotlin fun and Java public-X alternatives
are disjoint per language). Jetpack Compose composition already works
(@Composable→child are plain function calls). Tests: Kotlin @GetMapping+fun,
class-prefix + stacked annotation. Suite green (800).
Frontier: Ktor inline-lambda routing, Compose recomposition, coroutines/Flow.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record Kotlin validation (Spring Boot Kotlin + Compose)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record Lua/Luau validation (module dispatch already covered)
Measure-first: Neovim/Roblox dispatch is module-heavy (require + cross-file
mod.fn calls), already resolved by general import+name resolution
(telescope.nvim 220 imports + 335 cross-file calls; traces end-to-end). The
matrix's assumed "callback synthesizer" hole isn't real — event-callback
registration (keymap/autocmd/:Connect) is predominantly inline anonymous
closures (corpus ~12 inline vs ~2 named), too rare to synthesize. A/B: 0
read/0 grep with codegraph vs 1 read without. No code change; validated.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): Play Framework conf/routes → controller routing (Scala/Java)
Play declares routes in an extensionless conf/routes file (GET /computers
controllers.Application.list(p: Int ?= 0)) the file walk never indexed
(isSourceFile requires an extension), so Play apps had 0 route nodes.
- grammars.ts: add isPlayRoutesFile (conf/routes + *.routes), opt it into
isSourceFile, and map it to the no-grammar (yaml-style) path in
detectLanguage so the framework resolver extracts it. Narrow match — only
ADDS Play routes files, never affects other indexing.
- play.ts: a Play resolver — detect (build.sbt/conf), extract (parse each
METHOD /path Controller.action(args) line, drop package + args), resolve
(Controller.action → the action method in that controller class),
claimsReference for the dotted Controller.action handler.
computer-database 0→8 routes, 7/8 resolved (the 1 unresolved is
controllers.Assets.versioned — Play's framework controller, external);
starter 0→4 (3/4). Flow connects request→route→controller→DAO. No-regression
(excalidraw 9,290 / suite unchanged). Tests: routes parse + `->` include
skipped, conf/routes file detection.
Frontier: SIRD programmatic routers (-> include + case GET(p"/x")) + Akka
actor message→handler.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record Scala/Play validation (conf/routes → controller)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(extraction): C++ inheritance (base_class_clause) + virtual-override synthesis
C/C++ direct dispatch already resolves well (redis 29k / leveldb 1.4k
cross-file calls). Two changes close the C++ virtual-dispatch gap:
- extractInheritance handled base_clause (PHP) but not C++'s
base_class_clause, so C++ `extends` edges were missing/partial. Add the
C++ branch (emit an extends ref per base type, skipping access
specifiers) — leveldb extends 219→298.
- cpp-override synthesizer channel (the C++ analog of react-render): for
each extends edge, link each base method → the subclass override of the
same name, so trace/callees from a virtual/interface method reach the
implementation. Gated to C++, capped per class. leveldb 12 precise edges
(Iterator::Next/Seek/Prev → MergingIterator), 0 on C (redis) and TS
(excalidraw). Test: base virtual → subclass override bridge.
Frontier: C callback structs (cmd->proc() → 422-way fan-out, too noisy)
and C++ pure-virtual base methods (declarations aren't nodes, so those
overrides can't bridge). Suite green (804).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record C/C++ validation (inheritance fix + override synthesis)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): React Router object data-router + Next.js route precision
- Object data-router (v6.4+): createBrowserRouter([{ path, element: <Comp/> }])
/ { path, Component: Comp } — extract route + component (gated to files using
the data-router API; requires a component so a stray `path:` field isn't a route).
- Next.js precision: filePathToRoute treated config files (next.config.mjs,
vite.config.ts) and a `nextjs-pages/` dir (substring of "pages/") as routes.
Require a real page extension (.tsx/.ts/.jsx/.js), exclude *.config.* and
_app/_document, and match pages/ + app/ as path SEGMENTS. bulletproof-react
4 bogus config "routes" → 0.
Frontier: lazy data-router routes (path: paths.x.path + lazy: () => import())
use variable paths + lazily-imported modules — no literal path/named component.
Tests: object-router literal form, config/nextjs-pages exclusion. Suite 806.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): Flask-RESTful add_resource + tuple methods + broader detection
Three Flask gaps closed (redash Flask-RESTful 6→77 py routes; flask-realworld 0→19):
- Flask-RESTful: api.add_resource(ResourceClass, '/path') (+ redash's
add_org_resource) now extracts a route per path referencing the Resource
class, whose get/post verb methods resolve as the handlers.
- Tuple methods: @x.route('/p', methods=('POST',)) — the method regex only
accepted a list [...]; now accepts a tuple (...) too, so POST/DELETE routes
aren't mislabeled GET.
- Detection: detect() only checked root app.py for the literal Flask(__name__);
broadened to requirements/pyproject/Pipfile/setup.py + any entrypoint file
(root or subdir, e.g. conduit/app.py) that imports flask and instantiates
Flask(...). flask-realworld (subdir app-factory) 0→19; django not falsely
detected.
Tests: tuple methods, add_resource. Suite green (808).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record frontier pass; test(go): gorilla/mux subrouter coverage
Frontier triage after the main sweep — tractable partials closed (React object
data-router, Next.js false-positive fix, Flask-RESTful add_resource, Flask
tuple methods + detection, gorilla/mux confirmed), and the genuinely
hard/low-precision ones (C callback fan-out, metaprogramming finders, reactive
runtimes, Akka, anonymous closures, lazy data-router, C++ pure-virtual) left
documented with rationale. Adds a gorilla/mux subrouter-var HandleFunc test
(confirms the any-receiver handling already covers it).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(benchmarks): A/B with/without codegraph across every language (S/M/L)
37-cell matrix (every flow-relevant language × small/medium/large indexed
repos): a headless agent answers one canonical flow question per repo, with the
codegraph MCP vs without any MCP. Fresh re-index per cell so the with-arm
reflects current resolvers.
Result: 75% fewer file reads with codegraph (40 vs 158 across cells), ~70%
fewer greps, never more reads in any cell. Biggest wins on medium/large
backends (excalidraw 0R vs 9R, spring-halo 0R vs 9R+8 Bash, jellyfin 4R vs 13R+
21 Bash + a spawned sub-agent); tie zone on tiny repos where the flow fits in
1-2 files.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(mcp): self-sufficient codegraph_trace + CODEGRAPH_MCP_TOOLS allowlist
codegraph_trace now returns a complete flow dossier in one call: each hop with its full body inlined (not just the call-site line), plus the destination's own outgoing calls — the last mile agents otherwise explore/Read to get. Validated by A/B (arm I, 6 repos x 2): >= baseline on reads/turns/cost with no wall-clock regression, because one richer trace call displaces the explore+node+Read follow-ups. Sufficiency, not steering: complete context is what stops further investigation.
Also adds CODEGRAPH_MCP_TOOLS, an optional comma-separated allowlist that trims the exposed MCP tool surface (inert when unset); used to run the tool-ablation experiment cleanly, and useful for constraining an agent to a minimal surface.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(benchmarks): call-sequence + tool-ablation analysis; agent-eval arms harness
Records why codegraph read savings (-75%) under-convert to wall-clock (-16%): the bottleneck is round-trips + the synthesis turn, not reads. Ablation (arms A-I) shows explore is 68% of payload but load-bearing, trace is path-scoped but under-adopted, instruction/description steering cannot match an append-prompt's salience (and regresses), and the shippable win is making the trace output sufficient (arm I). Adds harness: seq-matrix, run-arms/arms-*, parse-arms.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(mcp): line-number codegraph_node + codegraph_trace source output
node's code block and trace's inlined hop/destination bodies now carry cat -n line numbers (reusing numberSourceLines, matching codegraph_explore and Read), so the agent can cite or edit exact lines without re-Reading the file just to get them. Consistency across the code-returning tools + edit-workflow sufficiency.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(resolution): Java/Kotlin interface & abstract dispatch synthesis
A call through an injected interface (Spring @Autowired svc.list()) or an abstract base dead-ended at the interface method — no static edge to the implementation — so request->service->impl flows broke at the DI boundary. Adds interfaceOverrideEdges: for each class implementing an interface (or extending an abstract base), synthesize interface/base-method -> same-name override 'calls' edges (JVM-gated, capped per class, overload-aware), with an 'interface-impl' trace label. trace + callees now follow the flow into the implementation.
Validated on spring-mall: 310 synth edges, node count unchanged (edges only); trace(PmsProductController.getList, PmsProductServiceImpl.list) connects in 3 hops (controller -> service interface -> impl) where it previously dead-ended at the interface.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(playbook): record Java/Kotlin interface-DI synthesizer (probe-validated; agent A/B adoption-gated)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(mcp): codegraph_explore surfaces the execution flow from its named symbols
Agents call explore far more than trace and pass a bag of symbol names that spans the flow they're after. explore now resolves those names and surfaces the longest call path AMONG them — riding synthesized dynamic-dispatch edges (callback/react-render/jsx/interface-impl) — leading the output with it, so a flow question answered via explore gets the trace-quality path without switching tools.
Precision: ambiguous tokens disambiguated by CO-NAMING (keep candidates whose qualifiedName SEGMENT matches another named token, so 'list' resolves to PmsProductServiceImpl::list not OmsOrderService::list); BFS anchored at named symbols on both ends with <=1 consecutive unnamed bridge (crosses a missing intermediate, never wanders a god-function's fan-out). Validated by probe: spring-mall getList->service-interface->impl (3 hops); excalidraw mutateElement->triggerUpdate->[callback]->triggerRender->[react-render]->render->[jsx]->StaticCanvas (full re-render chain). No flow section on fuzzy queries (safe). Suite green.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(mcp): explore-flow resolves qualified Class.method query tokens
The agent often passes fully-qualified names to explore (PostEndpoint.publishPost, PmsProductServiceImpl.list) — its most precise input. The tokenizer's file-extension strip mangled Class.method into Class (treating .method as an extension), then the identifier filter dropped anything with a dot, throwing the method away. Now strips only REAL file extensions and keeps qualified tokens, which findAllSymbols resolves exactly; disambiguates ambiguous SIMPLE names by whether their container class is also named (segment match). Validated: 'PmsProductController.getList PmsProductServiceImpl.list' now surfaces getList->interface->impl. (spring-halo's publish flow stays absent — it's reactive/reconciler dispatch with no static edges, a coverage frontier, not an explore-flow gap.)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(claude): record the 'adapt the tool to the agent' retrieval principle
The lever that decides whether a retrieval change lands: make a tool the agent already calls do more with the input it already gives; changes that need the agent to behave differently (different tool, query, examples) hit codegraph's low-salience channels and don't land. Captures the validated evidence (sufficiency + explore-flow pass; steering + new-tools + context-fuzzy-flow fail) and points coverage as the remaining lever.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs: correct 'cost stays flat' → neutral-to-lower (excalidraw with/without A/B)
Fresh with-vs-without A/B on excalidraw (current build, n=3): 3x faster (49s vs 145s), 15x fewer tool calls, ~0 vs 23 reads, and -40% cost ($0.41 vs $0.68). Cost is neutral-to-lower, not flat — compact codegraph answers cache across turns while the without-arm's read/grep thrash is fresh, poorly-cacheable input. Recorded in call-sequence-analysis.md; corrected the CLAUDE.md optimization-target note (still: don't optimize for cost; target wall-clock + tool-call count).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(benchmarks): current-build A/B on all 7 README repos + fix token-measurement bug
Re-ran the README benchmark on the current build (7 repos reindexed, median of 4): avg 35% cost / 57% tokens / 46% time / 71% tool calls saved — reproduces the published README (35/59/49/70), no regression. Adds bench-readme.sh + parse-bench-readme.mjs harness.
Fixes a token-measurement bug: result.usage is last-turn-only in current Claude Code; must sum per-turn assistant usage for cumulative tokens. Corrects the earlier excalidraw note (its '-34% tokens' was off this bug; real ~90%) and the cost MECHANISM (volume/fewer-turns, not cache-ability — the without-arm's huge token volume is mostly cheap cache-reads, so token savings 57% > cost savings 35%). Cost/time were always correct.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs: finalize 0.9.4 — consolidate CHANGELOG + re-validate README benchmark
Folds the framework sweep + retrieval work into [0.9.4] (2026-05-24). README benchmark table refreshed with current-build medians (avg 35% cost / 57% tokens / 46% time / 71% tool calls) + a v0.9.4 re-validation note.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs(readme): add codegraph_trace to the MCP Tools table
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
3903 lines
125 KiB
TypeScript
3903 lines
125 KiB
TypeScript
/**
|
|
* Extraction Tests
|
|
*
|
|
* Tests for the tree-sitter extraction system.
|
|
*/
|
|
|
|
import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import * as os from 'os';
|
|
import { CodeGraph } from '../src';
|
|
import { extractFromSource, scanDirectory } from '../src/extraction';
|
|
import { detectLanguage, isLanguageSupported, getSupportedLanguages, initGrammars, loadAllGrammars } from '../src/extraction/grammars';
|
|
import { normalizePath } from '../src/utils';
|
|
|
|
beforeAll(async () => {
|
|
await initGrammars();
|
|
await loadAllGrammars();
|
|
});
|
|
|
|
// Create a temporary directory for each test
|
|
function createTempDir(): string {
|
|
return fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-test-'));
|
|
}
|
|
|
|
// Clean up temporary directory
|
|
function cleanupTempDir(dir: string): void {
|
|
if (fs.existsSync(dir)) {
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
describe('Language Detection', () => {
|
|
it('should detect TypeScript files', () => {
|
|
expect(detectLanguage('src/index.ts')).toBe('typescript');
|
|
expect(detectLanguage('components/Button.tsx')).toBe('tsx');
|
|
});
|
|
|
|
it('should detect JavaScript files', () => {
|
|
expect(detectLanguage('index.js')).toBe('javascript');
|
|
expect(detectLanguage('App.jsx')).toBe('jsx');
|
|
expect(detectLanguage('config.mjs')).toBe('javascript');
|
|
});
|
|
|
|
it('should detect Python files', () => {
|
|
expect(detectLanguage('main.py')).toBe('python');
|
|
});
|
|
|
|
it('should detect Go files', () => {
|
|
expect(detectLanguage('main.go')).toBe('go');
|
|
});
|
|
|
|
it('should detect Rust files', () => {
|
|
expect(detectLanguage('lib.rs')).toBe('rust');
|
|
});
|
|
|
|
it('should detect Java files', () => {
|
|
expect(detectLanguage('Main.java')).toBe('java');
|
|
});
|
|
|
|
it('should detect C files', () => {
|
|
expect(detectLanguage('main.c')).toBe('c');
|
|
expect(detectLanguage('utils.h')).toBe('c');
|
|
});
|
|
|
|
it('should detect C++ files', () => {
|
|
expect(detectLanguage('main.cpp')).toBe('cpp');
|
|
expect(detectLanguage('class.hpp')).toBe('cpp');
|
|
});
|
|
|
|
it('should detect C# files', () => {
|
|
expect(detectLanguage('Program.cs')).toBe('csharp');
|
|
});
|
|
|
|
it('should detect PHP files', () => {
|
|
expect(detectLanguage('index.php')).toBe('php');
|
|
});
|
|
|
|
it('should detect Ruby files', () => {
|
|
expect(detectLanguage('app.rb')).toBe('ruby');
|
|
});
|
|
|
|
it('should detect Swift files', () => {
|
|
expect(detectLanguage('ViewController.swift')).toBe('swift');
|
|
});
|
|
|
|
it('should detect Kotlin files', () => {
|
|
expect(detectLanguage('MainActivity.kt')).toBe('kotlin');
|
|
expect(detectLanguage('build.gradle.kts')).toBe('kotlin');
|
|
});
|
|
|
|
it('should detect Dart files', () => {
|
|
expect(detectLanguage('main.dart')).toBe('dart');
|
|
});
|
|
|
|
it('should return unknown for unsupported extensions', () => {
|
|
expect(detectLanguage('styles.css')).toBe('unknown');
|
|
expect(detectLanguage('data.json')).toBe('unknown');
|
|
});
|
|
});
|
|
|
|
describe('Language Support', () => {
|
|
it('should report supported languages', () => {
|
|
expect(isLanguageSupported('typescript')).toBe(true);
|
|
expect(isLanguageSupported('python')).toBe(true);
|
|
expect(isLanguageSupported('go')).toBe(true);
|
|
expect(isLanguageSupported('unknown')).toBe(false);
|
|
});
|
|
|
|
it('should list all supported languages', () => {
|
|
const languages = getSupportedLanguages();
|
|
expect(languages).toContain('typescript');
|
|
expect(languages).toContain('javascript');
|
|
expect(languages).toContain('python');
|
|
expect(languages).toContain('go');
|
|
expect(languages).toContain('rust');
|
|
expect(languages).toContain('java');
|
|
expect(languages).toContain('csharp');
|
|
expect(languages).toContain('php');
|
|
expect(languages).toContain('ruby');
|
|
expect(languages).toContain('swift');
|
|
expect(languages).toContain('kotlin');
|
|
expect(languages).toContain('dart');
|
|
});
|
|
});
|
|
|
|
describe('TypeScript Extraction', () => {
|
|
it('should extract function declarations', () => {
|
|
const code = `
|
|
export function processPayment(amount: number): Promise<Receipt> {
|
|
return stripe.charge(amount);
|
|
}
|
|
`;
|
|
const result = extractFromSource('payment.ts', code);
|
|
|
|
// File node + function node
|
|
const fileNode = result.nodes.find((n) => n.kind === 'file');
|
|
expect(fileNode).toBeDefined();
|
|
expect(fileNode?.name).toBe('payment.ts');
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function');
|
|
expect(funcNode).toMatchObject({
|
|
kind: 'function',
|
|
name: 'processPayment',
|
|
language: 'typescript',
|
|
isExported: true,
|
|
});
|
|
expect(funcNode?.signature).toContain('amount: number');
|
|
});
|
|
|
|
it('should extract class declarations', () => {
|
|
const code = `
|
|
export class PaymentService {
|
|
private stripe: StripeClient;
|
|
|
|
constructor(apiKey: string) {
|
|
this.stripe = new StripeClient(apiKey);
|
|
}
|
|
|
|
async charge(amount: number): Promise<Receipt> {
|
|
return this.stripe.charge(amount);
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('service.ts', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
const methodNodes = result.nodes.filter((n) => n.kind === 'method');
|
|
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('PaymentService');
|
|
expect(classNode?.isExported).toBe(true);
|
|
|
|
expect(methodNodes.length).toBeGreaterThanOrEqual(1);
|
|
const chargeMethod = methodNodes.find((m) => m.name === 'charge');
|
|
expect(chargeMethod).toBeDefined();
|
|
});
|
|
|
|
it('should extract interfaces', () => {
|
|
const code = `
|
|
export interface User {
|
|
id: string;
|
|
name: string;
|
|
email: string;
|
|
}
|
|
`;
|
|
const result = extractFromSource('types.ts', code);
|
|
|
|
const fileNode = result.nodes.find((n) => n.kind === 'file');
|
|
expect(fileNode).toBeDefined();
|
|
|
|
const ifaceNode = result.nodes.find((n) => n.kind === 'interface');
|
|
expect(ifaceNode).toMatchObject({
|
|
kind: 'interface',
|
|
name: 'User',
|
|
isExported: true,
|
|
});
|
|
});
|
|
|
|
it('should track function calls', () => {
|
|
const code = `
|
|
function main() {
|
|
const result = processData();
|
|
console.log(result);
|
|
}
|
|
`;
|
|
const result = extractFromSource('main.ts', code);
|
|
|
|
expect(result.unresolvedReferences.length).toBeGreaterThan(0);
|
|
const calls = result.unresolvedReferences.filter((r) => r.referenceKind === 'calls');
|
|
expect(calls.some((c) => c.referenceName === 'processData')).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('Arrow Function Export Extraction', () => {
|
|
it('should extract exported arrow functions assigned to const', () => {
|
|
const code = `
|
|
export const useAuth = (): AuthContextValue => {
|
|
return useContext(AuthContext);
|
|
};
|
|
`;
|
|
const result = extractFromSource('hooks.ts', code);
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function' && n.name === 'useAuth');
|
|
expect(funcNode).toBeDefined();
|
|
expect(funcNode).toMatchObject({
|
|
kind: 'function',
|
|
name: 'useAuth',
|
|
isExported: true,
|
|
});
|
|
});
|
|
|
|
it('should extract exported function expressions assigned to const', () => {
|
|
const code = `
|
|
export const processData = function(input: string): string {
|
|
return input.trim();
|
|
};
|
|
`;
|
|
const result = extractFromSource('utils.ts', code);
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function' && n.name === 'processData');
|
|
expect(funcNode).toBeDefined();
|
|
expect(funcNode).toMatchObject({
|
|
kind: 'function',
|
|
name: 'processData',
|
|
isExported: true,
|
|
});
|
|
});
|
|
|
|
it('should not extract non-exported arrow functions as exported', () => {
|
|
const code = `
|
|
const internalHelper = () => {
|
|
return 42;
|
|
};
|
|
`;
|
|
const result = extractFromSource('internal.ts', code);
|
|
|
|
const helperNode = result.nodes.find((n) => n.name === 'internalHelper');
|
|
expect(helperNode).toBeDefined();
|
|
expect(helperNode?.isExported).toBeFalsy();
|
|
});
|
|
|
|
it('should still skip truly anonymous arrow functions', () => {
|
|
const code = `
|
|
const items = [1, 2, 3].map((x) => x * 2);
|
|
`;
|
|
const result = extractFromSource('anon.ts', code);
|
|
|
|
// The inline arrow function passed to .map() has no variable_declarator parent
|
|
// and should remain anonymous (skipped)
|
|
const anonFunctions = result.nodes.filter(
|
|
(n) => n.kind === 'function' && n.name === '<anonymous>'
|
|
);
|
|
expect(anonFunctions).toHaveLength(0);
|
|
});
|
|
|
|
it('should extract multiple exported arrow functions from the same file', () => {
|
|
const code = `
|
|
export const add = (a: number, b: number): number => a + b;
|
|
|
|
export const subtract = (a: number, b: number): number => a - b;
|
|
|
|
const internal = () => 'not exported';
|
|
`;
|
|
const result = extractFromSource('math.ts', code);
|
|
|
|
const exported = result.nodes.filter((n) => n.kind === 'function' && n.isExported);
|
|
expect(exported).toHaveLength(2);
|
|
expect(exported.map((n) => n.name).sort()).toEqual(['add', 'subtract']);
|
|
|
|
const internalNode = result.nodes.find((n) => n.name === 'internal');
|
|
expect(internalNode).toBeDefined();
|
|
expect(internalNode?.isExported).toBeFalsy();
|
|
});
|
|
|
|
it('should extract arrow functions in JavaScript files', () => {
|
|
const code = `
|
|
export const fetchData = async () => {
|
|
const response = await fetch('/api/data');
|
|
return response.json();
|
|
};
|
|
`;
|
|
const result = extractFromSource('api.js', code);
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function' && n.name === 'fetchData');
|
|
expect(funcNode).toBeDefined();
|
|
expect(funcNode).toMatchObject({
|
|
kind: 'function',
|
|
name: 'fetchData',
|
|
isExported: true,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('Type Alias Extraction', () => {
|
|
it('should extract exported type aliases in TypeScript', () => {
|
|
const code = `
|
|
export type AuthContextValue = {
|
|
user: User | null;
|
|
login: () => void;
|
|
logout: () => void;
|
|
};
|
|
`;
|
|
const result = extractFromSource('types.ts', code);
|
|
|
|
const typeNode = result.nodes.find((n) => n.kind === 'type_alias');
|
|
expect(typeNode).toMatchObject({
|
|
kind: 'type_alias',
|
|
name: 'AuthContextValue',
|
|
isExported: true,
|
|
});
|
|
});
|
|
|
|
it('should extract non-exported type aliases', () => {
|
|
const code = `
|
|
type InternalState = {
|
|
loading: boolean;
|
|
error: string | null;
|
|
};
|
|
`;
|
|
const result = extractFromSource('internal.ts', code);
|
|
|
|
const typeNode = result.nodes.find((n) => n.kind === 'type_alias');
|
|
expect(typeNode).toMatchObject({
|
|
kind: 'type_alias',
|
|
name: 'InternalState',
|
|
isExported: false,
|
|
});
|
|
});
|
|
|
|
it('should extract multiple type aliases from the same file', () => {
|
|
const code = `
|
|
export type UnitSystem = 'metric' | 'imperial';
|
|
export type DateFormat = 'ISO' | 'US' | 'EU';
|
|
type Internal = string;
|
|
`;
|
|
const result = extractFromSource('config.ts', code);
|
|
|
|
const typeAliases = result.nodes.filter((n) => n.kind === 'type_alias');
|
|
expect(typeAliases).toHaveLength(3);
|
|
|
|
const exported = typeAliases.filter((n) => n.isExported);
|
|
expect(exported).toHaveLength(2);
|
|
expect(exported.map((n) => n.name).sort()).toEqual(['DateFormat', 'UnitSystem']);
|
|
});
|
|
});
|
|
|
|
describe('Exported Variable Extraction', () => {
|
|
it('should extract exported const with call expression (Zustand store)', () => {
|
|
const code = `
|
|
export const useUIStore = create<UIState>((set) => ({
|
|
isOpen: false,
|
|
toggle: () => set((s) => ({ isOpen: !s.isOpen })),
|
|
}));
|
|
`;
|
|
const result = extractFromSource('store.ts', code);
|
|
|
|
const varNode = result.nodes.find((n) => n.kind === 'constant' && n.name === 'useUIStore');
|
|
expect(varNode).toBeDefined();
|
|
expect(varNode?.isExported).toBe(true);
|
|
});
|
|
|
|
it('should extract exported const with object literal', () => {
|
|
const code = `
|
|
export const config = {
|
|
apiUrl: 'https://api.example.com',
|
|
timeout: 5000,
|
|
};
|
|
`;
|
|
const result = extractFromSource('config.ts', code);
|
|
|
|
const varNode = result.nodes.find((n) => n.kind === 'constant' && n.name === 'config');
|
|
expect(varNode).toBeDefined();
|
|
expect(varNode?.isExported).toBe(true);
|
|
});
|
|
|
|
it('should extract exported const with array literal', () => {
|
|
const code = `
|
|
export const SCREEN_NAMES = ['home', 'settings', 'profile'] as const;
|
|
`;
|
|
const result = extractFromSource('constants.ts', code);
|
|
|
|
const varNode = result.nodes.find((n) => n.kind === 'constant' && n.name === 'SCREEN_NAMES');
|
|
expect(varNode).toBeDefined();
|
|
expect(varNode?.isExported).toBe(true);
|
|
});
|
|
|
|
it('should extract exported const with primitive value', () => {
|
|
const code = `
|
|
export const MAX_RETRIES = 3;
|
|
export const API_VERSION = "v2";
|
|
`;
|
|
const result = extractFromSource('constants.ts', code);
|
|
|
|
const variables = result.nodes.filter((n) => n.kind === 'constant');
|
|
expect(variables).toHaveLength(2);
|
|
expect(variables.map((n) => n.name).sort()).toEqual(['API_VERSION', 'MAX_RETRIES']);
|
|
});
|
|
|
|
it('should NOT duplicate arrow functions as both function and variable', () => {
|
|
const code = `
|
|
export const useAuth = () => {
|
|
return useContext(AuthContext);
|
|
};
|
|
`;
|
|
const result = extractFromSource('hooks.ts', code);
|
|
|
|
// Should be extracted as function (from arrow function handler), NOT as variable
|
|
const funcNodes = result.nodes.filter((n) => n.kind === 'function' && n.name === 'useAuth');
|
|
const varNodes = result.nodes.filter((n) => n.kind === 'variable' && n.name === 'useAuth');
|
|
expect(funcNodes).toHaveLength(1);
|
|
expect(varNodes).toHaveLength(0);
|
|
});
|
|
|
|
it('should extract non-exported const as non-exported variable', () => {
|
|
const code = `
|
|
const internalConfig = {
|
|
debug: true,
|
|
};
|
|
`;
|
|
const result = extractFromSource('internal.ts', code);
|
|
|
|
// Non-exported const at file level should be extracted as a constant (not exported)
|
|
const varNodes = result.nodes.filter((n) => (n.kind === 'variable' || n.kind === 'constant') && n.name === 'internalConfig');
|
|
expect(varNodes).toHaveLength(1);
|
|
expect(varNodes[0]?.isExported).toBeFalsy();
|
|
});
|
|
|
|
it('should extract Zod schema exports', () => {
|
|
const code = `
|
|
export const userSchema = z.object({
|
|
id: z.string(),
|
|
name: z.string(),
|
|
email: z.string().email(),
|
|
});
|
|
`;
|
|
const result = extractFromSource('schemas.ts', code);
|
|
|
|
const varNode = result.nodes.find((n) => n.kind === 'constant' && n.name === 'userSchema');
|
|
expect(varNode).toBeDefined();
|
|
expect(varNode?.isExported).toBe(true);
|
|
});
|
|
|
|
it('should extract XState machine exports', () => {
|
|
const code = `
|
|
export const authMachine = createMachine({
|
|
id: "auth",
|
|
initial: "idle",
|
|
states: {
|
|
idle: {},
|
|
authenticated: {},
|
|
},
|
|
});
|
|
`;
|
|
const result = extractFromSource('machine.ts', code);
|
|
|
|
const varNode = result.nodes.find((n) => n.kind === 'constant' && n.name === 'authMachine');
|
|
expect(varNode).toBeDefined();
|
|
expect(varNode?.isExported).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('File Node Extraction', () => {
|
|
it('should create a file-kind node for each parsed file', () => {
|
|
const code = `
|
|
export function greet(name: string): string {
|
|
return "Hello " + name;
|
|
}
|
|
`;
|
|
const result = extractFromSource('greeter.ts', code);
|
|
|
|
const fileNode = result.nodes.find((n) => n.kind === 'file');
|
|
expect(fileNode).toBeDefined();
|
|
expect(fileNode?.name).toBe('greeter.ts');
|
|
expect(fileNode?.filePath).toBe('greeter.ts');
|
|
expect(fileNode?.language).toBe('typescript');
|
|
expect(fileNode?.startLine).toBe(1);
|
|
});
|
|
|
|
it('should create file nodes for Python files', () => {
|
|
const code = `
|
|
def main():
|
|
pass
|
|
`;
|
|
const result = extractFromSource('main.py', code);
|
|
|
|
const fileNode = result.nodes.find((n) => n.kind === 'file');
|
|
expect(fileNode).toBeDefined();
|
|
expect(fileNode?.name).toBe('main.py');
|
|
expect(fileNode?.language).toBe('python');
|
|
});
|
|
|
|
it('should create containment edges from file node to top-level declarations', () => {
|
|
const code = `
|
|
export function foo() {}
|
|
export function bar() {}
|
|
`;
|
|
const result = extractFromSource('fns.ts', code);
|
|
|
|
const fileNode = result.nodes.find((n) => n.kind === 'file');
|
|
expect(fileNode).toBeDefined();
|
|
|
|
// There should be contains edges from the file node to each function
|
|
const containsEdges = result.edges.filter(
|
|
(e) => e.source === fileNode?.id && e.kind === 'contains'
|
|
);
|
|
expect(containsEdges.length).toBeGreaterThanOrEqual(2);
|
|
});
|
|
});
|
|
|
|
describe('Python Extraction', () => {
|
|
it('should extract function definitions', () => {
|
|
const code = `
|
|
def calculate_total(items: list, tax_rate: float) -> float:
|
|
"""Calculate total with tax."""
|
|
subtotal = sum(item.price for item in items)
|
|
return subtotal * (1 + tax_rate)
|
|
`;
|
|
const result = extractFromSource('calc.py', code);
|
|
|
|
const fileNode = result.nodes.find((n) => n.kind === 'file');
|
|
expect(fileNode).toBeDefined();
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function');
|
|
expect(funcNode).toMatchObject({
|
|
kind: 'function',
|
|
name: 'calculate_total',
|
|
language: 'python',
|
|
});
|
|
});
|
|
|
|
it('should extract class definitions', () => {
|
|
const code = `
|
|
class UserService:
|
|
"""Service for managing users."""
|
|
|
|
def __init__(self, db):
|
|
self.db = db
|
|
|
|
def get_user(self, user_id: str) -> User:
|
|
return self.db.find_user(user_id)
|
|
`;
|
|
const result = extractFromSource('service.py', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('UserService');
|
|
});
|
|
});
|
|
|
|
describe('Go Extraction', () => {
|
|
it('should extract function declarations', () => {
|
|
const code = `
|
|
package main
|
|
|
|
func ProcessOrder(order Order) (Receipt, error) {
|
|
// Process the order
|
|
return Receipt{}, nil
|
|
}
|
|
`;
|
|
const result = extractFromSource('main.go', code);
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function');
|
|
expect(funcNode).toBeDefined();
|
|
expect(funcNode?.name).toBe('ProcessOrder');
|
|
});
|
|
|
|
it('should extract method declarations', () => {
|
|
const code = `
|
|
package main
|
|
|
|
type Service struct {
|
|
db *Database
|
|
}
|
|
|
|
func (s *Service) GetUser(id string) (*User, error) {
|
|
return s.db.FindUser(id)
|
|
}
|
|
`;
|
|
const result = extractFromSource('service.go', code);
|
|
|
|
const methodNode = result.nodes.find((n) => n.kind === 'method');
|
|
expect(methodNode).toBeDefined();
|
|
expect(methodNode?.name).toBe('GetUser');
|
|
});
|
|
});
|
|
|
|
describe('Rust Extraction', () => {
|
|
it('should extract function declarations', () => {
|
|
const code = `
|
|
pub fn process_data(input: &str) -> Result<Output, Error> {
|
|
// Process data
|
|
Ok(Output::new())
|
|
}
|
|
`;
|
|
const result = extractFromSource('lib.rs', code);
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function');
|
|
expect(funcNode).toBeDefined();
|
|
expect(funcNode?.name).toBe('process_data');
|
|
expect(funcNode?.visibility).toBe('public');
|
|
});
|
|
|
|
it('should extract struct declarations', () => {
|
|
const code = `
|
|
pub struct User {
|
|
pub id: String,
|
|
pub name: String,
|
|
email: String,
|
|
}
|
|
`;
|
|
const result = extractFromSource('models.rs', code);
|
|
|
|
const structNode = result.nodes.find((n) => n.kind === 'struct');
|
|
expect(structNode).toBeDefined();
|
|
expect(structNode?.name).toBe('User');
|
|
});
|
|
|
|
it('should extract trait declarations', () => {
|
|
const code = `
|
|
pub trait Repository {
|
|
fn find(&self, id: &str) -> Option<Entity>;
|
|
fn save(&mut self, entity: Entity) -> Result<(), Error>;
|
|
}
|
|
`;
|
|
const result = extractFromSource('traits.rs', code);
|
|
|
|
const traitNode = result.nodes.find((n) => n.kind === 'trait');
|
|
expect(traitNode).toBeDefined();
|
|
expect(traitNode?.name).toBe('Repository');
|
|
});
|
|
|
|
it('should extract impl Trait for Type as implements edges', () => {
|
|
const code = `
|
|
pub struct MyCache {}
|
|
|
|
pub trait Cache {
|
|
fn get(&self, key: &str) -> Option<String>;
|
|
}
|
|
|
|
impl Cache for MyCache {
|
|
fn get(&self, key: &str) -> Option<String> {
|
|
None
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('cache.rs', code);
|
|
|
|
// Should have an unresolved reference for implements
|
|
const implRef = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'implements' && r.referenceName === 'Cache'
|
|
);
|
|
expect(implRef).toBeDefined();
|
|
|
|
// The struct MyCache should be the source
|
|
const myCacheNode = result.nodes.find((n) => n.name === 'MyCache' && n.kind === 'struct');
|
|
expect(myCacheNode).toBeDefined();
|
|
expect(implRef?.fromNodeId).toBe(myCacheNode?.id);
|
|
});
|
|
|
|
it('should extract trait supertraits as extends references', () => {
|
|
const code = `
|
|
pub trait Display {}
|
|
|
|
pub trait Error: Display {
|
|
fn description(&self) -> &str;
|
|
}
|
|
`;
|
|
const result = extractFromSource('error.rs', code);
|
|
|
|
const extendsRef = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'extends' && r.referenceName === 'Display'
|
|
);
|
|
expect(extendsRef).toBeDefined();
|
|
|
|
const errorTrait = result.nodes.find((n) => n.name === 'Error' && n.kind === 'trait');
|
|
expect(errorTrait).toBeDefined();
|
|
expect(extendsRef?.fromNodeId).toBe(errorTrait?.id);
|
|
});
|
|
|
|
it('should not create implements edges for plain impl blocks', () => {
|
|
const code = `
|
|
pub struct Counter {
|
|
count: u32,
|
|
}
|
|
|
|
impl Counter {
|
|
pub fn new() -> Counter {
|
|
Counter { count: 0 }
|
|
}
|
|
pub fn increment(&mut self) {
|
|
self.count += 1;
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('counter.rs', code);
|
|
|
|
// Should have no implements references (no trait involved)
|
|
const implRefs = result.unresolvedReferences.filter(
|
|
(r) => r.referenceKind === 'implements'
|
|
);
|
|
expect(implRefs).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe('Java Extraction', () => {
|
|
it('should extract class declarations', () => {
|
|
const code = `
|
|
public class UserService {
|
|
private final UserRepository repository;
|
|
|
|
public UserService(UserRepository repository) {
|
|
this.repository = repository;
|
|
}
|
|
|
|
public User getUser(String id) {
|
|
return repository.findById(id);
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('UserService.java', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('UserService');
|
|
expect(classNode?.visibility).toBe('public');
|
|
});
|
|
|
|
it('should extract method declarations', () => {
|
|
const code = `
|
|
public class Calculator {
|
|
public static int add(int a, int b) {
|
|
return a + b;
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('Calculator.java', code);
|
|
|
|
const methodNode = result.nodes.find((n) => n.kind === 'method' && n.name === 'add');
|
|
expect(methodNode).toBeDefined();
|
|
expect(methodNode?.isStatic).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('C# Extraction', () => {
|
|
it('should extract class declarations', () => {
|
|
const code = `
|
|
public class OrderService
|
|
{
|
|
private readonly IOrderRepository _repository;
|
|
|
|
public OrderService(IOrderRepository repository)
|
|
{
|
|
_repository = repository;
|
|
}
|
|
|
|
public async Task<Order> GetOrderAsync(string id)
|
|
{
|
|
return await _repository.FindByIdAsync(id);
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('OrderService.cs', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('OrderService');
|
|
expect(classNode?.visibility).toBe('public');
|
|
});
|
|
});
|
|
|
|
describe('PHP Extraction', () => {
|
|
it('should extract class declarations', () => {
|
|
const code = `<?php
|
|
|
|
class UserController
|
|
{
|
|
private UserService $userService;
|
|
|
|
public function __construct(UserService $userService)
|
|
{
|
|
$this->userService = $userService;
|
|
}
|
|
|
|
public function show(string $id): User
|
|
{
|
|
return $this->userService->find($id);
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('UserController.php', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('UserController');
|
|
});
|
|
|
|
it('should extract class inheritance (extends) and interface implementation', () => {
|
|
const code = `<?php
|
|
|
|
class ChildController extends BaseController implements Serializable, JsonSerializable
|
|
{
|
|
public function serialize(): string
|
|
{
|
|
return json_encode($this);
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('ChildController.php', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('ChildController');
|
|
|
|
const extendsRef = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'extends'
|
|
);
|
|
expect(extendsRef).toBeDefined();
|
|
expect(extendsRef?.referenceName).toBe('BaseController');
|
|
|
|
const implementsRefs = result.unresolvedReferences.filter(
|
|
(r) => r.referenceKind === 'implements'
|
|
);
|
|
expect(implementsRefs.length).toBe(2);
|
|
expect(implementsRefs.map((r) => r.referenceName)).toContain('Serializable');
|
|
expect(implementsRefs.map((r) => r.referenceName)).toContain('JsonSerializable');
|
|
});
|
|
});
|
|
|
|
describe('Swift Extraction', () => {
|
|
it('should extract class declarations', () => {
|
|
const code = `
|
|
public class NetworkManager {
|
|
private let session: URLSession
|
|
|
|
public init(session: URLSession = .shared) {
|
|
self.session = session
|
|
}
|
|
|
|
public func fetchData(from url: URL) async throws -> Data {
|
|
let (data, _) = try await session.data(from: url)
|
|
return data
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('NetworkManager.swift', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('NetworkManager');
|
|
});
|
|
|
|
it('should extract function declarations', () => {
|
|
const code = `
|
|
func calculateSum(_ numbers: [Int]) -> Int {
|
|
return numbers.reduce(0, +)
|
|
}
|
|
|
|
public func formatCurrency(amount: Double) -> String {
|
|
return String(format: "$%.2f", amount)
|
|
}
|
|
`;
|
|
const result = extractFromSource('utils.swift', code);
|
|
|
|
const functions = result.nodes.filter((n) => n.kind === 'function');
|
|
expect(functions.length).toBeGreaterThanOrEqual(1);
|
|
});
|
|
|
|
it('should extract struct declarations', () => {
|
|
const code = `
|
|
public struct User {
|
|
let id: UUID
|
|
var name: String
|
|
var email: String
|
|
|
|
func displayName() -> String {
|
|
return name
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('User.swift', code);
|
|
|
|
const structNode = result.nodes.find((n) => n.kind === 'struct');
|
|
expect(structNode).toBeDefined();
|
|
expect(structNode?.name).toBe('User');
|
|
});
|
|
|
|
it('should extract protocol declarations', () => {
|
|
const code = `
|
|
public protocol Repository {
|
|
associatedtype Entity
|
|
|
|
func find(id: String) async throws -> Entity?
|
|
func save(_ entity: Entity) async throws
|
|
}
|
|
`;
|
|
const result = extractFromSource('Repository.swift', code);
|
|
|
|
const protocolNode = result.nodes.find((n) => n.kind === 'interface');
|
|
expect(protocolNode).toBeDefined();
|
|
expect(protocolNode?.name).toBe('Repository');
|
|
});
|
|
|
|
it('should extract class inheritance and protocol conformance', () => {
|
|
const code = `
|
|
class DataRequest: Request {
|
|
func validate() {}
|
|
}
|
|
|
|
class UploadRequest: DataRequest, Sendable {
|
|
func upload() {}
|
|
}
|
|
|
|
enum AFError: Error {
|
|
case invalidURL
|
|
}
|
|
|
|
struct HTTPMethod: RawRepresentable {
|
|
let rawValue: String
|
|
}
|
|
|
|
protocol UploadConvertible: URLRequestConvertible {
|
|
func asURLRequest() throws -> URLRequest
|
|
}
|
|
`;
|
|
const result = extractFromSource('Inheritance.swift', code);
|
|
|
|
const extendsRefs = result.unresolvedReferences.filter(
|
|
(r) => r.referenceKind === 'extends'
|
|
);
|
|
|
|
// DataRequest extends Request
|
|
expect(extendsRefs.find((r) => r.referenceName === 'Request')).toBeDefined();
|
|
// UploadRequest extends DataRequest and Sendable
|
|
expect(extendsRefs.find((r) => r.referenceName === 'DataRequest')).toBeDefined();
|
|
expect(extendsRefs.find((r) => r.referenceName === 'Sendable')).toBeDefined();
|
|
// AFError extends Error
|
|
expect(extendsRefs.find((r) => r.referenceName === 'Error')).toBeDefined();
|
|
// HTTPMethod extends RawRepresentable
|
|
expect(extendsRefs.find((r) => r.referenceName === 'RawRepresentable')).toBeDefined();
|
|
// UploadConvertible extends URLRequestConvertible
|
|
expect(extendsRefs.find((r) => r.referenceName === 'URLRequestConvertible')).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('Kotlin Extraction', () => {
|
|
it('should extract class declarations', () => {
|
|
const code = `
|
|
class UserRepository(private val database: Database) {
|
|
fun findById(id: String): User? {
|
|
return database.query("SELECT * FROM users WHERE id = ?", id)
|
|
}
|
|
|
|
suspend fun save(user: User) {
|
|
database.insert(user)
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('UserRepository.kt', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('UserRepository');
|
|
});
|
|
|
|
it('should extract function declarations', () => {
|
|
const code = `
|
|
fun calculateTotal(items: List<Item>): Double {
|
|
return items.sumOf { it.price }
|
|
}
|
|
|
|
suspend fun fetchUserData(userId: String): User {
|
|
return api.getUser(userId)
|
|
}
|
|
`;
|
|
const result = extractFromSource('utils.kt', code);
|
|
|
|
const functions = result.nodes.filter((n) => n.kind === 'function');
|
|
expect(functions.length).toBeGreaterThanOrEqual(1);
|
|
});
|
|
|
|
it('should detect suspend functions as async', () => {
|
|
const code = `
|
|
suspend fun loadData(): List<String> {
|
|
delay(1000)
|
|
return listOf("a", "b", "c")
|
|
}
|
|
`;
|
|
const result = extractFromSource('loader.kt', code);
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function');
|
|
expect(funcNode).toBeDefined();
|
|
expect(funcNode?.isAsync).toBe(true);
|
|
});
|
|
|
|
it('should extract fun interface declarations', () => {
|
|
const code = `
|
|
fun interface OnObjectRetainedListener {
|
|
fun onObjectRetained()
|
|
}
|
|
`;
|
|
const result = extractFromSource('listener.kt', code);
|
|
|
|
const ifaceNode = result.nodes.find((n) => n.kind === 'interface');
|
|
expect(ifaceNode).toBeDefined();
|
|
expect(ifaceNode?.name).toBe('OnObjectRetainedListener');
|
|
|
|
const methodNode = result.nodes.find((n) => n.kind === 'method');
|
|
expect(methodNode).toBeDefined();
|
|
expect(methodNode?.name).toBe('onObjectRetained');
|
|
expect(methodNode?.qualifiedName).toBe('OnObjectRetainedListener::onObjectRetained');
|
|
});
|
|
|
|
it('should extract complex fun interface with nested classes', () => {
|
|
const code = `
|
|
fun interface EventListener {
|
|
fun onEvent(event: Event)
|
|
|
|
sealed class Event {
|
|
class DumpingHeap : Event()
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('events.kt', code);
|
|
|
|
const ifaceNode = result.nodes.find((n) => n.kind === 'interface');
|
|
expect(ifaceNode).toBeDefined();
|
|
expect(ifaceNode?.name).toBe('EventListener');
|
|
|
|
// Nested sealed class should still be extracted (as sibling due to grammar limitations)
|
|
const eventClass = result.nodes.find((n) => n.kind === 'class' && n.name === 'Event');
|
|
expect(eventClass).toBeDefined();
|
|
|
|
const dumpingHeap = result.nodes.find((n) => n.kind === 'class' && n.name === 'DumpingHeap');
|
|
expect(dumpingHeap).toBeDefined();
|
|
});
|
|
|
|
it('should not affect regular function declarations', () => {
|
|
const code = `
|
|
fun interface MyCallback {
|
|
fun invoke(value: Int)
|
|
}
|
|
|
|
fun regularFunction(): String {
|
|
return "hello"
|
|
}
|
|
`;
|
|
const result = extractFromSource('mixed.kt', code);
|
|
|
|
const ifaceNode = result.nodes.find((n) => n.kind === 'interface');
|
|
expect(ifaceNode).toBeDefined();
|
|
expect(ifaceNode?.name).toBe('MyCallback');
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function');
|
|
expect(funcNode).toBeDefined();
|
|
expect(funcNode?.name).toBe('regularFunction');
|
|
});
|
|
|
|
it('should extract fun interface with annotation on method (Pattern 2b)', () => {
|
|
// When the SAM method has annotations like @Throws, tree-sitter produces a different
|
|
// misparse: function_declaration > ERROR("interface Name {") instead of
|
|
// function_declaration > user_type("interface"). This is the OkHttp Interceptor pattern.
|
|
const code = `
|
|
import java.io.IOException
|
|
|
|
fun interface Interceptor {
|
|
@Throws(IOException::class)
|
|
fun intercept(chain: Chain): Response
|
|
}
|
|
`;
|
|
const result = extractFromSource('interceptor.kt', code);
|
|
|
|
const ifaceNode = result.nodes.find((n) => n.kind === 'interface');
|
|
expect(ifaceNode).toBeDefined();
|
|
expect(ifaceNode?.name).toBe('Interceptor');
|
|
});
|
|
|
|
it('should extract methods from interface with nested fun interface', () => {
|
|
// When an interface contains a nested `fun interface`, tree-sitter misparsed
|
|
// the parent body as ERROR. Methods inside should still be extracted.
|
|
const code = `
|
|
interface WebSocket {
|
|
fun request(): Request
|
|
fun send(text: String): Boolean
|
|
fun cancel()
|
|
fun interface Factory {
|
|
fun newWebSocket(request: Request): WebSocket
|
|
}
|
|
}
|
|
`;
|
|
const result = extractFromSource('websocket.kt', code);
|
|
|
|
const wsIface = result.nodes.find((n) => n.kind === 'interface' && n.name === 'WebSocket');
|
|
expect(wsIface).toBeDefined();
|
|
|
|
const methods = result.nodes.filter((n) => n.kind === 'method' && n.qualifiedName?.startsWith('WebSocket::'));
|
|
const methodNames = methods.map((m) => m.name);
|
|
expect(methodNames).toContain('request');
|
|
expect(methodNames).toContain('send');
|
|
expect(methodNames).toContain('cancel');
|
|
});
|
|
});
|
|
|
|
describe('Dart Extraction', () => {
|
|
it('should extract class declarations', () => {
|
|
const code = `
|
|
class UserService {
|
|
final Database _db;
|
|
|
|
Future<User> findById(String id) async {
|
|
return await _db.query(id);
|
|
}
|
|
|
|
void _privateMethod() {}
|
|
}
|
|
`;
|
|
const result = extractFromSource('service.dart', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('UserService');
|
|
expect(classNode?.visibility).toBe('public');
|
|
|
|
const methodNodes = result.nodes.filter((n) => n.kind === 'method');
|
|
expect(methodNodes.length).toBeGreaterThanOrEqual(2);
|
|
|
|
const findById = methodNodes.find((m) => m.name === 'findById');
|
|
expect(findById).toBeDefined();
|
|
expect(findById?.isAsync).toBe(true);
|
|
|
|
const privateMethod = methodNodes.find((m) => m.name === '_privateMethod');
|
|
expect(privateMethod).toBeDefined();
|
|
expect(privateMethod?.visibility).toBe('private');
|
|
|
|
// Dart models a method body as a SIBLING of the signature, so the method
|
|
// node must be extended to span its body (not just the signature line) —
|
|
// required for body-level analysis (callees, the callback synthesizer).
|
|
expect(findById!.endLine).toBeGreaterThan(findById!.startLine);
|
|
});
|
|
|
|
it('should extract top-level function declarations', () => {
|
|
const code = `
|
|
void topLevelFunction(String name) {
|
|
print(name);
|
|
}
|
|
`;
|
|
const result = extractFromSource('utils.dart', code);
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function');
|
|
expect(funcNode).toBeDefined();
|
|
expect(funcNode?.name).toBe('topLevelFunction');
|
|
expect(funcNode?.language).toBe('dart');
|
|
});
|
|
|
|
it('should extract enum declarations', () => {
|
|
const code = `
|
|
enum Status { active, inactive, pending }
|
|
`;
|
|
const result = extractFromSource('models.dart', code);
|
|
|
|
const enumNode = result.nodes.find((n) => n.kind === 'enum');
|
|
expect(enumNode).toBeDefined();
|
|
expect(enumNode?.name).toBe('Status');
|
|
});
|
|
|
|
it('should extract mixin declarations', () => {
|
|
const code = `
|
|
mixin LoggerMixin {
|
|
void log(String message) {}
|
|
}
|
|
`;
|
|
const result = extractFromSource('mixins.dart', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('LoggerMixin');
|
|
|
|
const methodNode = result.nodes.find((n) => n.kind === 'method');
|
|
expect(methodNode).toBeDefined();
|
|
expect(methodNode?.name).toBe('log');
|
|
});
|
|
|
|
it('should extract extension declarations', () => {
|
|
const code = `
|
|
extension StringExt on String {
|
|
bool get isBlank => trim().isEmpty;
|
|
}
|
|
`;
|
|
const result = extractFromSource('extensions.dart', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('StringExt');
|
|
});
|
|
|
|
it('should detect static methods', () => {
|
|
const code = `
|
|
class Utils {
|
|
static void doWork() {}
|
|
}
|
|
`;
|
|
const result = extractFromSource('utils.dart', code);
|
|
|
|
const methodNode = result.nodes.find((n) => n.kind === 'method');
|
|
expect(methodNode).toBeDefined();
|
|
expect(methodNode?.name).toBe('doWork');
|
|
expect(methodNode?.isStatic).toBe(true);
|
|
});
|
|
|
|
it('should detect async functions', () => {
|
|
const code = `
|
|
Future<String> fetchData() async {
|
|
return await http.get('/data');
|
|
}
|
|
`;
|
|
const result = extractFromSource('api.dart', code);
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function');
|
|
expect(funcNode).toBeDefined();
|
|
expect(funcNode?.name).toBe('fetchData');
|
|
expect(funcNode?.isAsync).toBe(true);
|
|
});
|
|
|
|
it('should detect private visibility via underscore convention', () => {
|
|
const code = `
|
|
void _privateHelper() {}
|
|
|
|
void publicFunction() {}
|
|
`;
|
|
const result = extractFromSource('helpers.dart', code);
|
|
|
|
const functions = result.nodes.filter((n) => n.kind === 'function');
|
|
const privateFunc = functions.find((f) => f.name === '_privateHelper');
|
|
const publicFunc = functions.find((f) => f.name === 'publicFunction');
|
|
|
|
expect(privateFunc?.visibility).toBe('private');
|
|
expect(publicFunc?.visibility).toBe('public');
|
|
});
|
|
});
|
|
|
|
describe('Import Extraction', () => {
|
|
describe('TypeScript/JavaScript imports', () => {
|
|
it('should extract default imports', () => {
|
|
const code = `import React from 'react';`;
|
|
const result = extractFromSource('app.tsx', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('react');
|
|
expect(importNode?.signature).toBe("import React from 'react';");
|
|
});
|
|
|
|
it('should extract named imports', () => {
|
|
const code = `import { Bug, Database } from '@phosphor-icons/react';`;
|
|
const result = extractFromSource('icons.tsx', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('@phosphor-icons/react');
|
|
expect(importNode?.signature).toContain('Bug');
|
|
expect(importNode?.signature).toContain('Database');
|
|
});
|
|
|
|
it('should extract namespace imports', () => {
|
|
const code = `import * as Icons from '@phosphor-icons/react';`;
|
|
const result = extractFromSource('icons.tsx', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('@phosphor-icons/react');
|
|
expect(importNode?.signature).toContain('* as Icons');
|
|
});
|
|
|
|
it('should extract side-effect imports', () => {
|
|
const code = `import './styles.css';`;
|
|
const result = extractFromSource('app.tsx', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('./styles.css');
|
|
});
|
|
|
|
it('should extract mixed imports (default + named)', () => {
|
|
const code = `import React, { useState, useEffect } from 'react';`;
|
|
const result = extractFromSource('app.tsx', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('react');
|
|
expect(importNode?.signature).toContain('React');
|
|
expect(importNode?.signature).toContain('useState');
|
|
expect(importNode?.signature).toContain('useEffect');
|
|
});
|
|
|
|
it('should extract multiple import statements', () => {
|
|
const code = `
|
|
import React from 'react';
|
|
import { Button } from './components';
|
|
import './styles.css';
|
|
`;
|
|
const result = extractFromSource('app.tsx', code);
|
|
|
|
const importNodes = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(importNodes.length).toBe(3);
|
|
|
|
const names = importNodes.map((n) => n.name);
|
|
expect(names).toContain('react');
|
|
expect(names).toContain('./components');
|
|
expect(names).toContain('./styles.css');
|
|
});
|
|
|
|
it('should extract type imports', () => {
|
|
const code = `import type { FC, ReactNode } from 'react';`;
|
|
const result = extractFromSource('types.ts', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('react');
|
|
expect(importNode?.signature).toContain('type');
|
|
expect(importNode?.signature).toContain('FC');
|
|
});
|
|
|
|
it('should extract aliased named imports', () => {
|
|
const code = `import { useState as useStateAlias } from 'react';`;
|
|
const result = extractFromSource('hooks.ts', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('react');
|
|
expect(importNode?.signature).toContain('useState');
|
|
expect(importNode?.signature).toContain('useStateAlias');
|
|
});
|
|
|
|
it('should extract relative path imports', () => {
|
|
const code = `import { helper } from '../utils/helper';`;
|
|
const result = extractFromSource('components/Button.tsx', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('../utils/helper');
|
|
expect(importNode?.signature).toContain('helper');
|
|
});
|
|
});
|
|
|
|
describe('Python imports', () => {
|
|
it('should extract simple import statement', () => {
|
|
const code = `import json`;
|
|
const result = extractFromSource('utils.py', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('json');
|
|
});
|
|
|
|
it('should extract from import statement', () => {
|
|
const code = `from os import path`;
|
|
const result = extractFromSource('utils.py', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('os');
|
|
expect(importNode?.signature).toContain('path');
|
|
});
|
|
|
|
it('should extract multiple imports from same module', () => {
|
|
const code = `from typing import List, Dict, Optional`;
|
|
const result = extractFromSource('types.py', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('typing');
|
|
expect(importNode?.signature).toContain('List');
|
|
expect(importNode?.signature).toContain('Dict');
|
|
});
|
|
|
|
it('should extract multiple import statements', () => {
|
|
const code = `
|
|
import os
|
|
import sys
|
|
`;
|
|
const result = extractFromSource('main.py', code);
|
|
|
|
const importNodes = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(importNodes.length).toBe(2);
|
|
|
|
const names = importNodes.map((n) => n.name);
|
|
expect(names).toContain('os');
|
|
expect(names).toContain('sys');
|
|
});
|
|
|
|
it('should extract aliased import', () => {
|
|
const code = `import numpy as np`;
|
|
const result = extractFromSource('data.py', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('numpy');
|
|
expect(importNode?.signature).toContain('as np');
|
|
});
|
|
|
|
it('should extract relative import', () => {
|
|
const code = `from .utils import helper`;
|
|
const result = extractFromSource('module.py', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('.utils');
|
|
expect(importNode?.signature).toContain('helper');
|
|
});
|
|
|
|
it('should extract wildcard import', () => {
|
|
const code = `from typing import *`;
|
|
const result = extractFromSource('types.py', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('typing');
|
|
expect(importNode?.signature).toContain('*');
|
|
});
|
|
});
|
|
|
|
describe('Rust imports', () => {
|
|
it('should extract simple use declaration', () => {
|
|
const code = `use std::io;`;
|
|
const result = extractFromSource('main.rs', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('std');
|
|
expect(importNode?.signature).toBe('use std::io;');
|
|
});
|
|
|
|
it('should extract scoped use list', () => {
|
|
const code = `use std::{ffi::OsStr, io, path::Path};`;
|
|
const result = extractFromSource('main.rs', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('std');
|
|
expect(importNode?.signature).toContain('ffi::OsStr');
|
|
expect(importNode?.signature).toContain('path::Path');
|
|
});
|
|
|
|
it('should extract crate imports', () => {
|
|
const code = `use crate::error::Error;`;
|
|
const result = extractFromSource('lib.rs', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('crate');
|
|
});
|
|
|
|
it('should extract super imports', () => {
|
|
const code = `use super::utils;`;
|
|
const result = extractFromSource('submod.rs', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('super');
|
|
});
|
|
|
|
it('should extract external crate imports', () => {
|
|
const code = `use serde::{Serialize, Deserialize};`;
|
|
const result = extractFromSource('types.rs', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('serde');
|
|
expect(importNode?.signature).toContain('Serialize');
|
|
expect(importNode?.signature).toContain('Deserialize');
|
|
});
|
|
});
|
|
|
|
describe('Go imports', () => {
|
|
it('should extract single import', () => {
|
|
const code = `
|
|
package main
|
|
|
|
import "fmt"
|
|
`;
|
|
const result = extractFromSource('main.go', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('fmt');
|
|
});
|
|
|
|
it('should extract grouped imports', () => {
|
|
const code = `
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"encoding/json"
|
|
)
|
|
`;
|
|
const result = extractFromSource('main.go', code);
|
|
|
|
const importNodes = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(importNodes.length).toBe(3);
|
|
|
|
const names = importNodes.map((n) => n.name);
|
|
expect(names).toContain('fmt');
|
|
expect(names).toContain('os');
|
|
expect(names).toContain('encoding/json');
|
|
});
|
|
|
|
it('should extract aliased import', () => {
|
|
const code = `
|
|
package main
|
|
|
|
import f "fmt"
|
|
`;
|
|
const result = extractFromSource('main.go', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('fmt');
|
|
expect(importNode?.signature).toContain('f');
|
|
});
|
|
|
|
it('should extract dot import', () => {
|
|
const code = `
|
|
package main
|
|
|
|
import . "math"
|
|
`;
|
|
const result = extractFromSource('main.go', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('math');
|
|
expect(importNode?.signature).toContain('.');
|
|
});
|
|
|
|
it('should extract blank import', () => {
|
|
const code = `
|
|
package main
|
|
|
|
import _ "github.com/go-sql-driver/mysql"
|
|
`;
|
|
const result = extractFromSource('main.go', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('github.com/go-sql-driver/mysql');
|
|
expect(importNode?.signature).toContain('_');
|
|
});
|
|
});
|
|
|
|
describe('Swift imports', () => {
|
|
it('should extract simple import', () => {
|
|
const code = `import Foundation`;
|
|
const result = extractFromSource('main.swift', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('Foundation');
|
|
expect(importNode?.signature).toBe('import Foundation');
|
|
});
|
|
|
|
it('should extract @testable import', () => {
|
|
const code = `@testable import Alamofire`;
|
|
const result = extractFromSource('Tests.swift', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('Alamofire');
|
|
expect(importNode?.signature).toContain('@testable');
|
|
});
|
|
|
|
it('should extract @preconcurrency import', () => {
|
|
const code = `@preconcurrency import Security`;
|
|
const result = extractFromSource('Auth.swift', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('Security');
|
|
});
|
|
|
|
it('should extract multiple imports', () => {
|
|
const code = `
|
|
import Foundation
|
|
import UIKit
|
|
import Alamofire
|
|
`;
|
|
const result = extractFromSource('App.swift', code);
|
|
|
|
const importNodes = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(importNodes.length).toBe(3);
|
|
|
|
const names = importNodes.map((n) => n.name);
|
|
expect(names).toContain('Foundation');
|
|
expect(names).toContain('UIKit');
|
|
expect(names).toContain('Alamofire');
|
|
});
|
|
});
|
|
|
|
describe('Kotlin imports', () => {
|
|
it('should extract simple import', () => {
|
|
const code = `import java.io.IOException`;
|
|
const result = extractFromSource('Main.kt', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('java.io.IOException');
|
|
expect(importNode?.signature).toBe('import java.io.IOException');
|
|
});
|
|
|
|
it('should extract aliased import', () => {
|
|
const code = `import okhttp3.Request.Builder as RequestBuilder`;
|
|
const result = extractFromSource('Utils.kt', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('okhttp3.Request.Builder');
|
|
expect(importNode?.signature).toContain('as RequestBuilder');
|
|
});
|
|
|
|
it('should extract wildcard import', () => {
|
|
const code = `import java.util.concurrent.TimeUnit.*`;
|
|
const result = extractFromSource('Time.kt', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('java.util.concurrent.TimeUnit');
|
|
expect(importNode?.signature).toContain('.*');
|
|
});
|
|
|
|
it('should extract multiple imports', () => {
|
|
const code = `
|
|
import java.io.IOException
|
|
import kotlin.test.assertFailsWith
|
|
import okhttp3.OkHttpClient
|
|
`;
|
|
const result = extractFromSource('Test.kt', code);
|
|
|
|
const importNodes = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(importNodes.length).toBe(3);
|
|
|
|
const names = importNodes.map((n) => n.name);
|
|
expect(names).toContain('java.io.IOException');
|
|
expect(names).toContain('kotlin.test.assertFailsWith');
|
|
expect(names).toContain('okhttp3.OkHttpClient');
|
|
});
|
|
});
|
|
|
|
describe('Java imports', () => {
|
|
it('should extract simple import', () => {
|
|
const code = `import java.util.List;`;
|
|
const result = extractFromSource('Main.java', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('java.util.List');
|
|
expect(importNode?.signature).toBe('import java.util.List;');
|
|
});
|
|
|
|
it('should extract static import', () => {
|
|
const code = `import static java.util.Collections.emptyList;`;
|
|
const result = extractFromSource('Utils.java', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('java.util.Collections.emptyList');
|
|
expect(importNode?.signature).toContain('static');
|
|
});
|
|
|
|
it('should extract wildcard import', () => {
|
|
const code = `import java.util.*;`;
|
|
const result = extractFromSource('App.java', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('java.util');
|
|
expect(importNode?.signature).toContain('.*');
|
|
});
|
|
|
|
it('should extract nested class import', () => {
|
|
const code = `import java.util.Map.Entry;`;
|
|
const result = extractFromSource('MapUtil.java', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('java.util.Map.Entry');
|
|
});
|
|
|
|
it('should extract multiple imports', () => {
|
|
const code = `
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.io.IOException;
|
|
`;
|
|
const result = extractFromSource('Service.java', code);
|
|
|
|
const importNodes = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(importNodes.length).toBe(3);
|
|
|
|
const names = importNodes.map((n) => n.name);
|
|
expect(names).toContain('java.util.List');
|
|
expect(names).toContain('java.util.Map');
|
|
expect(names).toContain('java.io.IOException');
|
|
});
|
|
});
|
|
|
|
describe('C# imports', () => {
|
|
it('should extract simple using', () => {
|
|
const code = `using System;`;
|
|
const result = extractFromSource('Program.cs', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('System');
|
|
expect(importNode?.signature).toBe('using System;');
|
|
});
|
|
|
|
it('should extract qualified using', () => {
|
|
const code = `using System.Collections.Generic;`;
|
|
const result = extractFromSource('Utils.cs', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('System.Collections.Generic');
|
|
});
|
|
|
|
it('should extract static using', () => {
|
|
const code = `using static System.Console;`;
|
|
const result = extractFromSource('App.cs', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('System.Console');
|
|
expect(importNode?.signature).toContain('static');
|
|
});
|
|
|
|
it('should extract alias using', () => {
|
|
const code = `using MyList = System.Collections.Generic.List<int>;`;
|
|
const result = extractFromSource('Types.cs', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('System.Collections.Generic.List<int>');
|
|
expect(importNode?.signature).toContain('MyList =');
|
|
});
|
|
|
|
it('should extract multiple usings', () => {
|
|
const code = `
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
`;
|
|
const result = extractFromSource('Service.cs', code);
|
|
|
|
const importNodes = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(importNodes.length).toBe(3);
|
|
|
|
const names = importNodes.map((n) => n.name);
|
|
expect(names).toContain('System');
|
|
expect(names).toContain('System.Threading.Tasks');
|
|
expect(names).toContain('Microsoft.Extensions.DependencyInjection');
|
|
});
|
|
});
|
|
|
|
describe('PHP imports', () => {
|
|
it('should extract simple use', () => {
|
|
const code = `<?php use PHPUnit\\Framework\\TestCase;`;
|
|
const result = extractFromSource('Test.php', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('PHPUnit\\Framework\\TestCase');
|
|
});
|
|
|
|
it('should extract aliased use', () => {
|
|
const code = `<?php use Mockery as m;`;
|
|
const result = extractFromSource('Test.php', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('Mockery');
|
|
expect(importNode?.signature).toContain('as m');
|
|
});
|
|
|
|
it('should extract function use', () => {
|
|
const code = `<?php use function Illuminate\\Support\\env;`;
|
|
const result = extractFromSource('helpers.php', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('Illuminate\\Support\\env');
|
|
expect(importNode?.signature).toContain('function');
|
|
});
|
|
|
|
it('should extract grouped use', () => {
|
|
const code = `<?php use Illuminate\\Database\\{Model, Builder};`;
|
|
const result = extractFromSource('Models.php', code);
|
|
|
|
const importNodes = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(importNodes.length).toBe(2);
|
|
|
|
const names = importNodes.map((n) => n.name);
|
|
expect(names).toContain('Illuminate\\Database\\Model');
|
|
expect(names).toContain('Illuminate\\Database\\Builder');
|
|
});
|
|
|
|
it('should extract multiple uses', () => {
|
|
const code = `<?php
|
|
use Illuminate\\Support\\Collection;
|
|
use Illuminate\\Support\\Str;
|
|
use Closure;
|
|
`;
|
|
const result = extractFromSource('Service.php', code);
|
|
|
|
const importNodes = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(importNodes.length).toBe(3);
|
|
|
|
const names = importNodes.map((n) => n.name);
|
|
expect(names).toContain('Illuminate\\Support\\Collection');
|
|
expect(names).toContain('Illuminate\\Support\\Str');
|
|
expect(names).toContain('Closure');
|
|
});
|
|
});
|
|
|
|
describe('Ruby imports', () => {
|
|
it('should extract require', () => {
|
|
const code = `require 'json'`;
|
|
const result = extractFromSource('app.rb', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('json');
|
|
expect(importNode?.signature).toBe("require 'json'");
|
|
});
|
|
|
|
it('should extract require with path', () => {
|
|
const code = `require 'active_support/core_ext/string'`;
|
|
const result = extractFromSource('config.rb', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('active_support/core_ext/string');
|
|
});
|
|
|
|
it('should extract require_relative', () => {
|
|
const code = `require_relative '../test_helper'`;
|
|
const result = extractFromSource('test/my_test.rb', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('../test_helper');
|
|
expect(importNode?.signature).toContain('require_relative');
|
|
});
|
|
|
|
it('should not extract non-require calls', () => {
|
|
const code = `puts 'hello'`;
|
|
const result = extractFromSource('app.rb', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeUndefined();
|
|
});
|
|
|
|
it('should extract multiple requires', () => {
|
|
const code = `
|
|
require 'json'
|
|
require 'yaml'
|
|
require_relative 'helper'
|
|
`;
|
|
const result = extractFromSource('lib.rb', code);
|
|
|
|
const importNodes = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(importNodes.length).toBe(3);
|
|
|
|
const names = importNodes.map((n) => n.name);
|
|
expect(names).toContain('json');
|
|
expect(names).toContain('yaml');
|
|
expect(names).toContain('helper');
|
|
});
|
|
});
|
|
|
|
describe('Ruby modules', () => {
|
|
it('should extract module as module node with containment', () => {
|
|
const code = `
|
|
module CachedCounting
|
|
def self.disable
|
|
@enabled = false
|
|
end
|
|
|
|
def perform_increment!(key, count)
|
|
write_cache!(key, count)
|
|
end
|
|
end
|
|
`;
|
|
const result = extractFromSource('concerns/cached_counting.rb', code);
|
|
|
|
const moduleNode = result.nodes.find((n) => n.kind === 'module' && n.name === 'CachedCounting');
|
|
expect(moduleNode).toBeDefined();
|
|
expect(moduleNode?.qualifiedName).toBe('CachedCounting');
|
|
|
|
// Methods inside module should have module-qualified names
|
|
const disableMethod = result.nodes.find((n) => n.name === 'disable' && n.kind === 'method');
|
|
expect(disableMethod).toBeDefined();
|
|
expect(disableMethod?.qualifiedName).toBe('CachedCounting::disable');
|
|
|
|
const incrementMethod = result.nodes.find((n) => n.name === 'perform_increment!' && n.kind === 'method');
|
|
expect(incrementMethod).toBeDefined();
|
|
expect(incrementMethod?.qualifiedName).toBe('CachedCounting::perform_increment!');
|
|
|
|
// Containment edge from module to methods
|
|
const containsEdges = result.edges.filter((e) => e.source === moduleNode?.id && e.kind === 'contains');
|
|
expect(containsEdges.length).toBeGreaterThanOrEqual(2);
|
|
});
|
|
|
|
it('should handle nested modules with classes', () => {
|
|
const code = `
|
|
module Discourse
|
|
module Auth
|
|
class AuthProvider
|
|
def authenticate(params)
|
|
validate(params)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
`;
|
|
const result = extractFromSource('lib/auth.rb', code);
|
|
|
|
const discourseModule = result.nodes.find((n) => n.kind === 'module' && n.name === 'Discourse');
|
|
expect(discourseModule).toBeDefined();
|
|
|
|
const authModule = result.nodes.find((n) => n.kind === 'module' && n.name === 'Auth');
|
|
expect(authModule).toBeDefined();
|
|
expect(authModule?.qualifiedName).toBe('Discourse::Auth');
|
|
|
|
const authProvider = result.nodes.find((n) => n.kind === 'class' && n.name === 'AuthProvider');
|
|
expect(authProvider).toBeDefined();
|
|
expect(authProvider?.qualifiedName).toBe('Discourse::Auth::AuthProvider');
|
|
|
|
const authMethod = result.nodes.find((n) => n.name === 'authenticate');
|
|
expect(authMethod).toBeDefined();
|
|
expect(authMethod?.qualifiedName).toBe('Discourse::Auth::AuthProvider::authenticate');
|
|
});
|
|
});
|
|
|
|
describe('C/C++ imports', () => {
|
|
it('should extract system include', () => {
|
|
const code = `#include <iostream>`;
|
|
const result = extractFromSource('main.cpp', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('iostream');
|
|
expect(importNode?.signature).toBe('#include <iostream>');
|
|
});
|
|
|
|
it('should extract system include with path', () => {
|
|
const code = `#include <nlohmann/json.hpp>`;
|
|
const result = extractFromSource('app.cpp', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('nlohmann/json.hpp');
|
|
});
|
|
|
|
it('should extract local include', () => {
|
|
const code = `#include "myheader.h"`;
|
|
const result = extractFromSource('main.cpp', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('myheader.h');
|
|
});
|
|
|
|
it('should extract C header', () => {
|
|
const code = `#include <stdio.h>`;
|
|
const result = extractFromSource('main.c', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('stdio.h');
|
|
});
|
|
|
|
it('should extract multiple includes', () => {
|
|
const code = `
|
|
#include <iostream>
|
|
#include <vector>
|
|
#include "config.h"
|
|
`;
|
|
const result = extractFromSource('app.cpp', code);
|
|
|
|
const importNodes = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(importNodes.length).toBe(3);
|
|
|
|
const names = importNodes.map((n) => n.name);
|
|
expect(names).toContain('iostream');
|
|
expect(names).toContain('vector');
|
|
expect(names).toContain('config.h');
|
|
});
|
|
});
|
|
|
|
describe('Dart imports', () => {
|
|
it('should extract dart: import', () => {
|
|
const code = `import 'dart:async';`;
|
|
const result = extractFromSource('main.dart', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('dart:async');
|
|
expect(importNode?.signature).toBe("import 'dart:async';");
|
|
});
|
|
|
|
it('should extract package import', () => {
|
|
const code = `import 'package:flutter/material.dart';`;
|
|
const result = extractFromSource('app.dart', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('package:flutter/material.dart');
|
|
});
|
|
|
|
it('should extract aliased import', () => {
|
|
const code = `import 'package:http/http.dart' as http;`;
|
|
const result = extractFromSource('api.dart', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('package:http/http.dart');
|
|
expect(importNode?.signature).toContain('as http');
|
|
});
|
|
|
|
it('should extract multiple imports', () => {
|
|
const code = `
|
|
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
`;
|
|
const result = extractFromSource('main.dart', code);
|
|
|
|
const importNodes = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(importNodes.length).toBe(3);
|
|
|
|
const names = importNodes.map((n) => n.name);
|
|
expect(names).toContain('dart:async');
|
|
expect(names).toContain('dart:convert');
|
|
expect(names).toContain('package:flutter/material.dart');
|
|
});
|
|
|
|
it('should extract relative import', () => {
|
|
const code = `import '../utils/helpers.dart';`;
|
|
const result = extractFromSource('lib/main.dart', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('../utils/helpers.dart');
|
|
});
|
|
});
|
|
|
|
describe('Liquid imports', () => {
|
|
it('should extract render tag', () => {
|
|
const code = `{% render 'loading-spinner' %}`;
|
|
const result = extractFromSource('template.liquid', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('loading-spinner');
|
|
expect(importNode?.signature).toContain('render');
|
|
});
|
|
|
|
it('should extract section tag', () => {
|
|
const code = `{% section 'header' %}`;
|
|
const result = extractFromSource('layout/theme.liquid', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('header');
|
|
expect(importNode?.signature).toContain('section');
|
|
});
|
|
|
|
it('should extract include tag', () => {
|
|
const code = `{% include 'icon-cart' %}`;
|
|
const result = extractFromSource('snippets/header.liquid', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('icon-cart');
|
|
expect(importNode?.signature).toContain('include');
|
|
});
|
|
|
|
it('should extract render with whitespace control', () => {
|
|
const code = `{%- render 'price' -%}`;
|
|
const result = extractFromSource('snippets/product.liquid', code);
|
|
|
|
const importNode = result.nodes.find((n) => n.kind === 'import');
|
|
expect(importNode).toBeDefined();
|
|
expect(importNode?.name).toBe('price');
|
|
});
|
|
|
|
it('should extract multiple imports', () => {
|
|
const code = `
|
|
{% section 'header' %}
|
|
{% render 'loading-spinner' %}
|
|
{% render 'cart-drawer' %}
|
|
`;
|
|
const result = extractFromSource('layout/theme.liquid', code);
|
|
|
|
const importNodes = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(importNodes.length).toBe(3);
|
|
|
|
const names = importNodes.map((n) => n.name);
|
|
expect(names).toContain('header');
|
|
expect(names).toContain('loading-spinner');
|
|
expect(names).toContain('cart-drawer');
|
|
});
|
|
});
|
|
});
|
|
|
|
// =============================================================================
|
|
// Pascal / Delphi Extraction
|
|
// =============================================================================
|
|
|
|
describe('Pascal / Delphi Extraction', () => {
|
|
describe('Language detection', () => {
|
|
it('should detect Pascal files', () => {
|
|
expect(detectLanguage('UAuth.pas')).toBe('pascal');
|
|
expect(detectLanguage('App.dpr')).toBe('pascal');
|
|
expect(detectLanguage('Package.dpk')).toBe('pascal');
|
|
expect(detectLanguage('App.lpr')).toBe('pascal');
|
|
expect(detectLanguage('MainForm.dfm')).toBe('pascal');
|
|
expect(detectLanguage('MainForm.fmx')).toBe('pascal');
|
|
});
|
|
|
|
it('should report Pascal as supported', () => {
|
|
expect(isLanguageSupported('pascal')).toBe(true);
|
|
expect(getSupportedLanguages()).toContain('pascal');
|
|
});
|
|
});
|
|
|
|
describe('Unit extraction', () => {
|
|
it('should extract unit as module', () => {
|
|
const code = `unit MyUnit;\ninterface\nimplementation\nend.`;
|
|
const result = extractFromSource('MyUnit.pas', code);
|
|
|
|
const moduleNode = result.nodes.find((n) => n.kind === 'module');
|
|
expect(moduleNode).toBeDefined();
|
|
expect(moduleNode?.name).toBe('MyUnit');
|
|
expect(moduleNode?.language).toBe('pascal');
|
|
});
|
|
|
|
it('should extract program as module', () => {
|
|
const code = `program MyApp;\nbegin\nend.`;
|
|
const result = extractFromSource('MyApp.dpr', code);
|
|
|
|
const moduleNode = result.nodes.find((n) => n.kind === 'module');
|
|
expect(moduleNode).toBeDefined();
|
|
expect(moduleNode?.name).toBe('MyApp');
|
|
});
|
|
|
|
it('should fallback to filename when module name is empty', () => {
|
|
// Some .dpr templates use "program;" without a name
|
|
const code = `program;\nuses SysUtils;\nbegin\nend.`;
|
|
const result = extractFromSource('Console.dpr', code);
|
|
|
|
const moduleNode = result.nodes.find((n) => n.kind === 'module');
|
|
expect(moduleNode).toBeDefined();
|
|
expect(moduleNode?.name).toBe('Console');
|
|
});
|
|
});
|
|
|
|
describe('Uses clause (imports)', () => {
|
|
it('should extract uses as individual imports', () => {
|
|
const code = `unit Test;\ninterface\nuses\n System.SysUtils,\n System.Classes;\nimplementation\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const imports = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(imports.length).toBe(2);
|
|
expect(imports.map((n) => n.name)).toContain('System.SysUtils');
|
|
expect(imports.map((n) => n.name)).toContain('System.Classes');
|
|
});
|
|
|
|
it('should create unresolved references for imports', () => {
|
|
const code = `unit Test;\ninterface\nuses\n UAuth;\nimplementation\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const importRef = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'imports'
|
|
);
|
|
expect(importRef).toBeDefined();
|
|
expect(importRef?.referenceName).toBe('UAuth');
|
|
});
|
|
});
|
|
|
|
describe('Class extraction', () => {
|
|
it('should extract class declarations', () => {
|
|
const code = `unit Test;\ninterface\ntype\n TMyClass = class\n public\n procedure DoSomething;\n end;\nimplementation\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('TMyClass');
|
|
});
|
|
|
|
it('should extract class with inheritance', () => {
|
|
const code = `unit Test;\ninterface\ntype\n TChild = class(TParent)\n end;\nimplementation\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const extendsRef = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'extends'
|
|
);
|
|
expect(extendsRef).toBeDefined();
|
|
expect(extendsRef?.referenceName).toBe('TParent');
|
|
});
|
|
|
|
it('should extract class with interface implementation', () => {
|
|
const code = `unit Test;\ninterface\ntype\n TService = class(TInterfacedObject, ILogger)\n end;\nimplementation\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const extendsRef = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'extends'
|
|
);
|
|
const implementsRef = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'implements'
|
|
);
|
|
expect(extendsRef?.referenceName).toBe('TInterfacedObject');
|
|
expect(implementsRef?.referenceName).toBe('ILogger');
|
|
});
|
|
});
|
|
|
|
describe('Record extraction', () => {
|
|
it('should extract records as class nodes', () => {
|
|
const code = `unit Test;\ninterface\ntype\n TPoint = record\n X: Double;\n Y: Double;\n end;\nimplementation\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode).toBeDefined();
|
|
expect(classNode?.name).toBe('TPoint');
|
|
|
|
const fields = result.nodes.filter((n) => n.kind === 'field');
|
|
expect(fields.length).toBe(2);
|
|
expect(fields.map((f) => f.name)).toContain('X');
|
|
expect(fields.map((f) => f.name)).toContain('Y');
|
|
});
|
|
});
|
|
|
|
describe('Interface extraction', () => {
|
|
it('should extract interface declarations', () => {
|
|
const code = `unit Test;\ninterface\ntype\n ILogger = interface\n procedure Log(const AMsg: string);\n end;\nimplementation\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const ifaceNode = result.nodes.find((n) => n.kind === 'interface');
|
|
expect(ifaceNode).toBeDefined();
|
|
expect(ifaceNode?.name).toBe('ILogger');
|
|
});
|
|
});
|
|
|
|
describe('Method extraction', () => {
|
|
it('should extract methods with visibility', () => {
|
|
const code = `unit Test;\ninterface\ntype\n TMyClass = class\n private\n FValue: Integer;\n public\n constructor Create;\n function GetValue: Integer;\n end;\nimplementation\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const methods = result.nodes.filter((n) => n.kind === 'method');
|
|
expect(methods.length).toBe(2);
|
|
|
|
const createMethod = methods.find((m) => m.name === 'Create');
|
|
expect(createMethod?.visibility).toBe('public');
|
|
|
|
const getValue = methods.find((m) => m.name === 'GetValue');
|
|
expect(getValue?.visibility).toBe('public');
|
|
|
|
const fields = result.nodes.filter((n) => n.kind === 'field');
|
|
const fValue = fields.find((f) => f.name === 'FValue');
|
|
expect(fValue?.visibility).toBe('private');
|
|
});
|
|
|
|
it('should detect static methods (class methods)', () => {
|
|
const code = `unit Test;\ninterface\ntype\n THelper = class\n public\n class function Create: THelper; static;\n end;\nimplementation\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const methods = result.nodes.filter((n) => n.kind === 'method');
|
|
const staticMethod = methods.find((m) => m.name === 'Create');
|
|
expect(staticMethod?.isStatic).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('Enum extraction', () => {
|
|
it('should extract enums with members', () => {
|
|
const code = `unit Test;\ninterface\ntype\n TColor = (clRed, clGreen, clBlue);\nimplementation\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const enumNode = result.nodes.find((n) => n.kind === 'enum');
|
|
expect(enumNode).toBeDefined();
|
|
expect(enumNode?.name).toBe('TColor');
|
|
|
|
const members = result.nodes.filter((n) => n.kind === 'enum_member');
|
|
expect(members.length).toBe(3);
|
|
expect(members.map((m) => m.name)).toEqual(['clRed', 'clGreen', 'clBlue']);
|
|
});
|
|
});
|
|
|
|
describe('Property extraction', () => {
|
|
it('should extract properties', () => {
|
|
const code = `unit Test;\ninterface\ntype\n TObj = class\n public\n property Name: string read FName write FName;\n end;\nimplementation\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const propNode = result.nodes.find((n) => n.kind === 'property');
|
|
expect(propNode).toBeDefined();
|
|
expect(propNode?.name).toBe('Name');
|
|
expect(propNode?.visibility).toBe('public');
|
|
});
|
|
});
|
|
|
|
describe('Constant extraction', () => {
|
|
it('should extract constants', () => {
|
|
const code = `unit Test;\ninterface\nconst\n MAX_RETRIES = 3;\n APP_NAME = 'MyApp';\nimplementation\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const constants = result.nodes.filter((n) => n.kind === 'constant');
|
|
expect(constants.length).toBe(2);
|
|
expect(constants.map((c) => c.name)).toContain('MAX_RETRIES');
|
|
expect(constants.map((c) => c.name)).toContain('APP_NAME');
|
|
});
|
|
});
|
|
|
|
describe('Type alias extraction', () => {
|
|
it('should extract type aliases', () => {
|
|
const code = `unit Test;\ninterface\ntype\n TUserName = string;\nimplementation\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const aliasNode = result.nodes.find((n) => n.kind === 'type_alias');
|
|
expect(aliasNode).toBeDefined();
|
|
expect(aliasNode?.name).toBe('TUserName');
|
|
});
|
|
});
|
|
|
|
describe('Call extraction', () => {
|
|
it('should extract calls from implementation bodies', () => {
|
|
const code = `unit Test;\ninterface\ntype\n TObj = class\n public\n procedure DoWork;\n end;\nimplementation\nprocedure TObj.DoWork;\nbegin\n WriteLn('hello');\nend;\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const callRef = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'calls'
|
|
);
|
|
expect(callRef).toBeDefined();
|
|
expect(callRef?.referenceName).toBe('WriteLn');
|
|
});
|
|
});
|
|
|
|
describe('Containment edges', () => {
|
|
it('should create contains edges for class members', () => {
|
|
const code = `unit Test;\ninterface\ntype\n TObj = class\n public\n procedure Foo;\n end;\nimplementation\nend.`;
|
|
const result = extractFromSource('Test.pas', code);
|
|
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
const methodNode = result.nodes.find((n) => n.kind === 'method');
|
|
expect(classNode).toBeDefined();
|
|
expect(methodNode).toBeDefined();
|
|
|
|
const containsEdge = result.edges.find(
|
|
(e) => e.source === classNode?.id && e.target === methodNode?.id && e.kind === 'contains'
|
|
);
|
|
expect(containsEdge).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('Full fixture: UAuth.pas', () => {
|
|
const code = `unit UAuth;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.SysUtils,
|
|
System.Classes;
|
|
|
|
type
|
|
ITokenValidator = interface
|
|
['{11111111-1111-1111-1111-111111111111}']
|
|
function Validate(const AToken: string): Boolean;
|
|
end;
|
|
|
|
TAuthService = class(TInterfacedObject, ITokenValidator)
|
|
private
|
|
FToken: string;
|
|
FLoginCount: Integer;
|
|
procedure IncLoginCount;
|
|
protected
|
|
function GetToken: string;
|
|
public
|
|
constructor Create;
|
|
destructor Destroy; override;
|
|
function Validate(const AToken: string): Boolean;
|
|
function Login(const AUser, APass: string): string;
|
|
property Token: string read GetToken;
|
|
property LoginCount: Integer read FLoginCount;
|
|
end;
|
|
|
|
implementation
|
|
|
|
constructor TAuthService.Create;
|
|
begin
|
|
inherited Create;
|
|
FToken := '';
|
|
FLoginCount := 0;
|
|
end;
|
|
|
|
destructor TAuthService.Destroy;
|
|
begin
|
|
FToken := '';
|
|
inherited Destroy;
|
|
end;
|
|
|
|
procedure TAuthService.IncLoginCount;
|
|
begin
|
|
Inc(FLoginCount);
|
|
end;
|
|
|
|
function TAuthService.GetToken: string;
|
|
begin
|
|
Result := FToken;
|
|
end;
|
|
|
|
function TAuthService.Validate(const AToken: string): Boolean;
|
|
begin
|
|
Result := AToken <> '';
|
|
end;
|
|
|
|
function TAuthService.Login(const AUser, APass: string): string;
|
|
begin
|
|
IncLoginCount;
|
|
if Validate(AUser + ':' + APass) then
|
|
begin
|
|
FToken := AUser;
|
|
Result := 'ok';
|
|
end
|
|
else
|
|
Result := '';
|
|
end;
|
|
|
|
end.`;
|
|
|
|
it('should extract all expected nodes', () => {
|
|
const result = extractFromSource('UAuth.pas', code);
|
|
|
|
expect(result.errors).toHaveLength(0);
|
|
|
|
// Module
|
|
const moduleNode = result.nodes.find((n) => n.kind === 'module');
|
|
expect(moduleNode?.name).toBe('UAuth');
|
|
|
|
// Imports
|
|
const imports = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(imports.length).toBe(2);
|
|
|
|
// Interface
|
|
const ifaceNode = result.nodes.find((n) => n.kind === 'interface');
|
|
expect(ifaceNode?.name).toBe('ITokenValidator');
|
|
|
|
// Class
|
|
const classNode = result.nodes.find((n) => n.kind === 'class');
|
|
expect(classNode?.name).toBe('TAuthService');
|
|
|
|
// Methods
|
|
const methods = result.nodes.filter((n) => n.kind === 'method');
|
|
expect(methods.length).toBeGreaterThanOrEqual(6);
|
|
expect(methods.map((m) => m.name)).toContain('Create');
|
|
expect(methods.map((m) => m.name)).toContain('Destroy');
|
|
expect(methods.map((m) => m.name)).toContain('Login');
|
|
|
|
// Fields
|
|
const fields = result.nodes.filter((n) => n.kind === 'field');
|
|
expect(fields.length).toBe(2);
|
|
expect(fields.every((f) => f.visibility === 'private')).toBe(true);
|
|
|
|
// Properties
|
|
const props = result.nodes.filter((n) => n.kind === 'property');
|
|
expect(props.length).toBe(2);
|
|
expect(props.map((p) => p.name)).toContain('Token');
|
|
expect(props.map((p) => p.name)).toContain('LoginCount');
|
|
});
|
|
|
|
it('should extract inheritance and interface implementation', () => {
|
|
const result = extractFromSource('UAuth.pas', code);
|
|
|
|
const extendsRef = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'extends'
|
|
);
|
|
expect(extendsRef?.referenceName).toBe('TInterfacedObject');
|
|
|
|
const implementsRef = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'implements'
|
|
);
|
|
expect(implementsRef?.referenceName).toBe('ITokenValidator');
|
|
});
|
|
|
|
it('should extract calls from implementation', () => {
|
|
const result = extractFromSource('UAuth.pas', code);
|
|
|
|
const callRefs = result.unresolvedReferences.filter(
|
|
(r) => r.referenceKind === 'calls'
|
|
);
|
|
expect(callRefs.map((r) => r.referenceName)).toContain('Inc');
|
|
expect(callRefs.map((r) => r.referenceName)).toContain('Validate');
|
|
});
|
|
});
|
|
|
|
describe('Full fixture: UTypes.pas', () => {
|
|
const code = `unit UTypes;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.SysUtils;
|
|
|
|
const
|
|
C_MAX_RETRIES = 3;
|
|
C_DEFAULT_NAME = 'Guest';
|
|
|
|
type
|
|
TUserRole = (urAdmin, urEditor, urViewer);
|
|
|
|
TPoint2D = record
|
|
X: Double;
|
|
Y: Double;
|
|
end;
|
|
|
|
TUserName = string;
|
|
|
|
TUserInfo = class
|
|
public
|
|
type
|
|
TAddress = record
|
|
Street: string;
|
|
City: string;
|
|
Zip: string;
|
|
end;
|
|
private
|
|
FName: TUserName;
|
|
FRole: TUserRole;
|
|
FAddress: TAddress;
|
|
public
|
|
constructor Create(const AName: TUserName; ARole: TUserRole);
|
|
function GetDisplayName: string;
|
|
class function CreateAdmin(const AName: TUserName): TUserInfo; static;
|
|
property Name: TUserName read FName write FName;
|
|
property Role: TUserRole read FRole;
|
|
property Address: TAddress read FAddress write FAddress;
|
|
end;
|
|
|
|
implementation
|
|
|
|
constructor TUserInfo.Create(const AName: TUserName; ARole: TUserRole);
|
|
begin
|
|
FName := AName;
|
|
FRole := ARole;
|
|
end;
|
|
|
|
function TUserInfo.GetDisplayName: string;
|
|
begin
|
|
if FRole = urAdmin then
|
|
Result := '[Admin] ' + FName
|
|
else
|
|
Result := FName;
|
|
end;
|
|
|
|
class function TUserInfo.CreateAdmin(const AName: TUserName): TUserInfo;
|
|
begin
|
|
Result := TUserInfo.Create(AName, urAdmin);
|
|
end;
|
|
|
|
end.`;
|
|
|
|
it('should extract enums with members', () => {
|
|
const result = extractFromSource('UTypes.pas', code);
|
|
|
|
const enumNode = result.nodes.find((n) => n.kind === 'enum');
|
|
expect(enumNode?.name).toBe('TUserRole');
|
|
|
|
const members = result.nodes.filter((n) => n.kind === 'enum_member');
|
|
expect(members.length).toBe(3);
|
|
expect(members.map((m) => m.name)).toEqual(['urAdmin', 'urEditor', 'urViewer']);
|
|
});
|
|
|
|
it('should extract constants', () => {
|
|
const result = extractFromSource('UTypes.pas', code);
|
|
|
|
const constants = result.nodes.filter((n) => n.kind === 'constant');
|
|
expect(constants.length).toBe(2);
|
|
expect(constants.map((c) => c.name)).toContain('C_MAX_RETRIES');
|
|
expect(constants.map((c) => c.name)).toContain('C_DEFAULT_NAME');
|
|
});
|
|
|
|
it('should extract type aliases', () => {
|
|
const result = extractFromSource('UTypes.pas', code);
|
|
|
|
const aliases = result.nodes.filter((n) => n.kind === 'type_alias');
|
|
expect(aliases.map((a) => a.name)).toContain('TUserName');
|
|
});
|
|
|
|
it('should extract records as classes with fields', () => {
|
|
const result = extractFromSource('UTypes.pas', code);
|
|
|
|
const classes = result.nodes.filter((n) => n.kind === 'class');
|
|
expect(classes.map((c) => c.name)).toContain('TPoint2D');
|
|
|
|
// TPoint2D fields
|
|
const fields = result.nodes.filter((n) => n.kind === 'field');
|
|
expect(fields.map((f) => f.name)).toContain('X');
|
|
expect(fields.map((f) => f.name)).toContain('Y');
|
|
});
|
|
|
|
it('should extract static class methods', () => {
|
|
const result = extractFromSource('UTypes.pas', code);
|
|
|
|
const methods = result.nodes.filter((n) => n.kind === 'method');
|
|
const staticMethod = methods.find((m) => m.name === 'CreateAdmin');
|
|
expect(staticMethod).toBeDefined();
|
|
expect(staticMethod?.isStatic).toBe(true);
|
|
});
|
|
|
|
it('should extract nested types', () => {
|
|
const result = extractFromSource('UTypes.pas', code);
|
|
|
|
const classes = result.nodes.filter((n) => n.kind === 'class');
|
|
expect(classes.map((c) => c.name)).toContain('TAddress');
|
|
});
|
|
});
|
|
});
|
|
|
|
// =============================================================================
|
|
// DFM/FMX Extraction
|
|
// =============================================================================
|
|
|
|
describe('DFM/FMX Extraction', () => {
|
|
it('should extract components from DFM', () => {
|
|
const code = `object Form1: TForm1
|
|
Left = 0
|
|
Top = 0
|
|
Caption = 'My Form'
|
|
object Button1: TButton
|
|
Left = 10
|
|
Top = 10
|
|
Caption = 'Click Me'
|
|
end
|
|
end`;
|
|
const result = extractFromSource('Form1.dfm', code);
|
|
|
|
const components = result.nodes.filter((n) => n.kind === 'component');
|
|
expect(components.length).toBe(2);
|
|
expect(components.map((c) => c.name)).toContain('Form1');
|
|
expect(components.map((c) => c.name)).toContain('Button1');
|
|
|
|
const button = components.find((c) => c.name === 'Button1');
|
|
expect(button?.signature).toBe('TButton');
|
|
});
|
|
|
|
it('should extract nested component hierarchy', () => {
|
|
const code = `object Form1: TForm1
|
|
object Panel1: TPanel
|
|
object Label1: TLabel
|
|
Caption = 'Hello'
|
|
end
|
|
end
|
|
end`;
|
|
const result = extractFromSource('Form1.dfm', code);
|
|
|
|
const components = result.nodes.filter((n) => n.kind === 'component');
|
|
expect(components.length).toBe(3);
|
|
|
|
// Check nesting: Panel1 contains Label1
|
|
const panel = components.find((c) => c.name === 'Panel1');
|
|
const label = components.find((c) => c.name === 'Label1');
|
|
const containsEdge = result.edges.find(
|
|
(e) => e.source === panel?.id && e.target === label?.id && e.kind === 'contains'
|
|
);
|
|
expect(containsEdge).toBeDefined();
|
|
});
|
|
|
|
it('should extract event handler references', () => {
|
|
const code = `object Form1: TForm1
|
|
OnCreate = FormCreate
|
|
OnDestroy = FormDestroy
|
|
object Button1: TButton
|
|
OnClick = Button1Click
|
|
end
|
|
end`;
|
|
const result = extractFromSource('Form1.dfm', code);
|
|
|
|
const refs = result.unresolvedReferences;
|
|
expect(refs.length).toBe(3);
|
|
expect(refs.map((r) => r.referenceName)).toContain('FormCreate');
|
|
expect(refs.map((r) => r.referenceName)).toContain('FormDestroy');
|
|
expect(refs.map((r) => r.referenceName)).toContain('Button1Click');
|
|
expect(refs.every((r) => r.referenceKind === 'references')).toBe(true);
|
|
});
|
|
|
|
it('should handle multi-line properties', () => {
|
|
const code = `object Form1: TForm1
|
|
SQL.Strings = (
|
|
'SELECT * FROM users'
|
|
'WHERE active = 1')
|
|
object Button1: TButton
|
|
OnClick = Button1Click
|
|
end
|
|
end`;
|
|
const result = extractFromSource('Form1.dfm', code);
|
|
|
|
const components = result.nodes.filter((n) => n.kind === 'component');
|
|
expect(components.length).toBe(2);
|
|
|
|
const refs = result.unresolvedReferences;
|
|
expect(refs.length).toBe(1);
|
|
expect(refs[0]?.referenceName).toBe('Button1Click');
|
|
});
|
|
|
|
it('should handle inherited keyword', () => {
|
|
const code = `inherited Form1: TForm1
|
|
Caption = 'Inherited Form'
|
|
object Button1: TButton
|
|
OnClick = Button1Click
|
|
end
|
|
end`;
|
|
const result = extractFromSource('Form1.dfm', code);
|
|
|
|
const components = result.nodes.filter((n) => n.kind === 'component');
|
|
expect(components.length).toBe(2);
|
|
expect(components.map((c) => c.name)).toContain('Form1');
|
|
});
|
|
|
|
it('should handle item collection properties', () => {
|
|
const code = `object Form1: TForm1
|
|
object StatusBar1: TStatusBar
|
|
Panels = <
|
|
item
|
|
Width = 200
|
|
end
|
|
item
|
|
Width = 200
|
|
end>
|
|
end
|
|
end`;
|
|
const result = extractFromSource('Form1.dfm', code);
|
|
|
|
const components = result.nodes.filter((n) => n.kind === 'component');
|
|
expect(components.length).toBe(2);
|
|
});
|
|
|
|
describe('Full fixture: MainForm.dfm', () => {
|
|
const code = `object frmMain: TfrmMain
|
|
Left = 0
|
|
Top = 0
|
|
Caption = 'CodeGraph DFM Fixture'
|
|
ClientHeight = 480
|
|
ClientWidth = 640
|
|
OnCreate = FormCreate
|
|
OnDestroy = FormDestroy
|
|
object pnlTop: TPanel
|
|
Left = 0
|
|
Top = 0
|
|
Width = 640
|
|
Height = 50
|
|
object lblTitle: TLabel
|
|
Left = 16
|
|
Top = 16
|
|
Caption = 'Authentication Service'
|
|
end
|
|
object btnLogin: TButton
|
|
Left = 540
|
|
Top = 12
|
|
OnClick = btnLoginClick
|
|
end
|
|
end
|
|
object pnlContent: TPanel
|
|
Left = 0
|
|
Top = 50
|
|
object edtUsername: TEdit
|
|
Left = 16
|
|
Top = 16
|
|
OnChange = edtUsernameChange
|
|
end
|
|
object edtPassword: TEdit
|
|
Left = 16
|
|
Top = 48
|
|
OnKeyPress = edtPasswordKeyPress
|
|
end
|
|
object mmoLog: TMemo
|
|
Left = 16
|
|
Top = 88
|
|
end
|
|
end
|
|
object pnlStatus: TStatusBar
|
|
Left = 0
|
|
Top = 440
|
|
Panels = <
|
|
item
|
|
Width = 200
|
|
end
|
|
item
|
|
Width = 200
|
|
end>
|
|
end
|
|
end`;
|
|
|
|
it('should extract all components', () => {
|
|
const result = extractFromSource('MainForm.dfm', code);
|
|
|
|
const components = result.nodes.filter((n) => n.kind === 'component');
|
|
expect(components.length).toBe(9);
|
|
expect(components.map((c) => c.name)).toEqual(
|
|
expect.arrayContaining([
|
|
'frmMain', 'pnlTop', 'lblTitle', 'btnLogin',
|
|
'pnlContent', 'edtUsername', 'edtPassword', 'mmoLog', 'pnlStatus',
|
|
])
|
|
);
|
|
});
|
|
|
|
it('should extract all event handlers', () => {
|
|
const result = extractFromSource('MainForm.dfm', code);
|
|
|
|
const refs = result.unresolvedReferences;
|
|
expect(refs.length).toBe(5);
|
|
expect(refs.map((r) => r.referenceName)).toEqual(
|
|
expect.arrayContaining([
|
|
'FormCreate', 'FormDestroy', 'btnLoginClick',
|
|
'edtUsernameChange', 'edtPasswordKeyPress',
|
|
])
|
|
);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('Full Indexing', () => {
|
|
let tempDir: string;
|
|
|
|
beforeEach(() => {
|
|
tempDir = createTempDir();
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanupTempDir(tempDir);
|
|
});
|
|
|
|
it('should index a TypeScript file', async () => {
|
|
// Create test file
|
|
const srcDir = path.join(tempDir, 'src');
|
|
fs.mkdirSync(srcDir);
|
|
fs.writeFileSync(
|
|
path.join(srcDir, 'utils.ts'),
|
|
`
|
|
export function add(a: number, b: number): number {
|
|
return a + b;
|
|
}
|
|
|
|
export function multiply(a: number, b: number): number {
|
|
return a * b;
|
|
}
|
|
`
|
|
);
|
|
|
|
// Initialize and index
|
|
const cg = CodeGraph.initSync(tempDir);
|
|
const result = await cg.indexAll();
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.filesIndexed).toBe(1);
|
|
expect(result.nodesCreated).toBeGreaterThanOrEqual(2);
|
|
|
|
// Check nodes were stored
|
|
const nodes = cg.getNodesInFile('src/utils.ts');
|
|
expect(nodes.length).toBeGreaterThanOrEqual(2);
|
|
|
|
const addFunc = nodes.find((n) => n.name === 'add');
|
|
expect(addFunc).toBeDefined();
|
|
expect(addFunc?.kind).toBe('function');
|
|
|
|
cg.close();
|
|
});
|
|
|
|
it('should index multiple files', async () => {
|
|
// Create test files
|
|
const srcDir = path.join(tempDir, 'src');
|
|
fs.mkdirSync(srcDir);
|
|
|
|
fs.writeFileSync(
|
|
path.join(srcDir, 'math.ts'),
|
|
`export function add(a: number, b: number) { return a + b; }`
|
|
);
|
|
|
|
fs.writeFileSync(
|
|
path.join(srcDir, 'string.ts'),
|
|
`export function capitalize(s: string) { return s.toUpperCase(); }`
|
|
);
|
|
|
|
// Initialize and index
|
|
const cg = CodeGraph.initSync(tempDir);
|
|
const result = await cg.indexAll();
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.filesIndexed).toBe(2);
|
|
|
|
const files = cg.getFiles();
|
|
expect(files.length).toBe(2);
|
|
|
|
cg.close();
|
|
});
|
|
|
|
it('should track file hashes for incremental updates', async () => {
|
|
// Create initial file
|
|
const srcDir = path.join(tempDir, 'src');
|
|
fs.mkdirSync(srcDir);
|
|
fs.writeFileSync(path.join(srcDir, 'main.ts'), `export const x = 1;`);
|
|
|
|
// Initialize and index
|
|
const cg = CodeGraph.initSync(tempDir);
|
|
await cg.indexAll();
|
|
|
|
// Check file is tracked
|
|
const file = cg.getFile('src/main.ts');
|
|
expect(file).toBeDefined();
|
|
expect(file?.contentHash).toBeDefined();
|
|
|
|
// Modify file
|
|
fs.writeFileSync(path.join(srcDir, 'main.ts'), `export const x = 2;`);
|
|
|
|
// Check for changes
|
|
const changes = cg.getChangedFiles();
|
|
expect(changes.modified).toContain('src/main.ts');
|
|
|
|
cg.close();
|
|
});
|
|
|
|
it('should sync and detect changes', async () => {
|
|
// Create initial file
|
|
const srcDir = path.join(tempDir, 'src');
|
|
fs.mkdirSync(srcDir);
|
|
fs.writeFileSync(
|
|
path.join(srcDir, 'main.ts'),
|
|
`export function original() { return 1; }`
|
|
);
|
|
|
|
// Initialize and index
|
|
const cg = CodeGraph.initSync(tempDir);
|
|
await cg.indexAll();
|
|
|
|
const initialNodes = cg.getNodesInFile('src/main.ts');
|
|
expect(initialNodes.some((n) => n.name === 'original')).toBe(true);
|
|
|
|
// Modify file
|
|
fs.writeFileSync(
|
|
path.join(srcDir, 'main.ts'),
|
|
`export function updated() { return 2; }`
|
|
);
|
|
|
|
// Sync
|
|
const syncResult = await cg.sync();
|
|
expect(syncResult.filesModified).toBe(1);
|
|
|
|
// Check nodes were updated
|
|
const updatedNodes = cg.getNodesInFile('src/main.ts');
|
|
expect(updatedNodes.some((n) => n.name === 'updated')).toBe(true);
|
|
expect(updatedNodes.some((n) => n.name === 'original')).toBe(false);
|
|
|
|
cg.close();
|
|
});
|
|
});
|
|
|
|
describe('Path Normalization', () => {
|
|
it('should convert backslashes to forward slashes', () => {
|
|
expect(normalizePath('gui\\node_modules\\foo')).toBe('gui/node_modules/foo');
|
|
expect(normalizePath('src\\components\\Button.tsx')).toBe('src/components/Button.tsx');
|
|
});
|
|
|
|
it('should leave forward-slash paths unchanged', () => {
|
|
expect(normalizePath('src/components/Button.tsx')).toBe('src/components/Button.tsx');
|
|
});
|
|
|
|
it('should handle empty string', () => {
|
|
expect(normalizePath('')).toBe('');
|
|
});
|
|
});
|
|
|
|
describe('Directory Exclusion', () => {
|
|
let tempDir: string;
|
|
|
|
beforeEach(() => {
|
|
tempDir = createTempDir();
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanupTempDir(tempDir);
|
|
});
|
|
|
|
it('should exclude directories listed in .gitignore', () => {
|
|
// Create structure: src/index.ts + node_modules/pkg/index.js, gitignore node_modules
|
|
const srcDir = path.join(tempDir, 'src');
|
|
const nmDir = path.join(tempDir, 'node_modules', 'pkg');
|
|
fs.mkdirSync(srcDir, { recursive: true });
|
|
fs.mkdirSync(nmDir, { recursive: true });
|
|
fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;');
|
|
fs.writeFileSync(path.join(nmDir, 'index.js'), 'module.exports = {};');
|
|
fs.writeFileSync(path.join(tempDir, '.gitignore'), 'node_modules/\n');
|
|
|
|
const files = scanDirectory(tempDir);
|
|
|
|
expect(files).toContain('src/index.ts');
|
|
expect(files.every((f) => !f.includes('node_modules'))).toBe(true);
|
|
});
|
|
|
|
it('should exclude nested node_modules via a root .gitignore', () => {
|
|
// A trailing-slash pattern with no leading slash matches at any depth.
|
|
const srcDir = path.join(tempDir, 'packages', 'app', 'src');
|
|
const nmDir = path.join(tempDir, 'packages', 'app', 'node_modules', 'pkg');
|
|
fs.mkdirSync(srcDir, { recursive: true });
|
|
fs.mkdirSync(nmDir, { recursive: true });
|
|
fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;');
|
|
fs.writeFileSync(path.join(nmDir, 'index.js'), 'module.exports = {};');
|
|
fs.writeFileSync(path.join(tempDir, '.gitignore'), 'node_modules/\n');
|
|
|
|
const files = scanDirectory(tempDir);
|
|
|
|
expect(files).toContain('packages/app/src/index.ts');
|
|
expect(files.every((f) => !f.includes('node_modules'))).toBe(true);
|
|
});
|
|
|
|
it('should apply a nested .gitignore only to its own subtree', () => {
|
|
const appSrc = path.join(tempDir, 'app', 'src');
|
|
fs.mkdirSync(appSrc, { recursive: true });
|
|
fs.writeFileSync(path.join(appSrc, 'keep.ts'), 'export const a = 1;');
|
|
fs.writeFileSync(path.join(appSrc, 'skip.ts'), 'export const b = 2;');
|
|
fs.writeFileSync(path.join(tempDir, 'app', '.gitignore'), 'src/skip.ts\n');
|
|
// A sibling with the same name outside app/ must NOT be ignored.
|
|
const otherDir = path.join(tempDir, 'other', 'src');
|
|
fs.mkdirSync(otherDir, { recursive: true });
|
|
fs.writeFileSync(path.join(otherDir, 'skip.ts'), 'export const c = 3;');
|
|
|
|
const files = scanDirectory(tempDir);
|
|
|
|
expect(files).toContain('app/src/keep.ts');
|
|
expect(files).not.toContain('app/src/skip.ts');
|
|
expect(files).toContain('other/src/skip.ts');
|
|
});
|
|
|
|
it('should always skip .git directories', () => {
|
|
const srcDir = path.join(tempDir, 'src');
|
|
const gitDir = path.join(tempDir, '.git', 'objects');
|
|
fs.mkdirSync(srcDir, { recursive: true });
|
|
fs.mkdirSync(gitDir, { recursive: true });
|
|
fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;');
|
|
fs.writeFileSync(path.join(gitDir, 'pack.ts'), 'export const y = 2;');
|
|
|
|
const files = scanDirectory(tempDir);
|
|
|
|
expect(files).toContain('src/index.ts');
|
|
expect(files.every((f) => !f.includes('.git'))).toBe(true);
|
|
});
|
|
|
|
it('should return forward-slash paths on all platforms', () => {
|
|
const srcDir = path.join(tempDir, 'src', 'components');
|
|
fs.mkdirSync(srcDir, { recursive: true });
|
|
fs.writeFileSync(path.join(srcDir, 'Button.tsx'), 'export function Button() {}');
|
|
|
|
const files = scanDirectory(tempDir);
|
|
|
|
expect(files.length).toBe(1);
|
|
expect(files[0]).toBe('src/components/Button.tsx');
|
|
expect(files[0]).not.toContain('\\');
|
|
});
|
|
});
|
|
|
|
describe('Git Submodules', () => {
|
|
let tempDir: string;
|
|
|
|
beforeEach(() => {
|
|
tempDir = createTempDir();
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanupTempDir(tempDir);
|
|
});
|
|
|
|
it('should index files inside git submodules (issue #147)', async () => {
|
|
const { execFileSync } = await import('child_process');
|
|
const git = (cwd: string, ...args: string[]) =>
|
|
execFileSync('git', args, { cwd, stdio: 'pipe' });
|
|
|
|
// Build a separate "library" repo to use as a submodule source.
|
|
const libDir = path.join(tempDir, '_lib');
|
|
fs.mkdirSync(libDir, { recursive: true });
|
|
git(libDir, 'init', '-q');
|
|
git(libDir, 'config', 'user.email', 'test@test.com');
|
|
git(libDir, 'config', 'user.name', 'Test');
|
|
fs.writeFileSync(path.join(libDir, 'lib.ts'), 'export const fromSubmodule = 1;');
|
|
git(libDir, 'add', '-A');
|
|
git(libDir, 'commit', '-q', '-m', 'lib init');
|
|
|
|
// Build the main repo and add the lib repo as a submodule.
|
|
const mainDir = path.join(tempDir, 'main');
|
|
fs.mkdirSync(mainDir, { recursive: true });
|
|
git(mainDir, 'init', '-q');
|
|
git(mainDir, 'config', 'user.email', 'test@test.com');
|
|
git(mainDir, 'config', 'user.name', 'Test');
|
|
fs.writeFileSync(path.join(mainDir, 'app.ts'), 'export const app = 1;');
|
|
git(mainDir, 'add', '-A');
|
|
git(mainDir, 'commit', '-q', '-m', 'app init');
|
|
// protocol.file.allow=always is required to add a local-path submodule on
|
|
// recent git versions (CVE-2022-39253 mitigation).
|
|
execFileSync(
|
|
'git',
|
|
['-c', 'protocol.file.allow=always', 'submodule', 'add', '-q', libDir, 'libs/lib'],
|
|
{ cwd: mainDir, stdio: 'pipe' }
|
|
);
|
|
git(mainDir, 'commit', '-q', '-m', 'add submodule');
|
|
|
|
const files = scanDirectory(mainDir);
|
|
|
|
expect(files).toContain('app.ts');
|
|
expect(files).toContain('libs/lib/lib.ts');
|
|
});
|
|
});
|
|
|
|
describe('Nested non-submodule git repos', () => {
|
|
let tempDir: string;
|
|
|
|
beforeEach(() => {
|
|
tempDir = createTempDir();
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanupTempDir(tempDir);
|
|
});
|
|
|
|
it('should index files in embedded git repos run from a git super-repo (issue #193)', async () => {
|
|
const { execFileSync } = await import('child_process');
|
|
const git = (cwd: string, ...args: string[]) =>
|
|
execFileSync('git', args, { cwd, stdio: 'pipe' });
|
|
|
|
// Top-level workspace is itself a git repo, holding no source directly —
|
|
// the CMake "super-repo" layout from the issue.
|
|
const root = path.join(tempDir, 'root');
|
|
fs.mkdirSync(path.join(root, 'coding'), { recursive: true });
|
|
git(root, 'init', '-q');
|
|
git(root, 'config', 'user.email', 'test@test.com');
|
|
git(root, 'config', 'user.name', 'Test');
|
|
fs.writeFileSync(path.join(root, 'CMakeLists.txt'), 'cmake_minimum_required(VERSION 3.10)\n');
|
|
|
|
// Two independent clones living inside the workspace (NOT submodules):
|
|
// one with committed source, one with only untracked source.
|
|
const sub1 = path.join(root, 'sub_repo1', 'src');
|
|
fs.mkdirSync(sub1, { recursive: true });
|
|
git(path.join(root, 'sub_repo1'), 'init', '-q');
|
|
git(path.join(root, 'sub_repo1'), 'config', 'user.email', 'test@test.com');
|
|
git(path.join(root, 'sub_repo1'), 'config', 'user.name', 'Test');
|
|
fs.writeFileSync(path.join(sub1, 'one.ts'), 'export const one = 1;');
|
|
git(path.join(root, 'sub_repo1'), 'add', '-A');
|
|
git(path.join(root, 'sub_repo1'), 'commit', '-q', '-m', 'sub1 init');
|
|
|
|
const sub2 = path.join(root, 'sub_repo2', 'src');
|
|
fs.mkdirSync(sub2, { recursive: true });
|
|
git(path.join(root, 'sub_repo2'), 'init', '-q');
|
|
fs.writeFileSync(path.join(sub2, 'two.ts'), 'export const two = 2;');
|
|
|
|
const files = scanDirectory(root);
|
|
|
|
// Both committed and untracked source from the nested repos must be found.
|
|
expect(files).toContain('sub_repo1/src/one.ts');
|
|
expect(files).toContain('sub_repo2/src/two.ts');
|
|
});
|
|
|
|
it('should respect each embedded repo\'s own .gitignore', async () => {
|
|
const { execFileSync } = await import('child_process');
|
|
const git = (cwd: string, ...args: string[]) =>
|
|
execFileSync('git', args, { cwd, stdio: 'pipe' });
|
|
|
|
const root = path.join(tempDir, 'root');
|
|
fs.mkdirSync(root, { recursive: true });
|
|
git(root, 'init', '-q');
|
|
|
|
const sub = path.join(root, 'sub_repo', 'src');
|
|
fs.mkdirSync(sub, { recursive: true });
|
|
git(path.join(root, 'sub_repo'), 'init', '-q');
|
|
fs.writeFileSync(path.join(root, 'sub_repo', '.gitignore'), 'src/generated.ts\n');
|
|
fs.writeFileSync(path.join(sub, 'real.ts'), 'export const real = 1;');
|
|
fs.writeFileSync(path.join(sub, 'generated.ts'), 'export const generated = 1;');
|
|
|
|
const files = scanDirectory(root);
|
|
|
|
expect(files).toContain('sub_repo/src/real.ts');
|
|
expect(files).not.toContain('sub_repo/src/generated.ts');
|
|
});
|
|
});
|
|
|
|
// =============================================================================
|
|
// Scala
|
|
// =============================================================================
|
|
|
|
describe('Scala Extraction', () => {
|
|
describe('Language detection', () => {
|
|
it('should detect Scala files', () => {
|
|
expect(detectLanguage('Main.scala')).toBe('scala');
|
|
expect(detectLanguage('script.sc')).toBe('scala');
|
|
expect(detectLanguage('src/UserService.scala')).toBe('scala');
|
|
});
|
|
|
|
it('should report Scala as supported', () => {
|
|
expect(isLanguageSupported('scala')).toBe(true);
|
|
expect(getSupportedLanguages()).toContain('scala');
|
|
});
|
|
});
|
|
|
|
describe('Class extraction', () => {
|
|
it('should extract class definitions', () => {
|
|
const code = `
|
|
class UserService(private val repo: UserRepository) {
|
|
def findUser(id: String): Option[String] = Some(id)
|
|
}
|
|
`;
|
|
const result = extractFromSource('UserService.scala', code);
|
|
const cls = result.nodes.find((n) => n.kind === 'class' && n.name === 'UserService');
|
|
expect(cls).toBeDefined();
|
|
expect(cls?.language).toBe('scala');
|
|
});
|
|
|
|
it('should extract object definitions as class kind', () => {
|
|
const code = `
|
|
object DatabaseConfig {
|
|
val url = "jdbc:postgresql://localhost/mydb"
|
|
}
|
|
`;
|
|
const result = extractFromSource('Config.scala', code);
|
|
const obj = result.nodes.find((n) => n.kind === 'class' && n.name === 'DatabaseConfig');
|
|
expect(obj).toBeDefined();
|
|
});
|
|
|
|
it('should extract trait definitions as trait kind', () => {
|
|
const code = `
|
|
trait Repository[A] {
|
|
def findById(id: String): Option[A]
|
|
def save(entity: A): Unit
|
|
}
|
|
`;
|
|
const result = extractFromSource('Repository.scala', code);
|
|
const trait_ = result.nodes.find((n) => n.kind === 'trait' && n.name === 'Repository');
|
|
expect(trait_).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('Method and function extraction', () => {
|
|
it('should extract method definitions inside a class', () => {
|
|
const code = `
|
|
class Calculator {
|
|
def add(a: Int, b: Int): Int = a + b
|
|
def divide(a: Double, b: Double): Double = a / b
|
|
}
|
|
`;
|
|
const result = extractFromSource('Calculator.scala', code);
|
|
const methods = result.nodes.filter((n) => n.kind === 'method');
|
|
expect(methods.find((m) => m.name === 'add')).toBeDefined();
|
|
expect(methods.find((m) => m.name === 'divide')).toBeDefined();
|
|
});
|
|
|
|
it('should extract method signatures', () => {
|
|
const code = `
|
|
class Greeter {
|
|
def greet(name: String): String = s"Hello, \${name}!"
|
|
}
|
|
`;
|
|
const result = extractFromSource('Greeter.scala', code);
|
|
const method = result.nodes.find((n) => n.name === 'greet');
|
|
expect(method?.signature).toContain('name: String');
|
|
expect(method?.signature).toContain('String');
|
|
});
|
|
|
|
it('should extract top-level function definitions as functions', () => {
|
|
const code = `
|
|
def factorial(n: Int): Int = if (n <= 1) 1 else n * factorial(n - 1)
|
|
def greet(name: String): String = s"Hello, \${name}!"
|
|
`;
|
|
const result = extractFromSource('utils.scala', code);
|
|
const fns = result.nodes.filter((n) => n.kind === 'function');
|
|
expect(fns.find((f) => f.name === 'factorial')).toBeDefined();
|
|
expect(fns.find((f) => f.name === 'greet')).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('Val and var extraction', () => {
|
|
it('should extract val inside a class as field', () => {
|
|
const code = `
|
|
class Config {
|
|
val timeout: Int = 30
|
|
val host: String = "localhost"
|
|
}
|
|
`;
|
|
const result = extractFromSource('Config.scala', code);
|
|
const fields = result.nodes.filter((n) => n.kind === 'field');
|
|
expect(fields.find((f) => f.name === 'timeout')).toBeDefined();
|
|
expect(fields.find((f) => f.name === 'host')).toBeDefined();
|
|
});
|
|
|
|
it('should extract var inside a class as field', () => {
|
|
const code = `
|
|
class Counter {
|
|
var count: Int = 0
|
|
}
|
|
`;
|
|
const result = extractFromSource('Counter.scala', code);
|
|
const field = result.nodes.find((n) => n.kind === 'field' && n.name === 'count');
|
|
expect(field).toBeDefined();
|
|
});
|
|
|
|
it('should extract top-level val as constant', () => {
|
|
const code = `
|
|
val MaxConnections: Int = 100
|
|
val DefaultTimeout = 30
|
|
`;
|
|
const result = extractFromSource('constants.scala', code);
|
|
const consts = result.nodes.filter((n) => n.kind === 'constant');
|
|
expect(consts.find((c) => c.name === 'MaxConnections')).toBeDefined();
|
|
});
|
|
|
|
it('should extract top-level var as variable', () => {
|
|
const code = `
|
|
var retries: Int = 3
|
|
`;
|
|
const result = extractFromSource('state.scala', code);
|
|
const v = result.nodes.find((n) => n.kind === 'variable' && n.name === 'retries');
|
|
expect(v).toBeDefined();
|
|
});
|
|
|
|
it('should include type in val/var signature', () => {
|
|
const code = `
|
|
class Service {
|
|
val timeout: Int = 30
|
|
}
|
|
`;
|
|
const result = extractFromSource('Service.scala', code);
|
|
const field = result.nodes.find((n) => n.name === 'timeout');
|
|
expect(field?.signature).toContain('timeout');
|
|
expect(field?.signature).toContain('Int');
|
|
});
|
|
});
|
|
|
|
describe('Enum extraction', () => {
|
|
it('should extract enum definitions', () => {
|
|
const code = `
|
|
enum Color:
|
|
case Red
|
|
case Green
|
|
case Blue
|
|
`;
|
|
const result = extractFromSource('Color.scala', code);
|
|
const enumNode = result.nodes.find((n) => n.kind === 'enum' && n.name === 'Color');
|
|
expect(enumNode).toBeDefined();
|
|
});
|
|
|
|
it('should extract enum cases as enum_member', () => {
|
|
const code = `
|
|
enum Direction:
|
|
case North
|
|
case South
|
|
case East
|
|
case West
|
|
`;
|
|
const result = extractFromSource('Direction.scala', code);
|
|
const members = result.nodes.filter((n) => n.kind === 'enum_member');
|
|
expect(members.find((m) => m.name === 'North')).toBeDefined();
|
|
expect(members.find((m) => m.name === 'South')).toBeDefined();
|
|
expect(members.length).toBeGreaterThanOrEqual(4);
|
|
});
|
|
});
|
|
|
|
describe('Type alias extraction', () => {
|
|
it('should extract type aliases', () => {
|
|
const code = `
|
|
type UserId = String
|
|
type UserMap = Map[String, String]
|
|
`;
|
|
const result = extractFromSource('types.scala', code);
|
|
const aliases = result.nodes.filter((n) => n.kind === 'type_alias');
|
|
expect(aliases.find((a) => a.name === 'UserId')).toBeDefined();
|
|
expect(aliases.find((a) => a.name === 'UserMap')).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('Import extraction', () => {
|
|
it('should extract import declarations', () => {
|
|
const code = `
|
|
import scala.collection.mutable.ListBuffer
|
|
import scala.concurrent.Future
|
|
`;
|
|
const result = extractFromSource('imports.scala', code);
|
|
const imports = result.nodes.filter((n) => n.kind === 'import');
|
|
expect(imports.length).toBeGreaterThanOrEqual(2);
|
|
});
|
|
});
|
|
|
|
describe('Visibility modifiers', () => {
|
|
it('should extract private visibility', () => {
|
|
const code = `
|
|
class Service {
|
|
private val secret: String = "abc"
|
|
private def helper(): Unit = {}
|
|
}
|
|
`;
|
|
const result = extractFromSource('Service.scala', code);
|
|
const secretField = result.nodes.find((n) => n.name === 'secret');
|
|
expect(secretField?.visibility).toBe('private');
|
|
const helperMethod = result.nodes.find((n) => n.name === 'helper');
|
|
expect(helperMethod?.visibility).toBe('private');
|
|
});
|
|
|
|
it('should extract protected visibility', () => {
|
|
const code = `
|
|
class Base {
|
|
protected def helperMethod(): Unit = {}
|
|
}
|
|
`;
|
|
const result = extractFromSource('Base.scala', code);
|
|
const method = result.nodes.find((n) => n.name === 'helperMethod');
|
|
expect(method?.visibility).toBe('protected');
|
|
});
|
|
|
|
it('should default to public visibility', () => {
|
|
const code = `
|
|
class Greeter {
|
|
def hello(): Unit = {}
|
|
}
|
|
`;
|
|
const result = extractFromSource('Greeter.scala', code);
|
|
const method = result.nodes.find((n) => n.name === 'hello');
|
|
expect(method?.visibility).toBe('public');
|
|
});
|
|
});
|
|
|
|
describe('Inheritance', () => {
|
|
it('should extract extends relationships', () => {
|
|
const code = `
|
|
class AdminUser extends User {
|
|
def adminAction(): Unit = {}
|
|
}
|
|
`;
|
|
const result = extractFromSource('AdminUser.scala', code);
|
|
const extendsRefs = result.unresolvedReferences.filter((r) => r.referenceKind === 'extends');
|
|
expect(extendsRefs.find((r) => r.referenceName === 'User')).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('Call extraction', () => {
|
|
it('should extract function call expressions', () => {
|
|
const code = `
|
|
def processData(): Unit = {
|
|
val result = computeResult()
|
|
println(result)
|
|
}
|
|
`;
|
|
const result = extractFromSource('processor.scala', code);
|
|
const calls = result.unresolvedReferences.filter((r) => r.referenceKind === 'calls');
|
|
expect(calls.length).toBeGreaterThan(0);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('Vue Extraction', () => {
|
|
it('should detect Vue files', () => {
|
|
expect(detectLanguage('App.vue')).toBe('vue');
|
|
expect(detectLanguage('components/Button.vue')).toBe('vue');
|
|
expect(isLanguageSupported('vue')).toBe(true);
|
|
});
|
|
|
|
it('should extract component node from a Vue SFC', () => {
|
|
const code = `<template>
|
|
<div>{{ message }}</div>
|
|
</template>
|
|
|
|
<script>
|
|
export default {
|
|
data() {
|
|
return { message: 'Hello' };
|
|
}
|
|
}
|
|
</script>
|
|
`;
|
|
const result = extractFromSource('HelloWorld.vue', code);
|
|
|
|
const componentNode = result.nodes.find((n) => n.kind === 'component');
|
|
expect(componentNode).toBeDefined();
|
|
expect(componentNode?.name).toBe('HelloWorld');
|
|
expect(componentNode?.language).toBe('vue');
|
|
expect(componentNode?.isExported).toBe(true);
|
|
});
|
|
|
|
it('should extract functions from <script> block', () => {
|
|
const code = `<template>
|
|
<button @click="handleClick">Click</button>
|
|
</template>
|
|
|
|
<script>
|
|
function handleClick() {
|
|
console.log('clicked');
|
|
}
|
|
|
|
const count = 0;
|
|
</script>
|
|
`;
|
|
const result = extractFromSource('Button.vue', code);
|
|
|
|
const componentNode = result.nodes.find((n) => n.kind === 'component');
|
|
expect(componentNode).toBeDefined();
|
|
expect(componentNode?.name).toBe('Button');
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function' && n.name === 'handleClick');
|
|
expect(funcNode).toBeDefined();
|
|
expect(funcNode?.language).toBe('vue');
|
|
});
|
|
|
|
it('should extract from <script setup lang="ts"> block', () => {
|
|
const code = `<template>
|
|
<div>{{ count }}</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { ref } from 'vue';
|
|
|
|
const count = ref(0);
|
|
|
|
function increment(): void {
|
|
count.value++;
|
|
}
|
|
</script>
|
|
`;
|
|
const result = extractFromSource('Counter.vue', code);
|
|
|
|
const componentNode = result.nodes.find((n) => n.kind === 'component');
|
|
expect(componentNode).toBeDefined();
|
|
expect(componentNode?.name).toBe('Counter');
|
|
|
|
const funcNode = result.nodes.find((n) => n.kind === 'function' && n.name === 'increment');
|
|
expect(funcNode).toBeDefined();
|
|
expect(funcNode?.language).toBe('vue');
|
|
|
|
// All nodes should be marked as vue language
|
|
for (const node of result.nodes) {
|
|
expect(node.language).toBe('vue');
|
|
}
|
|
});
|
|
|
|
it('should extract from both <script> and <script setup> blocks', () => {
|
|
const code = `<template>
|
|
<div>{{ msg }}</div>
|
|
</template>
|
|
|
|
<script>
|
|
export default {
|
|
name: 'DualScript'
|
|
}
|
|
</script>
|
|
|
|
<script setup>
|
|
const msg = 'hello';
|
|
|
|
function greet() {
|
|
return msg;
|
|
}
|
|
</script>
|
|
`;
|
|
const result = extractFromSource('DualScript.vue', code);
|
|
|
|
const componentNode = result.nodes.find((n) => n.kind === 'component');
|
|
expect(componentNode).toBeDefined();
|
|
|
|
const greetFunc = result.nodes.find((n) => n.kind === 'function' && n.name === 'greet');
|
|
expect(greetFunc).toBeDefined();
|
|
});
|
|
|
|
it('should create component node for template-only Vue file', () => {
|
|
const code = `<template>
|
|
<div>Static content</div>
|
|
</template>
|
|
`;
|
|
const result = extractFromSource('Static.vue', code);
|
|
|
|
const componentNode = result.nodes.find((n) => n.kind === 'component');
|
|
expect(componentNode).toBeDefined();
|
|
expect(componentNode?.name).toBe('Static');
|
|
expect(componentNode?.language).toBe('vue');
|
|
|
|
// Only the component node should exist (no script nodes)
|
|
expect(result.nodes.length).toBe(1);
|
|
});
|
|
|
|
it('should create containment edges from component to script nodes', () => {
|
|
const code = `<template>
|
|
<div>{{ value }}</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
const value = 42;
|
|
</script>
|
|
`;
|
|
const result = extractFromSource('Contained.vue', code);
|
|
|
|
const componentNode = result.nodes.find((n) => n.kind === 'component');
|
|
expect(componentNode).toBeDefined();
|
|
|
|
// Should have containment edges from component to child nodes
|
|
const containEdges = result.edges.filter(
|
|
(e) => e.source === componentNode!.id && e.kind === 'contains'
|
|
);
|
|
expect(containEdges.length).toBeGreaterThan(0);
|
|
});
|
|
});
|
|
|
|
describe('Instantiates + Decorates edge extraction', () => {
|
|
it('emits an instantiates ref for `new Foo()`', () => {
|
|
const code = `
|
|
class Foo {}
|
|
function bootstrap() { return new Foo(); }
|
|
`;
|
|
const result = extractFromSource('app.ts', code);
|
|
const ref = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'instantiates' && r.referenceName === 'Foo'
|
|
);
|
|
expect(ref).toBeDefined();
|
|
});
|
|
|
|
it('strips type-argument suffix from generic constructors', () => {
|
|
const code = `
|
|
class Container<T> { constructor(_: T) {} }
|
|
function go() { return new Container<string>('x'); }
|
|
`;
|
|
const result = extractFromSource('app.ts', code);
|
|
const ref = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'instantiates'
|
|
);
|
|
expect(ref).toBeDefined();
|
|
// Container<string> must be normalised to "Container" — otherwise
|
|
// resolution can never match the class node.
|
|
expect(ref!.referenceName).toBe('Container');
|
|
});
|
|
|
|
it('keeps trailing identifier from qualified `new ns.Foo()`', () => {
|
|
const code = `
|
|
const ns = { Foo: class {} };
|
|
function go() { return new ns.Foo(); }
|
|
`;
|
|
const result = extractFromSource('app.ts', code);
|
|
const ref = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'instantiates'
|
|
);
|
|
// We can't always resolve which Foo, but the name should be the
|
|
// simple identifier so name-matching has a chance.
|
|
expect(ref?.referenceName).toBe('Foo');
|
|
});
|
|
|
|
it('emits a decorates ref for `@Foo class X {}`', () => {
|
|
const code = `
|
|
function Foo(_arg: string) { return (cls: any) => cls; }
|
|
@Foo('x')
|
|
class X {}
|
|
`;
|
|
const result = extractFromSource('app.ts', code);
|
|
const decorClass = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'decorates' && r.referenceName === 'Foo'
|
|
);
|
|
expect(decorClass).toBeDefined();
|
|
});
|
|
|
|
it('does NOT attribute a prior class\'s decorator to the next class', () => {
|
|
// Regression: the sibling-walk must stop at the first non-
|
|
// decorator separator. `@A class Foo {} @B class Bar {}` must
|
|
// produce `decorates(Foo, A)` and `decorates(Bar, B)` — never
|
|
// `decorates(Bar, A)`.
|
|
const code = `
|
|
function A(cls: any) { return cls; }
|
|
function B(cls: any) { return cls; }
|
|
@A
|
|
class Foo {}
|
|
@B
|
|
class Bar {}
|
|
`;
|
|
const result = extractFromSource('app.ts', code);
|
|
const decoratesEdges = result.unresolvedReferences.filter(
|
|
(r) => r.referenceKind === 'decorates'
|
|
);
|
|
// Exactly one decorates ref per decorated class, no cross-attribution.
|
|
const fromBar = decoratesEdges.filter((r) =>
|
|
result.nodes.find((n) => n.id === r.fromNodeId && n.name === 'Bar')
|
|
);
|
|
expect(fromBar.length).toBe(1);
|
|
expect(fromBar[0]!.referenceName).toBe('B');
|
|
});
|
|
|
|
it('emits a decorates ref for `@Foo method() {}`', () => {
|
|
const code = `
|
|
function Get(p: string) { return (t: any, k: string) => t; }
|
|
class Svc {
|
|
@Get('/x') method() { return 1; }
|
|
}
|
|
`;
|
|
const result = extractFromSource('app.ts', code);
|
|
const decorMethod = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'decorates' && r.referenceName === 'Get'
|
|
);
|
|
expect(decorMethod).toBeDefined();
|
|
// The decorated symbol must be `method`, not the constructor or class.
|
|
const decoratedNode = result.nodes.find((n) => n.id === decorMethod!.fromNodeId);
|
|
expect(decoratedNode?.name).toBe('method');
|
|
});
|
|
});
|
|
|
|
// =============================================================================
|
|
// Lua
|
|
// =============================================================================
|
|
|
|
describe('Lua Extraction', () => {
|
|
describe('Language detection', () => {
|
|
it('should detect Lua files', () => {
|
|
expect(detectLanguage('init.lua')).toBe('lua');
|
|
expect(detectLanguage('src/util.lua')).toBe('lua');
|
|
});
|
|
|
|
it('should report Lua as supported', () => {
|
|
expect(isLanguageSupported('lua')).toBe(true);
|
|
expect(getSupportedLanguages()).toContain('lua');
|
|
});
|
|
});
|
|
|
|
describe('Function extraction', () => {
|
|
it('should extract global and local functions', () => {
|
|
const code = `
|
|
function configure(opts) return opts end
|
|
local function helper(x) return x * 2 end
|
|
`;
|
|
const result = extractFromSource('init.lua', code);
|
|
const funcs = result.nodes.filter((n) => n.kind === 'function').map((n) => n.name);
|
|
expect(funcs).toContain('configure');
|
|
expect(funcs).toContain('helper');
|
|
const configure = result.nodes.find((n) => n.name === 'configure');
|
|
expect(configure?.language).toBe('lua');
|
|
expect(configure?.signature).toBe('(opts)');
|
|
});
|
|
|
|
it('should split table/method functions into a receiver and method name', () => {
|
|
const code = `
|
|
function M.connect(host, port) return host end
|
|
function M:send(data) return self end
|
|
`;
|
|
const result = extractFromSource('init.lua', code);
|
|
const methods = result.nodes.filter((n) => n.kind === 'method');
|
|
const connect = methods.find((m) => m.name === 'connect');
|
|
expect(connect?.qualifiedName).toBe('M::connect');
|
|
const send = methods.find((m) => m.name === 'send');
|
|
expect(send?.qualifiedName).toBe('M::send');
|
|
});
|
|
});
|
|
|
|
describe('Variable extraction', () => {
|
|
it('should extract local variable declarations', () => {
|
|
const code = `
|
|
local M = {}
|
|
local count = 0
|
|
`;
|
|
const result = extractFromSource('mod.lua', code);
|
|
const vars = result.nodes.filter((n) => n.kind === 'variable').map((n) => n.name);
|
|
expect(vars).toContain('M');
|
|
expect(vars).toContain('count');
|
|
});
|
|
});
|
|
|
|
describe('Import extraction (require)', () => {
|
|
it('should extract require() in local declarations and bare calls', () => {
|
|
const code = `
|
|
local socket = require("socket")
|
|
local http = require "resty.http"
|
|
require("side.effect")
|
|
`;
|
|
const result = extractFromSource('net.lua', code);
|
|
const imports = result.nodes.filter((n) => n.kind === 'import').map((n) => n.name);
|
|
expect(imports).toContain('socket');
|
|
expect(imports).toContain('resty.http');
|
|
expect(imports).toContain('side.effect');
|
|
|
|
const ref = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'imports' && r.referenceName === 'socket'
|
|
);
|
|
expect(ref).toBeDefined();
|
|
});
|
|
|
|
// Regression: the tree-sitter-wasms Lua grammar (ABI 13) corrupts the shared
|
|
// WASM heap under web-tree-sitter 0.25, dropping nested calls/imports on every
|
|
// parse after the first. We vendor the ABI-15 grammar instead — this guards it
|
|
// by extracting several sources in sequence and asserting the LAST still works.
|
|
it('should keep extracting require across many sequential parses', () => {
|
|
let last;
|
|
for (let i = 0; i < 8; i++) {
|
|
last = extractFromSource(`f${i}.lua`, `local m = require("module.${i}")\nreturn m\n`);
|
|
}
|
|
const imports = last!.nodes.filter((n) => n.kind === 'import').map((n) => n.name);
|
|
expect(imports).toContain('module.7');
|
|
});
|
|
});
|
|
|
|
describe('Call extraction', () => {
|
|
it('should record intra-file calls as resolvable references', () => {
|
|
const code = `
|
|
local function helper(x) return x end
|
|
local function run(y) return helper(y) end
|
|
`;
|
|
const result = extractFromSource('calls.lua', code);
|
|
const call = result.unresolvedReferences.find(
|
|
(r) => r.referenceKind === 'calls' && r.referenceName === 'helper'
|
|
);
|
|
expect(call).toBeDefined();
|
|
});
|
|
});
|
|
});
|
|
|
|
// =============================================================================
|
|
// Luau (typed superset of Lua — https://luau.org)
|
|
// =============================================================================
|
|
|
|
describe('Luau Extraction', () => {
|
|
describe('Language detection', () => {
|
|
it('should detect Luau files', () => {
|
|
expect(detectLanguage('init.luau')).toBe('luau');
|
|
expect(detectLanguage('src/Client.luau')).toBe('luau');
|
|
});
|
|
|
|
it('should report Luau as supported', () => {
|
|
expect(isLanguageSupported('luau')).toBe(true);
|
|
expect(getSupportedLanguages()).toContain('luau');
|
|
});
|
|
});
|
|
|
|
describe('Type aliases', () => {
|
|
it('should extract `type` and `export type` definitions', () => {
|
|
const code = `
|
|
export type Vector = { x: number, y: number }
|
|
type Handler = (msg: string) -> boolean
|
|
`;
|
|
const result = extractFromSource('types.luau', code);
|
|
const aliases = result.nodes.filter((n) => n.kind === 'type_alias');
|
|
const vector = aliases.find((a) => a.name === 'Vector');
|
|
expect(vector).toBeDefined();
|
|
expect(vector?.isExported).toBe(true);
|
|
const handler = aliases.find((a) => a.name === 'Handler');
|
|
expect(handler).toBeDefined();
|
|
expect(handler?.isExported).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('Typed functions and methods', () => {
|
|
it('should capture typed signatures and split methods by receiver', () => {
|
|
const code = `
|
|
function configure(opts: { debug: boolean }): boolean
|
|
return opts.debug
|
|
end
|
|
function Client:fetch(path: string): Response
|
|
return path
|
|
end
|
|
`;
|
|
const result = extractFromSource('client.luau', code);
|
|
const configure = result.nodes.find((n) => n.kind === 'function' && n.name === 'configure');
|
|
expect(configure?.language).toBe('luau');
|
|
expect(configure?.signature).toBe('(opts: { debug: boolean }): boolean');
|
|
const fetch = result.nodes.find((n) => n.kind === 'method' && n.name === 'fetch');
|
|
expect(fetch?.qualifiedName).toBe('Client::fetch');
|
|
});
|
|
});
|
|
|
|
describe('Imports and variables', () => {
|
|
it('should extract string and Roblox instance-path require imports', () => {
|
|
const code = `
|
|
local http = require("http")
|
|
local Signal = require(script.Parent.Signal)
|
|
local count = 0
|
|
`;
|
|
const result = extractFromSource('mod.luau', code);
|
|
const imports = result.nodes.filter((n) => n.kind === 'import').map((n) => n.name);
|
|
expect(imports).toContain('http'); // string require
|
|
expect(imports).toContain('Signal'); // Roblox instance-path require
|
|
const vars = result.nodes.filter((n) => n.kind === 'variable').map((n) => n.name);
|
|
expect(vars).toContain('count');
|
|
});
|
|
});
|
|
});
|