diff --git a/.github/workflows/build-tree-sitter-prebuilds.yml b/.github/workflows/build-tree-sitter-prebuilds.yml index b592ebe16..348afec30 100644 --- a/.github/workflows/build-tree-sitter-prebuilds.yml +++ b/.github/workflows/build-tree-sitter-prebuilds.yml @@ -219,8 +219,18 @@ jobs: throw new Error('synchronize requires valid before/head SHAs; refusing an unbounded rebuild'); } } - // Fail closed if either commit is unavailable. Never fall back to - // the cumulative PR diff, which would re-enable the loop. + // The checkout can be a PR merge ref and omit the previous PR-head + // commit despite fetch-depth: 0. Fetch the validated endpoint on + // demand; if that still fails, keep failing closed rather than + // falling back to the cumulative PR diff and re-enabling the loop. + for (const sha of [before, head]) { + try { + execFileSync('git', ['cat-file', '-e', `${sha}^{commit}`]); + } catch { + execFileSync('git', ['fetch', '--no-tags', 'origin', sha]); + execFileSync('git', ['cat-file', '-e', `${sha}^{commit}`]); + } + } const changed = execFileSync('git', [ 'diff', '--name-only', '--no-renames', '-z', before, head, '--', ], { encoding: 'utf8' }).split('\0').filter(Boolean); diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index bf8a5e381..e3c7271ea 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -407,6 +407,8 @@ Each language implements `LanguageProvider` (`language-provider.ts`). Key fields 18 providers in `languages/index.ts` via `satisfies Record` — missing a language is a compile error. +Elixir's experimental CFG and framework facts are provider-owned. Its taint model covers local bindings only; destructured Phoenix heads, arbitrary `Repo` receivers, callable-value dispatch, captured-closure taint, macro expansion, and cross-process `receive` flow remain explicit gaps. + ### Unified capture tags Per-language tree-sitter queries use different AST node names but produce the **same semantic capture tags**: `@definition.class`, `@definition.function`, `@call.name`, `@import.source`, `@reference.inherits`. Downstream extraction needs no language branching. Defined in `tree-sitter-queries.ts`. diff --git a/README.md b/README.md index 3748deca4..beaf54ccf 100644 --- a/README.md +++ b/README.md @@ -698,10 +698,13 @@ GitNexus builds a complete knowledge graph of your codebase through a multi-phas | Objective-C | ✓ | — | ✓ | ✓ | ✓ | — | — | — | — | | Dart | ✓ | — | ✓ | ✓ | ✓ | ✓ | — | ✓ | ✓ | | Zig | ✓ | — | ✓ | — | ✓ | ✓ | ✓ | — | ✓ | +| Elixir (experimental) | ✓ | — | ✓ | ✓ | — | — | ✓ | ✓ | — | **Imports** — cross-file import resolution · **Named Bindings** — `import { X as Y }` / re-export tracking · **Exports** — public/exported symbol detection · **Heritage** — class inheritance, interfaces, mixins · **Type Annotations** — explicit type extraction for receiver resolution · **Constructor Inference** — infer receiver type from constructor calls (`self`/`this` resolution included for all languages) · **Config** — language toolchain config parsing (tsconfig, go.mod, etc.) · **Frameworks** — AST-based framework pattern detection · **Entry Points** — entry point scoring heuristics -**Control flow (CFG, opt-in `--pdg`)** — per-function control-flow graphs (`BasicBlock` nodes + `CFG` edges) feeding the PDG/taint substrate, currently **TypeScript & JavaScript** (#2081 M1); other languages planned. Off by default. +**Control flow (CFG, opt-in `--pdg`)** — per-function control-flow graphs (`BasicBlock` nodes + `CFG` edges) feeding the PDG/taint substrate. TypeScript, JavaScript, and experimental Elixir support it; other languages are planned. Off by default. + +Elixir support is experimental. Its provider-owned CFG supports local flow only; destructured Phoenix heads, arbitrary `Repo` receivers, callable-value dispatch, captured-closure taint, macro expansion, and cross-process `receive` flow are not modelled. ### Multi-Repo Architecture diff --git a/gitnexus-shared/src/language-detection.ts b/gitnexus-shared/src/language-detection.ts index 3fe27f7b7..cede46956 100644 --- a/gitnexus-shared/src/language-detection.ts +++ b/gitnexus-shared/src/language-detection.ts @@ -55,6 +55,7 @@ const EXTENSION_MAP: Record = { [SupportedLanguages.Vue]: ['.vue'], [SupportedLanguages.Cobol]: ['.cbl', '.cob', '.cpy', '.cobol'], [SupportedLanguages.Zig]: ['.zig'], + [SupportedLanguages.Elixir]: ['.ex', '.exs'], } satisfies Record; // Ensure exhaustiveness /** Pre-built reverse lookup: extension → language (built once at module load). */ @@ -125,6 +126,7 @@ const SYNTAX_MAP: Record = { [SupportedLanguages.Vue]: 'typescript', [SupportedLanguages.Cobol]: 'cobol', [SupportedLanguages.Zig]: 'zig', + [SupportedLanguages.Elixir]: 'elixir', } satisfies Record; // Ensure exhaustiveness /** Non-code file extensions → Prism-compatible syntax identifiers */ diff --git a/gitnexus-shared/src/languages.ts b/gitnexus-shared/src/languages.ts index eec45e612..376478bb5 100644 --- a/gitnexus-shared/src/languages.ts +++ b/gitnexus-shared/src/languages.ts @@ -24,4 +24,5 @@ export enum SupportedLanguages { /** Standalone regex processor — no tree-sitter, no LanguageProvider. */ Cobol = 'cobol', Zig = 'zig', + Elixir = 'elixir', } diff --git a/gitnexus-shared/src/scope-resolution/language-classification.ts b/gitnexus-shared/src/scope-resolution/language-classification.ts index 2e635c8e2..262ec768d 100644 --- a/gitnexus-shared/src/scope-resolution/language-classification.ts +++ b/gitnexus-shared/src/scope-resolution/language-classification.ts @@ -47,6 +47,7 @@ export const LanguageClassifications: Readonly" + }, + "_measured": { + "collide_ms": 0.234, + "collide_scaling_ratio": 1.053, + "depth_ratio": 0.971, + "scaling_ratio": 0.983, + "small_ms": 0.243 + } + }, "zig": { "small": { "files": 400, diff --git a/gitnexus/bench/import-target/measure.mjs b/gitnexus/bench/import-target/measure.mjs index b66932d77..11ec68ad7 100644 --- a/gitnexus/bench/import-target/measure.mjs +++ b/gitnexus/bench/import-target/measure.mjs @@ -484,6 +484,7 @@ import { resolveSwiftImportTarget } from '../../src/core/ingestion/languages/swi import { resolveRustImportTarget } from '../../src/core/ingestion/languages/rust/import-target.ts'; import { resolveZigImportInternal } from '../../src/core/ingestion/import-resolvers/zig.ts'; import { resolvePythonImportTarget } from '../../src/core/ingestion/languages/python/import-target.ts'; +import { elixirScopeResolver } from '../../src/core/ingestion/languages/elixir/scope-resolver.ts'; import { makeJsResolveImportTarget } from '../../src/core/ingestion/languages/javascript/import-target.ts'; import { makeVueResolveImportTarget } from '../../src/core/ingestion/languages/vue/import-target.ts'; // The two `ScopeResolver`s, not their inner resolvers — see `RESOLVE_HOOK`. @@ -600,6 +601,7 @@ const HEAP_BUDGETED = [ 'go', 'cpp', 'objc', + 'elixir', ]; // javascript, typescript and vue were budgeted here until #2953 and are now // BOUNDED, which is a demotion in gate strength and a promotion in what the @@ -627,7 +629,7 @@ const HEAP_BUDGETED = [ * their hooks declare three or four parameters — so their numbers stay exactly * where they were. */ -const CONTEXT_LANGS = ['php', 'java', 'kotlin', 'python', 'swift']; +const CONTEXT_LANGS = ['php', 'java', 'kotlin', 'python', 'swift', 'elixir']; /** * Needs `node --expose-gc` to force collection for a clean delta; without it @@ -759,6 +761,7 @@ const EXTENSION = { c: '.c', cpp: '.cpp', objc: '.m', + elixir: '.ex', zig: '.zig', }; /** C and C++ resolve `#include` against HEADERS, which reach the resolver @@ -883,6 +886,7 @@ function uniqueDir(lang, d, i) { // `resolutionConfig` load-bearing. Odd `i` is the header. if (lang === 'c' || lang === 'cpp') return i % 2 === 1 ? `include/comp${d}` : `src/comp${d}`; if (lang === 'objc') return `src/comp${d}`; + if (lang === 'elixir') return `lib/App${d}`; if (lang === 'ruby') return `lib/mod${d}`; // One flat `src/mod{d}/` per index and NO nested slice, on purpose: a Zig // import is spelled RELATIVE TO THE IMPORTER, and `uniqueTarget` does not @@ -979,6 +983,7 @@ function collideDir(lang, d, i) { if (lang === 'vue') return `src/pkg${d}/components`; if (lang === 'c' || lang === 'cpp') return i % 2 === 1 ? `svc${d}/include` : `svc${d}/src`; if (lang === 'objc') return `svc${d}/src`; + if (lang === 'elixir') return `lib/Svc${d}/App`; if (lang === 'ruby') return `svc${d}/lib/models`; // Rust's reasoning, verbatim: the resolver walks path components and probes // `.has()`, never searches, so file count is not an axis its cost has and a @@ -1408,6 +1413,9 @@ function uniqueTarget(lang, { local, r, d, j, dirs }) { if (lang === 'objc') { return local ? `comp${j % dirs}/file${j}.m` : `vendor${(r >>> 4) % 97}/missing.m`; } + if (lang === 'elixir') { + return local ? `App${j % dirs}.file${j}` : `Vendor${(r >>> 4) % 97}.Missing`; + } if (lang === 'ruby') { return local ? `mod${d}/file${j}` @@ -1641,6 +1649,9 @@ function collideTarget(lang, { local, r, d, j, dirs }) { if (lang === 'objc') { return local ? `src/file${j}.m` : `vendor${(r >>> 4) % 97}/missing.m`; } + if (lang === 'elixir') { + return local ? `App.file${j}` : `Vendor${(r >>> 4) % 97}.Missing`; + } if (lang === 'ruby') { // `models/mod{n}.rb` in every package. Ruby answers `require` from a keyed // suffix map, so the repeated basename cannot grow a bucket: this arm @@ -1926,6 +1937,15 @@ function resolveOne(lang, from, target, pass) { if (lang === 'objc') { return objectiveCScopeResolver.resolveImportTarget(target, from, allFilePaths, pass.config); } + if (lang === 'elixir') { + return elixirScopeResolver.resolveImportTarget( + target, + from, + allFilePaths, + pass.config, + contextFor(pass, { kind: 'namespace', localName: 'X', importedName: 'X', targetRaw: target }), + ); + } if (lang === 'csharp' || lang === 'csharp_csproj') { return resolveCsharpImportTarget( { kind: 'namespace', localName: '_', importedName: '_', targetRaw: target }, @@ -2147,6 +2167,7 @@ const HEAP_PROBE_TARGET = { python: 'vendor0.deep.missing', c: 'vendor0/missing.h', objc: 'vendor0/missing.m', + elixir: 'Vendor0.Missing', // The entries below cover the BOUNDED tier — see `HEAP_BOUNDED`, which // derives to cobol, swift and rust; the rest were promoted. Same rule as the // budgeted ones above: a spelling `uniqueTarget` already mints for that language, and @@ -2346,6 +2367,14 @@ const CONTEXT_PROBE = { probeFile('Sources/Client/Main.swift', [['Class', 'Client.Main']]), ], }, + elixir: { + from: 'lib/Client.ex', + target: 'Demo.Target', + parsedFiles: [ + probeFile('lib/Client.ex', [['Class', 'Client']]), + probeFile('lib/Target.ex', [['Class', 'Demo.Target']]), + ], + }, }; /** Resolve the probe twice through `resolveOne` — once with the pass's parsed @@ -2430,6 +2459,7 @@ const LANG_REGISTRY = { c: SupportedLanguages.C, cpp: SupportedLanguages.CPlusPlus, objc: SupportedLanguages.ObjectiveC, + elixir: SupportedLanguages.Elixir, zig: SupportedLanguages.Zig, }; const LANGS = Object.keys(LANG_REGISTRY); diff --git a/gitnexus/package-lock.json b/gitnexus/package-lock.json index 300a0cfb9..5a690880d 100644 --- a/gitnexus/package-lock.json +++ b/gitnexus/package-lock.json @@ -41,6 +41,7 @@ "tree-sitter": "0.21.1", "tree-sitter-c-sharp": "0.23.1", "tree-sitter-cpp": "0.23.2", + "tree-sitter-elixir": "^0.3.5", "tree-sitter-go": "^0.23.0", "tree-sitter-java": "^0.23.5", "tree-sitter-javascript": "^0.23.0", @@ -4805,6 +4806,26 @@ } } }, + "node_modules/tree-sitter-elixir": { + "version": "0.3.5", + "resolved": "https://registry.npmjs.org/tree-sitter-elixir/-/tree-sitter-elixir-0.3.5.tgz", + "integrity": "sha512-xozQMvYK0aSolcQZAx2d84Xe/YMWFuRPYFlLVxO01bM2GITh5jyiIp0TqPCQa8754UzRAI7A83hZmfiYub5TZQ==", + "hasInstallScript": true, + "license": "Apache-2.0", + "dependencies": { + "node-addon-api": "^7.1.0", + "node-gyp-build": "^4.8.0" + }, + "peerDependencies": { + "tree-sitter": "^0.21.0" + } + }, + "node_modules/tree-sitter-elixir/node_modules/node-addon-api": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-7.1.1.tgz", + "integrity": "sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==", + "license": "MIT" + }, "node_modules/tree-sitter-go": { "version": "0.23.4", "resolved": "https://registry.npmjs.org/tree-sitter-go/-/tree-sitter-go-0.23.4.tgz", diff --git a/gitnexus/package.json b/gitnexus/package.json index c5ab6f3c6..5ff1ccc68 100644 --- a/gitnexus/package.json +++ b/gitnexus/package.json @@ -96,6 +96,7 @@ "tree-sitter": "0.21.1", "tree-sitter-c-sharp": "0.23.1", "tree-sitter-cpp": "0.23.2", + "tree-sitter-elixir": "^0.3.5", "tree-sitter-go": "^0.23.0", "tree-sitter-java": "^0.23.5", "tree-sitter-javascript": "^0.23.0", diff --git a/gitnexus/src/config/ignore-service.ts b/gitnexus/src/config/ignore-service.ts index ac2dc7704..4156535c2 100644 --- a/gitnexus/src/config/ignore-service.ts +++ b/gitnexus/src/config/ignore-service.ts @@ -390,6 +390,9 @@ export const isHardcodedIgnoredDirectoryAtPath = ( const relative = nodePath.relative(repoRoot, directoryPath).replace(/\\/g, '/'); if (isRootArtifactDirectory(relative, name)) return true; + if (name === 'deps' && relative === 'deps' && existsSync(nodePath.join(repoRoot, 'mix.exs'))) + return true; + return name === 'env' && existsSync(nodePath.join(directoryPath, 'pyvenv.cfg')); }; diff --git a/gitnexus/src/core/embeddings/ast-utils.ts b/gitnexus/src/core/embeddings/ast-utils.ts index b29ca80fb..12a9ec69e 100644 --- a/gitnexus/src/core/embeddings/ast-utils.ts +++ b/gitnexus/src/core/embeddings/ast-utils.ts @@ -44,6 +44,25 @@ export const ensureAndParse = async (content: string, filePath: string): Promise return parseSourceSafe(parserInstance, parseContent); }; +const ELIXIR_MODULE_CALL_KEYWORDS: Record = { + defmodule: true, + defprotocol: true, +}; + +type DeclarationCandidateNode = { + type: string; + childForFieldName?: (fieldName: string) => { type: string; text: string } | null; +}; + +const isElixirModuleCall = (node: DeclarationCandidateNode): boolean => { + const target = node.childForFieldName?.('target'); + return ( + node.type === 'call' && + target?.type === 'identifier' && + ELIXIR_MODULE_CALL_KEYWORDS[target.text] === true + ); +}; + const FUNCTION_LIKE_TYPES = new Set([ 'function_declaration', 'function_definition', @@ -117,7 +136,7 @@ export const findDeclarationNode = (root: any): any | null => { const stack = [root]; while (stack.length > 0) { const node = stack.pop()!; - if (CLASS_LIKE_TYPES.has(node.type)) return node; + if (CLASS_LIKE_TYPES.has(node.type) || isElixirModuleCall(node)) return node; for (let i = node.namedChildCount - 1; i >= 0; i--) { const child = node.namedChild(i); if (child) stack.push(child); diff --git a/gitnexus/src/core/ingestion/cfg/visitors/elixir.ts b/gitnexus/src/core/ingestion/cfg/visitors/elixir.ts new file mode 100644 index 000000000..40424fbb3 --- /dev/null +++ b/gitnexus/src/core/ingestion/cfg/visitors/elixir.ts @@ -0,0 +1,537 @@ +/** + * Elixir CFG visitor. Elixir's `def` clauses are deliberately kept as separate + * CFGs: the semantic graph coalesces their callable identity, while each clause + * retains its own guards and control dependencies. + * + * This is intentionally structural. A receive is a local branch (there is no + * process/message-delivery edge), and anonymous functions are collected as + * independent closures by `collectFunctionCfgs`. + */ +import type { SyntaxNode } from '../../utils/ast-helpers.js'; +import { CfgBuilder } from '../cfg-builder.js'; +import type { + BindingEntry, + CfgVisitor, + FunctionCfg, + SiteRecord, + StatementFacts, +} from '../types.js'; + +const FUNCTION_TYPES = new Set(['call', 'anonymous_function']); +const start = (n: SyntaxNode) => n.startPosition.row + 1; +const end = (n: SyntaxNode) => n.endPosition.row + 1; + +function isFunction(node: SyntaxNode): boolean { + if (node.type === 'anonymous_function') return true; + return node.type === 'call' && /^(def|defp|defmacro|defmacrop)\b/.test(node.text); +} + +function bodyOf(node: SyntaxNode): SyntaxNode | undefined { + const doBlock = node.namedChildren.find((child) => child.type === 'do_block'); + if (doBlock) return doBlock; + // Keep every `fn` clause in the closure CFG. Picking the first stab clause + // silently drops calls and taint sites in later pattern-match alternatives. + if (node.type === 'anonymous_function') return node; + // `def f(x), do: expr` has no do_block; its keyword pair is still a distinct + // clause body and must receive its own CFG. + return node.namedChildren + .find((child) => child.type === 'arguments') + ?.namedChildren.find((child) => child.type === 'keywords') + ?.namedChildren.find((child) => child.type === 'pair') + ?.namedChildren.at(-1); +} + +/** + * Small, grammar-shaped fact collector. Elixir represents both `=` matches and + * guards as `binary_operator`; only the left side of a match introduces a + * binding. A pinned identifier is deliberately a read. This is kept here + * rather than borrowing a JS harvester: names and call/member syntax differ. + */ +class ElixirHarvester { + private readonly bindings: BindingEntry[] = []; + private readonly byName = new Map(); + + constructor(fn: SyntaxNode) { + const args = fn.namedChildren.find((n) => n.type === 'arguments'); + // The definition arguments hold a signature call (`def f(value)`), whose + // own arguments are the formal bindings. A guarded head wraps that call + // in a binary operator. + if (args) { + let formalIndex = 0; + for (const child of args.namedChildren) { + const signature = + child.type === 'call' + ? child + : child.type === 'binary_operator' && /\bwhen\b/.test(child.text) + ? child.namedChildren.find((node) => node.type === 'call') + : undefined; + const patterns = signature?.namedChildren.find((node) => node.type === 'arguments'); + for (const pattern of patterns?.namedChildren ?? []) { + this.declareFormalPattern(pattern, formalIndex); + formalIndex++; + } + } + } + } + + private bind( + name: string, + node: SyntaxNode, + kind: BindingEntry['kind'] = 'var', + formalIndex?: number, + ): number { + const existing = this.byName.get(name); + if (existing !== undefined) return existing; + const idx = this.bindings.length; + this.bindings.push({ + name, + kind, + declLine: start(node), + declColumn: node.startPosition.column, + ...(formalIndex === undefined ? {} : { formalIndex }), + }); + this.byName.set(name, idx); + return idx; + } + + declareClauseParams(clause: SyntaxNode): void { + const body = clause.namedChildren.find((child) => child.type === 'body'); + let formalIndex = 0; + for (const pattern of clause.namedChildren) { + if (pattern === body) continue; + this.declareFormalPattern(pattern, formalIndex); + formalIndex++; + } + } + + /** Bind the pattern side of a formal only; defaults and guards are expressions. */ + private declareFormalPattern(pattern: SyntaxNode, formalIndex: number): void { + const bindPattern = (node: SyntaxNode): void => { + if (node.type === 'identifier') { + if (node.text !== '_') this.bind(node.text, node, 'param', formalIndex); + return; + } + // Defaults bind only their left pattern; never their RHS expression. + if (node.type === 'binary_operator' && node.text.includes('\\')) { + const left = node.childForFieldName?.('left') ?? node.namedChild(0); + if (left) bindPattern(left as SyntaxNode); + return; + } + for (const child of node.namedChildren) bindPattern(child); + }; + bindPattern(pattern); + } + + private read(name: string, node: SyntaxNode): number { + return this.byName.get(name) ?? this.bind(name, node, 'var'); + } + + private identifiers(node: SyntaxNode, excludeBodies = false): SyntaxNode[] { + const out: SyntaxNode[] = []; + const walk = (n: SyntaxNode): void => { + if (n !== node && isFunction(n)) return; + if (n.type === 'identifier' && n.text !== '_' && !/^(do|end|when)$/.test(n.text)) out.push(n); + const callTarget = n.type === 'call' ? n.childForFieldName?.('target') : undefined; + for (const c of n.namedChildren) + if ( + c !== callTarget && + !( + excludeBodies && + ['do_block', 'else_block', 'rescue_block', 'catch_block', 'after_block'].includes( + c.type, + ) + ) + ) + walk(c); + }; + walk(node); + return out; + } + + facts(node: SyntaxNode, header = false): StatementFacts { + const defs: number[] = [], + uses: number[] = []; + const match = + node.type === 'binary_operator' && + /^\s*(=|<-)\s*$/.test(node.children.find((c) => !c.isNamed)?.text ?? ''); + const left = match ? node.namedChildren[0] : undefined; + const leftIdentifiers: SyntaxNode[] = []; + const pinnedIdentifiers = new Set(); + if (left) { + const collectLeft = (n: SyntaxNode, pinned = false): void => { + if (n.type === 'identifier' && n.text !== '_') { + leftIdentifiers.push(n); + if (pinned) pinnedIdentifiers.add(n); + } + if (n.type === 'unary_operator' && n.text.startsWith('^')) { + for (const child of n.namedChildren) collectLeft(child, true); + return; + } + for (const child of n.namedChildren) collectLeft(child, pinned); + }; + collectLeft(left); + } + if (left) + for (const identifier of leftIdentifiers) { + if (pinnedIdentifiers.has(identifier)) uses.push(this.read(identifier.text, identifier)); + else defs.push(this.bind(identifier.text, identifier)); + } + const bareCallee = + node.type === 'call' && node.childForFieldName?.('target')?.type === 'identifier' + ? node.childForFieldName('target')!.text + : undefined; + for (const identifier of this.identifiers(node, header)) { + if (identifier.text === bareCallee) continue; + if ( + leftIdentifiers.some( + (leftIdentifier) => + leftIdentifier.startIndex === identifier.startIndex && + leftIdentifier.endIndex === identifier.endIndex, + ) + ) + continue; + uses.push(this.read(identifier.text, identifier)); + } + const sites: SiteRecord[] = []; + const memberNodes: SyntaxNode[] = []; + const callSites = new Map(); + const key = (n: SyntaxNode) => `${n.startIndex}:${n.endIndex}`; + const isPipe = (n: SyntaxNode | null | undefined) => + n?.type === 'binary_operator' && + n.children.find((child) => !child.isNamed)?.text.trim() === '|>'; + // Phoenix exposes request input both through conn.params-like fields and + // directly through the conventional `params["key"]` map argument. Keep + // these as member-read facts: the language-neutral taint matcher owns the + // policy while this visitor only records the syntactic substrate. + const visitAccesses = (n: SyntaxNode): void => { + if (n !== node && isFunction(n)) return; + if (n.type === 'access_call') { + const root = n.namedChildren[0]; + if (root?.type === 'identifier') { + sites.push({ + kind: 'member-read', + object: this.read(root.text, root), + property: root.text, + }); + memberNodes.push(n); + } + } + if (n.type === 'call' && n.parent?.type === 'access_call') { + const dot = n.namedChildren.find((c) => c.type === 'dot'); + const root = dot?.namedChildren[0]; + const property = dot?.namedChildren.at(-1); + if (root?.type === 'identifier' && property?.type === 'identifier') { + sites.push({ + kind: 'member-read', + object: this.read(root.text, root), + property: property.text, + }); + memberNodes.push(n); + } + } + for (const c of n.namedChildren) + if ( + !( + header && + ['do_block', 'else_block', 'rescue_block', 'catch_block', 'after_block'].includes( + c.type, + ) + ) + ) + visitAccesses(c); + }; + visitAccesses(node); + const visitCalls = (n: SyntaxNode): void => { + if (n !== node && isFunction(n)) return; + if (n.type === 'call') { + const dot = n.namedChildren.find((c) => c.type === 'dot'); + const callee = dot?.text || n.namedChildren.find((c) => c.type === 'identifier')?.text; + if ( + callee && + !/^(if|unless|case|cond|with|try|receive|for|def|defp|defmacro|defmacrop)$/.test(callee) + ) { + const argumentsNode = n.namedChildren.find((c) => c.type === 'arguments'); + const args = argumentsNode?.namedChildren.map((arg) => [ + ...new Set( + this.identifiers(arg).map((identifier) => this.read(identifier.text, identifier)), + ), + ]); + const pipeLeft = isPipe(n.parent) ? n.parent.namedChildren[0] : undefined; + if (pipeLeft) { + const callArgs = args ?? []; + callArgs.unshift([ + ...new Set( + this.identifiers(pipeLeft).map((identifier) => + this.read(identifier.text, identifier), + ), + ), + ]); + sites.push({ + kind: 'call', + callee, + ...(callArgs.some((arg) => arg.length) ? { args: callArgs } : {}), + at: [start(n), n.startPosition.column], + }); + callSites.set(key(n), sites.length - 1); + for (const child of n.namedChildren) visitCalls(child); + return; + } + let parent: [number, number] | undefined; + for (let ancestor = n.parent; ancestor; ancestor = ancestor.parent) { + if (ancestor.type !== 'call') continue; + const parentIndex = callSites.get(key(ancestor)) ?? -1; + const parentArgs = ancestor.namedChildren.find((child) => child.type === 'arguments'); + const rawIndex = + parentArgs?.namedChildren.findIndex( + (arg) => arg.startIndex <= n.startIndex && arg.endIndex >= n.endIndex, + ) ?? -1; + if (rawIndex >= 0 && parentIndex >= 0) + parent = [parentIndex, rawIndex + (isPipe(ancestor.parent) ? 1 : 0)]; + break; + } + sites.push({ + kind: 'call', + callee, + ...(args?.some((arg) => arg.length) ? { args } : {}), + ...(parent ? { parent } : {}), + at: [start(n), n.startPosition.column], + }); + callSites.set(key(n), sites.length - 1); + } + } + for (const c of n.namedChildren) + if ( + !( + header && + ['do_block', 'else_block', 'rescue_block', 'catch_block', 'after_block'].includes( + c.type, + ) + ) + ) + visitCalls(c); + }; + visitCalls(node); + for (let i = 0; i < memberNodes.length; i++) { + const member = memberNodes[i]!; + const pipe = isPipe(member.parent) ? member.parent : undefined; + const pipedCall = pipe?.namedChildren[1]; + if (pipedCall?.type === 'call') { + const parentIndex = callSites.get(key(pipedCall)) ?? -1; + if (parentIndex >= 0) { + sites[i] = { ...sites[i]!, parent: [parentIndex, 0] }; + continue; + } + } + for (let ancestor = member.parent; ancestor; ancestor = ancestor.parent) { + if (ancestor.type !== 'call') continue; + const args = ancestor.namedChildren.find((child) => child.type === 'arguments'); + const argIndex = + args?.namedChildren.findIndex( + (arg) => + arg === member || + (arg.startIndex <= member.startIndex && arg.endIndex >= member.endIndex), + ) ?? -1; + if (argIndex >= 0) { + const parentIndex = callSites.get(key(ancestor)) ?? -1; + if (parentIndex >= 0) + sites[i] = { + ...sites[i]!, + parent: [parentIndex, argIndex + (isPipe(ancestor.parent) ? 1 : 0)], + }; + } + break; + } + } + return { + line: start(node), + defs: [...new Set(defs)], + uses: [...new Set(uses)], + ...(sites.length ? { sites } : {}), + }; + } + + get all(): readonly BindingEntry[] { + return this.bindings; + } +} + +function buildFunctionCfg(fn: SyntaxNode, filePath: string): FunctionCfg | undefined { + if (!isFunction(fn)) return undefined; + const body = bodyOf(fn); + if (!body) return undefined; + const builder = new CfgBuilder(filePath, start(fn), end(fn), fn.startPosition.column); + const harvest = new ElixirHarvester(fn); + const statementsOf = (n: SyntaxNode): SyntaxNode[] => + n.namedChildren.filter( + (child) => + !['comment', 'else_block', 'rescue_block', 'catch_block', 'after_block'].includes( + child.type, + ), + ); + const block = (node: SyntaxNode, header = false) => + builder.newBlock(start(node), end(node), node.text, 'normal', harvest.facts(node, header)); + const keyword = (node: SyntaxNode): string | undefined => { + if (node.type !== 'call') return undefined; + const name = node.namedChildren.find((child) => child.type === 'identifier')?.text; + return name && /^(if|unless|case|cond|with|try|receive|for)$/.test(name) ? name : undefined; + }; + const clauseBody = (clause: SyntaxNode): SyntaxNode[] => + statementsOf(clause.namedChildren.find((child) => child.type === 'body') ?? clause).filter( + (child) => child.type !== 'arguments', + ); + + const visitSeq = (nodes: readonly SyntaxNode[], incoming: readonly number[]): number[] => + builder.withNesting(() => { + let exits = [...incoming]; + for (const node of nodes) exits = visit(node, exits); + return exits; + }); + const visitClauses = (clauses: readonly SyntaxNode[], header: number, join: number): void => { + let fallback = header; + for (const clause of clauses) { + const guard = block(clause); + builder.edge(fallback, guard, 'cond-false'); + builder.edge(header, guard, 'cond-true'); + const exits = visitSeq(clauseBody(clause), [guard]); + builder.connect(exits, join, 'seq'); + fallback = guard; + } + builder.edge(fallback, join, 'cond-false'); + }; + const visit = (node: SyntaxNode, incoming: readonly number[]): number[] => + builder.withNesting(() => { + if ( + node.type === 'anonymous_function' && + (node.startIndex !== fn.startIndex || node.endIndex !== fn.endIndex) + ) + return [...incoming]; + const form = keyword(node); + if (!form && node.type === 'binary_operator' && /\b(and|or)\b|&&|\|\|/.test(node.text)) { + const control = block(node); + builder.connect(incoming, control); + const join = builder.newBlock(end(node), end(node), '', 'normal'); + builder.edge(control, join, 'cond-false'); + builder.edge(control, join, 'cond-true'); + return [join]; + } + if (!form) { + const normal = block(node); + builder.connect(incoming, normal); + return [normal]; + } + const header = block(node, true); + builder.connect(incoming, header); + const join = builder.newBlock(end(node), end(node), '', 'normal'); + const doBlock = node.namedChildren.find((child) => child.type === 'do_block'); + const elseBlock = doBlock?.namedChildren.find((child) => child.type === 'else_block'); + const clauses = doBlock?.namedChildren.filter((child) => child.type === 'stab_clause') ?? []; + + if (form === 'case' || form === 'cond' || form === 'receive') { + visitClauses(clauses, header, join); + const timeout = doBlock?.namedChildren.find((child) => child.type === 'after_block'); + if (timeout) { + const timeoutExits = visitSeq( + timeout.namedChildren + .filter((child) => child.type === 'stab_clause') + .flatMap(clauseBody), + [header], + ); + builder.connect(timeoutExits, join, 'seq'); + } + return [join]; + } + if (form === 'try') { + const main = visitSeq(statementsOf(doBlock ?? node), [header]); + builder.connect(main, join, 'seq'); + for (const handler of doBlock?.namedChildren.filter( + (child) => child.type === 'rescue_block' || child.type === 'catch_block', + ) ?? []) { + visitClauses( + handler.namedChildren.filter((child) => child.type === 'stab_clause'), + header, + join, + ); + } + const after = doBlock?.namedChildren.find((child) => child.type === 'after_block'); + if (after) { + const afterExits = visitSeq(statementsOf(after), [join]); + const afterJoin = builder.newBlock(end(after), end(after), '', 'normal'); + builder.connect(afterExits, afterJoin, 'seq'); + return [afterJoin]; + } + return [join]; + } + if (form === 'with' || form === 'for') { + const argumentsNode = node.namedChildren.find((child) => child.type === 'arguments'); + let generator = header; + for (const part of argumentsNode?.namedChildren.filter( + (child) => child.type !== 'keywords', + ) ?? []) { + const check = block(part); + builder.edge(generator, check, 'cond-true'); + builder.edge(generator, join, 'cond-false'); + generator = check; + } + const bodyExits = visitSeq(statementsOf(doBlock ?? node), [generator]); + builder.connect(bodyExits, join, 'seq'); + if (elseBlock) + visitClauses( + elseBlock.namedChildren.filter((child) => child.type === 'stab_clause'), + header, + join, + ); + return [join]; + } + const main = visitSeq(statementsOf(doBlock ?? node), [header]); + builder.connect(main, join, 'cond-true'); + if (elseBlock) { + const alternate = visitSeq(statementsOf(elseBlock), [header]); + builder.connect(alternate, join, 'cond-false'); + } else builder.edge(header, join, 'cond-false'); + return [join]; + }); + + // A guarded clause can reject before its body runs. Keep that decision in + // this clause's CFG instead of merging it with sibling clauses: the semantic + // graph owns canonical callable identity, while CFG remains clause-local. + const headGuard = fn.namedChildren + .find((child) => child.type === 'arguments') + ?.namedChildren.find( + (child) => child.type === 'binary_operator' && /\bwhen\b/.test(child.text), + ); + let entry = builder.entryIndex; + if (headGuard) { + const guard = block(headGuard); + builder.edge(entry, guard, 'cond-true'); + builder.edge(entry, builder.exitIndex, 'cond-false'); + entry = guard; + } + if (fn.type === 'anonymous_function') { + const clauses = fn.namedChildren.filter((child) => child.type === 'stab_clause'); + if (clauses.length) { + let fallback = builder.entryIndex; + const join = builder.newBlock(end(fn), end(fn), '', 'normal'); + for (const clause of clauses) { + harvest.declareClauseParams(clause); + const guard = block(clause); + builder.edge(fallback, guard, 'cond-false'); + builder.edge(builder.entryIndex, guard, 'cond-true'); + const clauseExits = visitSeq(clauseBody(clause), [guard]); + builder.connect(clauseExits, join, 'seq'); + fallback = guard; + } + builder.edge(fallback, join, 'cond-false'); + builder.edge(join, builder.exitIndex, 'seq'); + return builder.finish(harvest.all); + } + } + const exits = visitSeq(body.type === 'do_block' ? statementsOf(body) : [body], [entry]); + builder.connect(exits, builder.exitIndex, 'seq'); + return builder.finish(harvest.all); +} + +export function createElixirCfgVisitor(): CfgVisitor { + return { isFunction, buildFunctionCfg }; +} + +export { FUNCTION_TYPES as ELIXIR_FUNCTION_TYPES }; diff --git a/gitnexus/src/core/ingestion/class-extractors/configs/elixir.ts b/gitnexus/src/core/ingestion/class-extractors/configs/elixir.ts new file mode 100644 index 000000000..4681ed7cf --- /dev/null +++ b/gitnexus/src/core/ingestion/class-extractors/configs/elixir.ts @@ -0,0 +1,47 @@ +// gitnexus/src/core/ingestion/class-extractors/configs/elixir.ts + +import { SupportedLanguages } from 'gitnexus-shared'; +import type { ClassExtractionConfig } from '../../class-types.js'; +import type { SyntaxNode } from '../../utils/ast-helpers.js'; + +const ELIXIR_MODULE_KEYWORDS = new Set(['defmodule', 'defprotocol']); + +/** Return the `target` identifier text of a call node, or undefined. */ +function callKeyword(node: SyntaxNode): string | undefined { + const t = node.childForFieldName?.('target'); + return t?.type === 'identifier' ? t.text : undefined; +} + +/** Find the arguments node among named children. */ +function findArguments(node: SyntaxNode): SyntaxNode | null { + for (let i = 0; i < node.namedChildCount; i++) { + const c = node.namedChild(i); + if (c?.type === 'arguments') return c; + } + return null; +} + +export const elixirClassConfig: ClassExtractionConfig = { + language: SupportedLanguages.Elixir, + + // defmodule / defprotocol are the class-like nodes (both are `call` nodes in tree-sitter) + typeDeclarationNodes: ['call'], + + extractName(node) { + if (!ELIXIR_MODULE_KEYWORDS.has(callKeyword(node) ?? '')) return undefined; + const args = findArguments(node); + if (!args) return undefined; + for (let i = 0; i < args.namedChildCount; i++) { + const a = args.namedChild(i); + if (a?.type === 'alias') return a.text; + } + return undefined; + }, + + extractType(node) { + const kw = callKeyword(node); + if (kw === 'defprotocol') return 'Interface'; + if (kw === 'defmodule') return 'Class'; + return undefined; + }, +}; diff --git a/gitnexus/src/core/ingestion/export-detection.ts b/gitnexus/src/core/ingestion/export-detection.ts index 143358c6f..83ba81470 100644 --- a/gitnexus/src/core/ingestion/export-detection.ts +++ b/gitnexus/src/core/ingestion/export-detection.ts @@ -242,7 +242,6 @@ export const rubyExportChecker: ExportChecker = (_node, _name) => true; /** Dart: public if no leading underscore (convention, same as Python). */ export const dartExportChecker: ExportChecker = (_node, name) => !name.startsWith('_'); - /** Zig declaration node types whose `pub` / `export` keyword child marks the symbol public. */ const ZIG_DECL_TYPES = new Set(['function_declaration', 'variable_declaration']); @@ -303,3 +302,24 @@ export function hasZigPubKeyword(declNode: SyntaxNode): boolean { } return false; } +const ELIXIR_PRIVATE_KEYWORDS = new Set(['defp', 'defmacrop', 'defguardp']); +const ELIXIR_PUBLIC_KEYWORDS = new Set(['def', 'defmacro', 'defguard', 'defdelegate']); + +/** + * Elixir: def = public, defp = private. Walk up to the enclosing def/defp call + * to determine visibility. defmodule/defprotocol/defimpl are always public. + */ +export const elixirExportChecker: ExportChecker = (node, _name) => { + let current: SyntaxNode | null = node; + while (current) { + if (current.type === 'call') { + const target = current.childForFieldName?.('target'); + if (target?.type === 'identifier') { + if (ELIXIR_PRIVATE_KEYWORDS.has(target.text)) return false; + if (ELIXIR_PUBLIC_KEYWORDS.has(target.text)) return true; + } + } + current = current.parent; + } + return true; +}; diff --git a/gitnexus/src/core/ingestion/import-resolvers/configs/elixir.ts b/gitnexus/src/core/ingestion/import-resolvers/configs/elixir.ts new file mode 100644 index 000000000..979e67e3d --- /dev/null +++ b/gitnexus/src/core/ingestion/import-resolvers/configs/elixir.ts @@ -0,0 +1,82 @@ +/** + * Elixir import resolution config. + * + * Elixir modules are identified by atoms (capitalized names, e.g. MyApp.User). + * By convention, `MyApp.User` maps to `lib/my_app/user.ex` (or `.exs`). + * Each dotted segment is snake_cased independently. + * + * Handles: import, alias, use, require directives. + * External/hex dependencies (no slash in module path context) return empty. + */ + +import { SupportedLanguages } from 'gitnexus-shared'; +import type { ImportResolutionConfig, ImportResolverStrategy } from '../types.js'; + +/** + * Convert a PascalCase module name segment to snake_case. + * HTTPClient → http_client, MyApp → my_app, XmlParser → xml_parser + */ +function toSnakeCase(segment: string): string { + return segment + .replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2') + .replace(/([a-z\d])([A-Z])/g, '$1_$2') + .toLowerCase(); +} + +/** + * Convert a dotted Elixir module alias to a relative file path. + * MyApp.User → my_app/user + * Elixir.MyApp.User → elixir/my_app/user (Erlang-namespaced modules) + */ +function moduleToRelPath(moduleName: string): string { + return moduleName.split('.').map(toSnakeCase).join('/'); +} + +const ELIXIR_EXTS = ['.ex', '.exs']; + +function expandGroupedAlias(moduleName: string): readonly string[] { + const grouped = /^(.*)\.\{([^}]+)\}$/.exec(moduleName); + if (!grouped) return [moduleName]; + const [, prefix, members] = grouped; + return members + .split(',') + .map((member) => member.trim()) + .filter(Boolean) + .map((member) => `${prefix}.${member}`); +} + +/** Elixir module alias → file path strategy. */ +const elixirModuleStrategy: ImportResolverStrategy = (rawImportPath, _filePath, ctx) => { + const moduleName = rawImportPath.trim(); + + const moduleNames = expandGroupedAlias(moduleName); + // Must start with a capital letter (Elixir module alias convention). + if (!moduleName || moduleNames.some((name) => !/^[A-Z]/.test(name))) return null; + + // Try common Elixir project roots: lib/, test/, apps/*/lib/, apps/*/test/ + const prefixes = ['lib/', 'test/', '']; + const files: string[] = []; + + for (const name of moduleNames) { + const relPath = moduleToRelPath(name); + const resolvedAt = files.length; + for (const prefix of prefixes) { + for (const ext of ELIXIR_EXTS) { + const candidate = `${prefix}${relPath}${ext}`; + const exact = ctx.allFileList.find((fp) => fp === candidate); + const fallback = ctx.allFileList.find((fp) => fp.endsWith(`/${candidate}`)); + if (exact ?? fallback) files.push(exact ?? fallback!); + } + // Preserve the existing root-priority rule for each expanded alias. + if (files.length > resolvedAt) break; + } + } + + // No local file found — likely an external hex dependency + return { kind: 'files', files: [...new Set(files)] }; +}; + +export const elixirImportConfig: ImportResolutionConfig = { + language: SupportedLanguages.Elixir, + strategies: [elixirModuleStrategy], +}; diff --git a/gitnexus/src/core/ingestion/languages/elixir.ts b/gitnexus/src/core/ingestion/languages/elixir.ts new file mode 100644 index 000000000..35a134cf3 --- /dev/null +++ b/gitnexus/src/core/ingestion/languages/elixir.ts @@ -0,0 +1,1041 @@ +/** + * Elixir Language Provider + * + * Key Elixir traits: + * - importSemantics: 'wildcard-leaf' (import/use bring all public functions into scope) + * - mroStrategy: 'none' (functional language — no class inheritance) + * - heritageDefaultEdge: 'IMPLEMENTS' (protocol/behaviour relationships) + * - Modules defined with defmodule, functions with def/defp + * - Module names are atoms: MyApp.User → lib/my_app/user.ex by convention + */ + +import { SupportedLanguages, type NodeLabel } from 'gitnexus-shared'; +import type { AstFrameworkPatternConfig } from '../language-provider.js'; +import { defineLanguage } from '../language-provider.js'; +import { typeConfig as elixirTypeConfig } from '../type-extractors/elixir.js'; +import { elixirExportChecker } from '../export-detection.js'; +import { createImportResolver } from '../import-resolvers/resolver-factory.js'; +import { elixirImportConfig } from '../import-resolvers/configs/elixir.js'; +import { ELIXIR_QUERIES } from '../tree-sitter-queries.js'; +import { createMethodExtractor } from '../method-extractors/generic.js'; +import { createElixirCfgVisitor } from '../cfg/visitors/elixir.js'; +import { elixirMethodConfig } from '../method-extractors/configs/elixir.js'; +import { createClassExtractor } from '../class-extractors/generic.js'; +import { elixirClassConfig } from '../class-extractors/configs/elixir.js'; +import type { CallExtractor, ExtractedCallSite } from '../call-types.js'; +import type { SyntaxNode } from '../utils/ast-helpers.js'; +import { countCallArguments } from '../utils/call-analysis.js'; +import Parser from 'tree-sitter'; +import type { Capture, CaptureMatch, ParsedImport } from 'gitnexus-shared'; +import { getLanguageGrammar } from '../../tree-sitter/parser-loader.js'; +import { parseSourceSafe } from '../../tree-sitter/safe-parse.js'; +import { nodeToCapture } from '../utils/ast-helpers.js'; +import { normalizeExtractedRoutePath } from '../route-extractors/route-path.js'; +import { extractElixirSemanticGraph } from './elixir/semantic-graph.js'; +import { + clearElixirImportExcepts, + collectElixirCaptureSideChannel, + recordElixirFrameworkFacts, + recordElixirImportExcept, + recordElixirImportOnly, + type ElixirFrameworkFact, +} from './elixir/import-filters.js'; + +const ELIXIR_DEF_KEYWORDS = new Set([ + 'def', + 'defp', + 'defmacro', + 'defmacrop', + 'defguard', + 'defguardp', + 'defdelegate', +]); + +const ELIXIR_MODULE_KEYWORDS = new Set(['defmodule', 'defprotocol']); + +const ELIXIR_NON_CALL_KEYWORDS = new Set([ + ...ELIXIR_DEF_KEYWORDS, + ...ELIXIR_MODULE_KEYWORDS, + 'defimpl', + 'defstruct', + 'defoverridable', + 'defexception', + 'callback', + 'import', + 'alias', + 'use', + 'require', + 'case', + 'cond', + 'if', + 'unless', + 'for', + 'with', + 'receive', + 'try', + 'quote', + 'fn', +]); + +function callKeyword(node: SyntaxNode): string | undefined { + const target = node.childForFieldName?.('target'); + return target?.type === 'identifier' ? target.text : undefined; +} + +function findArguments(node: SyntaxNode): SyntaxNode | null { + for (let i = 0; i < node.namedChildCount; i++) { + const child = node.namedChild(i); + if (child?.type === 'arguments') return child; + } + return null; +} + +function firstAliasArgument(node: SyntaxNode): string | undefined { + const args = findArguments(node); + if (!args) return undefined; + for (let i = 0; i < args.namedChildCount; i++) { + const child = args.namedChild(i); + if (child?.type === 'alias') return child.text; + } + return undefined; +} + +function extractFunctionNameFromDef(node: SyntaxNode): string | undefined { + if (!ELIXIR_DEF_KEYWORDS.has(callKeyword(node) ?? '')) return undefined; + const args = findArguments(node); + if (!args) return undefined; + for (let i = 0; i < args.namedChildCount; i++) { + const arg = args.namedChild(i); + if (!arg) continue; + if (arg.type === 'identifier') return arg.text; + if (arg.type === 'call') { + const target = arg.childForFieldName?.('target'); + if (target?.type === 'identifier') return target.text; + } + if (arg.type === 'binary_operator') { + const left = arg.childForFieldName?.('left'); + if (left?.type === 'call') { + const target = left.childForFieldName?.('target'); + if (target?.type === 'identifier') return target.text; + } + } + } + return undefined; +} + +function isInsideDefinitionHead(node: SyntaxNode): boolean { + let current = node.parent; + while (current) { + if (current.type === 'do_block') return false; + if (current.type === 'call' && ELIXIR_DEF_KEYWORDS.has(callKeyword(current) ?? '')) { + const signature = findArguments(current)?.namedChild(0); + return ( + signature !== null && + signature !== undefined && + node.startIndex >= signature.startIndex && + node.endIndex <= signature.endIndex + ); + } + current = current.parent; + } + return false; +} + +function isModuleAttributeCall(node: SyntaxNode): boolean { + return node.parent?.type === 'unary_operator' && node.parent.text.startsWith('@'); +} + +function isInsideModuleAttribute(node: SyntaxNode): boolean { + for ( + let current: SyntaxNode | null | undefined = node; + current; + current = current.parent as SyntaxNode | null + ) + if (isModuleAttributeCall(current)) return true; + return false; +} + +function isBehaviourModule(node: SyntaxNode): boolean { + for (const child of node.namedChildren) { + if (child.type === 'unary_operator' && child.text.startsWith('@callback')) return true; + if (child.type === 'call' && callKeyword(child as SyntaxNode) === 'quote') continue; + if (child.type !== 'call' || callKeyword(child) !== 'defmodule') { + if (isBehaviourModule(child as SyntaxNode)) return true; + } + } + return false; +} + +function elixirString(node: SyntaxNode | undefined): string | undefined { + if (node?.type !== 'string') return undefined; + try { + const value: unknown = JSON.parse(node.text); + return typeof value === 'string' ? value : undefined; + } catch { + return undefined; + } +} + +/** Extract only literal Phoenix/Ecto declarations; this is capture-time data, never macro output. */ +function extractElixirFrameworkFacts(tree: Parser.Tree): readonly ElixirFrameworkFact[] { + const facts: ElixirFrameworkFact[] = []; + const http = new Map([ + ['get', 'GET'], + ['post', 'POST'], + ['put', 'PUT'], + ['patch', 'PATCH'], + ['delete', 'DELETE'], + ['head', 'HEAD'], + ['options', 'OPTIONS'], + ]); + const resourceActions = [ + ['GET', '', 'index'], + ['GET', '/:id', 'show'], + ['GET', '/new', 'new'], + ['GET', '/:id/edit', 'edit'], + ['POST', '', 'create'], + ['PATCH', '/:id', 'update'], + ['PUT', '/:id', 'update'], + ['DELETE', '/:id', 'delete'], + ] as const; + const args = (node: SyntaxNode) => + Array.from( + { length: findArguments(node)?.namedChildCount ?? 0 }, + (_, i) => findArguments(node)!.namedChild(i) as SyntaxNode, + ); + const alias = (node: SyntaxNode | undefined) => (node?.type === 'alias' ? node.text : undefined); + const atom = (node: SyntaxNode | undefined) => + node?.type === 'atom' ? node.text.replace(/^:/, '') : undefined; + const methodAt = (node: SyntaxNode): string | undefined => { + const target = node.childForFieldName?.('target'); + return target?.type === 'dot' && target.childForFieldName?.('right')?.type === 'identifier' + ? target.childForFieldName('right')!.text + : undefined; + }; + const moduleAt = (node: SyntaxNode): string | undefined => { + for (let n = node.parent; n; n = n.parent) + if (n.type === 'call' && callKeyword(n) === 'defmodule') return alias(args(n)[0]); + return undefined; + }; + const isSchemaDeclaration = (node: SyntaxNode): boolean => { + const body = node.parent; + if (body?.type !== 'do_block' || body.parent?.type !== 'call') return false; + const declaration = callKeyword(body.parent); + return declaration === 'schema' || declaration === 'embedded_schema'; + }; + const visit = ( + node: SyntaxNode, + prefix = '', + pipelines: readonly string[] = [], + scopeModule?: string, + ): void => { + if (node.type === 'call' && !isQuotedElixirNode(node)) { + const kind = callKeyword(node); + const values = args(node); + const body = node.namedChildren.find((child) => child?.type === 'do_block') as + | SyntaxNode + | undefined; + if (kind === 'scope' && body) { + const keywordValue = (name: string) => + values + .find((value) => value.type === 'keywords') + ?.namedChildren.find( + (value) => + value.type === 'pair' && + value.namedChild(0)?.text.trim().replace(/:$/, '') === name, + ) + ?.namedChild(1) as SyntaxNode | undefined; + const scopePath = keywordValue('path'); + const scopeAlias = keywordValue('alias'); + const path = elixirString(values[0]) ?? elixirString(scopePath); + const mod = alias(values[1]) ?? alias(scopeAlias) ?? scopeModule; + { + const scopedPipelines = [ + ...pipelines, + ...body.namedChildren.flatMap((child) => { + if (child?.type !== 'call' || callKeyword(child as SyntaxNode) !== 'pipe_through') + return []; + const pipeline = args(child as SyntaxNode)[0]; + return pipeline?.type === 'atom' + ? [pipeline.text.slice(1)] + : pipeline?.type === 'list' + ? pipeline.namedChildren + .filter((entry) => entry.type === 'atom') + .map((entry) => entry.text.slice(1)) + : []; + }), + ]; + for (const child of body.namedChildren) + if ( + child && + (child.type !== 'call' || callKeyword(child as SyntaxNode) !== 'pipe_through') + ) + visit(child as SyntaxNode, `${prefix}${path ?? ''}`, scopedPipelines, mod); + } + return; + } + if ((kind === 'pipeline' || kind === 'live_session') && body) { + for (const child of body.namedChildren) + visit(child as SyntaxNode, prefix, pipelines, scopeModule); + return; + } + if (kind === 'pipe_through') { + const name = values[0]?.type === 'atom' ? values[0].text.slice(1) : undefined; + if (name) pipelines = [...pipelines, name]; + } + const method = kind ? http.get(kind) : undefined; + if (method) { + const path = elixirString(values[0]); + const controller = alias(values[1]); + const action = values[2]?.type === 'atom' ? values[2].text.slice(1) : undefined; + if (path !== undefined && controller && action) + facts.push({ + kind: 'route', + path: normalizeExtractedRoutePath(path, prefix), + method, + handler: + controller.includes('.') || !scopeModule + ? controller + : `${scopeModule}.${controller}`, + action, + line: node.startPosition.row + 1, + pipelines, + }); + } + if (kind === 'resources') { + const path = elixirString(values[0]); + const controller = alias(values[1]); + const keywords = values.find((value) => value.type === 'keywords'); + const filter = (name: string) => { + const value = keywords?.namedChildren + .find( + (entry) => entry.type === 'pair' && entry.namedChild(0)?.text.trim() === `${name}:`, + ) + ?.namedChild(1) as SyntaxNode | undefined; + return value?.type === 'atom' + ? [value.text.slice(1)] + : value?.type === 'list' + ? value.namedChildren + .map(atom) + .filter((entry): entry is string => entry !== undefined) + : undefined; + }; + const only = filter('only'); + const except = filter('except'); + if (path !== undefined && controller) + for (const [verb, suffix, action] of resourceActions) + if ((!only || only.includes(action)) && (!except || !except.includes(action))) + facts.push({ + kind: 'route', + path: normalizeExtractedRoutePath(`${path}${suffix}`, prefix), + method: verb, + handler: + controller.includes('.') || !scopeModule + ? controller + : `${scopeModule}.${controller}`, + action, + line: node.startPosition.row + 1, + pipelines, + }); + } + if (kind === 'live') { + const path = elixirString(values[0]); + const live = alias(values[1]); + if (path !== undefined && live) + facts.push({ + kind: 'route', + path: normalizeExtractedRoutePath(path, prefix), + method: 'GET', + handler: live.includes('.') || !scopeModule ? live : `${scopeModule}.${live}`, + line: node.startPosition.row + 1, + pipelines, + }); + } + const model = moduleAt(node); + if (model && (kind === 'schema' || kind === 'embedded_schema')) { + const table = kind === 'schema' ? elixirString(values[0]) : undefined; + facts.push({ kind: 'schema', model, table, line: node.startPosition.row + 1 }); + } + if ( + model && + isSchemaDeclaration(node) && + [ + 'field', + 'belongs_to', + 'has_one', + 'has_many', + 'many_to_many', + 'embeds_one', + 'embeds_many', + ].includes(kind ?? '') + ) { + const name = atom(values[0]); + const target = alias(values[1]); + if (name) + facts.push({ + kind: 'property', + model, + name, + propertyKind: kind!, + target, + line: node.startPosition.row + 1, + }); + } + if (kind === 'from') { + const keywords = values.find((value) => value.type === 'keywords'); + const inPair = keywords?.namedChildren.find( + (value) => value.type === 'pair' && value.namedChild(0)?.text.trim() === 'in:', + ); + const inExpression = values.find( + (value) => + value.type === 'binary_operator' && + value.childForFieldName?.('left')?.type === 'identifier' && + value.childForFieldName?.('right')?.type === 'alias' && + /\bin\b/.test(value.text), + ); + const model = alias( + (inPair?.namedChild(1) ?? inExpression?.childForFieldName?.('right')) as SyntaxNode, + ); + if (model) + facts.push({ kind: 'query', model, method: 'from', line: node.startPosition.row + 1 }); + } + if ( + remoteReceiver(node) === 'Repo' && + ['all', 'get', 'get_by', 'one', 'insert', 'update', 'delete', 'preload'].includes( + methodAt(node) ?? '', + ) + ) { + const model = + alias(values[0]) ?? alias(node.parent?.childForFieldName?.('left') as SyntaxNode); + if (model) + facts.push({ + kind: 'query', + model, + method: methodAt(node)!, + line: node.startPosition.row + 1, + }); + } + } + for (const child of node.namedChildren) + visit(child as SyntaxNode, prefix, pipelines, scopeModule); + }; + visit(tree.rootNode as SyntaxNode); + return facts; +} + +function remoteReceiver(callNode: SyntaxNode): string | undefined { + const target = callNode.childForFieldName?.('target'); + if (target?.type !== 'dot') return undefined; + const left = target.childForFieldName?.('left'); + return left?.type === 'alias' ? left.text : undefined; +} + +function pipelineArity(callNode: SyntaxNode): number { + const parent = callNode.parent; + return parent?.type === 'binary_operator' && + parent.childForFieldName?.('right') === callNode && + parent.text.includes('|>') + ? countCallArguments(callNode) + 1 + : countCallArguments(callNode); +} + +const elixirCallExtractor: CallExtractor = { + language: SupportedLanguages.Elixir, + + extract(callNode, callNameNode): ExtractedCallSite | null { + if (!callNameNode) return null; + if (isInsideModuleAttribute(callNode) || isInsideDefinitionHead(callNode)) return null; + + const calledName = callNameNode.text; + if (ELIXIR_NON_CALL_KEYWORDS.has(calledName)) return null; + + const receiverName = remoteReceiver(callNode); + return { + calledName, + callForm: receiverName ? 'member' : 'free', + ...(receiverName ? { receiverName } : {}), + argCount: pipelineArity(callNode), + }; + }, +}; + +/** Scope captures deliberately mirror Elixir declarations, never evaluated macro output. */ +function emitElixirScopeCaptures( + source: string, + _file: string, + cachedTree?: unknown, +): readonly CaptureMatch[] { + clearElixirImportExcepts(_file); + const grammar = getLanguageGrammar(SupportedLanguages.Elixir) as Parameters< + Parser['setLanguage'] + >[0]; + const parser = new Parser(); + parser.setLanguage(grammar); + const tree = + (cachedTree as ReturnType | undefined) ?? parseSourceSafe(parser, source); + recordElixirFrameworkFacts(_file, extractElixirFrameworkFacts(tree)); + const query = new Parser.Query( + grammar, + ` + (source) @scope.module + (call target: (identifier) @_ (#eq? @_ "defmodule")) @scope.class + (call target: (identifier) @_ (#match? @_ "^def(macrop?|guardp?|p)?$")) @scope.function + (call target: (identifier) @_ (#eq? @_ "defdelegate")) @scope.function + (call target: (identifier) @_ (#eq? @_ "defmodule") (arguments (alias) @declaration.name)) @declaration.class + (call target: (identifier) @_ (#eq? @_ "defprotocol") (arguments (alias) @declaration.name)) @declaration.interface + (call target: (dot left: (alias) @reference.receiver right: (identifier) @reference.name)) @reference.call.member + (call target: (identifier) @reference.name) @reference.call.free + `, + ); + const captures = query.matches(tree.rootNode).flatMap((match) => { + const referenceName = match.captures.find((capture) => capture.name === 'reference.name') + ?.node as SyntaxNode | undefined; + if ( + referenceName && + (ELIXIR_NON_CALL_KEYWORDS.has(referenceName.text) || + isInsideDefinitionHead(referenceName) || + isQuotedElixirNode(referenceName) || + isElixirCaptureNode(referenceName) || + isInsideModuleAttribute(referenceName)) + ) + return []; + const grouped: Record = {}; + for (const capture of match.captures) { + const tag = `@${capture.name}`; + grouped[tag] = nodeToCapture(tag, capture.node); + } + const moduleDeclaration = match.captures.find((capture) => capture.name === 'declaration.class') + ?.node as SyntaxNode | undefined; + const moduleCall = + moduleDeclaration?.type === 'call' + ? moduleDeclaration + : (moduleDeclaration?.parent?.parent as SyntaxNode | null | undefined); + if (moduleCall && isBehaviourModule(moduleCall)) { + delete grouped['@declaration.class']; + grouped['@declaration.interface'] = nodeToCapture( + '@declaration.interface', + moduleDeclaration, + ); + } + const call = match.captures.find( + (capture) => + capture.name === 'reference.call.free' || capture.name === 'reference.call.member', + )?.node as SyntaxNode | undefined; + if (call) { + const anchor = + grouped['@reference.name'] ?? + grouped['@reference.call.free'] ?? + grouped['@reference.call.member']; + if (anchor) + grouped['@reference.arity'] = { + ...anchor, + name: '@reference.arity', + text: String(pipelineArity(call)), + }; + } + return [grouped]; + }); + const synthetic: CaptureMatch[] = []; + const add = (name: string, node: SyntaxNode, extra: Record = {}) => { + const match: Record = { [name]: nodeToCapture(name, node) }; + for (const [tag, value] of Object.entries(extra)) { + match[tag] = + typeof value === 'string' + ? { ...nodeToCapture(tag, node), text: value, name: tag } + : nodeToCapture(tag, value); + } + synthetic.push(match); + }; + const moduleNameAt = (node: SyntaxNode): string | undefined => { + for (let current = node.parent; current; current = current.parent) { + if (current.type === 'call' && ELIXIR_MODULE_KEYWORDS.has(callKeyword(current) ?? '')) + return firstAliasArgument(current); + } + return undefined; + }; + const keywordValue = (node: SyntaxNode, name: string): SyntaxNode | undefined => { + const args = findArguments(node); + const keywords = args?.namedChildren.find((child) => child?.type === 'keywords'); + const pair = keywords?.namedChildren.find( + (child) => + child?.type === 'pair' && child.namedChild(0)?.text.trim().replace(/:$/, '') === name, + ); + return pair?.namedChild(1) ?? undefined; + }; + const enclosingModule = (node: SyntaxNode): SyntaxNode | undefined => { + for (let current = node.parent; current; current = current.parent) { + if (current.type === 'call' && callKeyword(current) === 'defmodule') return current; + } + return undefined; + }; + const resolveAliasReceiver = (node: SyntaxNode, receiver: SyntaxNode): string | undefined => { + const [local, ...suffix] = receiver.text.split('.'); + const owner = enclosingModule(node); + if (!local || suffix.length === 0 || !owner) return undefined; + let resolved: string | undefined; + const contains = (ancestor: SyntaxNode, descendant: SyntaxNode): boolean => { + for ( + let current: SyntaxNode | null | undefined = descendant; + current; + current = current.parent + ) + if (current.startIndex === ancestor.startIndex && current.endIndex === ancestor.endIndex) + return true; + return false; + }; + const visibleAt = (candidate: SyntaxNode): boolean => { + for (let current = candidate.parent; current; current = current.parent) { + if (!contains(current as SyntaxNode, node)) return false; + if (current.startIndex === owner.startIndex && current.endIndex === owner.endIndex) + return true; + } + return false; + }; + const visitAliases = (candidate: SyntaxNode): void => { + if ( + candidate.type === 'call' && + callKeyword(candidate) === 'alias' && + candidate.startIndex < node.startIndex && + visibleAt(candidate) + ) { + const target = findArguments(candidate)?.namedChild(0); + const as = keywordValue(candidate, 'as'); + if ( + target?.type === 'alias' && + (as?.text.replace(/^:/, '') ?? target.text.split('.').at(-1)) === local + ) + resolved = target.text; + } + for (const child of candidate.namedChildren) visitAliases(child as SyntaxNode); + }; + visitAliases(tree.rootNode as SyntaxNode); + return resolved ? `${resolved}.${suffix.join('.')}` : undefined; + }; + const visit = (node: SyntaxNode): void => { + if (node.type === 'call' && !isQuotedElixirNode(node)) { + const kind = callKeyword(node); + const args = findArguments(node); + const signature = args?.namedChildren.find( + (child) => child?.type === 'call' || child?.type === 'identifier', + ); + if (kind && ELIXIR_DEF_KEYWORDS.has(kind) && signature) { + const name = + signature.type === 'call' ? signature.childForFieldName?.('target') : signature; + if (name?.type === 'identifier') { + const parameters = signature.type === 'call' ? findArguments(signature) : null; + const count = parameters?.namedChildCount ?? 0; + const required = + parameters === null + ? 0 + : Array.from({ length: parameters.namedChildCount }, (_, i) => + parameters.namedChild(i), + ).filter((p) => p?.type !== 'binary_operator' || !p.text.includes('\\\\')).length; + const owner = moduleNameAt(node); + const declarationKind = + kind === 'defmacro' || + kind === 'defmacrop' || + kind === 'defguard' || + kind === 'defguardp' + ? 'macro' + : 'function'; + add(`@declaration.${declarationKind}`, node, { + '@declaration.name': name, + '@declaration.qualified_name': owner ? `${owner}.${name.text}` : name.text, + '@declaration.parameter-count': String(count), + '@declaration.required-parameter-count': String(required), + '@declaration.is-exported': + kind === 'defp' || kind === 'defmacrop' || kind === 'defguardp' ? 'false' : 'true', + }); + if (kind === 'defdelegate') { + const target = keywordValue(node, 'to'); + const delegated = keywordValue(node, 'as'); + if (target?.type === 'alias') + add('@reference.call.member', node, { + '@reference.receiver': target, + '@reference.name': delegated?.text.replace(/^:/, '') ?? name, + '@reference.arity': String(count), + }); + } + } + } else if (kind === 'alias' && args) { + const target = args.namedChild(0); + const as = keywordValue(node, 'as'); + const owner = moduleNameAt(node); + const emitAlias = (local: SyntaxNode | string, raw: string) => + add('@type-binding.alias', target, { + '@type-binding.name': local, + '@type-binding.type': raw, + }); + if (target?.type === 'alias') emitAlias(as ?? target.text.split('.').at(-1)!, target.text); + else if ( + target?.type === 'dot' && + target.childForFieldName?.('left')?.type === 'alias' && + target.childForFieldName?.('right')?.type === 'tuple' + ) { + const prefix = target.childForFieldName('left')!.text; + for (const child of target.childForFieldName('right')!.namedChildren) + if (child.type === 'alias') emitAlias(child, `${prefix}.${child.text}`); + } else if ( + target?.type === 'dot' && + target.childForFieldName?.('left')?.text === '__MODULE__' && + target.childForFieldName?.('right')?.type === 'alias' && + owner + ) { + const local = as ?? target.childForFieldName('right')!; + emitAlias(local, `${owner}.${target.childForFieldName('right')!.text}`); + } + } else if (kind === 'import' && args) { + const target = args.namedChild(0); + const only = keywordValue(node, 'only'); + const except = keywordValue(node, 'except'); + if (target?.type === 'alias' && (only || /\bonly:/.test(node.text))) { + const allowed = (only?.namedChildren ?? []) + .flatMap((entry) => (entry.type === 'keywords' ? entry.namedChildren : [entry])) + .flatMap((pair) => { + const name = pair.namedChild(0)?.text.trim().replace(/:$/, ''); + const arity = Number(pair.namedChild(1)?.text); + return name !== undefined && Number.isInteger(arity) && arity >= 0 + ? [{ name, arity }] + : []; + }); + const category = /\bonly:\s*:(functions|macros)\b/.exec(node.text)?.[1]; + // Category selectors need a lexical import scope, but filtering is + // deferred until public declarations are available workspace-wide. + if (category === 'functions' || category === 'macros') + add('@import.statement', node, { + '@import.source': target, + '@import.wildcard': target, + }); + recordElixirImportOnly(_file, { + target: target.text, + allowed, + ...(category === 'functions' || category === 'macros' ? { category } : {}), + startLine: node.startPosition.row + 1, + startCol: node.startPosition.column, + }); + } else if (target?.type === 'alias') { + // `bindingAugmentations` can only add references after wildcard imports + // are finalized, so an `except` import must not emit a wildcard here. + if (!except) + add('@import.statement', node, { + '@import.source': target, + '@import.wildcard': target, + }); + if (except?.type === 'list') { + const excluded = except.namedChildren + .flatMap((entry) => (entry.type === 'keywords' ? entry.namedChildren : [entry])) + .flatMap((pair) => { + const name = pair.namedChild(0)?.text.trim().replace(/:$/, ''); + const arity = Number(pair.namedChild(1)?.text); + return name !== undefined && Number.isInteger(arity) && arity >= 0 + ? [{ name, arity }] + : []; + }); + recordElixirImportExcept(_file, { + target: target.text, + excluded, + startLine: node.startPosition.row + 1, + startCol: node.startPosition.column, + }); + } + } + } else if (kind === 'callback' && args) { + // A behaviour callback is a declaration, not a call. Keep it in the + // normal scope model so the existing METHOD_IMPLEMENTS pass can pair + // it with a same-name/same-arity module function. + const first = args.namedChildren.find( + (child) => child?.type === 'call' || child?.type === 'binary_operator', + ); + const signature = + first?.type === 'binary_operator' ? first.childForFieldName?.('left') : first; + const name = signature?.childForFieldName?.('target'); + if (name?.type === 'identifier') { + const parameters = findArguments(signature); + const count = parameters?.namedChildCount ?? 0; + const owner = moduleNameAt(node); + if (owner) + add('@declaration.function', node, { + '@declaration.name': name, + '@declaration.qualified_name': `${owner}.${name.text}`, + '@declaration.parameter-count': String(count), + '@declaration.required-parameter-count': String(count), + '@declaration.is-exported': 'false', + }); + } + } else if (kind === 'defimpl' && args) { + // `defimpl Protocol, for: Type` has no enclosing class scope for the + // generic heritage pass. Preserve both names for the provider hook. + const protocol = args.namedChildren.find((child) => child?.type === 'alias'); + const implementation = keywordValue(node, 'for'); + if (protocol?.type === 'alias' && implementation?.type === 'alias') + add('@reference.inherits', node, { + '@reference.name': protocol, + '@reference.receiver': implementation, + }); + } + } else if (node.type === 'unary_operator' && node.text.startsWith('@behaviour')) { + const operand = node.childForFieldName?.('operand') ?? node.namedChild(0); + const behaviour = + operand?.type === 'call' + ? firstAliasArgument(operand) + : operand?.type === 'atom' + ? operand.text.slice(1) + : undefined; + if (behaviour) add('@reference.inherits', node, { '@reference.name': behaviour }); + } else if ( + node.type === 'unary_operator' && + node.text.startsWith('&') && + !isQuotedElixirNode(node) + ) { + const binary = node.namedChild(0); + const target = binary?.childForFieldName?.('left'); + const arity = binary?.childForFieldName?.('right')?.text; + if (target?.type === 'identifier' && arity !== undefined) + add('@reference.call.free', node, { '@reference.name': target, '@reference.arity': arity }); + else if (target?.type === 'call' && arity !== undefined) { + const dot = target.childForFieldName?.('target'); + const receiver = dot?.childForFieldName?.('left'); + const name = dot?.childForFieldName?.('right'); + if (receiver?.type === 'alias' && name?.type === 'identifier') { + const resolved = resolveAliasReceiver(node, receiver); + add('@reference.call.member', node, { + '@reference.receiver': resolved ? `@elixir-alias:${resolved}` : receiver, + '@reference.name': name, + '@reference.arity': arity, + }); + } + } + } + for (const child of node.namedChildren) visit(child as SyntaxNode); + }; + visit(tree.rootNode as SyntaxNode); + return [...captures, ...synthetic]; +} + +function isQuotedElixirNode(node: SyntaxNode): boolean { + for (let current = node.parent; current; current = current.parent) { + if (current.type === 'call' && callKeyword(current) === 'quote') return true; + } + return false; +} + +function isElixirCaptureNode(node: SyntaxNode): boolean { + for (let current = node.parent; current; current = current.parent) { + if (current.type === 'unary_operator' && current.text.startsWith('&')) return true; + if (current.type === 'call' && ELIXIR_DEF_KEYWORDS.has(callKeyword(current) ?? '')) + return false; + } + return false; +} + +function interpretElixirImport(captures: CaptureMatch): ParsedImport | null { + const source = captures['@import.source']?.text; + if (source === undefined) return null; + const name = captures['@import.name']?.text; + if (captures['@import.wildcard'] !== undefined) return { kind: 'wildcard', targetRaw: source }; + return name === undefined + ? { + kind: 'namespace', + localName: source.split('.').at(-1)!, + importedName: source, + targetRaw: source, + } + : { + kind: 'named', + localName: name, + importedName: name, + targetRaw: source, + importedSymbolKind: 'function', + }; +} + +function interpretElixirTypeBinding(captures: CaptureMatch) { + const name = captures['@type-binding.name']?.text; + const type = captures['@type-binding.type']?.text; + return name === undefined || type === undefined + ? null + : { boundName: name, rawTypeName: type, source: 'assignment-inferred' as const }; +} + +function skipElixirStructuralDefinition( + captures: Record, + label: NodeLabel, +): boolean { + if (label !== 'Class' && label !== 'Interface' && label !== 'Function' && label !== 'Macro') + return false; + for (let node = captures.name; node; node = node.parent) { + if ( + node.type === 'call' && + (ELIXIR_MODULE_KEYWORDS.has(callKeyword(node) ?? '') || + ELIXIR_DEF_KEYWORDS.has(callKeyword(node) ?? '')) + ) + return true; + } + return false; +} + +const elixirIsClassContainerNode = (node: SyntaxNode): boolean => + node.type === 'call' && ELIXIR_MODULE_KEYWORDS.has(callKeyword(node) ?? ''); + +const elixirEnclosingFunctionFinder = ( + node: SyntaxNode, +): { funcName: string; label: NodeLabel; definitionNode: SyntaxNode } | null => { + if (node.type !== 'call') return null; + const funcName = extractFunctionNameFromDef(node); + return funcName ? { funcName, label: 'Function', definitionNode: node } : null; +}; + +const elixirImportPathPreprocessor = (cleaned: string, importNode: SyntaxNode): string | null => { + const keyword = callKeyword(importNode); + return keyword === 'import' || keyword === 'use' || keyword === 'require' || keyword === 'alias' + ? cleaned + : null; +}; + +const BUILT_INS: ReadonlySet = new Set([ + // Kernel built-ins + 'send', + 'spawn', + 'spawn_link', + 'spawn_monitor', + 'exit', + 'throw', + 'raise', + 'reraise', + 'apply', + 'is_atom', + 'is_binary', + 'is_bitstring', + 'is_boolean', + 'is_float', + 'is_function', + 'is_integer', + 'is_list', + 'is_map', + 'is_nil', + 'is_number', + 'is_pid', + 'is_port', + 'is_reference', + 'is_struct', + 'is_tuple', + 'length', + 'hd', + 'tl', + 'abs', + 'ceil', + 'floor', + 'round', + 'trunc', + 'rem', + 'div', + 'max', + 'min', + 'elem', + 'put_elem', + 'tuple_size', + 'map_size', + 'byte_size', + 'bit_size', + 'binary_part', + 'get_in', + 'put_in', + 'update_in', + 'pop_in', + 'get_and_update_in', + 'struct', + 'struct!', + // IO + 'IO.puts', + 'IO.inspect', + 'IO.gets', + 'IO.write', + 'IO.read', + // Common pipeline functions + 'then', + 'tap', + 'dbg', +]); + +export const elixirProvider = defineLanguage({ + id: SupportedLanguages.Elixir, + extensions: ['.ex', '.exs'], + + entryPointPatterns: [ + /^mount$/, + /^handle_event$/, + /^handle_info$/, + /^handle_call$/, + /^handle_cast$/, + /^handle_continue$/, + /^perform$/, + /^process$/, + /^call$/, + /^init$/, + /^start_link$/, + /^action$/, + ], + + astFrameworkPatterns: [ + { + framework: 'phoenix-liveview', + entryPointMultiplier: 3.0, + reason: 'liveview-handler', + patterns: ['Phoenix.LiveView', 'use Phoenix.LiveView', 'mount', 'handle_event'], + }, + { + framework: 'phoenix-controller', + entryPointMultiplier: 2.5, + reason: 'phoenix-action', + patterns: ['use Phoenix.Controller', 'conn', 'params', 'Phoenix.Controller'], + }, + { + framework: 'phoenix-channel', + entryPointMultiplier: 2.5, + reason: 'phoenix-channel', + patterns: ['use Phoenix.Channel', 'Phoenix.Channel', 'socket', 'join'], + }, + { + framework: 'oban-worker', + entryPointMultiplier: 2.8, + reason: 'oban-job', + patterns: ['use Oban.Worker', 'Oban.Worker', 'perform'], + }, + { + framework: 'genserver', + entryPointMultiplier: 2.0, + reason: 'genserver', + patterns: ['use GenServer', 'GenServer', 'handle_call', 'handle_cast', 'handle_info'], + }, + { + framework: 'plug', + entryPointMultiplier: 2.0, + reason: 'plug-middleware', + patterns: ['use Plug.Router', 'Plug.Conn', 'plug'], + }, + ] satisfies AstFrameworkPatternConfig[], + + treeSitterQueries: ELIXIR_QUERIES, + typeConfig: elixirTypeConfig, + exportChecker: elixirExportChecker, + importResolver: createImportResolver(elixirImportConfig), + importPathPreprocessor: elixirImportPathPreprocessor, + emitScopeCaptures: emitElixirScopeCaptures, + interpretImport: interpretElixirImport, + collectCaptureSideChannel: collectElixirCaptureSideChannel, + interpretTypeBinding: interpretElixirTypeBinding, + shouldSkipDefinitionCapture: skipElixirStructuralDefinition, + extractSemanticGraph: extractElixirSemanticGraph, + resolveContainerTypeOwner: (container) => { + if (!elixirIsClassContainerNode(container)) return null; + const name = firstAliasArgument(container); + if (name === undefined) return null; + return { name, label: callKeyword(container) === 'defprotocol' ? 'Interface' : 'Class' }; + }, + callExtractor: elixirCallExtractor, + enclosingFunctionFinder: elixirEnclosingFunctionFinder, + methodExtractor: createMethodExtractor(elixirMethodConfig), + classExtractor: createClassExtractor(elixirClassConfig), + cfgVisitor: createElixirCfgVisitor(), + builtInNames: BUILT_INS, +}); diff --git a/gitnexus/src/core/ingestion/languages/elixir/import-filters.ts b/gitnexus/src/core/ingestion/languages/elixir/import-filters.ts new file mode 100644 index 000000000..18de6927c --- /dev/null +++ b/gitnexus/src/core/ingestion/languages/elixir/import-filters.ts @@ -0,0 +1,120 @@ +import type { ParsedFile } from 'gitnexus-shared'; + +export interface ElixirImportExceptFact { + readonly target: string; + readonly excluded: readonly { readonly name: string; readonly arity: number }[]; + readonly startLine: number; + readonly startCol: number; +} + +export interface ElixirImportOnlyFact { + readonly target: string; + readonly allowed: readonly { readonly name: string; readonly arity: number }[]; + /** Category selectors (`only: :functions` / `:macros`) stay distinct from an empty list. */ + readonly category?: 'functions' | 'macros'; + readonly startLine: number; + readonly startCol: number; +} + +export interface ElixirCaptureSideChannel { + readonly kind: 'elixir'; + readonly importExcepts: readonly ElixirImportExceptFact[]; + readonly importOnly: readonly ElixirImportOnlyFact[]; + readonly frameworkFacts: readonly ElixirFrameworkFact[]; +} + +/** Plain worker-safe facts; resolution deliberately happens after every file is present. */ +export type ElixirFrameworkFact = + | { + readonly kind: 'route'; + readonly path: string; + readonly method: string; + readonly handler: string; + readonly action?: string; + readonly line: number; + readonly pipelines: readonly string[]; + } + | { + readonly kind: 'schema'; + readonly model: string; + readonly table?: string; + readonly line: number; + } + | { + readonly kind: 'property'; + readonly model: string; + readonly name: string; + readonly propertyKind: string; + readonly target?: string; + readonly line: number; + } + | { + readonly kind: 'query'; + readonly model: string; + readonly method: string; + readonly line: number; + }; + +const importExceptsByFile = new Map(); +const importOnlyByFile = new Map(); +const frameworkFactsByFile = new Map(); + +export function clearElixirImportExcepts(filePath: string): void { + importExceptsByFile.delete(filePath); + importOnlyByFile.delete(filePath); + frameworkFactsByFile.delete(filePath); +} + +export function recordElixirImportOnly(filePath: string, fact: ElixirImportOnlyFact): void { + const facts = importOnlyByFile.get(filePath) ?? []; + facts.push(fact); + importOnlyByFile.set(filePath, facts); +} + +export function recordElixirImportExcept(filePath: string, fact: ElixirImportExceptFact): void { + const facts = importExceptsByFile.get(filePath) ?? []; + facts.push(fact); + importExceptsByFile.set(filePath, facts); +} + +export function recordElixirFrameworkFacts( + filePath: string, + facts: readonly ElixirFrameworkFact[], +): void { + if (facts.length) frameworkFactsByFile.set(filePath, [...facts]); +} + +export function collectElixirCaptureSideChannel( + filePath: string, +): ElixirCaptureSideChannel | undefined { + const importExcepts = importExceptsByFile.get(filePath); + const importOnly = importOnlyByFile.get(filePath); + const frameworkFacts = frameworkFactsByFile.get(filePath); + return importExcepts?.length || importOnly?.length || frameworkFacts?.length + ? { + kind: 'elixir', + importExcepts: importExcepts ?? [], + importOnly: importOnly ?? [], + frameworkFacts: frameworkFacts ?? [], + } + : undefined; +} + +export function elixirImportOnlyFacts(parsed: ParsedFile): readonly ElixirImportOnlyFact[] { + const payload = parsed.captureSideChannel as ElixirCaptureSideChannel | undefined; + return payload?.kind === 'elixir' && Array.isArray(payload.importOnly) ? payload.importOnly : []; +} + +export function elixirFrameworkFacts(parsed: ParsedFile): readonly ElixirFrameworkFact[] { + const payload = parsed.captureSideChannel as ElixirCaptureSideChannel | undefined; + return payload?.kind === 'elixir' && Array.isArray(payload.frameworkFacts) + ? payload.frameworkFacts + : []; +} + +export function elixirImportExceptFacts(parsed: ParsedFile): readonly ElixirImportExceptFact[] { + const payload = parsed.captureSideChannel as ElixirCaptureSideChannel | undefined; + return payload?.kind === 'elixir' && Array.isArray(payload.importExcepts) + ? payload.importExcepts + : []; +} diff --git a/gitnexus/src/core/ingestion/languages/elixir/scope-resolver.ts b/gitnexus/src/core/ingestion/languages/elixir/scope-resolver.ts new file mode 100644 index 000000000..7afd98e1e --- /dev/null +++ b/gitnexus/src/core/ingestion/languages/elixir/scope-resolver.ts @@ -0,0 +1,413 @@ +import type { BindingRef, Callsite, ParsedFile, ScopeId, SymbolDefinition } from 'gitnexus-shared'; +import { SupportedLanguages } from 'gitnexus-shared'; +import type { ScopeResolver } from '../../scope-resolution/contract/scope-resolver.js'; +import { populateClassOwnedMembers } from '../../scope-resolution/scope/walkers.js'; +import { elixirProvider } from '../elixir.js'; +import { + elixirFrameworkFacts, + elixirImportExceptFacts, + elixirImportOnlyFacts, +} from './import-filters.js'; +import type { ScopeResolutionIndexes } from '../../model/scope-resolution-indexes.js'; +import type { KnowledgeGraph } from '../../../graph/types.js'; +import type { GraphNodeLookup } from '../../scope-resolution/graph-bridge/node-lookup.js'; +import { resolveDefGraphId } from '../../scope-resolution/graph-bridge/ids.js'; +import { + findClassBindingInScope, + resolveAmbiguousInheritanceBaseViaImports, +} from '../../scope-resolution/scope/walkers.js'; +import { generateId } from '../../../../lib/utils.js'; +import { routeNodeKey } from '../../route-extractors/route-path.js'; +import { toZeroBasedLine } from '../../utils/line-base.js'; + +const CAPTURED_ALIAS_PREFIX = '@elixir-alias:'; + +function callableName(def: SymbolDefinition): string { + return def.qualifiedName?.split('.').at(-1) ?? def.qualifiedName ?? ''; +} + +const moduleFilesByWorkspace = new WeakMap>(); + +function elixirModuleFiles(parsedFiles: readonly ParsedFile[]): ReadonlyMap { + const cached = moduleFilesByWorkspace.get(parsedFiles); + if (cached) return cached; + + const candidates = new Map>(); + for (const parsed of parsedFiles) { + for (const def of parsed.localDefs) { + if (!def.qualifiedName || (def.type !== 'Class' && def.type !== 'Interface')) continue; + const files = candidates.get(def.qualifiedName) ?? new Set(); + files.add(parsed.filePath); + candidates.set(def.qualifiedName, files); + } + } + + const unique = new Map(); + for (const [name, files] of candidates) { + if (files.size === 1) unique.set(name, [...files][0]!); + } + moduleFilesByWorkspace.set(parsedFiles, unique); + return unique; +} + +function importScope( + parsed: ParsedFile, + target: string, + line: number, + col: number, +): ScopeId | undefined { + const candidates = parsed.parsedImports.filter( + (imp) => + imp.kind === 'wildcard' && imp.targetRaw === target && imp.declaredAtScope !== undefined, + ); + if (candidates.length === 1) return candidates[0]!.declaredAtScope; + const scope = parsed.scopes + .filter( + (candidate) => + candidate.range.startLine <= line && + candidate.range.endLine >= line && + (candidate.range.startLine !== line || candidate.range.startCol <= col) && + (candidate.range.endLine !== line || candidate.range.endCol >= col), + ) + .sort((a, b) => a.range.endLine - a.range.startLine - (b.range.endLine - b.range.startLine))[0]; + return scope?.id; +} + +function populateElixirImportFilters( + parsedFiles: readonly ParsedFile[], + indexes: ScopeResolutionIndexes, +): void { + const byModule = new Map(); + for (const parsed of parsedFiles) + for (const def of parsed.localDefs) { + if ((def.type === 'Class' || def.type === 'Interface') && def.qualifiedName) { + const files = byModule.get(def.qualifiedName) ?? []; + if (!files.includes(parsed)) files.push(parsed); + byModule.set(def.qualifiedName, files); + } + } + const augmentations = indexes.bindingAugmentations as Map>; + for (const parsed of parsedFiles) + for (const fact of [ + ...elixirImportExceptFacts(parsed).map((fact) => ({ ...fact, mode: 'except' as const })), + ...elixirImportOnlyFacts(parsed).map((fact) => ({ ...fact, mode: 'only' as const })), + ]) { + const targets = byModule.get(fact.target); + if (targets?.length !== 1) continue; + const scopeId = importScope(parsed, fact.target, fact.startLine, fact.startCol); + if (scopeId === undefined) continue; + const selected = new Set( + (fact.mode === 'except' ? fact.excluded : fact.allowed).map( + ({ name, arity }) => `${name}/${arity}`, + ), + ); + const bucket = augmentations.get(scopeId) ?? new Map(); + augmentations.set(scopeId, bucket); + for (const def of targets[0]!.localDefs) { + if ( + (def.type !== 'Function' && def.type !== 'Macro') || + def.isExported === false || + def.qualifiedName !== `${fact.target}.${callableName(def)}` + ) + continue; + if (fact.mode === 'only' && fact.category === 'functions' && def.type !== 'Function') + continue; + if (fact.mode === 'only' && fact.category === 'macros' && def.type !== 'Macro') continue; + const name = callableName(def); + if ( + !name || + (fact.mode === 'except' + ? selected.has(`${name}/${def.parameterCount ?? 0}`) + : fact.category === undefined && !selected.has(`${name}/${def.parameterCount ?? 0}`)) + ) + continue; + const refs = bucket.get(name) ?? []; + if (!refs.some((ref) => ref.def.nodeId === def.nodeId)) + refs.push({ def, origin: 'wildcard' }); + bucket.set(name, refs); + } + } +} + +/** Resolve only capture-time verified lexical aliases, never inferred modules. */ +function resolveElixirCapturedAliasMember( + receiverName: string, + memberName: string, + _callerScope: ScopeId, + _scopes: ScopeResolutionIndexes, + parsedFiles: readonly ParsedFile[], + callsite?: Callsite, +): SymbolDefinition | 'ambiguous' | undefined { + if (!receiverName.startsWith(CAPTURED_ALIAS_PREFIX)) return undefined; + const target = receiverName.slice(CAPTURED_ALIAS_PREFIX.length); + const candidates = parsedFiles.flatMap((parsed) => + parsed.localDefs.filter( + (def) => + def.type === 'Function' && + def.isExported !== false && + def.qualifiedName === `${target}.${memberName}` && + (callsite?.arity === undefined || + (callsite.arity >= (def.requiredParameterCount ?? def.parameterCount ?? 0) && + callsite.arity <= (def.parameterCount ?? 0))), + ), + ); + return candidates.length === 1 ? candidates[0] : candidates.length > 1 ? 'ambiguous' : undefined; +} + +/** `defimpl P, for: T` is the one Elixir heritage form outside a module body. */ +function emitElixirProtocolImplEdges( + graph: KnowledgeGraph, + parsedFiles: readonly ParsedFile[], + nodeLookup: GraphNodeLookup, + scopes?: ScopeResolutionIndexes, +): void { + if (!scopes) return; + const emitted = new Set( + [...graph.iterRelationshipsByType('IMPLEMENTS')].map( + (rel) => `${rel.sourceId}->${rel.targetId}`, + ), + ); + for (const parsed of parsedFiles) + for (const site of parsed.referenceSites) { + if (site.kind !== 'inherits' || !site.explicitReceiver?.name) continue; + const implementation = + findClassBindingInScope(site.inScope, site.explicitReceiver.name, scopes) ?? + resolveAmbiguousInheritanceBaseViaImports(site.inScope, site.explicitReceiver.name, scopes); + const protocol = + findClassBindingInScope(site.inScope, site.name, scopes) ?? + resolveAmbiguousInheritanceBaseViaImports(site.inScope, site.name, scopes); + if (!implementation || !protocol || protocol.type !== 'Interface') continue; + const sourceId = resolveDefGraphId(implementation.filePath, implementation, nodeLookup); + const targetId = resolveDefGraphId(protocol.filePath, protocol, nodeLookup); + if (!sourceId || !targetId || emitted.has(`${sourceId}->${targetId}`)) continue; + emitted.add(`${sourceId}->${targetId}`); + graph.addRelationship({ + id: generateId('IMPLEMENTS', `${sourceId}->${targetId}:elixir-protocol`), + sourceId, + targetId, + type: 'IMPLEMENTS', + confidence: 0.85, + reason: 'elixir-protocol-impl', + }); + } +} + +/** Match public implementation functions to non-callable @callback contracts. */ +function emitElixirBehaviourMethodEdges( + graph: KnowledgeGraph, + parsedFiles: readonly ParsedFile[], + nodeLookup: GraphNodeLookup, + indexes: ScopeResolutionIndexes, +): void { + const emitted = new Set( + [...graph.iterRelationshipsByType('METHOD_IMPLEMENTS')].map( + (rel) => `${rel.sourceId}->${rel.targetId}`, + ), + ); + for (const parsed of parsedFiles) + for (const site of parsed.referenceSites) { + if (site.kind !== 'inherits') continue; + const behaviours = parsedFiles.flatMap((file) => + file.localDefs.filter((def) => def.qualifiedName === site.name && def.type === 'Interface'), + ); + let scope = indexes.scopeTree.getScope(site.inScope); + let implementation: SymbolDefinition | undefined; + while (scope) { + implementation = scope.ownedDefs.find((def) => def.type === 'Class'); + if (implementation) break; + scope = scope.parent ? indexes.scopeTree.getScope(scope.parent) : undefined; + } + if (behaviours.length !== 1 || !implementation) continue; + const behaviour = behaviours[0]!; + for (const contract of parsedFiles + .flatMap((file) => file.localDefs) + .filter( + (def) => + def.filePath === behaviour.filePath && + def.type === 'Function' && + def.qualifiedName?.startsWith(`${behaviour.qualifiedName}.`), + )) { + const method = parsed.localDefs.filter( + (def) => + def.type === 'Function' && + def.isExported !== false && + def.qualifiedName === `${implementation.qualifiedName}.${callableName(contract)}` && + def.parameterCount === contract.parameterCount, + ); + if (method.length !== 1) continue; + const sourceId = resolveDefGraphId(method[0]!.filePath, method[0]!, nodeLookup); + const targetId = resolveDefGraphId(contract.filePath, contract, nodeLookup); + if (!sourceId || !targetId || emitted.has(`${sourceId}->${targetId}`)) continue; + emitted.add(`${sourceId}->${targetId}`); + graph.addRelationship({ + id: generateId('METHOD_IMPLEMENTS', `${sourceId}->${targetId}:elixir-behaviour`), + sourceId, + targetId, + type: 'METHOD_IMPLEMENTS', + confidence: 0.9, + reason: 'elixir-behaviour-callback', + }); + } + } +} + +/** Materialize worker-captured literal Phoenix/Ecto facts only after all modules are known. */ +function emitElixirFrameworkEdges( + graph: KnowledgeGraph, + parsedFiles: readonly ParsedFile[], + nodeLookup: GraphNodeLookup, +): void { + const classDef = (name: string) => { + const matches = parsedFiles.flatMap((file) => + file.localDefs.filter( + (def) => + def.type === 'Class' && + (def.qualifiedName === name || def.qualifiedName?.split('.').at(-1) === name), + ), + ); + return matches.length === 1 ? matches[0] : undefined; + }; + const classId = (name: string) => { + const def = classDef(name); + return def ? resolveDefGraphId(def.filePath, def, nodeLookup) : undefined; + }; + for (const parsed of parsedFiles) + for (const fact of elixirFrameworkFacts(parsed)) { + if (fact.kind === 'route') { + const target = classDef(fact.handler); + const handler = + fact.action && target + ? parsedFiles.flatMap((file) => + file.localDefs.filter( + (def) => + def.type === 'Function' && + def.qualifiedName === `${target.qualifiedName}.${fact.action}`, + ), + ) + : target + ? [target] + : []; + const uniqueHandlers = [...new Map(handler.map((def) => [def.nodeId, def])).values()]; + if (uniqueHandlers.length !== 1) continue; + const handlerId = resolveDefGraphId( + uniqueHandlers[0]!.filePath, + uniqueHandlers[0]!, + nodeLookup, + ); + if (!handlerId) continue; + const key = routeNodeKey(fact.method, fact.path); + const routeId = generateId('Route', key); + const routeProperties = { + filePath: uniqueHandlers[0]!.filePath, + startLine: toZeroBasedLine(fact.line), + endLine: toZeroBasedLine(fact.line), + handlerSymbolId: handlerId, + ...(fact.pipelines.length ? { middleware: [...fact.pipelines] } : {}), + }; + const existingRoute = graph.getNode(routeId); + if (existingRoute) Object.assign(existingRoute.properties, routeProperties); + else + graph.addNode({ + id: routeId, + label: 'Route', + properties: { name: fact.path, method: fact.method, ...routeProperties }, + }); + const fileId = generateId('File', uniqueHandlers[0]!.filePath); + graph.addRelationship({ + id: generateId('HANDLES_ROUTE', `${fileId}->${routeId}`), + sourceId: fileId, + targetId: routeId, + type: 'HANDLES_ROUTE', + confidence: 1, + reason: 'phoenix-router', + }); + graph.addRelationship({ + id: generateId('HANDLES_ROUTE', `${handlerId}->${routeId}`), + sourceId: handlerId, + targetId: routeId, + type: 'HANDLES_ROUTE', + confidence: 1, + reason: 'phoenix-router', + }); + } else if (fact.kind === 'property') { + const ownerId = classId(fact.model); + if (!ownerId) continue; + const propertyId = generateId('Property', `${parsed.filePath}:${fact.model}.${fact.name}`); + if (!graph.getNode(propertyId)) + graph.addNode({ + id: propertyId, + label: 'Property', + properties: { + name: fact.name, + qualifiedName: `${fact.model}.${fact.name}`, + filePath: parsed.filePath, + startLine: toZeroBasedLine(fact.line), + endLine: toZeroBasedLine(fact.line), + language: SupportedLanguages.Elixir, + elixirKind: fact.propertyKind, + }, + }); + graph.addRelationship({ + id: generateId('HAS_PROPERTY', `${ownerId}->${propertyId}`), + sourceId: ownerId, + targetId: propertyId, + type: 'HAS_PROPERTY', + confidence: 1, + reason: `ecto-${fact.propertyKind}`, + }); + if (fact.target) { + const targetId = classId(fact.target); + if (targetId) + graph.addRelationship({ + id: generateId('USES', `${ownerId}->${targetId}:ecto-${fact.propertyKind}`), + sourceId: ownerId, + targetId, + type: 'USES', + confidence: 0.9, + reason: `ecto-${fact.propertyKind}`, + }); + } + } else if (fact.kind === 'query') { + const modelId = classId(fact.model); + if (!modelId) continue; + const fileId = generateId('File', parsed.filePath); + graph.addRelationship({ + id: generateId('QUERIES', `${fileId}->${modelId}:${fact.method}`), + sourceId: fileId, + targetId: modelId, + type: 'QUERIES', + confidence: 0.9, + reason: `ecto-${fact.method}`, + }); + } + } +} + +export const elixirScopeResolver: ScopeResolver = { + language: SupportedLanguages.Elixir, + languageProvider: elixirProvider, + importEdgeReason: 'elixir-scope: module declaration', + resolveImportTarget: (raw, _from, _paths, _config, context) => + elixirModuleFiles(context?.parsedFiles ?? []).get(raw) ?? null, + mergeBindings: (existing) => [...existing], + importsBindAtLexicalScope: true, + arityCompatibility: (callsite: Callsite, def: SymbolDefinition) => { + if (callsite.arity === undefined || def.parameterCount === undefined) return 'unknown'; + return callsite.arity >= (def.requiredParameterCount ?? def.parameterCount) && + callsite.arity <= def.parameterCount + ? 'compatible' + : 'incompatible'; + }, + buildMro: () => new Map(), + emitHeritageEdges: emitElixirProtocolImplEdges, + populateOwners: (parsed) => populateClassOwnedMembers(parsed), + isSuperReceiver: () => false, + fieldFallbackOnMethodLookup: false, + propagatesReturnTypesAcrossImports: false, + resolveQualifiedReceiverMember: resolveElixirCapturedAliasMember, + populateNamespaceSiblings: populateElixirImportFilters, + emitPostResolutionEdges(graph, parsedFiles, nodeLookup, indexes) { + emitElixirBehaviourMethodEdges(graph, parsedFiles, nodeLookup, indexes); + emitElixirFrameworkEdges(graph, parsedFiles, nodeLookup); + }, +}; diff --git a/gitnexus/src/core/ingestion/languages/elixir/semantic-graph.ts b/gitnexus/src/core/ingestion/languages/elixir/semantic-graph.ts new file mode 100644 index 000000000..0ae9f478a --- /dev/null +++ b/gitnexus/src/core/ingestion/languages/elixir/semantic-graph.ts @@ -0,0 +1,435 @@ +import { SupportedLanguages, type NodeLabel } from 'gitnexus-shared'; +import type Parser from 'tree-sitter'; +import type { + ProviderSemanticGraph, + ProviderSemanticNode, + ProviderSemanticRelationship, + ProviderSemanticSymbol, +} from '../../language-provider.js'; +import { generateId } from '../../../../lib/utils.js'; +import type { SyntaxNode } from '../../utils/ast-helpers.js'; + +const DECLARATIONS = new Set([ + 'def', + 'defp', + 'defmacro', + 'defmacrop', + 'defguard', + 'defguardp', + 'defdelegate', +]); +const MODULES = new Set(['defmodule', 'defprotocol']); + +function keyword(node: SyntaxNode): string | undefined { + const target = node.childForFieldName?.('target'); + return target?.type === 'identifier' ? target.text : undefined; +} + +function remoteReceiver(node: SyntaxNode): string | undefined { + const target = node.childForFieldName?.('target'); + return target?.type === 'dot' && target.childForFieldName?.('left')?.type === 'alias' + ? target.childForFieldName('left')!.text + : undefined; +} + +function remoteMethod(node: SyntaxNode): string | undefined { + const target = node.childForFieldName?.('target'); + return target?.type === 'dot' && target.childForFieldName?.('right')?.type === 'identifier' + ? target.childForFieldName('right')!.text + : undefined; +} + +function argumentsNode(node: SyntaxNode): SyntaxNode | null { + for (let i = 0; i < node.namedChildCount; i++) { + const child = node.namedChild(i); + if (child?.type === 'arguments') return child; + } + return null; +} + +function declaration( + node: SyntaxNode, +): { name: string; arity: number; required: number } | undefined { + const args = argumentsNode(node); + if (!args) return undefined; + const first = args.namedChildren.find( + (child) => + child?.type === 'call' || child?.type === 'binary_operator' || child?.type === 'identifier', + ); + const signature = first?.type === 'binary_operator' ? first.childForFieldName?.('left') : first; + const target = + signature?.type === 'identifier' ? signature : signature?.childForFieldName?.('target'); + if (target?.type !== 'identifier') return undefined; + const parameters = argumentsNode(signature); + if (!parameters) return { name: target.text, arity: 0, required: 0 }; + let required = 0; + for (let i = 0; i < parameters.namedChildCount; i++) { + const parameter = parameters.namedChild(i); + if (parameter?.type !== 'binary_operator' || !parameter.text.includes('\\\\')) required++; + } + return { name: target.text, arity: parameters.namedChildCount, required }; +} + +function declarationName(node: SyntaxNode): string | undefined { + return ( + declaration(node)?.name ?? + argumentsNode(node)?.namedChildren.find((child) => child?.type === 'identifier')?.text + ); +} + +function enclosingModule(node: SyntaxNode): { name: string; identity: number } | undefined { + for (let current = node.parent; current; current = current.parent) { + if (current.type !== 'call' || !MODULES.has(keyword(current) ?? '')) continue; + const alias = argumentsNode(current)?.namedChildren.find((child) => child?.type === 'alias'); + if (alias) return { name: alias.text, identity: current.startIndex }; + } + return undefined; +} + +function callableLabel(kind: string): NodeLabel { + return kind === 'defmacro' || kind === 'defmacrop' || kind === 'defguard' || kind === 'defguardp' + ? 'Macro' + : 'Function'; +} + +function isQuoted(node: SyntaxNode): boolean { + for (let current = node.parent; current; current = current.parent) { + if (current.type === 'call' && keyword(current) === 'quote') return true; + } + return false; +} + +function isBehaviourModule(node: SyntaxNode): boolean { + let found = false; + const visit = (child: SyntaxNode): void => { + if (found || (child.type === 'call' && keyword(child) === 'defmodule' && child !== node)) + return; + if (child.type === 'call' && keyword(child) === 'quote') return; + if (child.type === 'unary_operator' && child.text.startsWith('@callback')) found = true; + for (let i = 0; !found && i < child.namedChildCount; i++) { + const next = child.namedChild(i); + if (next) visit(next as SyntaxNode); + } + }; + visit(node); + return found; +} + +function keywordValue(node: SyntaxNode, name: string): SyntaxNode | undefined { + const keywords = + node.type === 'keywords' + ? node + : node.type === 'list' + ? node.namedChildren.find((child) => child?.type === 'keywords') + : argumentsNode(node)?.namedChildren.find((child) => child?.type === 'keywords'); + return keywords?.namedChildren + .find((child) => child?.type === 'pair' && child.namedChild(0)?.text.trim() === `${name}:`) + ?.namedChild(1) as SyntaxNode | undefined; +} + +function literalAtom(node: SyntaxNode | undefined): string | undefined { + return node?.type === 'atom' ? node.text.slice(1) : undefined; +} + +function literalString(node: SyntaxNode | undefined): string | undefined { + if (node?.type !== 'string') return undefined; + try { + const value: unknown = JSON.parse(node.text); + return typeof value === 'string' ? value : undefined; + } catch { + return undefined; + } +} + +function literalBoolean(node: SyntaxNode | undefined): boolean | undefined { + return node?.type === 'boolean' ? node.text === 'true' : undefined; +} + +function targetName(node: SyntaxNode | undefined): string | undefined { + if (node?.type === 'alias') return node.text; + // `{:local, Server}` is a statically named local registration; all other + // server tuples may be process routing and deliberately stay unresolved. + if (node?.type === 'tuple' && literalAtom(node.namedChild(0) as SyntaxNode) === 'local') { + return targetName(node.namedChild(1) as SyntaxNode); + } + return undefined; +} + +function listValues(node: SyntaxNode | undefined): readonly SyntaxNode[] | undefined { + return node?.type === 'list' ? (node.namedChildren as readonly SyntaxNode[]) : undefined; +} + +function definitionBody(node: SyntaxNode): SyntaxNode | undefined { + const inline = keywordValue(node, 'do'); + if (inline) return inline; + const block = node.namedChildren.find((child) => child?.type === 'do_block'); + return block?.namedChildren.length === 1 ? (block.namedChild(0) as SyntaxNode) : undefined; +} + +/** Provider-owned canonical declarations; scope captures deliberately remain per clause. */ +export function extractElixirSemanticGraph( + tree: Parser.Tree, + filePath: string, +): ProviderSemanticGraph { + const nodes: ProviderSemanticNode[] = []; + const relationships: ProviderSemanticRelationship[] = []; + const symbols: ProviderSemanticSymbol[] = []; + const seen = new Set(); + const fileId = generateId('File', filePath); + const moduleIds = new Map(); + const evidenceTargets: { evidenceId: string; kind: string; target: string }[] = []; + const definitions = new Map(); + const isMixFile = filePath === 'mix.exs' || filePath.endsWith('/mix.exs'); + + const collectDefinitions = (node: SyntaxNode): void => { + if (!isQuoted(node) && node.type === 'call' && DECLARATIONS.has(keyword(node) ?? '')) { + const info = declaration(node); + const owner = enclosingModule(node); + const body = definitionBody(node); + if (info && owner && body) definitions.set(`${owner.name}:${info.name}/${info.arity}`, body); + } + for (let i = 0; i < node.namedChildCount; i++) { + const child = node.namedChild(i); + if (child) collectDefinitions(child as SyntaxNode); + } + }; + if (isMixFile) collectDefinitions(tree.rootNode as SyntaxNode); + + const visit = (node: SyntaxNode): void => { + if (node.type === 'call') { + const kind = keyword(node); + if (isQuoted(node)) return; + if (kind && MODULES.has(kind)) { + const alias = argumentsNode(node)?.namedChildren.find((child) => child?.type === 'alias'); + if (alias) + add( + kind === 'defprotocol' || isBehaviourModule(node) ? 'Interface' : 'Class', + alias.text, + alias.text, + node.startIndex, + node.startPosition.row, + node.endPosition.row, + 0, + 0, + undefined, + ); + } else if (kind && DECLARATIONS.has(kind)) { + const declarationInfo = declaration(node); + const owner = enclosingModule(node); + if (declarationInfo && owner) { + const label = callableLabel(kind); + const qualifiedName = `${owner.name}.${declarationInfo.name}`; + // Same clauses share this key; distinct module declarations/arities/kinds do not. + const identity = `${owner.identity}:${qualifiedName}/${declarationInfo.arity}:${label}`; + add( + label, + declarationInfo.name, + qualifiedName, + identity, + node.startPosition.row, + node.endPosition.row, + declarationInfo.arity, + declarationInfo.required, + owner.name, + kind === 'defp' || kind === 'defmacrop' || kind === 'defguardp' ? 'private' : 'public', + ); + } + const metadataName = declarationName(node); + if (isMixFile && metadataName === 'project') { + const metadata = definitionBody(node); + if (metadata?.type === 'list') { + const app = literalAtom(keywordValue(metadata, 'app')); + if (app) addEvidence('mix-app', app, node, { app }); + const appsPath = literalString(keywordValue(metadata, 'apps_path')); + if (appsPath) addEvidence('mix-umbrella', appsPath, node, { appsPath }); + const deps = keywordValue(metadata, 'deps'); + emitMixDependencies( + deps?.type === 'call' && keyword(deps) === 'deps' + ? definitions.get(`${enclosingModule(node)?.name}:deps/0`) + : deps, + node, + ); + } + } else if (isMixFile && metadataName === 'application') { + const metadata = definitionBody(node); + if (metadata?.type === 'list') { + for (const key of ['applications', 'extra_applications']) { + for (const value of listValues(keywordValue(metadata, key)) ?? []) { + const app = literalAtom(value); + if (app) addEvidence('mix-application', app, node, { app, applicationKind: key }); + } + } + } + } + } else if (remoteMethod(node) === 'start_link' && remoteReceiver(node) === 'Supervisor') { + for (const child of listValues(argumentsNode(node)?.namedChild(0) as SyntaxNode) ?? []) { + const target = + child.type === 'tuple' + ? targetName(child.namedChild(0) as SyntaxNode) + : targetName(child); + if (target) addEvidence('supervisor-child', target, node, { target }); + } + } else if ( + ['start_link', 'call', 'cast'].includes(remoteMethod(node) ?? '') && + remoteReceiver(node) === 'GenServer' + ) { + const target = targetName(argumentsNode(node)?.namedChild(0) as SyntaxNode); + const method = remoteMethod(node)!; + if (target) addEvidence(`genserver-${method}`, target, node, { target }); + } else if (kind === 'callback') { + const declarationInfo = declaration(node); + const owner = enclosingModule(node); + if (declarationInfo && owner) { + add( + 'Function', + declarationInfo.name, + `${owner.name}.${declarationInfo.name}`, + `${owner.identity}:${owner.name}.${declarationInfo.name}/${declarationInfo.arity}:callback`, + node.startPosition.row, + node.endPosition.row, + declarationInfo.arity, + declarationInfo.required, + owner.name, + ); + } + } + } + for (let i = 0; i < node.namedChildCount; i++) { + const child = node.namedChild(i); + if (child) visit(child); + } + }; + + const add = ( + label: NodeLabel, + name: string, + qualifiedName: string, + identity: string | number, + startLine: number, + endLine: number, + parameterCount: number, + requiredParameterCount: number, + owner?: string, + visibility = 'public', + ): void => { + const key = `${label}:${identity}`; + if (seen.has(key)) return; + seen.add(key); + const id = generateId(label, `${filePath}:${key}`); + nodes.push({ + id, + label, + properties: { + name, + qualifiedName, + filePath, + startLine, + endLine, + language: SupportedLanguages.Elixir, + isExported: visibility === 'public', + ...(label === 'Function' || label === 'Macro' + ? { parameterCount, requiredParameterCount, visibility } + : {}), + }, + }); + relationships.push({ + id: generateId('DEFINES', `${fileId}->${id}`), + sourceId: fileId, + targetId: id, + type: 'DEFINES', + confidence: 1, + reason: 'elixir semantic declaration', + }); + const ownerId = owner ? moduleIds.get(owner) : undefined; + symbols.push({ + filePath, + name, + nodeId: id, + type: label, + qualifiedName, + ...(label === 'Function' || label === 'Macro' + ? { parameterCount, requiredParameterCount, visibility } + : {}), + ...(ownerId ? { ownerId } : {}), + }); + if (ownerId && (label === 'Function' || label === 'Macro')) + relationships.push({ + id: generateId('HAS_METHOD', `${ownerId}->${id}`), + sourceId: ownerId, + targetId: id, + type: 'HAS_METHOD', + confidence: 1, + reason: 'elixir module function', + }); + if (label === 'Class' || label === 'Interface') moduleIds.set(qualifiedName, id); + }; + + const addEvidence = ( + kind: string, + name: string, + node: SyntaxNode, + properties: Record, + ): void => { + const qualifiedName = `elixir:${kind}:${filePath}:${node.startIndex}:${name}`; + const id = generateId('CodeElement', qualifiedName); + nodes.push({ + id, + label: 'CodeElement', + properties: { + name, + qualifiedName, + filePath, + startLine: node.startPosition.row, + endLine: node.endPosition.row, + language: SupportedLanguages.Elixir, + isExported: true, + elixirKind: kind, + ...properties, + }, + }); + relationships.push({ + id: generateId('DEFINES', `${fileId}->${id}`), + sourceId: fileId, + targetId: id, + type: 'DEFINES', + confidence: 1, + reason: `elixir static ${kind} evidence`, + }); + if (typeof properties.target === 'string') + evidenceTargets.push({ evidenceId: id, kind, target: properties.target }); + }; + + const emitMixDependencies = (value: SyntaxNode | undefined, node: SyntaxNode): void => { + for (const entry of listValues(value) ?? []) { + if (entry.type !== 'tuple') continue; + const app = literalAtom(entry.namedChild(0) as SyntaxNode); + const options = entry.namedChildren.find((child) => child?.type === 'keywords'); + const path = literalString(options && keywordValue(options as SyntaxNode, 'path')); + const inUmbrella = literalBoolean( + options && keywordValue(options as SyntaxNode, 'in_umbrella'), + ); + if (app && (path !== undefined || inUmbrella === true)) { + addEvidence('mix-dependency', app, node, { + app, + ...(path !== undefined ? { path } : {}), + ...(inUmbrella === true ? { inUmbrella: true } : {}), + }); + } + } + }; + + visit(tree.rootNode as SyntaxNode); + for (const evidence of evidenceTargets) { + const target = moduleIds.get(evidence.target); + if (target) + relationships.push({ + id: generateId('DECLARES', `${evidence.evidenceId}->${target}`), + sourceId: evidence.evidenceId, + targetId: target, + type: 'DECLARES', + confidence: 1, + reason: `elixir static ${evidence.kind} target`, + }); + } + return { nodes, relationships, symbols }; +} diff --git a/gitnexus/src/core/ingestion/languages/index.ts b/gitnexus/src/core/ingestion/languages/index.ts index b10822a6f..c92f7994a 100644 --- a/gitnexus/src/core/ingestion/languages/index.ts +++ b/gitnexus/src/core/ingestion/languages/index.ts @@ -31,6 +31,7 @@ import { dartProvider } from './dart.js'; import { vueProvider } from './vue.js'; import { cobolProvider } from './cobol.js'; import { zigProvider } from './zig.js'; +import { elixirProvider } from './elixir.js'; export const providers = { [SupportedLanguages.JavaScript]: javascriptProvider, @@ -51,6 +52,7 @@ export const providers = { [SupportedLanguages.Vue]: vueProvider, [SupportedLanguages.Cobol]: cobolProvider, [SupportedLanguages.Zig]: zigProvider, + [SupportedLanguages.Elixir]: elixirProvider, } satisfies Record; /** Get provider by language enum (always succeeds for SupportedLanguages). */ diff --git a/gitnexus/src/core/ingestion/method-extractors/configs/elixir.ts b/gitnexus/src/core/ingestion/method-extractors/configs/elixir.ts new file mode 100644 index 000000000..cbb88948a --- /dev/null +++ b/gitnexus/src/core/ingestion/method-extractors/configs/elixir.ts @@ -0,0 +1,160 @@ +// gitnexus/src/core/ingestion/method-extractors/configs/elixir.ts + +import { SupportedLanguages } from 'gitnexus-shared'; +import type { + MethodExtractionConfig, + ParameterInfo, + MethodVisibility, +} from '../../method-types.js'; +import type { SyntaxNode } from '../../utils/ast-helpers.js'; + +const ELIXIR_DEF_KEYWORDS = new Set([ + 'def', + 'defp', + 'defmacro', + 'defmacrop', + 'defguard', + 'defguardp', + 'defdelegate', +]); + +const ELIXIR_PRIVATE_KEYWORDS = new Set(['defp', 'defmacrop', 'defguardp']); + +/** Return the `target` identifier text of a call node, or undefined. */ +function callKeyword(node: SyntaxNode): string | undefined { + const t = node.childForFieldName?.('target'); + return t?.type === 'identifier' ? t.text : undefined; +} + +/** Find the arguments node among named children. */ +function findArguments(node: SyntaxNode): SyntaxNode | null { + for (let i = 0; i < node.namedChildCount; i++) { + const c = node.namedChild(i); + if (c?.type === 'arguments') return c; + } + return null; +} + +/** + * Extract the function name from a def/defp call node. + * + * AST shape: call(target: identifier("def"), arguments(call(target: identifier("name"), ...), ...)) + * No-arg zero-clause: call(target: identifier("def"), arguments(identifier("name"), ...)) + */ +function extractElixirName(node: SyntaxNode): string | undefined { + if (!ELIXIR_DEF_KEYWORDS.has(callKeyword(node) ?? '')) return undefined; + const args = findArguments(node); + if (!args) return undefined; + for (let i = 0; i < args.namedChildCount; i++) { + const arg = args.namedChild(i); + if (!arg) continue; + if (arg.type === 'call') { + const innerTarget = arg.childForFieldName?.('target'); + if (innerTarget?.type === 'identifier') return innerTarget.text; + } + if (arg.type === 'identifier') return arg.text; + // binary_operator covers guard forms: def name(x) when is_integer(x) + if (arg.type === 'binary_operator') { + const left = arg.childForFieldName?.('left'); + if (left?.type === 'call') { + const innerTarget = left.childForFieldName?.('target'); + if (innerTarget?.type === 'identifier') return innerTarget.text; + } + } + } + return undefined; +} + +/** + * Extract parameters from the inner function-signature call inside a def node. + * Captures simple identifier parameters and their default values; skips + * pattern-match nodes and other complex destructuring. + */ +function extractElixirParameters(node: SyntaxNode): ParameterInfo[] { + const args = findArguments(node); + if (!args) return []; + + // Find the inner call (the function signature) + let sigArgs: SyntaxNode | null = null; + for (let i = 0; i < args.namedChildCount; i++) { + const arg = args.namedChild(i); + if (arg?.type === 'call') { + sigArgs = findArguments(arg); + break; + } + // guard form: binary_operator left: call + if (arg?.type === 'binary_operator') { + const left = arg.childForFieldName?.('left'); + if (left?.type === 'call') { + sigArgs = findArguments(left); + break; + } + } + } + if (!sigArgs) return []; + + const params: ParameterInfo[] = []; + for (let i = 0; i < sigArgs.namedChildCount; i++) { + const p = sigArgs.namedChild(i); + if (!p) continue; + if (p.type === 'identifier') { + params.push({ name: p.text, type: null, isOptional: false, isVariadic: false }); + } else if (p.type === 'binary_operator') { + const operator = p.children.find((child) => !child.isNamed)?.text; + const left = p.childForFieldName?.('left'); + if (operator === '\\\\' && left?.type === 'identifier') { + params.push({ name: left.text, type: null, isOptional: true, isVariadic: false }); + } + } + // Skip map/struct patterns, tuples, etc. — complex destructuring + } + return params; +} + +function extractElixirVisibility(node: SyntaxNode): MethodVisibility { + return ELIXIR_PRIVATE_KEYWORDS.has(callKeyword(node) ?? '') ? 'private' : 'public'; +} + +/** + * Walk up from a def/defp call to find the enclosing defmodule's alias name. + * Returns undefined for top-level functions (no parent defmodule). + */ +function extractElixirOwnerName(node: SyntaxNode): string | undefined { + let cur: SyntaxNode | null | undefined = node; + while (cur) { + if ( + cur.type === 'call' && + (callKeyword(cur) === 'defmodule' || callKeyword(cur) === 'defprotocol') + ) { + const pArgs = findArguments(cur); + if (pArgs) { + for (let i = 0; i < pArgs.namedChildCount; i++) { + const a = pArgs.namedChild(i); + if (a?.type === 'alias') return a.text; + } + } + } + cur = cur.parent; + } + return undefined; +} + +export const elixirMethodConfig: MethodExtractionConfig = { + language: SupportedLanguages.Elixir, + + // defmodule calls are the class-like containers; def/defp calls are the methods + typeDeclarationNodes: ['call'], + methodNodeTypes: ['call'], + bodyNodeTypes: ['do_block'], + staticOwnerTypes: new Set(), // opt out — Elixir has no static concept + + extractName: extractElixirName, + extractReturnType: () => undefined, // dynamic typing — no return type annotation in AST + extractParameters: extractElixirParameters, + extractVisibility: extractElixirVisibility, + extractOwnerName: extractElixirOwnerName, + + isStatic: () => false, + isAbstract: () => false, + isFinal: () => false, +}; diff --git a/gitnexus/src/core/ingestion/scope-resolution/pipeline/registry.ts b/gitnexus/src/core/ingestion/scope-resolution/pipeline/registry.ts index ee98e3807..afab1994e 100644 --- a/gitnexus/src/core/ingestion/scope-resolution/pipeline/registry.ts +++ b/gitnexus/src/core/ingestion/scope-resolution/pipeline/registry.ts @@ -29,6 +29,7 @@ import { dartScopeResolver } from '../../languages/dart/scope-resolver.js'; import { vueScopeResolver } from '../../languages/vue/scope-resolver.js'; import { zigScopeResolver } from '../../languages/zig/scope-resolver.js'; import { objectiveCScopeResolver } from '../../languages/objective-c/scope-resolver.js'; +import { elixirScopeResolver } from '../../languages/elixir/scope-resolver.js'; /** Map of `SupportedLanguages` → `ScopeResolver`. The scope-resolution phase * iterates this map directly — every registered resolver runs. This is the @@ -55,4 +56,5 @@ export const SCOPE_RESOLVERS: ReadonlyMap = n [SupportedLanguages.Vue, vueScopeResolver], [SupportedLanguages.Zig, zigScopeResolver], [SupportedLanguages.ObjectiveC, objectiveCScopeResolver], + [SupportedLanguages.Elixir, elixirScopeResolver], ]); diff --git a/gitnexus/src/core/ingestion/taint/elixir-model.ts b/gitnexus/src/core/ingestion/taint/elixir-model.ts new file mode 100644 index 000000000..73a8cf449 --- /dev/null +++ b/gitnexus/src/core/ingestion/taint/elixir-model.ts @@ -0,0 +1,29 @@ +import type { SourceSinkSanitizerSpec } from './source-sink-config.js'; + +/** Conservative Phoenix/Ecto and dynamic-evaluation model. */ +export const ELIXIR_TAINT_MODEL: SourceSinkSanitizerSpec = { + sources: [ + // `params["id"]` is represented by the Elixir grammar as an access call, + // whose conservative structural fact is `params.params`. + { + kind: 'remote-input', + objects: ['conn', 'socket'], + properties: ['params', 'body_params', 'query_params', 'path_params'], + }, + { kind: 'remote-input', objects: ['params'], properties: ['params'] }, + ], + sinks: [ + { name: 'eval_string', kind: 'code-injection', args: [0], receivers: ['Code'] }, + { name: 'eval_quoted', kind: 'code-injection', args: [0], receivers: ['Code'] }, + { name: 'apply', kind: 'code-injection', args: [0], receivers: ['erlang', ':erlang'] }, + { name: 'apply', kind: 'code-injection', args: [0], receivers: ['Kernel'] }, + { name: 'apply', kind: 'code-injection', args: [0], global: true }, + // Raw SQL/query APIs only: parameterized Repo.query(sql, params) is not a + // finding for params, because only SQL text (argument zero) is modelled. + { name: 'query', kind: 'sql-injection', args: [0], receivers: ['Repo'] }, + { name: 'query!', kind: 'sql-injection', args: [0], receivers: ['Repo'] }, + { name: 'query', kind: 'sql-injection', args: [1], receivers: ['Ecto.Adapters.SQL'] }, + { name: 'query!', kind: 'sql-injection', args: [1], receivers: ['Ecto.Adapters.SQL'] }, + ], + sanitizers: [], +}; diff --git a/gitnexus/src/core/ingestion/taint/match.ts b/gitnexus/src/core/ingestion/taint/match.ts index 8f4a69ff3..00aa49542 100644 --- a/gitnexus/src/core/ingestion/taint/match.ts +++ b/gitnexus/src/core/ingestion/taint/match.ts @@ -344,7 +344,11 @@ export function matchFunctionSites( return r.path.length >= 2 && r.path[r.path.length - 1] === entry.name; } if (entry.receivers !== undefined) { - return r.path.length === 2 && entry.receivers.includes(r.path[0]) && r.path[1] === entry.name; + return ( + r.path.length >= 2 && + entry.receivers.includes(r.path.slice(0, -1).join('.')) && + r.path.at(-1) === entry.name + ); } return false; }; diff --git a/gitnexus/src/core/ingestion/taint/typescript-model.ts b/gitnexus/src/core/ingestion/taint/typescript-model.ts index 6ef39cca9..891ab7da0 100644 --- a/gitnexus/src/core/ingestion/taint/typescript-model.ts +++ b/gitnexus/src/core/ingestion/taint/typescript-model.ts @@ -20,6 +20,7 @@ import { SupportedLanguages } from 'gitnexus-shared'; import type { SourceSinkSanitizerSpec } from './source-sink-config.js'; import { JAVA_TAINT_MODEL } from './java-model.js'; import { PYTHON_TAINT_MODEL } from './python-model.js'; +import { ELIXIR_TAINT_MODEL } from './elixir-model.js'; import { registerSourceSinkConfig } from './source-sink-registry.js'; /** @@ -135,6 +136,7 @@ export const BUILTIN_TAINT_MODELS = { [SupportedLanguages.Java]: JAVA_TAINT_MODEL, [SupportedLanguages.JavaScript]: TS_JS_TAINT_MODEL, [SupportedLanguages.Python]: PYTHON_TAINT_MODEL, + [SupportedLanguages.Elixir]: ELIXIR_TAINT_MODEL, [SupportedLanguages.TypeScript]: TS_JS_TAINT_MODEL, } as const satisfies Record; @@ -145,7 +147,7 @@ export const BUILTIN_TAINT_MODELS = { export const taintModelVersion: string = computeModelDigest(BUILTIN_TAINT_MODELS); /** - * Register the built-in models for Java, TypeScript, JavaScript, and Python. + * Register the built-in models for Java, TypeScript, JavaScript, Python, and Elixir. * Explicit init seam for the U4 emit path (call before the pdg window * consumes the registry); idempotent. Other language ids remain unregistered * until they have a dedicated model. @@ -155,4 +157,5 @@ export function registerBuiltinTaintModels(): void { registerSourceSinkConfig(SupportedLanguages.TypeScript, TS_JS_TAINT_MODEL); registerSourceSinkConfig(SupportedLanguages.JavaScript, TS_JS_TAINT_MODEL); registerSourceSinkConfig(SupportedLanguages.Python, PYTHON_TAINT_MODEL); + registerSourceSinkConfig(SupportedLanguages.Elixir, ELIXIR_TAINT_MODEL); } diff --git a/gitnexus/src/core/ingestion/tree-sitter-queries.ts b/gitnexus/src/core/ingestion/tree-sitter-queries.ts index fbde18729..1d05cdca8 100644 --- a/gitnexus/src/core/ingestion/tree-sitter-queries.ts +++ b/gitnexus/src/core/ingestion/tree-sitter-queries.ts @@ -2978,6 +2978,183 @@ export const ZIG_QUERIES = ` member: (identifier) @call.name)) @call `; +export const ELIXIR_QUERIES = ` +; ── Modules ────────────────────────────────────────────────────────────────── +(call + target: (identifier) @_def (#eq? @_def "defmodule") + (arguments + (alias) @name)) @definition.class + +; ── Protocols ───────────────────────────────────────────────────────────────── +(call + target: (identifier) @_def (#eq? @_def "defprotocol") + (arguments + (alias) @name)) @definition.interface + +; ── Public functions & macros ───────────────────────────────────────────────── +(call + target: (identifier) @_def (#eq? @_def "def") + (arguments + (call + target: (identifier) @name))) @definition.function + +(call + target: (identifier) @_def (#eq? @_def "def") + (arguments (identifier) @name)) @definition.function + +(call + target: (identifier) @_def (#eq? @_def "def") + (arguments (binary_operator left: (call target: (identifier) @name)))) @definition.function +(call + target: (identifier) @_def (#eq? @_def "def") + (arguments (binary_operator left: (identifier) @name))) @definition.function + + +(call + target: (identifier) @_def (#eq? @_def "defmacro") + (arguments + (call + target: (identifier) @name))) @definition.macro + +(call + target: (identifier) @_def (#eq? @_def "defmacro") + (arguments (identifier) @name)) @definition.macro + +(call + target: (identifier) @_def (#eq? @_def "defmacro") + (arguments (binary_operator left: (call target: (identifier) @name)))) @definition.macro +(call + target: (identifier) @_def (#eq? @_def "defmacro") + (arguments (binary_operator left: (identifier) @name))) @definition.macro + + +(call + target: (identifier) @_def (#eq? @_def "defguard") + (arguments + (call + target: (identifier) @name))) @definition.macro + +(call + target: (identifier) @_def (#eq? @_def "defguard") + (arguments (identifier) @name)) @definition.macro + +(call + target: (identifier) @_def (#eq? @_def "defguard") + (arguments (binary_operator left: (call target: (identifier) @name)))) @definition.macro +(call + target: (identifier) @_def (#eq? @_def "defguard") + (arguments (binary_operator left: (identifier) @name))) @definition.macro + + +(call + target: (identifier) @_def (#eq? @_def "defdelegate") + (arguments + (call + target: (identifier) @name))) @definition.function + +(call + target: (identifier) @_def (#eq? @_def "defdelegate") + (arguments (identifier) @name)) @definition.function + +(call + target: (identifier) @_def (#eq? @_def "defdelegate") + (arguments (binary_operator left: (call target: (identifier) @name)))) @definition.function + +; ── Private functions & macros ──────────────────────────────────────────────── +(call + target: (identifier) @_def (#eq? @_def "defp") + (arguments + (call + target: (identifier) @name))) @definition.function + +(call + target: (identifier) @_def (#eq? @_def "defp") + (arguments (identifier) @name)) @definition.function + +(call + target: (identifier) @_def (#eq? @_def "defp") + (arguments (binary_operator left: (call target: (identifier) @name)))) @definition.function +(call + target: (identifier) @_def (#eq? @_def "defp") + (arguments (binary_operator left: (identifier) @name))) @definition.function + + +(call + target: (identifier) @_def (#eq? @_def "defmacrop") + (arguments + (call + target: (identifier) @name))) @definition.macro + +(call + target: (identifier) @_def (#eq? @_def "defmacrop") + (arguments (identifier) @name)) @definition.macro + +(call + target: (identifier) @_def (#eq? @_def "defmacrop") + (arguments (binary_operator left: (call target: (identifier) @name)))) @definition.macro +(call + target: (identifier) @_def (#eq? @_def "defmacrop") + (arguments (binary_operator left: (identifier) @name))) @definition.macro + + +(call + target: (identifier) @_def (#eq? @_def "defguardp") + (arguments + (call + target: (identifier) @name))) @definition.macro + +(call + target: (identifier) @_def (#eq? @_def "defguardp") + (arguments (identifier) @name)) @definition.macro + +(call + target: (identifier) @_def (#eq? @_def "defguardp") + (arguments (binary_operator left: (call target: (identifier) @name)))) @definition.macro +(call + target: (identifier) @_def (#eq? @_def "defguardp") + (arguments (binary_operator left: (identifier) @name))) @definition.macro + + +; ── Imports: import/use/require ─────────────────────────────────────────────── +(call + target: (identifier) @_kw (#eq? @_kw "import") + (arguments + (alias) @import.source)) @import + +(call + target: (identifier) @_kw (#eq? @_kw "use") + (arguments + (alias) @import.source)) @import + +(call + target: (identifier) @_kw (#eq? @_kw "require") + (arguments + (alias) @import.source)) @import + +; ── Aliases ─────────────────────────────────────────────────────────────────── +(call + target: (identifier) @_kw (#eq? @_kw "alias") + (arguments + (alias) @import.source)) @import + +(call + target: (identifier) @_kw (#eq? @_kw "alias") + (arguments + (dot + left: (alias) + right: (tuple)) @import.source)) @import + +; ── Remote calls: Module.function() ────────────────────────────────────────── +(call + target: (dot + left: (alias) @call.receiver + right: (identifier) @call.name)) @call + +; ── Local calls: function() — provider filters definitions/control-flow ─────── +(call + target: (identifier) @call.name) @call +`; + import { SupportedLanguages } from 'gitnexus-shared'; const OBJECTIVE_C_QUERIES = `((translation_unit) @objc.root)`; @@ -3001,4 +3178,5 @@ export const LANGUAGE_QUERIES: Record = { [SupportedLanguages.Vue]: TYPESCRIPT_QUERIES, // Vue