mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-11-18 09:43:04 +00:00
Merge b8a235a4c1 into b92c14cdd0
This commit is contained in:
commit
8412442f62
56 changed files with 5764 additions and 35 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -407,6 +407,8 @@ Each language implements `LanguageProvider` (`language-provider.ts`). Key fields
|
|||
|
||||
18 providers in `languages/index.ts` via `satisfies Record<SupportedLanguages, LanguageProvider>` — 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`.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -55,6 +55,7 @@ const EXTENSION_MAP: Record<SupportedLanguages, readonly string[]> = {
|
|||
[SupportedLanguages.Vue]: ['.vue'],
|
||||
[SupportedLanguages.Cobol]: ['.cbl', '.cob', '.cpy', '.cobol'],
|
||||
[SupportedLanguages.Zig]: ['.zig'],
|
||||
[SupportedLanguages.Elixir]: ['.ex', '.exs'],
|
||||
} satisfies Record<SupportedLanguages, readonly string[]>; // Ensure exhaustiveness
|
||||
|
||||
/** Pre-built reverse lookup: extension → language (built once at module load). */
|
||||
|
|
@ -125,6 +126,7 @@ const SYNTAX_MAP: Record<SupportedLanguages, string> = {
|
|||
[SupportedLanguages.Vue]: 'typescript',
|
||||
[SupportedLanguages.Cobol]: 'cobol',
|
||||
[SupportedLanguages.Zig]: 'zig',
|
||||
[SupportedLanguages.Elixir]: 'elixir',
|
||||
} satisfies Record<SupportedLanguages, string>; // Ensure exhaustiveness
|
||||
|
||||
/** Non-code file extensions → Prism-compatible syntax identifiers */
|
||||
|
|
|
|||
|
|
@ -24,4 +24,5 @@ export enum SupportedLanguages {
|
|||
/** Standalone regex processor — no tree-sitter, no LanguageProvider. */
|
||||
Cobol = 'cobol',
|
||||
Zig = 'zig',
|
||||
Elixir = 'elixir',
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,6 +47,7 @@ export const LanguageClassifications: Readonly<Record<SupportedLanguages, Langua
|
|||
[SupportedLanguages.Vue]: 'experimental',
|
||||
[SupportedLanguages.Cobol]: 'experimental',
|
||||
[SupportedLanguages.Zig]: 'experimental',
|
||||
[SupportedLanguages.Elixir]: 'experimental',
|
||||
};
|
||||
|
||||
/** Convenience predicate: is this language gating Ring 4 retirement? */
|
||||
|
|
|
|||
|
|
@ -455,7 +455,7 @@ GitNexus supports indexing multiple repositories. Each `gitnexus analyze` regist
|
|||
|
||||
## Supported Languages
|
||||
|
||||
TypeScript, JavaScript, Python, Java, C, C++, C#, Go, Rust, PHP, Kotlin, Swift, Ruby, Dart, Zig
|
||||
TypeScript, JavaScript, Python, Java, C, C++, C#, Go, Rust, PHP, Kotlin, Swift, Ruby, Dart, Zig, and experimental Elixir
|
||||
|
||||
### Language Feature Matrix
|
||||
|
||||
|
|
@ -476,6 +476,7 @@ TypeScript, JavaScript, Python, Java, C, C++, C#, Go, Rust, PHP, Kotlin, Swift,
|
|||
| 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
|
||||
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -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);
|
||||
|
|
|
|||
21
gitnexus/package-lock.json
generated
21
gitnexus/package-lock.json
generated
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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'));
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -44,6 +44,25 @@ export const ensureAndParse = async (content: string, filePath: string): Promise
|
|||
return parseSourceSafe(parserInstance, parseContent);
|
||||
};
|
||||
|
||||
const ELIXIR_MODULE_CALL_KEYWORDS: Record<string, true> = {
|
||||
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);
|
||||
|
|
|
|||
537
gitnexus/src/core/ingestion/cfg/visitors/elixir.ts
Normal file
537
gitnexus/src/core/ingestion/cfg/visitors/elixir.ts
Normal file
|
|
@ -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<string, number>();
|
||||
|
||||
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<SyntaxNode>();
|
||||
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<string, number>();
|
||||
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<SyntaxNode> {
|
||||
return { isFunction, buildFunctionCfg };
|
||||
}
|
||||
|
||||
export { FUNCTION_TYPES as ELIXIR_FUNCTION_TYPES };
|
||||
|
|
@ -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;
|
||||
},
|
||||
};
|
||||
|
|
@ -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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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],
|
||||
};
|
||||
1041
gitnexus/src/core/ingestion/languages/elixir.ts
Normal file
1041
gitnexus/src/core/ingestion/languages/elixir.ts
Normal file
File diff suppressed because it is too large
Load diff
120
gitnexus/src/core/ingestion/languages/elixir/import-filters.ts
Normal file
120
gitnexus/src/core/ingestion/languages/elixir/import-filters.ts
Normal file
|
|
@ -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<string, ElixirImportExceptFact[]>();
|
||||
const importOnlyByFile = new Map<string, ElixirImportOnlyFact[]>();
|
||||
const frameworkFactsByFile = new Map<string, ElixirFrameworkFact[]>();
|
||||
|
||||
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
|
||||
: [];
|
||||
}
|
||||
413
gitnexus/src/core/ingestion/languages/elixir/scope-resolver.ts
Normal file
413
gitnexus/src/core/ingestion/languages/elixir/scope-resolver.ts
Normal file
|
|
@ -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<readonly ParsedFile[], ReadonlyMap<string, string>>();
|
||||
|
||||
function elixirModuleFiles(parsedFiles: readonly ParsedFile[]): ReadonlyMap<string, string> {
|
||||
const cached = moduleFilesByWorkspace.get(parsedFiles);
|
||||
if (cached) return cached;
|
||||
|
||||
const candidates = new Map<string, Set<string>>();
|
||||
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<string>();
|
||||
files.add(parsed.filePath);
|
||||
candidates.set(def.qualifiedName, files);
|
||||
}
|
||||
}
|
||||
|
||||
const unique = new Map<string, string>();
|
||||
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<string, ParsedFile[]>();
|
||||
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<ScopeId, Map<string, BindingRef[]>>;
|
||||
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<string, BindingRef[]>();
|
||||
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);
|
||||
},
|
||||
};
|
||||
435
gitnexus/src/core/ingestion/languages/elixir/semantic-graph.ts
Normal file
435
gitnexus/src/core/ingestion/languages/elixir/semantic-graph.ts
Normal file
|
|
@ -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<string>();
|
||||
const fileId = generateId('File', filePath);
|
||||
const moduleIds = new Map<string, string>();
|
||||
const evidenceTargets: { evidenceId: string; kind: string; target: string }[] = [];
|
||||
const definitions = new Map<string, SyntaxNode>();
|
||||
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<string, unknown>,
|
||||
): 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 };
|
||||
}
|
||||
|
|
@ -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<SupportedLanguages, LanguageProvider>;
|
||||
|
||||
/** Get provider by language enum (always succeeds for SupportedLanguages). */
|
||||
|
|
|
|||
160
gitnexus/src/core/ingestion/method-extractors/configs/elixir.ts
Normal file
160
gitnexus/src/core/ingestion/method-extractors/configs/elixir.ts
Normal file
|
|
@ -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,
|
||||
};
|
||||
|
|
@ -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<SupportedLanguages, ScopeResolver> = n
|
|||
[SupportedLanguages.Vue, vueScopeResolver],
|
||||
[SupportedLanguages.Zig, zigScopeResolver],
|
||||
[SupportedLanguages.ObjectiveC, objectiveCScopeResolver],
|
||||
[SupportedLanguages.Elixir, elixirScopeResolver],
|
||||
]);
|
||||
|
|
|
|||
29
gitnexus/src/core/ingestion/taint/elixir-model.ts
Normal file
29
gitnexus/src/core/ingestion/taint/elixir-model.ts
Normal file
|
|
@ -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: [],
|
||||
};
|
||||
|
|
@ -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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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<string, SourceSinkSanitizerSpec>;
|
||||
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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, string> = {
|
|||
[SupportedLanguages.Vue]: TYPESCRIPT_QUERIES, // Vue <script> blocks are parsed as TypeScript
|
||||
[SupportedLanguages.Cobol]: '', // Standalone regex processor — no tree-sitter queries
|
||||
[SupportedLanguages.Zig]: ZIG_QUERIES,
|
||||
[SupportedLanguages.Elixir]: ELIXIR_QUERIES,
|
||||
};
|
||||
|
|
|
|||
19
gitnexus/src/core/ingestion/type-extractors/elixir.ts
Normal file
19
gitnexus/src/core/ingestion/type-extractors/elixir.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import type { LanguageTypeConfig } from './types.js';
|
||||
|
||||
/**
|
||||
* Elixir type extractor — minimal implementation.
|
||||
*
|
||||
* Elixir is dynamically typed with no mandatory type annotations.
|
||||
* Typespecs (@spec) exist but are comments to the compiler and
|
||||
* don't appear as typed AST nodes in tree-sitter-elixir. Pattern
|
||||
* matching in function heads provides structural dispatch, not type
|
||||
* dispatch, so Tier 0 declaration extraction is not applicable.
|
||||
*
|
||||
* This config is intentionally minimal. Tier 1 constructor inference
|
||||
* and Tier 2 propagation can be added as a follow-up when needed.
|
||||
*/
|
||||
export const typeConfig: LanguageTypeConfig = {
|
||||
declarationNodeTypes: new Set<string>(),
|
||||
extractDeclaration: () => {},
|
||||
extractParameter: () => {},
|
||||
};
|
||||
|
|
@ -928,8 +928,10 @@ export const findEnclosingClassInfo = (
|
|||
/**
|
||||
* Optional: the type a CONTAINER node declares
|
||||
* (`LanguageProvider.resolveContainerTypeOwner`). Consulted for every
|
||||
* `CLASS_CONTAINER_TYPES` node the walk meets, before the generic name-child
|
||||
* derivation, for languages whose containers are named from context (a
|
||||
* ancestor that reaches generic container resolution; it must return null
|
||||
* for nodes it does not own.
|
||||
* This supports languages whose containers are outside `CLASS_CONTAINER_TYPES`
|
||||
* (for example, Elixir's `defmodule` call) or named from context (a
|
||||
* binding wrapper, an enclosing callable, an anonymous ordinal). Null falls
|
||||
* through to the generic derivation.
|
||||
*/
|
||||
|
|
@ -1010,6 +1012,17 @@ export const findEnclosingClassInfo = (
|
|||
};
|
||||
}
|
||||
}
|
||||
// Provider-owned containers may use grammar nodes outside the generic class
|
||||
// list (Elixir's `defmodule` is a call). Returning null preserves fallback.
|
||||
if (resolveContainerTypeOwner !== undefined) {
|
||||
const containerOwner = resolveContainerTypeOwner(current, filePath);
|
||||
if (containerOwner !== null) {
|
||||
return {
|
||||
classId: generateId(containerOwner.label, `${filePath}:${containerOwner.name}`),
|
||||
className: containerOwner.name,
|
||||
};
|
||||
}
|
||||
}
|
||||
if (CLASS_CONTAINER_TYPES.has(current.type)) {
|
||||
// Delegate language-specific container remapping to the provider hook.
|
||||
if (resolveEnclosingOwner) {
|
||||
|
|
@ -1034,19 +1047,6 @@ export const findEnclosingClassInfo = (
|
|||
}
|
||||
}
|
||||
|
||||
// A container the PROVIDER names from context (binding wrapper,
|
||||
// enclosing callable, anonymous ordinal — Zig). The name is what the
|
||||
// class-like node is minted under, so owner id == node id.
|
||||
if (resolveContainerTypeOwner !== undefined) {
|
||||
const containerOwner = resolveContainerTypeOwner(current, filePath);
|
||||
if (containerOwner !== null) {
|
||||
return {
|
||||
classId: generateId(containerOwner.label, `${filePath}:${containerOwner.name}`),
|
||||
className: containerOwner.name,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Rust impl_item: for `impl Trait for Struct {}`, pick the type after `for`
|
||||
// NOTE: This impl_item ownership logic is mirrored in
|
||||
// method-extractors/configs/rust.ts (extractOwnerName, metadata only).
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { parentPort, threadId, workerData } from 'node:worker_threads';
|
||||
import { createRequire } from 'node:module';
|
||||
import {
|
||||
boundCallableStartPosition,
|
||||
localIdentity,
|
||||
|
|
@ -45,6 +46,7 @@ import type {
|
|||
|
||||
/** Language grammar type accepted by Parser.setLanguage(). */
|
||||
type TreeSitterLanguage = Parameters<typeof Parser.prototype.setLanguage>[0];
|
||||
const _require = createRequire(import.meta.url);
|
||||
|
||||
// ── Worker grammar loading — enforcement boundary (#2091/#2093, #2101) ───────
|
||||
// The worker maintains its own grammar table (the guarded vendored-grammar
|
||||
|
|
@ -93,6 +95,11 @@ let ObjectiveC: TreeSitterLanguage | null = null;
|
|||
try {
|
||||
ObjectiveC = requireVendoredGrammar('tree-sitter-objc') as TreeSitterLanguage;
|
||||
} catch {}
|
||||
|
||||
let Elixir: TreeSitterLanguage | null = null;
|
||||
try {
|
||||
Elixir = _require('tree-sitter-elixir');
|
||||
} catch {}
|
||||
import { getLanguageFromFilename } from 'gitnexus-shared';
|
||||
import {
|
||||
buildDefinitionPreScan,
|
||||
|
|
@ -574,6 +581,7 @@ const languageMap: Record<string, TreeSitterLanguage> = {
|
|||
...(Dart ? { [SupportedLanguages.Dart]: Dart } : {}),
|
||||
...(Swift ? { [SupportedLanguages.Swift]: Swift } : {}),
|
||||
...(Zig ? { [SupportedLanguages.Zig]: Zig } : {}),
|
||||
...(Elixir ? { [SupportedLanguages.Elixir]: Elixir } : {}),
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -197,6 +197,14 @@ const SOURCES: Record<string, GrammarSource> = {
|
|||
'`gitnexus/vendor/tree-sitter-zig`) failed to load. ' +
|
||||
'Likely cause: no prebuilt `.node` for this platform/architecture.',
|
||||
},
|
||||
[SupportedLanguages.Elixir]: {
|
||||
load: () => _require('tree-sitter-elixir'),
|
||||
optional: true,
|
||||
severity: 'error',
|
||||
unavailableNote:
|
||||
'Elixir parsing disabled: `tree-sitter-elixir` could not be loaded. ' +
|
||||
'Try `npm rebuild tree-sitter-elixir` or reinstalling, then re-run analyze.',
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -766,6 +766,8 @@ import { copyV8CacheIfPresent, tryLoadV8Cache, writeV8CacheFile } from './v8-sid
|
|||
// v100 (#3253): Rust import captures preserve the leading `::` that selects
|
||||
// the extern prelude. Old warm captures erase it and cannot distinguish an
|
||||
// absolute library import from a same-named local module. Reparse both stores.
|
||||
// v102: Elixir tree-sitter captures and worker output are newly persisted.
|
||||
// Reparse unchanged Elixir chunks rather than replaying pre-provider output.
|
||||
// v101 (#3294 review): Rust bare-keyword glob imports retain crate/self/super
|
||||
// instead of an empty target path; restricted pub(...) imports are no longer
|
||||
// captured as unrestricted reexports. Re-extract both facts on warm indexes.
|
||||
|
|
@ -778,7 +780,19 @@ import { copyV8CacheIfPresent, tryLoadV8Cache, writeV8CacheFile } from './v8-sid
|
|||
// v104 (#3339 review): TS/JS pair-HOC queries now name object-pair
|
||||
// `mutation(withAuth(arrow))` handlers. Warm caches replay the pre-fix
|
||||
// capture set (anonymous arrows, no Function name), so both stores re-extract.
|
||||
const SCHEMA_BUMP = 104;
|
||||
// v105: Elixir provider-owned graph nodes now follow the graph's 0-based line
|
||||
// convention. Warm v104 entries otherwise retain 1-based declaration lines.
|
||||
// v106: Elixir scope captures now include defdelegates and relative captured
|
||||
// module calls. Warm v105 entries cannot resolve those edges correctly.
|
||||
// v107: Elixir lexical alias captures encode position-sensitive resolution.
|
||||
// v108: Alias captures now respect nested lexical-container visibility.
|
||||
// v109: Quoted Elixir captures are excluded from runtime call facts.
|
||||
// v110: Elixir import-category facts and CFG formal/anonymous-clause captures
|
||||
// change persisted worker output, so warm shards must be re-parsed.
|
||||
// v111: Elixir declaration captures, behaviour classification, pinned-match
|
||||
// CFG def/use facts, and literal Phoenix resource filters change persisted
|
||||
// ParsedFile and framework facts.
|
||||
const SCHEMA_BUMP = 111;
|
||||
const GITNEXUS_PKG_VERSION = (() => {
|
||||
try {
|
||||
// package.json sits at gitnexus/package.json — two levels up from
|
||||
|
|
|
|||
35
gitnexus/test/fixtures/sample-code/simple.ex
vendored
Normal file
35
gitnexus/test/fixtures/sample-code/simple.ex
vendored
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
defmodule MyApp.User do
|
||||
alias MyApp.Repo
|
||||
import Ecto.Query, only: [from: 2]
|
||||
use Phoenix.LiveView
|
||||
require Logger
|
||||
|
||||
@behaviour MyApp.UserBehaviour
|
||||
|
||||
def get(id) do
|
||||
Repo.get(MyApp.User, id)
|
||||
end
|
||||
|
||||
def get_by_email(email) do
|
||||
from(u in MyApp.User, where: u.email == ^email)
|
||||
|> Repo.one()
|
||||
end
|
||||
|
||||
defp validate(user) do
|
||||
user
|
||||
end
|
||||
|
||||
def create(attrs) do
|
||||
%MyApp.User{}
|
||||
|> MyApp.User.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
end
|
||||
|
||||
defmodule MyApp.UserProtocol do
|
||||
@doc "A protocol for user operations"
|
||||
end
|
||||
|
||||
defprotocol MyApp.Serializable do
|
||||
def serialize(data)
|
||||
end
|
||||
|
|
@ -66,6 +66,7 @@ const GRAMMAR_PACKAGES: Partial<Record<SupportedLanguages, { pkg: string; subpat
|
|||
[SupportedLanguages.Kotlin]: { pkg: 'tree-sitter-kotlin', subpaths: ['src/node-types.json'] },
|
||||
[SupportedLanguages.Swift]: { pkg: 'tree-sitter-swift', subpaths: ['src/node-types.json'] },
|
||||
[SupportedLanguages.Dart]: { pkg: 'tree-sitter-dart', subpaths: ['src/node-types.json'] },
|
||||
[SupportedLanguages.Elixir]: { pkg: 'tree-sitter-elixir', subpaths: ['src/node-types.json'] },
|
||||
[SupportedLanguages.Vue]: {
|
||||
pkg: 'tree-sitter-typescript',
|
||||
subpaths: ['typescript/src/node-types.json', 'tsx/src/node-types.json'],
|
||||
|
|
|
|||
|
|
@ -87,6 +87,7 @@ const DIR_LANG: Record<string, SupportedLanguages> = {
|
|||
kotlin: SupportedLanguages.Kotlin,
|
||||
swift: SupportedLanguages.Swift,
|
||||
dart: SupportedLanguages.Dart,
|
||||
elixir: SupportedLanguages.Elixir,
|
||||
vue: SupportedLanguages.Vue,
|
||||
zig: SupportedLanguages.Zig,
|
||||
};
|
||||
|
|
@ -98,6 +99,7 @@ const BASENAME_LANGS: Record<string, SupportedLanguages[]> = {
|
|||
'typescript-javascript': [SupportedLanguages.TypeScript, SupportedLanguages.JavaScript],
|
||||
csharp: [SupportedLanguages.CSharp],
|
||||
dart: [SupportedLanguages.Dart],
|
||||
elixir: [SupportedLanguages.Elixir],
|
||||
go: [SupportedLanguages.Go],
|
||||
php: [SupportedLanguages.PHP],
|
||||
python: [SupportedLanguages.Python],
|
||||
|
|
|
|||
183
gitnexus/test/integration/elixir-pdg-persistence.test.ts
Normal file
183
gitnexus/test/integration/elixir-pdg-persistence.test.ts
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
/** Persisted Elixir PDG/taint integration coverage. */
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
|
||||
import { LocalBackend } from '../../src/mcp/local/local-backend.js';
|
||||
import { runPipelineFromRepo } from '../../src/core/ingestion/pipeline.js';
|
||||
import { listRegisteredRepos } from '../../src/storage/repo-manager.js';
|
||||
import {
|
||||
loadParseCache,
|
||||
PARSE_CACHE_VERSION,
|
||||
pruneCache,
|
||||
saveParseCache,
|
||||
type ParseCache,
|
||||
} from '../../src/storage/parse-cache.js';
|
||||
import {
|
||||
getDurableParsedFileDir,
|
||||
pruneAndSaveDurableParsedFileStore,
|
||||
} from '../../src/storage/parsedfile-store.js';
|
||||
import { withTestLbugDB } from '../helpers/test-indexed-db.js';
|
||||
|
||||
vi.mock('../../src/storage/repo-manager.js', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('../../src/storage/repo-manager.js')>()),
|
||||
listRegisteredRepos: vi.fn().mockResolvedValue([]),
|
||||
cleanupOldKuzuFiles: vi.fn().mockResolvedValue({ found: false, needsReindex: false }),
|
||||
findSiblingClones: vi.fn().mockResolvedValue([]),
|
||||
}));
|
||||
|
||||
let fixtureRoot = '';
|
||||
|
||||
function writeFixture(root: string): void {
|
||||
fs.mkdirSync(path.join(root, 'lib/my_app'), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(root, 'lib/my_app/security.ex'),
|
||||
`
|
||||
defmodule MyApp.Security do
|
||||
def vulnerable(conn, params) do
|
||||
code = conn.params["code"]
|
||||
sql = params["sql"]
|
||||
Code.eval_string(code)
|
||||
Repo.query!(sql)
|
||||
Repo.query!("select * from posts where id = ?", [sql])
|
||||
end
|
||||
|
||||
def guarded(value) when value != nil do
|
||||
rebound = value
|
||||
if rebound do
|
||||
result = rebound
|
||||
Code.eval_string(result)
|
||||
else
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
def closure(value) do
|
||||
fn arg -> result = value <> arg; Code.eval_string(result) end
|
||||
end
|
||||
|
||||
def mailbox do
|
||||
receive do message -> message end
|
||||
end
|
||||
end
|
||||
`,
|
||||
);
|
||||
}
|
||||
|
||||
afterAll(() => {
|
||||
if (fixtureRoot) fs.rmSync(fixtureRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
withTestLbugDB(
|
||||
'elixir-pdg-persistence',
|
||||
(handle) => {
|
||||
describe('persisted Elixir PDG and taint', () => {
|
||||
let backend: LocalBackend;
|
||||
beforeAll(() => {
|
||||
const extended = handle as typeof handle & { _backend?: LocalBackend };
|
||||
if (!extended._backend) throw new Error('LocalBackend not initialized');
|
||||
backend = extended._backend;
|
||||
});
|
||||
|
||||
it('surfaces only dynamic-eval and raw-SQL taint findings after COPY persistence', async () => {
|
||||
const result = (await backend.callTool('explain', { target: 'vulnerable' })) as any;
|
||||
expect(result).not.toHaveProperty('error');
|
||||
expect(result.findings.map((finding: any) => finding.sinkKind).sort()).toEqual([
|
||||
'code-injection',
|
||||
'sql-injection',
|
||||
]);
|
||||
expect(result.findings.some((finding: any) => finding.sink?.line === 8)).toBe(false);
|
||||
});
|
||||
|
||||
it('persists representative CDG and reaching-definition results without receive cross-process flow', async () => {
|
||||
const controls = (await backend.callTool('pdg_query', {
|
||||
mode: 'controls',
|
||||
target: 'mailbox',
|
||||
})) as any;
|
||||
const flows = (await backend.callTool('pdg_query', {
|
||||
mode: 'flows',
|
||||
target: 'mailbox',
|
||||
variable: 'message',
|
||||
})) as any;
|
||||
expect(controls).not.toHaveProperty('error');
|
||||
expect(controls.results.length).toBeGreaterThan(0);
|
||||
expect(flows).not.toHaveProperty('error');
|
||||
expect(flows.results).toEqual([]);
|
||||
expect(JSON.stringify([...controls.results, ...flows.results])).not.toMatch(
|
||||
/message-delivery/i,
|
||||
);
|
||||
});
|
||||
});
|
||||
},
|
||||
{
|
||||
poolAdapter: true,
|
||||
beforeFTS: async (dbPath) => {
|
||||
fixtureRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'gn-elixir-pdg-'));
|
||||
writeFixture(fixtureRoot);
|
||||
const cachePath = path.join(fixtureRoot, '.cache');
|
||||
const cache: ParseCache = {
|
||||
version: PARSE_CACHE_VERSION,
|
||||
entries: new Map(),
|
||||
usedKeys: new Set(),
|
||||
storagePath: cachePath,
|
||||
onDiskKeys: new Set(),
|
||||
};
|
||||
const cold = await runPipelineFromRepo(fixtureRoot, () => {}, {
|
||||
pdg: true,
|
||||
parseCache: cache,
|
||||
workerPoolSize: 1,
|
||||
});
|
||||
pruneCache(cache, cache.usedKeys);
|
||||
const saved = await saveParseCache(cachePath, cache);
|
||||
await pruneAndSaveDurableParsedFileStore(
|
||||
getDurableParsedFileDir(cachePath),
|
||||
PARSE_CACHE_VERSION,
|
||||
new Set(saved),
|
||||
);
|
||||
const warm = await loadParseCache(cachePath);
|
||||
const replay = await runPipelineFromRepo(fixtureRoot, () => {}, {
|
||||
pdg: true,
|
||||
parseCache: warm ?? undefined,
|
||||
workerPoolSize: 1,
|
||||
});
|
||||
const normal = await runPipelineFromRepo(fixtureRoot, () => {}, { workerPoolSize: 1 });
|
||||
expect(cold.usedWorkerPool).toBe(true);
|
||||
expect(replay.usedWorkerPool).toBe(false);
|
||||
expect(
|
||||
[...replay.graph.iterRelationships()]
|
||||
.filter((rel) => ['CDG', 'REACHING_DEF', 'TAINTED'].includes(rel.type))
|
||||
.map((rel) => `${rel.type}:${rel.reason}`)
|
||||
.sort(),
|
||||
).toEqual(
|
||||
[...cold.graph.iterRelationships()]
|
||||
.filter((rel) => ['CDG', 'REACHING_DEF', 'TAINTED'].includes(rel.type))
|
||||
.map((rel) => `${rel.type}:${rel.reason}`)
|
||||
.sort(),
|
||||
);
|
||||
expect(
|
||||
[...cold.graph.iterNodes()].filter((node) => node.label === 'BasicBlock').length,
|
||||
).toBeGreaterThan(0);
|
||||
expect([...cold.graph.iterRelationships()].some((rel) => rel.type === 'TAINTED')).toBe(true);
|
||||
expect(
|
||||
[...normal.graph.iterNodes()].filter((node) => node.label === 'BasicBlock'),
|
||||
).toHaveLength(0);
|
||||
const adapter = await import('../../src/core/lbug/lbug-adapter.js');
|
||||
await adapter.loadGraphToLbug(cold.graph, fixtureRoot, path.dirname(dbPath));
|
||||
},
|
||||
afterSetup: async (handle) => {
|
||||
vi.mocked(listRegisteredRepos).mockResolvedValue([
|
||||
{
|
||||
name: 'elixir-pdg-repo',
|
||||
path: fixtureRoot,
|
||||
storagePath: handle.tmpHandle.dbPath,
|
||||
indexedAt: new Date().toISOString(),
|
||||
lastCommit: 'elixir-pdg',
|
||||
stats: { files: 1, nodes: 1, communities: 0, processes: 0 },
|
||||
},
|
||||
]);
|
||||
const backend = new LocalBackend();
|
||||
await backend.init();
|
||||
(handle as any)._backend = backend;
|
||||
},
|
||||
},
|
||||
);
|
||||
617
gitnexus/test/integration/elixir-phoenix-ecto-pipeline.test.ts
Normal file
617
gitnexus/test/integration/elixir-phoenix-ecto-pipeline.test.ts
Normal file
|
|
@ -0,0 +1,617 @@
|
|||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { runPipelineFromRepo } from '../../src/core/ingestion/pipeline.js';
|
||||
import {
|
||||
loadParseCache,
|
||||
PARSE_CACHE_VERSION,
|
||||
pruneCache,
|
||||
saveParseCache,
|
||||
type ParseCache,
|
||||
} from '../../src/storage/parse-cache.js';
|
||||
import {
|
||||
getDurableParsedFileDir,
|
||||
pruneAndSaveDurableParsedFileStore,
|
||||
} from '../../src/storage/parsedfile-store.js';
|
||||
import type { PipelineResult } from '../../src/types/pipeline.js';
|
||||
|
||||
function writeFixture(root: string): void {
|
||||
const write = (file: string, source: string) => {
|
||||
const target = path.join(root, file);
|
||||
fs.mkdirSync(path.dirname(target), { recursive: true });
|
||||
fs.writeFileSync(target, source);
|
||||
};
|
||||
write(
|
||||
'lib/my_app_web/router.ex',
|
||||
`
|
||||
defmodule MyAppWeb.Router do
|
||||
use MyAppWeb, :router
|
||||
pipeline :browser do
|
||||
plug :accepts, ["html"]
|
||||
plug :fetch_session
|
||||
end
|
||||
scope "/api", MyAppWeb do
|
||||
pipe_through :browser
|
||||
scope "/v1" do
|
||||
get "/posts", PostController, :index
|
||||
resources "/accounts", AccountController
|
||||
resources "/only-accounts", AccountController, only: :index
|
||||
resources "/except-accounts", AccountController, except: :show
|
||||
live "/dashboard", DashboardLive
|
||||
get dynamic_path(), PostController, :missing
|
||||
end
|
||||
end
|
||||
scope "/", MyAppWeb do
|
||||
get "/root", PostController, :index
|
||||
end
|
||||
scope alias: MyAppWeb do
|
||||
pipe_through [:browser, :api]
|
||||
get "/keyword", PostController, :index
|
||||
end
|
||||
end
|
||||
`,
|
||||
);
|
||||
write(
|
||||
'lib/my_app_web/controllers/post_controller.ex',
|
||||
`
|
||||
defmodule MyAppWeb.PostController do
|
||||
use MyAppWeb, :controller
|
||||
def index(conn, _params), do: conn
|
||||
end
|
||||
`,
|
||||
);
|
||||
write(
|
||||
'lib/my_app_web/controllers/account_controller.ex',
|
||||
`
|
||||
defmodule MyAppWeb.AccountController do
|
||||
use MyAppWeb, :controller
|
||||
def index(conn, _params), do: conn
|
||||
def show(conn, _params), do: conn
|
||||
def new(conn, _params), do: conn
|
||||
def create(conn, _params), do: conn
|
||||
def update(conn, _params), do: conn
|
||||
def delete(conn, _params), do: conn
|
||||
end
|
||||
`,
|
||||
);
|
||||
write(
|
||||
'lib/my_app_web/live/dashboard_live.ex',
|
||||
`
|
||||
defmodule MyAppWeb.DashboardLive do
|
||||
use MyAppWeb, :live_view
|
||||
def mount(_params, _session, socket), do: {:ok, socket}
|
||||
end
|
||||
`,
|
||||
);
|
||||
write(
|
||||
'lib/my_app/blog/post.ex',
|
||||
`
|
||||
defmodule MyApp.Blog.Post do
|
||||
use Ecto.Schema
|
||||
import Ecto.Query
|
||||
schema "posts" do
|
||||
field :title, :string
|
||||
belongs_to :author, MyApp.Accounts.Author
|
||||
has_many :comments, MyApp.Blog.Comment
|
||||
embeds_one :metadata, MyApp.Blog.Metadata
|
||||
end
|
||||
def runtime_field, do: field(:status)
|
||||
end
|
||||
defmodule MyApp.Blog.Metadata do
|
||||
use Ecto.Schema
|
||||
embedded_schema do
|
||||
field :slug, :string
|
||||
end
|
||||
end
|
||||
`,
|
||||
);
|
||||
write(
|
||||
'lib/my_app/blog/comment.ex',
|
||||
`
|
||||
defmodule MyApp.Blog.Comment do
|
||||
use Ecto.Schema
|
||||
schema "comments" do
|
||||
field :body, :string
|
||||
end
|
||||
end
|
||||
defmodule MyApp.Accounts.Author do
|
||||
use Ecto.Schema
|
||||
schema "authors" do
|
||||
field :name, :string
|
||||
end
|
||||
end
|
||||
`,
|
||||
);
|
||||
write(
|
||||
'lib/my_app/repo.ex',
|
||||
`
|
||||
defmodule MyApp.Repo do
|
||||
def all(_query), do: []
|
||||
def insert(_model), do: :ok
|
||||
end
|
||||
`,
|
||||
);
|
||||
write(
|
||||
'lib/my_app/blog.ex',
|
||||
`
|
||||
defmodule MyApp.Blog do
|
||||
import Ecto.Query
|
||||
import MyApp.Helpers, only: [f: 1]
|
||||
alias MyApp.Repo
|
||||
alias MyApp.Blog.Post
|
||||
def list_posts do
|
||||
from p in MyApp.Blog.Post, where: p.title != ""
|
||||
|> Repo.all()
|
||||
end
|
||||
def create_post, do: Repo.insert(MyApp.Blog.Post)
|
||||
def only_call(value), do: f(value)
|
||||
import MyApp.Helpers
|
||||
import MyApp.Helpers, except: [public_macro: 0]
|
||||
def wildcard_public(value), do: public(value)
|
||||
def wildcard_private(value), do: private(value)
|
||||
def wildcard_private_macro, do: private_macro()
|
||||
def except_public(value), do: public(value)
|
||||
def except_private(value), do: private(value)
|
||||
def except_private_macro, do: private_macro()
|
||||
import MyApp.Behaviour
|
||||
def callback_call(value), do: required(value)
|
||||
def ignored, do: Repo.all(dynamic_schema())
|
||||
end
|
||||
`,
|
||||
);
|
||||
write(
|
||||
'lib/my_app/query_only.ex',
|
||||
`
|
||||
defmodule MyApp.QueryOnly do
|
||||
import Ecto.Query
|
||||
def all, do: from p in MyApp.Blog.Post
|
||||
end
|
||||
`,
|
||||
);
|
||||
write(
|
||||
'lib/my_app/behaviour.ex',
|
||||
`
|
||||
defmodule MyApp.Behaviour do
|
||||
@callback required(term()) :: term()
|
||||
end
|
||||
defmodule MyApp.Implementation do
|
||||
@behaviour MyApp.Behaviour
|
||||
def required(value), do: value
|
||||
end
|
||||
`,
|
||||
);
|
||||
write(
|
||||
'lib/my_app/behaviour_negatives.ex',
|
||||
`
|
||||
defmodule MyApp.ArityBehaviour do
|
||||
@callback mismatch(left, right) :: term()
|
||||
end
|
||||
defmodule MyApp.DuplicateBehaviour do
|
||||
@callback ambiguous(value) :: term()
|
||||
end
|
||||
defmodule MyApp.DuplicateBehaviour do
|
||||
@callback ambiguous(value) :: term()
|
||||
end
|
||||
`,
|
||||
);
|
||||
write(
|
||||
'lib/my_app/behaviour_implementations.ex',
|
||||
`
|
||||
defmodule MyApp.ArityImplementation do
|
||||
@behaviour MyApp.ArityBehaviour
|
||||
def mismatch(value), do: value
|
||||
end
|
||||
defmodule MyApp.AmbiguousImplementation do
|
||||
@behaviour MyApp.DuplicateBehaviour
|
||||
def ambiguous(value), do: value
|
||||
end
|
||||
`,
|
||||
);
|
||||
write(
|
||||
'lib/my_app/helpers.ex',
|
||||
`
|
||||
defmodule MyApp.Helpers do
|
||||
def f(value), do: value
|
||||
def f(left, right), do: {left, right}
|
||||
def public(value), do: value
|
||||
defmacro public_macro, do: :ok
|
||||
defp private(value), do: value
|
||||
defmacrop private_macro, do: :ok
|
||||
end
|
||||
`,
|
||||
);
|
||||
write(
|
||||
'lib/my_app/captures.ex',
|
||||
`
|
||||
defmodule MyApp.Captures do
|
||||
alias MyApp.Tasks
|
||||
def run(fun), do: fun.()
|
||||
def example_task, do: run(&Tasks.ExampleTask.run/0)
|
||||
end
|
||||
defmodule MyApp.Tasks.ExampleTask do
|
||||
def run, do: :ok
|
||||
end
|
||||
defmodule MyApp.UnaliasedCaptures do
|
||||
def run(fun), do: fun.()
|
||||
def example_task, do: run(&Tasks.ExampleTask.run/0)
|
||||
end
|
||||
defmodule MyApp.UnaliasedCaptures.Tasks.ExampleTask do
|
||||
def run, do: :wrong
|
||||
end
|
||||
defmodule MyApp.DifferentRootCaptures do
|
||||
alias Elsewhere.Tasks
|
||||
def run(fun), do: fun.()
|
||||
def example_task, do: run(&Tasks.ExampleTask.run/0)
|
||||
end
|
||||
defmodule Elsewhere.Tasks.ExampleTask do
|
||||
def run, do: :ok
|
||||
end
|
||||
defmodule MyApp.AmbiguousCaptures do
|
||||
alias MyApp.AmbiguousTasks
|
||||
def run(fun), do: fun.()
|
||||
def example_task, do: run(&AmbiguousTasks.ExampleTask.run/0)
|
||||
end
|
||||
defmodule MyApp.AmbiguousTasks.ExampleTask do
|
||||
def run, do: :one
|
||||
end
|
||||
defmodule MyApp.AmbiguousTasks.ExampleTask do
|
||||
def run, do: :two
|
||||
end
|
||||
defmodule MyApp.PrivateCaptures do
|
||||
alias MyApp.PrivateTasks
|
||||
def run(fun), do: fun.()
|
||||
def example_task, do: run(&PrivateTasks.ExampleTask.run/0)
|
||||
end
|
||||
defmodule MyApp.PrivateTasks.ExampleTask do
|
||||
defp run, do: :private
|
||||
end
|
||||
defmodule MyApp.ArityCaptures do
|
||||
alias MyApp.ArityTasks
|
||||
def run(fun), do: fun.()
|
||||
def example_task, do: run(&ArityTasks.ExampleTask.run/0)
|
||||
end
|
||||
defmodule MyApp.ArityTasks.ExampleTask do
|
||||
def run(value), do: value
|
||||
end
|
||||
defmodule MyApp.AliasOrdering do
|
||||
def run(fun), do: fun.()
|
||||
def before_alias, do: run(&Tasks.ExampleTask.run/0)
|
||||
alias MyApp.Tasks
|
||||
def first_alias, do: run(&Tasks.ExampleTask.run/0)
|
||||
alias Elsewhere.Tasks
|
||||
def rebound_alias, do: run(&Tasks.ExampleTask.run/0)
|
||||
end
|
||||
defmodule MyApp.AliasContainment do
|
||||
def run(fun), do: fun.()
|
||||
def sibling_alias do
|
||||
alias MyApp.Tasks
|
||||
:ok
|
||||
end
|
||||
def after_sibling, do: run(&Tasks.ExampleTask.run/0)
|
||||
def quoted_alias do
|
||||
quote do
|
||||
alias MyApp.Tasks
|
||||
end
|
||||
end
|
||||
def after_quote, do: run(&Tasks.ExampleTask.run/0)
|
||||
def quoted_capture do
|
||||
quote do
|
||||
alias MyApp.Tasks
|
||||
&Tasks.ExampleTask.run/0
|
||||
end
|
||||
end
|
||||
def after_block do
|
||||
if true do
|
||||
alias MyApp.Tasks
|
||||
end
|
||||
run(&Tasks.ExampleTask.run/0)
|
||||
end
|
||||
end
|
||||
`,
|
||||
);
|
||||
write(
|
||||
'lib/my_app/delegates.ex',
|
||||
`
|
||||
defmodule MyApp.Delegates do
|
||||
defdelegate first(value), to: MyApp.DelegateTarget
|
||||
defdelegate second(value), to: MyApp.DelegateTarget, as: :different
|
||||
end
|
||||
defmodule MyApp.DelegateTarget do
|
||||
def first(value), do: value
|
||||
def different(value), do: value
|
||||
end
|
||||
`,
|
||||
);
|
||||
}
|
||||
|
||||
function snapshot(result: PipelineResult) {
|
||||
const nodes: Array<{
|
||||
label: string;
|
||||
name: string;
|
||||
qualifiedName?: string;
|
||||
filePath?: string;
|
||||
startLine?: number;
|
||||
method?: string;
|
||||
middleware?: unknown;
|
||||
}> = [];
|
||||
result.graph.forEachNode((node) =>
|
||||
nodes.push({
|
||||
label: node.label,
|
||||
name: String(node.properties.name),
|
||||
qualifiedName: node.properties.qualifiedName as string | undefined,
|
||||
filePath: node.properties.filePath as string | undefined,
|
||||
startLine: node.properties.startLine as number | undefined,
|
||||
method: node.properties.method as string | undefined,
|
||||
middleware: node.properties.middleware,
|
||||
}),
|
||||
);
|
||||
const relationships: Array<{ type: string; source: string; target: string }> = [];
|
||||
result.graph.forEachRelationship((relationship) => {
|
||||
relationships.push({
|
||||
type: relationship.type,
|
||||
source: String(
|
||||
result.graph.getNode(relationship.sourceId)?.properties.qualifiedName ??
|
||||
result.graph.getNode(relationship.sourceId)?.properties.name,
|
||||
),
|
||||
target: String(
|
||||
result.graph.getNode(relationship.targetId)?.properties.qualifiedName ??
|
||||
result.graph.getNode(relationship.targetId)?.properties.name,
|
||||
),
|
||||
});
|
||||
});
|
||||
return {
|
||||
nodes: nodes.sort((a, b) =>
|
||||
`${a.label}:${a.qualifiedName ?? a.name}`.localeCompare(
|
||||
`${b.label}:${b.qualifiedName ?? b.name}`,
|
||||
),
|
||||
),
|
||||
relationships: relationships.sort((a, b) =>
|
||||
`${a.type}:${a.source}:${a.target}`.localeCompare(`${b.type}:${b.source}:${b.target}`),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
describe('Elixir Phoenix/Ecto pipeline', () => {
|
||||
it('emits literal Phoenix and Ecto facts through worker and warm-cache replay', async () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'gitnexus-elixir-framework-'));
|
||||
const storage = path.join(root, 'cache');
|
||||
try {
|
||||
writeFixture(root);
|
||||
const coldCache: ParseCache = {
|
||||
version: PARSE_CACHE_VERSION,
|
||||
entries: new Map(),
|
||||
usedKeys: new Set(),
|
||||
storagePath: storage,
|
||||
onDiskKeys: new Set(),
|
||||
};
|
||||
const cold = await runPipelineFromRepo(root, () => {}, {
|
||||
parseCache: coldCache,
|
||||
workerPoolSize: 1,
|
||||
});
|
||||
expect(cold.usedWorkerPool).toBe(true);
|
||||
|
||||
pruneCache(coldCache, coldCache.usedKeys);
|
||||
const saved = await saveParseCache(storage, coldCache);
|
||||
await pruneAndSaveDurableParsedFileStore(
|
||||
getDurableParsedFileDir(storage),
|
||||
PARSE_CACHE_VERSION,
|
||||
new Set(saved),
|
||||
);
|
||||
const warm = await loadParseCache(storage);
|
||||
const replay = await runPipelineFromRepo(root, () => {}, {
|
||||
parseCache: warm ?? undefined,
|
||||
workerPoolSize: 1,
|
||||
});
|
||||
expect(replay.usedWorkerPool).toBe(false);
|
||||
expect(snapshot(replay)).toEqual(snapshot(cold));
|
||||
|
||||
const routes = snapshot(cold).nodes.filter((node) => node.label === 'Route');
|
||||
expect(routes).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
name: '/api/v1/posts',
|
||||
filePath: 'lib/my_app_web/controllers/post_controller.ex',
|
||||
startLine: 10,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: '/api/v1/dashboard',
|
||||
filePath: 'lib/my_app_web/live/dashboard_live.ex',
|
||||
startLine: expect.any(Number),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: '/api/v1/accounts/:id',
|
||||
filePath: 'lib/my_app_web/controllers/account_controller.ex',
|
||||
startLine: expect.any(Number),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: '/root',
|
||||
filePath: 'lib/my_app_web/controllers/post_controller.ex',
|
||||
startLine: expect.any(Number),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: '/keyword',
|
||||
filePath: 'lib/my_app_web/controllers/post_controller.ex',
|
||||
startLine: expect.any(Number),
|
||||
middleware: ['browser', 'api'],
|
||||
}),
|
||||
]),
|
||||
);
|
||||
expect(routes).toContainEqual(
|
||||
expect.objectContaining({ name: '/api/v1/only-accounts', method: 'GET' }),
|
||||
);
|
||||
expect(routes.map((route) => route.name)).not.toContain('/api/v1/only-accounts/:id');
|
||||
expect(routes).toContainEqual(
|
||||
expect.objectContaining({ name: '/api/v1/except-accounts/:id', method: 'PATCH' }),
|
||||
);
|
||||
expect(routes).not.toContainEqual(
|
||||
expect.objectContaining({ name: '/api/v1/except-accounts/:id', method: 'GET' }),
|
||||
);
|
||||
expect(routes.map((route) => route.name)).not.toContain('//root');
|
||||
expect(routes.map((route) => route.name)).not.toContain('dynamic_path');
|
||||
|
||||
const graph = snapshot(cold);
|
||||
expect(graph.relationships).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
type: 'HANDLES_ROUTE',
|
||||
source: 'MyAppWeb.PostController.index',
|
||||
target: '/api/v1/posts',
|
||||
}),
|
||||
expect.objectContaining({
|
||||
type: 'HANDLES_ROUTE',
|
||||
source: 'MyAppWeb.DashboardLive',
|
||||
target: '/api/v1/dashboard',
|
||||
}),
|
||||
expect.objectContaining({
|
||||
type: 'HAS_PROPERTY',
|
||||
source: 'MyApp.Blog.Post',
|
||||
target: 'MyApp.Blog.Post.title',
|
||||
}),
|
||||
expect.objectContaining({
|
||||
type: 'USES',
|
||||
source: 'MyApp.Blog.Post',
|
||||
target: 'MyApp.Blog.Comment',
|
||||
}),
|
||||
expect.objectContaining({
|
||||
type: 'QUERIES',
|
||||
source: 'blog.ex',
|
||||
target: 'MyApp.Blog.Post',
|
||||
}),
|
||||
expect.objectContaining({
|
||||
type: 'QUERIES',
|
||||
source: 'query_only.ex',
|
||||
target: 'MyApp.Blog.Post',
|
||||
}),
|
||||
expect.objectContaining({
|
||||
type: 'CALLS',
|
||||
source: 'MyApp.Blog.only_call',
|
||||
target: 'MyApp.Helpers.f',
|
||||
}),
|
||||
expect.objectContaining({
|
||||
type: 'CALLS',
|
||||
source: 'MyApp.Blog.wildcard_public',
|
||||
target: 'MyApp.Helpers.public',
|
||||
}),
|
||||
expect.objectContaining({
|
||||
type: 'CALLS',
|
||||
source: 'MyApp.Blog.except_public',
|
||||
target: 'MyApp.Helpers.public',
|
||||
}),
|
||||
expect.objectContaining({
|
||||
type: 'CALLS',
|
||||
source: 'MyApp.Captures.example_task',
|
||||
target: 'MyApp.Tasks.ExampleTask.run',
|
||||
}),
|
||||
expect.objectContaining({
|
||||
type: 'CALLS',
|
||||
source: 'MyApp.Delegates.first',
|
||||
target: 'MyApp.DelegateTarget.first',
|
||||
}),
|
||||
expect.objectContaining({
|
||||
type: 'CALLS',
|
||||
source: 'MyApp.Delegates.second',
|
||||
target: 'MyApp.DelegateTarget.different',
|
||||
}),
|
||||
]),
|
||||
);
|
||||
expect(
|
||||
graph.relationships.filter(
|
||||
(edge) => edge.type === 'CALLS' && edge.source === 'MyApp.Delegates.first',
|
||||
),
|
||||
).toEqual([expect.objectContaining({ target: 'MyApp.DelegateTarget.first' })]);
|
||||
const properties = graph.nodes.filter((node) => node.label === 'Property');
|
||||
expect(properties).toContainEqual(
|
||||
expect.objectContaining({ qualifiedName: 'MyApp.Blog.Post.title' }),
|
||||
);
|
||||
expect(properties).not.toContainEqual(
|
||||
expect.objectContaining({ qualifiedName: 'MyApp.Blog.Post.status' }),
|
||||
);
|
||||
const callTargets = (source: string) =>
|
||||
graph.relationships
|
||||
.filter((edge) => edge.type === 'CALLS' && edge.source === source)
|
||||
.map((edge) => edge.target);
|
||||
expect(callTargets('MyApp.UnaliasedCaptures.example_task')).not.toContain(
|
||||
'MyApp.UnaliasedCaptures.Tasks.ExampleTask.run',
|
||||
);
|
||||
expect(callTargets('MyApp.DifferentRootCaptures.example_task')).toContain(
|
||||
'Elsewhere.Tasks.ExampleTask.run',
|
||||
);
|
||||
expect(callTargets('MyApp.DifferentRootCaptures.example_task')).not.toContain(
|
||||
'MyApp.Tasks.ExampleTask.run',
|
||||
);
|
||||
expect(callTargets('MyApp.AmbiguousCaptures.example_task')).not.toContain(
|
||||
'MyApp.AmbiguousTasks.ExampleTask.run',
|
||||
);
|
||||
expect(callTargets('MyApp.PrivateCaptures.example_task')).not.toContain(
|
||||
'MyApp.PrivateTasks.ExampleTask.run',
|
||||
);
|
||||
expect(callTargets('MyApp.ArityCaptures.example_task')).not.toContain(
|
||||
'MyApp.ArityTasks.ExampleTask.run',
|
||||
);
|
||||
expect(callTargets('MyApp.AliasOrdering.before_alias')).not.toContain(
|
||||
'MyApp.Tasks.ExampleTask.run',
|
||||
);
|
||||
expect(callTargets('MyApp.AliasOrdering.before_alias')).not.toContain(
|
||||
'Elsewhere.Tasks.ExampleTask.run',
|
||||
);
|
||||
expect(callTargets('MyApp.AliasOrdering.first_alias')).toContain(
|
||||
'MyApp.Tasks.ExampleTask.run',
|
||||
);
|
||||
expect(callTargets('MyApp.AliasOrdering.rebound_alias')).toContain(
|
||||
'Elsewhere.Tasks.ExampleTask.run',
|
||||
);
|
||||
expect(callTargets('MyApp.AliasContainment.after_sibling')).not.toContain(
|
||||
'MyApp.Tasks.ExampleTask.run',
|
||||
);
|
||||
expect(callTargets('MyApp.AliasContainment.after_quote')).not.toContain(
|
||||
'MyApp.Tasks.ExampleTask.run',
|
||||
);
|
||||
expect(callTargets('MyApp.AliasContainment.quoted_capture')).not.toContain(
|
||||
'MyApp.Tasks.ExampleTask.run',
|
||||
);
|
||||
expect(callTargets('MyApp.AliasContainment.after_block')).not.toContain(
|
||||
'MyApp.Tasks.ExampleTask.run',
|
||||
);
|
||||
for (const [source, target] of [
|
||||
['MyApp.Blog.wildcard_private', 'MyApp.Helpers.private'],
|
||||
['MyApp.Blog.wildcard_private_macro', 'MyApp.Helpers.private_macro'],
|
||||
['MyApp.Blog.except_private', 'MyApp.Helpers.private'],
|
||||
['MyApp.Blog.except_private_macro', 'MyApp.Helpers.private_macro'],
|
||||
['MyApp.Blog.callback_call', 'MyApp.Behaviour.required'],
|
||||
['MyApp.Behaviour.required', 'MyApp.Behaviour.required'],
|
||||
])
|
||||
expect(graph.relationships).not.toContainEqual(
|
||||
expect.objectContaining({ type: 'CALLS', source, target }),
|
||||
);
|
||||
expect(graph.relationships).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
type: 'METHOD_IMPLEMENTS',
|
||||
source: 'MyApp.Implementation.required',
|
||||
target: 'MyApp.Behaviour.required',
|
||||
}),
|
||||
]),
|
||||
);
|
||||
for (const source of [
|
||||
'MyApp.ArityImplementation.mismatch',
|
||||
'MyApp.AmbiguousImplementation.ambiguous',
|
||||
])
|
||||
expect(graph.relationships).not.toContainEqual(
|
||||
expect.objectContaining({ type: 'METHOD_IMPLEMENTS', source }),
|
||||
);
|
||||
expect(
|
||||
graph.nodes.filter(
|
||||
(node) => node.label === 'Class' && node.qualifiedName === 'MyApp.Blog.Post',
|
||||
),
|
||||
).toHaveLength(1);
|
||||
expect(
|
||||
graph.nodes.filter(
|
||||
(node) => node.label === 'Class' && node.qualifiedName === 'MyApp.Blog.Metadata',
|
||||
),
|
||||
).toHaveLength(1);
|
||||
} finally {
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
}, 120_000);
|
||||
});
|
||||
|
|
@ -18,6 +18,8 @@ describe('filesystem-walker', () => {
|
|||
// Create test directory structure
|
||||
await fs.mkdir(path.join(tmpDir, 'src'), { recursive: true });
|
||||
await fs.mkdir(path.join(tmpDir, 'src', 'components'), { recursive: true });
|
||||
await fs.mkdir(path.join(tmpDir, 'src', 'deps', 'sample', 'lib'), { recursive: true });
|
||||
await fs.mkdir(path.join(tmpDir, 'deps', 'sample', 'lib'), { recursive: true });
|
||||
await fs.mkdir(path.join(tmpDir, 'node_modules', 'lodash'), { recursive: true });
|
||||
await fs.mkdir(path.join(tmpDir, '.git'), { recursive: true });
|
||||
|
||||
|
|
@ -27,6 +29,15 @@ describe('filesystem-walker', () => {
|
|||
path.join(tmpDir, 'src', 'components', 'Button.tsx'),
|
||||
'export const Button = () => <div/>',
|
||||
);
|
||||
await fs.writeFile(
|
||||
path.join(tmpDir, 'src', 'deps', 'sample', 'lib', 'sample.ex'),
|
||||
'defmodule Sample do\nend\n',
|
||||
);
|
||||
await fs.writeFile(
|
||||
path.join(tmpDir, 'deps', 'sample', 'lib', 'sample.ex'),
|
||||
'defmodule Dependency do\nend\n',
|
||||
);
|
||||
await fs.writeFile(path.join(tmpDir, 'mix.exs'), 'defmodule Fixture.MixProject do\nend\n');
|
||||
await fs.writeFile(
|
||||
path.join(tmpDir, 'node_modules', 'lodash', 'index.js'),
|
||||
'module.exports = {}',
|
||||
|
|
@ -67,6 +78,13 @@ describe('filesystem-walker', () => {
|
|||
expect(paths.every((p) => !p.includes('node_modules'))).toBe(true);
|
||||
});
|
||||
|
||||
it('skips root Mix dependencies without excluding nested first-party deps directories', async () => {
|
||||
const files = await walkRepositoryPaths(tmpDir);
|
||||
const paths = files.map((f) => f.path.replace(/\\/g, '/'));
|
||||
expect(paths).not.toContain('deps/sample/lib/sample.ex');
|
||||
expect(paths).toContain('src/deps/sample/lib/sample.ex');
|
||||
});
|
||||
|
||||
it('skips .git directory', async () => {
|
||||
const files = await walkRepositoryPaths(tmpDir);
|
||||
const paths = files.map((f) => f.path.replace(/\\/g, '/'));
|
||||
|
|
|
|||
|
|
@ -36,6 +36,9 @@ const CALLABLE_FLOW_PROVIDER_COVERAGE = {
|
|||
[SupportedLanguages.Vue]: 'matrix',
|
||||
[SupportedLanguages.Cobol]: 'matrix',
|
||||
[SupportedLanguages.Zig]: 'matrix',
|
||||
// Elixir has no generic callable-value-flow capture protocol. Its provider
|
||||
// models direct captures separately, but does not resolve value dispatch.
|
||||
[SupportedLanguages.Elixir]: 'not-applicable',
|
||||
// Objective-C message sends are resolved by its ScopeResolver. The provider
|
||||
// does not emit callable-value-flow captures in this MVP.
|
||||
[SupportedLanguages.ObjectiveC]: 'not-applicable',
|
||||
|
|
|
|||
37
gitnexus/test/integration/resolvers/elixir.test.ts
Normal file
37
gitnexus/test/integration/resolvers/elixir.test.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'path';
|
||||
import {
|
||||
getRelationships,
|
||||
runPipelineFromRepo,
|
||||
type PipelineResult,
|
||||
writeFixtureRepo,
|
||||
} from './helpers.js';
|
||||
|
||||
describe('Elixir scope resolution', () => {
|
||||
let result: PipelineResult;
|
||||
let root = '';
|
||||
|
||||
beforeAll(async () => {
|
||||
root = fs.mkdtempSync(path.join(os.tmpdir(), 'gitnexus-elixir-resolver-'));
|
||||
writeFixtureRepo(root, {
|
||||
'lib/my_app.ex': `
|
||||
defmodule MyApp.Target do
|
||||
def run, do: :ok
|
||||
end
|
||||
defmodule MyApp.Caller do
|
||||
def invoke, do: MyApp.Target.run()
|
||||
end
|
||||
`,
|
||||
});
|
||||
result = await runPipelineFromRepo(root, () => {});
|
||||
}, 60_000);
|
||||
|
||||
afterAll(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
|
||||
it('registers the Elixir resolver and resolves qualified calls', () => {
|
||||
const calls = getRelationships(result, 'CALLS');
|
||||
expect(calls.some((edge) => edge.source === 'invoke' && edge.target === 'run')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
import { describe, it, expect, beforeAll, vi } from 'vitest';
|
||||
import fs from 'fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import path from 'path';
|
||||
import {
|
||||
loadParser,
|
||||
|
|
@ -9,9 +10,12 @@ import {
|
|||
} from '../../src/core/tree-sitter/parser-loader.js';
|
||||
import { SupportedLanguages, getLanguageFromFilename } from 'gitnexus-shared';
|
||||
import { getProvider } from '../../src/core/ingestion/languages/index.js';
|
||||
import { findEnclosingClassInfo } from '../../src/core/ingestion/utils/ast-helpers.js';
|
||||
import Parser from 'tree-sitter';
|
||||
import { vendoredGrammarDir } from '../../src/core/tree-sitter/vendored-grammars.js';
|
||||
|
||||
const _require = createRequire(import.meta.url);
|
||||
|
||||
const fixturesDir = path.resolve(__dirname, '..', 'fixtures', 'sample-code');
|
||||
|
||||
function readFixture(filename: string): string {
|
||||
|
|
@ -1009,6 +1013,151 @@ describe('Tree-sitter multi-language parsing', () => {
|
|||
});
|
||||
});
|
||||
|
||||
const elixirPackageInstalled = (() => {
|
||||
try {
|
||||
_require.resolve('tree-sitter-elixir');
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
})();
|
||||
describe.skipIf(!elixirPackageInstalled)('Elixir', () => {
|
||||
const elixirQueries = () => getProvider(SupportedLanguages.Elixir).treeSitterQueries;
|
||||
|
||||
async function loadElixir(): Promise<void> {
|
||||
expect(isLanguageAvailable(SupportedLanguages.Elixir)).toBe(true);
|
||||
await loadLanguage(SupportedLanguages.Elixir);
|
||||
}
|
||||
|
||||
it('parses modules, functions, and protocols', async () => {
|
||||
await loadElixir();
|
||||
const { matches } = parseAndQuery(parser, readFixture('simple.ex'), elixirQueries());
|
||||
const defs = extractDefinitions(matches);
|
||||
const defTypes = defs.map((d) => d.type);
|
||||
const defNames = defs.map((d) => d.name);
|
||||
|
||||
expect(defTypes).toContain('definition.class');
|
||||
expect(defTypes).toContain('definition.function');
|
||||
expect(defTypes).toContain('definition.interface');
|
||||
|
||||
expect(defNames).toContain('MyApp.User');
|
||||
expect(defNames).toContain('get');
|
||||
expect(defNames).toContain('get_by_email');
|
||||
expect(defNames).toContain('validate');
|
||||
expect(defNames).toContain('create');
|
||||
expect(defNames).toContain('MyApp.Serializable');
|
||||
});
|
||||
|
||||
it('captures zero-arity and guarded declaration heads once', async () => {
|
||||
await loadElixir();
|
||||
const declarations = [
|
||||
['def zero_def, do: :ok', 'definition.function', 'zero_def'],
|
||||
[
|
||||
'def guarded_def(value) when is_binary(value), do: value',
|
||||
'definition.function',
|
||||
'guarded_def',
|
||||
],
|
||||
['defp zero_defp, do: :ok', 'definition.function', 'zero_defp'],
|
||||
[
|
||||
'defp guarded_defp(value) when is_binary(value), do: value',
|
||||
'definition.function',
|
||||
'guarded_defp',
|
||||
],
|
||||
['defmacro zero_macro, do: :ok', 'definition.macro', 'zero_macro'],
|
||||
[
|
||||
'defmacro guarded_macro(value) when is_binary(value), do: value',
|
||||
'definition.macro',
|
||||
'guarded_macro',
|
||||
],
|
||||
['defmacrop zero_macrop, do: :ok', 'definition.macro', 'zero_macrop'],
|
||||
[
|
||||
'defmacrop guarded_macrop(value) when is_binary(value), do: value',
|
||||
'definition.macro',
|
||||
'guarded_macrop',
|
||||
],
|
||||
['defguard zero_guard, do: true', 'definition.macro', 'zero_guard'],
|
||||
[
|
||||
'defguard guarded_guard(value) when is_binary(value)',
|
||||
'definition.macro',
|
||||
'guarded_guard',
|
||||
],
|
||||
['defguardp zero_guardp, do: true', 'definition.macro', 'zero_guardp'],
|
||||
[
|
||||
'defguardp guarded_guardp(value) when is_binary(value)',
|
||||
'definition.macro',
|
||||
'guarded_guardp',
|
||||
],
|
||||
['defdelegate zero_delegate, to: Remote', 'definition.function', 'zero_delegate'],
|
||||
] as const;
|
||||
const { matches } = parseAndQuery(
|
||||
parser,
|
||||
declarations.map(([source]) => source).join('\n'),
|
||||
elixirQueries(),
|
||||
);
|
||||
const definitions = extractDefinitions(matches).map(({ type, name }) => ({ type, name }));
|
||||
const expected = declarations.map(([, type, name]) => ({ type, name }));
|
||||
expect(definitions).toEqual(expected);
|
||||
});
|
||||
|
||||
it('captures import/alias/use/require as @import', async () => {
|
||||
await loadElixir();
|
||||
const { matches } = parseAndQuery(parser, readFixture('simple.ex'), elixirQueries());
|
||||
const importSources: string[] = [];
|
||||
for (const match of matches) {
|
||||
for (const c of match.captures) {
|
||||
if (c.name === 'import.source') importSources.push(c.node.text);
|
||||
}
|
||||
}
|
||||
expect(importSources).toEqual(['MyApp.Repo', 'Ecto.Query', 'Phoenix.LiveView', 'Logger']);
|
||||
});
|
||||
|
||||
it('extracts real calls once and skips definitions/attributes', async () => {
|
||||
await loadElixir();
|
||||
const { matches } = parseAndQuery(parser, readFixture('simple.ex'), elixirQueries());
|
||||
const provider = getProvider(SupportedLanguages.Elixir);
|
||||
const callNames: string[] = [];
|
||||
for (const match of matches) {
|
||||
const captureMap: Record<string, any> = {};
|
||||
for (const c of match.captures) {
|
||||
captureMap[c.name] = c.node;
|
||||
}
|
||||
if (captureMap['call']) {
|
||||
const extracted = provider.callExtractor?.extract(
|
||||
captureMap['call'],
|
||||
captureMap['call.name'],
|
||||
);
|
||||
if (extracted) callNames.push(extracted.calledName);
|
||||
}
|
||||
}
|
||||
|
||||
expect(callNames).toEqual(['get', 'from', 'one', 'changeset', 'insert']);
|
||||
});
|
||||
|
||||
it('resolves defmodule as the enclosing owner for functions', async () => {
|
||||
await loadElixir();
|
||||
const { matches } = parseAndQuery(parser, readFixture('simple.ex'), elixirQueries());
|
||||
const provider = getProvider(SupportedLanguages.Elixir);
|
||||
let createNode: any;
|
||||
for (const match of matches) {
|
||||
const hasFunctionDef = match.captures.some((c: any) => c.name === 'definition.function');
|
||||
const nameCapture = match.captures.find((c: any) => c.name === 'name');
|
||||
if (hasFunctionDef && nameCapture?.node.text === 'create') {
|
||||
createNode = nameCapture.node;
|
||||
}
|
||||
}
|
||||
|
||||
const owner = findEnclosingClassInfo(
|
||||
createNode,
|
||||
'lib/my_app/user.ex',
|
||||
provider.resolveEnclosingOwner,
|
||||
undefined,
|
||||
provider.resolveFileTypeOwner,
|
||||
provider.resolveContainerTypeOwner,
|
||||
);
|
||||
expect(owner?.className).toBe('MyApp.User');
|
||||
});
|
||||
});
|
||||
|
||||
describe('parser edge cases', () => {
|
||||
it('loadLanguage throws for unsupported language', async () => {
|
||||
await expect(loadLanguage('brainfuck' as any)).rejects.toThrow(/unsupported language/i);
|
||||
|
|
|
|||
|
|
@ -225,6 +225,25 @@ describe('worker pool integration', () => {
|
|||
20_000,
|
||||
);
|
||||
|
||||
it.skipIf(!hasDistWorker)('parses an Elixir file through worker', async () => {
|
||||
const workerUrl = pathToFileURL(DIST_WORKER) as URL;
|
||||
pool = createWorkerPool(workerUrl, 1);
|
||||
|
||||
const fixtureFile = path.resolve(__dirname, '..', 'fixtures', 'sample-code', 'simple.ex');
|
||||
const content = fs.readFileSync(fixtureFile, 'utf-8');
|
||||
|
||||
const results = await pool.dispatch<any, any>([{ path: 'lib/my_app/user.ex', content }]);
|
||||
|
||||
expect(results).toHaveLength(1);
|
||||
const result = results[0];
|
||||
expect(result.fileCount).toBe(1);
|
||||
expect(result.skippedLanguages).toEqual({});
|
||||
|
||||
const names = result.nodes.map((n: any) => n.properties.name);
|
||||
expect(names).toContain('MyApp.User');
|
||||
expect(names).toContain('create');
|
||||
});
|
||||
|
||||
it.skipIf(!hasDistWorker)('parses multiple files across workers', async () => {
|
||||
const workerUrl = pathToFileURL(DIST_WORKER) as URL;
|
||||
pool = createWorkerPool(workerUrl, 2);
|
||||
|
|
|
|||
331
gitnexus/test/unit/elixir-cfg.test.ts
Normal file
331
gitnexus/test/unit/elixir-cfg.test.ts
Normal file
|
|
@ -0,0 +1,331 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import Parser from 'tree-sitter';
|
||||
import { SupportedLanguages } from 'gitnexus-shared';
|
||||
import { loadLanguage, getLanguageGrammar } from '../../src/core/tree-sitter/parser-loader.js';
|
||||
import { createElixirCfgVisitor } from '../../src/core/ingestion/cfg/visitors/elixir.js';
|
||||
import { collectFunctionCfgs } from '../../src/core/ingestion/cfg/collect.js';
|
||||
import { computeReachingDefs } from '../../src/core/ingestion/cfg/reaching-defs.js';
|
||||
import { ELIXIR_TAINT_MODEL } from '../../src/core/ingestion/taint/elixir-model.js';
|
||||
import { buildTaintImportIndex, matchFunctionSites } from '../../src/core/ingestion/taint/match.js';
|
||||
import { computeTaintFlows } from '../../src/core/ingestion/taint/propagate.js';
|
||||
|
||||
describe('Elixir CFG visitor', () => {
|
||||
it('keeps clauses separate and emits local branch edges', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'cfg.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const defs = parser.parse(
|
||||
'def f(0), do: :zero\ndef f(x) when x > 0 do\n case x do\n y -> y\n end\nend',
|
||||
).rootNode.namedChildren;
|
||||
const visitor = createElixirCfgVisitor();
|
||||
const cfg = visitor.buildFunctionCfg(defs[1]!, 'cfg.ex');
|
||||
expect(cfg).toBeDefined();
|
||||
expect(cfg!.edges.some((edge) => edge.kind === 'cond-true')).toBe(true);
|
||||
expect(cfg!.edges.some((edge) => edge.kind === 'cond-false')).toBe(true);
|
||||
});
|
||||
|
||||
it('records match bindings and later reads', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'facts.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const def = parser.parse('def f(value) do\n result = value\n Code.eval_string(result)\nend')
|
||||
.rootNode.namedChildren[0]!;
|
||||
const cfg = createElixirCfgVisitor().buildFunctionCfg(def, 'facts.ex')!;
|
||||
const assignment = cfg.blocks.find((block) => block.text.includes('result = value'))!;
|
||||
const call = cfg.blocks.find((block) => block.text.includes('Code.eval_string'))!;
|
||||
expect(assignment.statements![0]!.defs).toHaveLength(1);
|
||||
expect(assignment.statements![0]!.uses).toHaveLength(1);
|
||||
expect(call.statements![0]!.uses).not.toHaveLength(0);
|
||||
expect(call.statements![0]!.sites).toContainEqual(
|
||||
expect.objectContaining({ callee: 'Code.eval_string' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('treats pinned patterns as reads and keeps header facts outside branch bodies', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'pin-header.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const root = parser.parse(`def f(user_id, pair, flag) do
|
||||
{^user_id, id} = pair
|
||||
{^user_id, user_id} = pair
|
||||
if flag do body_only(id) end
|
||||
end`).rootNode;
|
||||
const cfg = createElixirCfgVisitor().buildFunctionCfg(root.namedChildren[0]!, 'pin-header.ex')!;
|
||||
const match = cfg.blocks.find((block) => block.text.includes('{^user_id, id}'))!
|
||||
.statements![0]!;
|
||||
expect(cfg.bindings[match.uses[0]!]!.name).toBe('user_id');
|
||||
expect(cfg.bindings[match.defs[0]!]!.name).toBe('id');
|
||||
const rebound = cfg.blocks.find((block) => block.text.includes('{^user_id, user_id}'))!
|
||||
.statements![0]!;
|
||||
expect(rebound.uses.map((index) => cfg.bindings[index]!.name)).toEqual(['user_id', 'pair']);
|
||||
expect(rebound.defs.map((index) => cfg.bindings[index]!.name)).toEqual(['user_id']);
|
||||
const header = cfg.blocks.find((block) => block.text.startsWith('if flag'))!.statements![0]!;
|
||||
expect(header.uses.map((index) => cfg.bindings[index]!.name)).toEqual(['flag']);
|
||||
});
|
||||
|
||||
it('keeps the previous binding as a use when rebinding it from the RHS', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'rebind.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const def = parser.parse('def f(count) do\n count = count + 1\nend').rootNode
|
||||
.namedChildren[0]!;
|
||||
const cfg = createElixirCfgVisitor().buildFunctionCfg(def, 'rebind.ex')!;
|
||||
const rebind = cfg.blocks.find((block) => block.text.includes('count = count + 1'))!
|
||||
.statements![0]!;
|
||||
expect(rebind.defs.map((index) => cfg.bindings[index]!.name)).toEqual(['count']);
|
||||
expect(rebind.uses.map((index) => cfg.bindings[index]!.name)).toEqual(['count']);
|
||||
});
|
||||
|
||||
it('propagates Phoenix parameters to dynamic-eval and raw-SQL text only', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'taint.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const root = parser.parse(`def f(conn, params) do
|
||||
code = conn.params["code"]
|
||||
sql = params["sql"]
|
||||
Code.eval_string(code)
|
||||
Repo.query!(sql, [code])
|
||||
Repo.query!("select * from posts where id = ?", [sql])
|
||||
end`).rootNode;
|
||||
const cfg = createElixirCfgVisitor().buildFunctionCfg(root.namedChildren[0]!, 'taint.ex')!;
|
||||
const matches = matchFunctionSites(cfg, ELIXIR_TAINT_MODEL, buildTaintImportIndex([]));
|
||||
const result = computeTaintFlows(cfg, computeReachingDefs(cfg), matches);
|
||||
expect(
|
||||
result.findings.map((finding) => [finding.sink.entryName, finding.sinkKind]).sort(),
|
||||
).toEqual([
|
||||
['eval_string', 'code-injection'],
|
||||
['query!', 'sql-injection'],
|
||||
]);
|
||||
});
|
||||
|
||||
it('records nested parent sites and taints direct and piped parameter reads', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'nested-sites.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const root = parser.parse(`def f(params) do
|
||||
Code.eval_string(wrapper(params["code"]))
|
||||
Code.eval_string(params["code"])
|
||||
params["code"] |> Code.eval_string
|
||||
end`).rootNode;
|
||||
const cfg = createElixirCfgVisitor().buildFunctionCfg(
|
||||
root.namedChildren[0]!,
|
||||
'nested-sites.ex',
|
||||
)!;
|
||||
const nested = cfg.blocks.find((block) => block.text.includes('wrapper(params'))!
|
||||
.statements![0]!.sites!;
|
||||
const wrapper = nested.findIndex((site) => site.kind === 'call' && site.callee === 'wrapper');
|
||||
const read = nested.findIndex((site) => site.kind === 'member-read');
|
||||
const evalSite = nested.findIndex(
|
||||
(site) => site.kind === 'call' && site.callee === 'Code.eval_string',
|
||||
);
|
||||
expect(wrapper).toBeGreaterThanOrEqual(0);
|
||||
expect(read).toBeGreaterThanOrEqual(0);
|
||||
expect(nested[wrapper]!.parent).toEqual([evalSite, 0]);
|
||||
expect(nested[read]!.parent).toEqual([wrapper, 0]);
|
||||
const piped = cfg.blocks.find((block) => block.text.includes('|> Code.eval_string'))!
|
||||
.statements![0]!.sites!;
|
||||
const pipedRead = piped.findIndex((site) => site.kind === 'member-read');
|
||||
const pipedEval = piped.findIndex(
|
||||
(site) => site.kind === 'call' && site.callee === 'Code.eval_string',
|
||||
);
|
||||
expect(pipedRead).toBeGreaterThanOrEqual(0);
|
||||
expect(piped[pipedRead]!.parent).toEqual([pipedEval, 0]);
|
||||
const findings = computeTaintFlows(
|
||||
cfg,
|
||||
computeReachingDefs(cfg),
|
||||
matchFunctionSites(cfg, ELIXIR_TAINT_MODEL, buildTaintImportIndex([])),
|
||||
).findings.filter((finding) => finding.sink.entryName === 'eval_string');
|
||||
expect(findings.map((finding) => finding.sink.point.line).sort()).toEqual([2, 3, 4]);
|
||||
});
|
||||
|
||||
it('uses source-range parents for repeated callees and shifted pipe arguments', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'parent-ranges.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const root = parser.parse(`def f(params, input, code) do
|
||||
wrapper(wrapper(params["x"]))
|
||||
input |> sink(wrapper(code))
|
||||
(input |> foo()) + sink()
|
||||
end`).rootNode;
|
||||
const cfg = createElixirCfgVisitor().buildFunctionCfg(
|
||||
root.namedChildren[0]!,
|
||||
'parent-ranges.ex',
|
||||
)!;
|
||||
const repeated = cfg.blocks.find((block) => block.text.includes('wrapper(wrapper'))!
|
||||
.statements![0]!.sites!;
|
||||
const wrappers = repeated
|
||||
.map((site, index) => ({ site, index }))
|
||||
.filter(({ site }) => site.kind === 'call' && site.callee === 'wrapper');
|
||||
const read = repeated.find((site) => site.kind === 'member-read')!;
|
||||
expect(read.parent).toEqual([wrappers[1]!.index, 0]);
|
||||
const piped = cfg.blocks.find((block) => block.text.includes('sink(wrapper'))!.statements![0]!
|
||||
.sites!;
|
||||
const sink = piped.findIndex((site) => site.kind === 'call' && site.callee === 'sink');
|
||||
const inner = piped.find((site) => site.kind === 'call' && site.callee === 'wrapper')!;
|
||||
expect(inner.parent).toEqual([sink, 1]);
|
||||
const addition = cfg.blocks.find((block) => block.text.includes('foo()) + sink'))!
|
||||
.statements![0]!.sites!;
|
||||
const foo = addition.find((site) => site.kind === 'call' && site.callee === 'foo')!;
|
||||
const standaloneSink = addition.find((site) => site.kind === 'call' && site.callee === 'sink')!;
|
||||
expect(foo.parent).toBeUndefined();
|
||||
expect(standaloneSink.parent).toBeUndefined();
|
||||
expect(standaloneSink.args).toBeUndefined();
|
||||
});
|
||||
|
||||
it('uses Ecto adapter SQL argument one while preserving Repo SQL argument zero', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'adapter-taint.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const root = parser.parse(`def f(params) do
|
||||
sql = params["sql"]
|
||||
Ecto.Adapters.SQL.query!(Repo, sql, [])
|
||||
Ecto.Adapters.SQL.query!(Repo, "safe", [sql])
|
||||
Repo.query!(sql)
|
||||
end`).rootNode;
|
||||
const cfg = createElixirCfgVisitor().buildFunctionCfg(
|
||||
root.namedChildren[0]!,
|
||||
'adapter-taint.ex',
|
||||
)!;
|
||||
const result = computeTaintFlows(
|
||||
cfg,
|
||||
computeReachingDefs(cfg),
|
||||
matchFunctionSites(cfg, ELIXIR_TAINT_MODEL, buildTaintImportIndex([])),
|
||||
);
|
||||
expect(result.findings.filter((finding) => finding.sinkKind === 'sql-injection')).toHaveLength(
|
||||
2,
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps every anonymous-function clause and matches bare Kernel.apply', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'closure-taint.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const root = parser.parse(`def f(params) do
|
||||
code = params["code"]
|
||||
fn :eval -> code = params["code"]; apply(code, [], []); value -> Code.eval_string(code) end
|
||||
end`).rootNode;
|
||||
const cfg = collectFunctionCfgs(root, createElixirCfgVisitor(), 'closure-taint.ex').cfgs.find(
|
||||
(candidate) => candidate.blocks.some((block) => block.text.includes('apply(code')),
|
||||
)!;
|
||||
const findings = computeTaintFlows(
|
||||
cfg,
|
||||
computeReachingDefs(cfg),
|
||||
matchFunctionSites(cfg, ELIXIR_TAINT_MODEL, buildTaintImportIndex([])),
|
||||
).findings;
|
||||
expect(cfg.blocks.some((block) => block.text.includes('Code.eval_string'))).toBe(true);
|
||||
expect(findings.map((finding) => finding.sink.entryName)).toEqual(['apply']);
|
||||
});
|
||||
|
||||
it('does not flow taint sequentially between anonymous-function clauses', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'closure-branches.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const root = parser.parse(`def f(params) do
|
||||
fn :source -> code = params["code"]; :ok
|
||||
:sink -> Code.eval_string(code)
|
||||
end
|
||||
end`).rootNode;
|
||||
const cfg = collectFunctionCfgs(
|
||||
root,
|
||||
createElixirCfgVisitor(),
|
||||
'closure-branches.ex',
|
||||
).cfgs.find((candidate) =>
|
||||
candidate.blocks.some((block) => block.text.includes('Code.eval_string')),
|
||||
)!;
|
||||
const findings = computeTaintFlows(
|
||||
cfg,
|
||||
computeReachingDefs(cfg),
|
||||
matchFunctionSites(cfg, ELIXIR_TAINT_MODEL, buildTaintImportIndex([])),
|
||||
).findings;
|
||||
expect(findings.map((finding) => finding.sink.entryName)).not.toContain('eval_string');
|
||||
});
|
||||
|
||||
it('binds guarded default formals by pattern with zero-based indexes only', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'guarded-defaults.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const def = parser.parse(
|
||||
'def f(value \\\\ @fallback, other) when is_binary(value) do\n value <> other\nend',
|
||||
).rootNode.namedChildren[0]!;
|
||||
const cfg = createElixirCfgVisitor().buildFunctionCfg(def, 'guarded-defaults.ex')!;
|
||||
expect(cfg.bindings.filter((binding) => binding.kind === 'param')).toEqual([
|
||||
expect.objectContaining({ name: 'value', formalIndex: 0 }),
|
||||
expect.objectContaining({ name: 'other', formalIndex: 1 }),
|
||||
]);
|
||||
expect(
|
||||
cfg.bindings.filter((binding) => binding.kind === 'param').map((binding) => binding.name),
|
||||
).not.toContain('fallback');
|
||||
});
|
||||
|
||||
it('models the structured grammar forms as local control flow', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'shapes.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const source = `
|
||||
def shape(0), do: :zero
|
||||
def shape(x) when x > 0 do
|
||||
if x > 1 do :yes else :no end
|
||||
unless x < 9 do :large else :small end
|
||||
case x do y when y > 2 -> y; _ -> :other end
|
||||
cond do x > 3 -> :big; true -> :small end
|
||||
with {:ok, value} <- fetch(x), true <- valid(value) do value else _ -> :bad end
|
||||
for value <- values, value > 0, do: value * 2
|
||||
try do work(x) rescue error -> error catch _, reason -> reason after cleanup() end
|
||||
receive do message -> message after 1000 -> :timeout end
|
||||
fn value -> x && value || fallback() end
|
||||
end`;
|
||||
const root = parser.parse(source).rootNode;
|
||||
const cfgs = collectFunctionCfgs(root, createElixirCfgVisitor(), 'shapes.ex').cfgs;
|
||||
expect(cfgs).toHaveLength(3); // two clauses plus the captured closure
|
||||
const cfg = cfgs.find((candidate) =>
|
||||
candidate.blocks.some((block) => block.text.includes('receive do')),
|
||||
)!;
|
||||
expect(cfg.blocks.some((block) => block.text.includes('receive do'))).toBe(true);
|
||||
expect(
|
||||
new Set(cfg.edges.filter((edge) => edge.kind.startsWith('cond')).map((edge) => edge.from))
|
||||
.size,
|
||||
).toBe(21);
|
||||
expect(cfg.edges.filter((edge) => edge.kind === 'cond-true')).toHaveLength(15);
|
||||
expect(cfg.edges.filter((edge) => edge.kind === 'cond-false')).toHaveLength(21);
|
||||
expect(cfg.edges.some((edge) => edge.kind === ('message-delivery' as never))).toBe(false);
|
||||
expect(
|
||||
cfg.blocks.some(
|
||||
(block) => block.text.includes('value <- values') && block.statements![0]!.defs.length > 0,
|
||||
),
|
||||
).toBe(true);
|
||||
const closure = cfgs.find((candidate) =>
|
||||
candidate.blocks.some((block) => block.text.includes('fallback')),
|
||||
)!;
|
||||
expect(
|
||||
closure.blocks.some((block) =>
|
||||
block.statements?.some((statement) =>
|
||||
statement.sites?.some((site) => site.kind === 'call'),
|
||||
),
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
27
gitnexus/test/unit/elixir-import-resolver.test.ts
Normal file
27
gitnexus/test/unit/elixir-import-resolver.test.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import { elixirImportConfig } from '../../src/core/ingestion/import-resolvers/configs/elixir.js';
|
||||
import { createImportResolver } from '../../src/core/ingestion/import-resolvers/resolver-factory.js';
|
||||
|
||||
describe('Elixir import resolver', () => {
|
||||
it('expands grouped aliases into their individual module files', () => {
|
||||
const resolve = createImportResolver(elixirImportConfig);
|
||||
expect(
|
||||
resolve('Prefix.{One, Two}', 'lib/app.ex', {
|
||||
allFileList: ['lib/prefix/one.ex', 'lib/prefix/two.ex'],
|
||||
}),
|
||||
).toEqual({ kind: 'files', files: ['lib/prefix/one.ex', 'lib/prefix/two.ex'] });
|
||||
});
|
||||
|
||||
it('prefers an exact root module candidate regardless of file-list order', () => {
|
||||
const resolve = createImportResolver(elixirImportConfig);
|
||||
for (const allFileList of [
|
||||
['apps/umbrella/lib/my_app/user.ex', 'lib/my_app/user.ex'],
|
||||
['lib/my_app/user.ex', 'apps/umbrella/lib/my_app/user.ex'],
|
||||
]) {
|
||||
expect(resolve('MyApp.User', 'lib/app.ex', { allFileList })).toEqual({
|
||||
kind: 'files',
|
||||
files: ['lib/my_app/user.ex'],
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
272
gitnexus/test/unit/elixir-semantic-graph.test.ts
Normal file
272
gitnexus/test/unit/elixir-semantic-graph.test.ts
Normal file
|
|
@ -0,0 +1,272 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import Parser from 'tree-sitter';
|
||||
import { SupportedLanguages } from 'gitnexus-shared';
|
||||
import { loadLanguage } from '../../src/core/tree-sitter/parser-loader.js';
|
||||
import { getLanguageGrammar } from '../../src/core/tree-sitter/parser-loader.js';
|
||||
import { extractElixirSemanticGraph } from '../../src/core/ingestion/languages/elixir/semantic-graph.js';
|
||||
|
||||
describe('Elixir semantic graph', () => {
|
||||
it('coalesces same-arity clauses but preserves arity and macro identity', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'clauses.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const graph = extractElixirSemanticGraph(
|
||||
parser.parse(`
|
||||
defmodule M do
|
||||
def f(:one), do: one()
|
||||
def f(:two), do: two()
|
||||
def f(a, b), do: pair(a, b)
|
||||
defmacro f(x), do: x
|
||||
end
|
||||
`),
|
||||
'lib/nonconventional.ex',
|
||||
);
|
||||
const callables = graph.nodes.filter(
|
||||
(node) => node.label === 'Function' || node.label === 'Macro',
|
||||
);
|
||||
expect(callables).toHaveLength(3);
|
||||
expect(
|
||||
callables.filter(
|
||||
(node) => node.properties.qualifiedName === 'M.f' && node.label === 'Function',
|
||||
),
|
||||
).toHaveLength(2);
|
||||
expect(callables.find((node) => node.label === 'Macro')?.properties.parameterCount).toBe(1);
|
||||
});
|
||||
|
||||
it('preserves private visibility and does not materialize quoted declarations', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'quoted.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const graph = extractElixirSemanticGraph(
|
||||
parser.parse(`
|
||||
defmodule M do
|
||||
defp hidden(value), do: value
|
||||
quote do
|
||||
def generated(value), do: value
|
||||
defmodule Generated do
|
||||
def run, do: :ok
|
||||
end
|
||||
end
|
||||
end
|
||||
`),
|
||||
'anything.ex',
|
||||
);
|
||||
const hidden = graph.nodes.find((node) => node.properties.qualifiedName === 'M.hidden');
|
||||
expect(hidden?.properties).toMatchObject({ visibility: 'private', isExported: false });
|
||||
for (const name of ['M.generated', 'Generated', 'Generated.run'])
|
||||
expect(graph.nodes.map((node) => node.properties.qualifiedName)).not.toContain(name);
|
||||
});
|
||||
|
||||
it('does not classify a module as an interface from a quoted callback', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'quoted-callback.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const graph = extractElixirSemanticGraph(
|
||||
parser.parse(`defmodule M do
|
||||
quote do
|
||||
@callback run(term()) :: term()
|
||||
end
|
||||
end`),
|
||||
'lib/quoted_callback.ex',
|
||||
);
|
||||
expect(graph.nodes.find((node) => node.properties.qualifiedName === 'M')?.label).toBe('Class');
|
||||
expect(graph.nodes.some((node) => node.properties.qualifiedName === 'M.run')).toBe(false);
|
||||
});
|
||||
|
||||
it('emits canonical zero-arity ordinary and guarded declarations', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'zero-arity.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const graph = extractElixirSemanticGraph(
|
||||
parser.parse(`
|
||||
defmodule Zero do
|
||||
def run, do: :ok
|
||||
def guarded when true, do: :ok
|
||||
defmacro build, do: :ok
|
||||
end
|
||||
`),
|
||||
'lib/zero.ex',
|
||||
);
|
||||
expect(
|
||||
graph.nodes
|
||||
.filter((node) => node.label === 'Function' || node.label === 'Macro')
|
||||
.map((node) => node.properties),
|
||||
).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
qualifiedName: 'Zero.run',
|
||||
parameterCount: 0,
|
||||
requiredParameterCount: 0,
|
||||
startLine: 2,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
qualifiedName: 'Zero.guarded',
|
||||
parameterCount: 0,
|
||||
requiredParameterCount: 0,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
qualifiedName: 'Zero.build',
|
||||
parameterCount: 0,
|
||||
requiredParameterCount: 0,
|
||||
}),
|
||||
]),
|
||||
);
|
||||
expect(graph.relationships).toContainEqual(
|
||||
expect.objectContaining({ type: 'HAS_METHOD', reason: 'elixir module function' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps repeated modules as separate ambiguous candidates without qualified call edges', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'repeated.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const first = extractElixirSemanticGraph(
|
||||
parser.parse('defmodule M do\n def f, do: :one\nend\ndefmodule M do\n def f, do: :two\nend'),
|
||||
'lib/same.ex',
|
||||
);
|
||||
const second = extractElixirSemanticGraph(
|
||||
parser.parse('defmodule M do\n def f, do: :other\nend'),
|
||||
'lib/other.ex',
|
||||
);
|
||||
expect(first.nodes.filter((node) => node.properties.qualifiedName === 'M.f')).toHaveLength(2);
|
||||
expect(
|
||||
new Set(
|
||||
[...first.nodes, ...second.nodes]
|
||||
.filter((node) => node.properties.qualifiedName === 'M.f')
|
||||
.map((node) => node.id),
|
||||
).size,
|
||||
).toBe(3);
|
||||
expect(
|
||||
[...first.relationships, ...second.relationships].filter(
|
||||
(relationship) => relationship.type === 'CALLS',
|
||||
),
|
||||
).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('models static behaviour callbacks as an interface contract', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'behaviour.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const graph = extractElixirSemanticGraph(
|
||||
parser.parse(`
|
||||
defmodule WorkerBehaviour do
|
||||
@callback run(term()) :: term()
|
||||
end
|
||||
`),
|
||||
'lib/worker_behaviour.ex',
|
||||
);
|
||||
expect(graph.nodes).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
label: 'Interface',
|
||||
properties: expect.objectContaining({ qualifiedName: 'WorkerBehaviour' }),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
label: 'Function',
|
||||
properties: expect.objectContaining({
|
||||
qualifiedName: 'WorkerBehaviour.run',
|
||||
parameterCount: 1,
|
||||
}),
|
||||
}),
|
||||
]),
|
||||
);
|
||||
const contract = graph.nodes.find(
|
||||
(node) => node.properties.qualifiedName === 'WorkerBehaviour',
|
||||
)!;
|
||||
expect(
|
||||
graph.symbols.find((symbol) => symbol.qualifiedName === 'WorkerBehaviour.run'),
|
||||
).toMatchObject({
|
||||
ownerId: contract.id,
|
||||
});
|
||||
});
|
||||
|
||||
it('records literal Mix metadata without evaluating the project file', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'mix.exs');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const graph = extractElixirSemanticGraph(
|
||||
parser.parse(`
|
||||
defmodule Demo.MixProject do
|
||||
def project, do: [app: :demo, apps_path: "apps", deps: deps()]
|
||||
def application, do: [applications: [:kernel], extra_applications: [:logger]]
|
||||
defp deps, do: [{:sibling, path: "../sibling"}, {:umbrella, in_umbrella: true}, dynamic()]
|
||||
end
|
||||
`),
|
||||
'mix.exs',
|
||||
);
|
||||
const metadata = graph.nodes.filter((node) => node.label === 'CodeElement');
|
||||
expect(metadata.map((node) => node.properties)).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ elixirKind: 'mix-app', app: 'demo' }),
|
||||
expect.objectContaining({ elixirKind: 'mix-umbrella', appsPath: 'apps' }),
|
||||
expect.objectContaining({ elixirKind: 'mix-application', app: 'kernel' }),
|
||||
expect.objectContaining({ elixirKind: 'mix-application', app: 'logger' }),
|
||||
expect.objectContaining({
|
||||
elixirKind: 'mix-dependency',
|
||||
app: 'sibling',
|
||||
path: '../sibling',
|
||||
}),
|
||||
expect.objectContaining({
|
||||
elixirKind: 'mix-dependency',
|
||||
app: 'umbrella',
|
||||
inUmbrella: true,
|
||||
}),
|
||||
]),
|
||||
);
|
||||
expect(metadata.map((node) => node.properties.app)).not.toContain('dynamic');
|
||||
expect(
|
||||
metadata.find((node) => node.properties.elixirKind === 'mix-app')?.properties.startLine,
|
||||
).toBe(2);
|
||||
});
|
||||
|
||||
it('adds static OTP dependency evidence while retaining normal call extraction', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'otp.ex');
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const graph = extractElixirSemanticGraph(
|
||||
parser.parse(`
|
||||
defmodule Worker do end
|
||||
defmodule Other do end
|
||||
defmodule App do
|
||||
def start, do: Supervisor.start_link([Worker, {Other, []}, dynamic_child()], strategy: :one_for_one)
|
||||
def boot, do: GenServer.start_link(Worker, [], name: Worker)
|
||||
def request, do: GenServer.call({:local, Worker}, :work)
|
||||
def notify, do: GenServer.cast(server_name(), :work)
|
||||
end
|
||||
`),
|
||||
'lib/app.ex',
|
||||
);
|
||||
const evidence = graph.nodes
|
||||
.filter((node) => node.label === 'CodeElement')
|
||||
.map((node) => node.properties);
|
||||
expect(evidence).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ elixirKind: 'supervisor-child', target: 'Worker' }),
|
||||
expect.objectContaining({ elixirKind: 'supervisor-child', target: 'Other' }),
|
||||
expect.objectContaining({ elixirKind: 'genserver-start_link', target: 'Worker' }),
|
||||
expect.objectContaining({ elixirKind: 'genserver-call', target: 'Worker' }),
|
||||
]),
|
||||
);
|
||||
expect(evidence).not.toContainEqual(
|
||||
expect.objectContaining({ elixirKind: 'supervisor-child', target: 'dynamic_child' }),
|
||||
);
|
||||
expect(evidence).not.toContainEqual(expect.objectContaining({ elixirKind: 'genserver-cast' }));
|
||||
expect(graph.relationships.filter((edge) => edge.type === 'DECLARES')).toHaveLength(4);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { createRequire } from 'node:module';
|
||||
import { createHash } from 'crypto';
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { SupportedLanguages } from 'gitnexus-shared';
|
||||
import {
|
||||
contentHashForNode,
|
||||
EMBEDDING_TEXT_VERSION,
|
||||
|
|
@ -16,10 +18,22 @@ import {
|
|||
STRUCTURAL_LABELS,
|
||||
} from '../../src/core/embeddings/types.js';
|
||||
import { STALE_HASH_SENTINEL } from '../../src/core/lbug/schema.js';
|
||||
import { extractStructuralNames } from '../../src/core/embeddings/structural-extractor.js';
|
||||
import { isLanguageAvailable } from '../../src/core/tree-sitter/parser-loader.js';
|
||||
|
||||
const CLASS_CHUNK_SIZE = 90;
|
||||
const CLASS_OVERLAP = 10;
|
||||
|
||||
const _require = createRequire(import.meta.url);
|
||||
const elixirPackageInstalled = (() => {
|
||||
try {
|
||||
_require.resolve('tree-sitter-elixir');
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
})();
|
||||
|
||||
describe('embedding schema column contracts', () => {
|
||||
it('does not query Objective-C protocol/category tables for an isExported column', () => {
|
||||
expect(LABELS_WITH_EXPORTED.has(LABEL_PROTOCOL)).toBe(false);
|
||||
|
|
@ -32,6 +46,24 @@ describe('embedding schema column contracts', () => {
|
|||
expect(EMBEDDABLE_LABELS).toContain(LABEL_PROTOCOL);
|
||||
expect(EMBEDDABLE_LABELS).toContain(LABEL_CATEGORY);
|
||||
});
|
||||
|
||||
it.skipIf(!elixirPackageInstalled)(
|
||||
'extracts defmodule methods without treating ordinary calls as declarations',
|
||||
async () => {
|
||||
expect(isLanguageAvailable(SupportedLanguages.Elixir)).toBe(true);
|
||||
|
||||
await expect(
|
||||
extractStructuralNames(
|
||||
'defmodule Example do\n def run(value), do: value\nend',
|
||||
'example.ex',
|
||||
),
|
||||
).resolves.toMatchObject({ methodNames: ['run'] });
|
||||
|
||||
await expect(
|
||||
extractStructuralNames('configure do\n def leaked(value), do: value\nend', 'example.ex'),
|
||||
).resolves.toMatchObject({ methodNames: [] });
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
|
|
|||
|
|
@ -257,6 +257,7 @@ describe('shouldIgnorePath', () => {
|
|||
describe('isHardcodedIgnoredDirectory', () => {
|
||||
it('returns true for known ignored directories', () => {
|
||||
expect(isHardcodedIgnoredDirectory('node_modules')).toBe(true);
|
||||
expect(isHardcodedIgnoredDirectory('deps')).toBe(false);
|
||||
expect(isHardcodedIgnoredDirectory('.git')).toBe(true);
|
||||
expect(isHardcodedIgnoredDirectory('dist')).toBe(true);
|
||||
expect(isHardcodedIgnoredDirectory('monaco-workers')).toBe(true);
|
||||
|
|
@ -580,6 +581,9 @@ describe('createIgnoreFilter', () => {
|
|||
const mockPath = { name: 'node_modules', relative: () => 'node_modules' } as any;
|
||||
expect(filter.childrenIgnored(mockPath)).toBe(true);
|
||||
|
||||
const sourceDepsPath = { name: 'deps', relative: () => 'src/deps' } as any;
|
||||
expect(filter.childrenIgnored(sourceDepsPath)).toBe(false);
|
||||
|
||||
const srcPath = { name: 'src', relative: () => 'src' } as any;
|
||||
expect(filter.childrenIgnored(srcPath)).toBe(false);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -288,8 +288,11 @@ describe('PARSE_CACHE_VERSION', () => {
|
|||
// Moved 103 -> 104 for #3339 review: pair-HOC queries name
|
||||
// `mutation(withAuth(arrow))` object-pair handlers. Warm caches replay
|
||||
// anonymous arrows, so both stores re-extract.
|
||||
it('pins SCHEMA_BUMP to 104 so concurrent bumps cannot silently collide (#2766, #3015, #3088, #2885, #3128, #2865, #3130, #1432, #3161, #3179, #3219, #3190, #3253, #3273, #3339)', () => {
|
||||
expect(Number(PARSE_CACHE_VERSION.split('+', 1)[0])).toBe(104);
|
||||
// Moved 108 -> 109 to exclude quoted Elixir runtime capture facts, then
|
||||
// 109 -> 110 for Elixir import-category and CFG capture persistence, then
|
||||
// 110 -> 111 for Elixir declaration and Phoenix resource capture changes.
|
||||
it('pins SCHEMA_BUMP to 111 so concurrent bumps cannot silently collide (#2766, #3015, #3088, #2885, #3128, #2865, #3130, #1432, #3161, #3179, #3219, #3190, #3253, #3273, #3339)', () => {
|
||||
expect(Number(PARSE_CACHE_VERSION.split('+', 1)[0])).toBe(111);
|
||||
expect(PARSE_CACHE_BUCKET_COUNT).toBe(128);
|
||||
// The PREVIOUS version must fail the reuse gate, not merely differ from the
|
||||
// current one — a hardcoded number outside the conflict hunk rebases cleanly
|
||||
|
|
@ -298,6 +301,7 @@ describe('PARSE_CACHE_VERSION', () => {
|
|||
for (const taken of [
|
||||
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
|
||||
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103,
|
||||
104, 105, 106, 107, 108, 109, 110,
|
||||
]) {
|
||||
expect(Number(PARSE_CACHE_VERSION.split('+', 1)[0])).not.toBe(taken);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -124,6 +124,11 @@ const SMOKE_CASES: Record<string, SmokeCase> = {
|
|||
snippet: 'fun main() {}\n',
|
||||
rootType: 'source_file',
|
||||
},
|
||||
[SupportedLanguages.Elixir]: {
|
||||
language: SupportedLanguages.Elixir,
|
||||
snippet: 'defmodule Smoke do\nend\n',
|
||||
rootType: 'source',
|
||||
},
|
||||
[SupportedLanguages.Zig]: {
|
||||
language: SupportedLanguages.Zig,
|
||||
snippet: 'pub fn main() void {}\n',
|
||||
|
|
|
|||
|
|
@ -311,6 +311,54 @@ describe('prebuild workflow rebuild loop guard', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('fetches an unavailable valid synchronize endpoint before preserving the binary-only diff', () => {
|
||||
const remote = mkdtempSync(path.join(tmpdir(), 'gitnexus-prebuild-origin-'));
|
||||
const checkout = mkdtempSync(path.join(tmpdir(), 'gitnexus-prebuild-checkout-'));
|
||||
try {
|
||||
execFileSync('git', ['clone', '--bare', root, remote]);
|
||||
execFileSync('git', ['clone', '--depth', '1', `file://${remote}`, checkout]);
|
||||
expect(() =>
|
||||
execFileSync('git', ['cat-file', '-e', `${source}^{commit}`], {
|
||||
cwd: checkout,
|
||||
stdio: 'ignore',
|
||||
}),
|
||||
).toThrow();
|
||||
|
||||
const runnerTemp = mkdtempSync(path.join(checkout, 'runner-'));
|
||||
const output = path.join(runnerTemp, 'output');
|
||||
const result = spawnSync(process.execPath, ['--input-type=module', '-'], {
|
||||
cwd: checkout,
|
||||
input: script,
|
||||
encoding: 'utf8',
|
||||
env: {
|
||||
...process.env,
|
||||
EVENT: 'pull_request',
|
||||
ACTION: 'synchronize',
|
||||
BASE_SHA: base,
|
||||
BEFORE_SHA: source,
|
||||
HEAD_SHA: execFileSync('git', ['rev-parse', 'HEAD'], {
|
||||
cwd: checkout,
|
||||
encoding: 'utf8',
|
||||
}).trim(),
|
||||
INPUT_GRAMMARS: '',
|
||||
INPUT_REF: '',
|
||||
FORCE: 'false',
|
||||
RUNNER_TEMP: runnerTemp,
|
||||
GITHUB_OUTPUT: output,
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.status, result.stderr).toBe(0);
|
||||
expect(readFileSync(output, 'utf8')).toBe('any=false\nmatrix={"include":[]}\n');
|
||||
expect(
|
||||
execFileSync('git', ['cat-file', '-e', `${source}^{commit}`], { cwd: checkout }),
|
||||
).toBeDefined();
|
||||
} finally {
|
||||
rmSync(checkout, { recursive: true, force: true });
|
||||
rmSync(remote, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('stops repeated binary-only updates while the PR still contains new source', () => {
|
||||
let before = source;
|
||||
for (let build = 2; build <= 4; build++) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,355 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import Parser from 'tree-sitter';
|
||||
import { SupportedLanguages } from 'gitnexus-shared';
|
||||
import { extractParsedFile } from '../../../../src/core/ingestion/scope-extractor-bridge.js';
|
||||
import { elixirProvider } from '../../../../src/core/ingestion/languages/elixir.js';
|
||||
import {
|
||||
getLanguageGrammar,
|
||||
loadLanguage,
|
||||
} from '../../../../src/core/tree-sitter/parser-loader.js';
|
||||
import { ELIXIR_QUERIES } from '../../../../src/core/ingestion/tree-sitter-queries.js';
|
||||
|
||||
describe('Elixir scope captures', () => {
|
||||
it('keeps clause callsites and arities while semantic declarations coalesce separately', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'lib/nonconventional_name.ex');
|
||||
const parsed = extractParsedFile(
|
||||
elixirProvider,
|
||||
`
|
||||
defmodule M do
|
||||
def f(:one), do: target(:one)
|
||||
def f(:two), do: target(:two)
|
||||
def target(value), do: value
|
||||
end
|
||||
`,
|
||||
'lib/nonconventional_name.ex',
|
||||
);
|
||||
expect(parsed).toBeDefined();
|
||||
const targetCalls = parsed?.referenceSites.filter((site) => site.name === 'target') ?? [];
|
||||
expect(targetCalls.map((site) => site.atRange.startLine)).toEqual([3, 4]);
|
||||
expect(targetCalls.map((site) => site.arity)).toEqual([1, 1]);
|
||||
});
|
||||
|
||||
it('captures zero-arity and guarded definitions once, and grouped aliases into scope', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'captures.ex');
|
||||
const source = `defmodule Captures do
|
||||
def zero, do: :ok
|
||||
def guarded(value) when is_binary(value), do: value
|
||||
def guarded_zero when true, do: :ok
|
||||
defp guarded_private when true, do: :ok
|
||||
defmacro guarded_macro when true, do: :ok
|
||||
defmacrop guarded_private_macro when true, do: :ok
|
||||
defguard guarded_guard when true
|
||||
defguardp guarded_private_guard when true
|
||||
alias Prefix.{One, Two}
|
||||
end`;
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const matches = new Parser.Query(parser.getLanguage(), ELIXIR_QUERIES).matches(
|
||||
parser.parse(source).rootNode,
|
||||
);
|
||||
const definitionNames = matches
|
||||
.filter((match) =>
|
||||
match.captures.some((capture) =>
|
||||
['definition.function', 'definition.macro'].includes(capture.name),
|
||||
),
|
||||
)
|
||||
.flatMap((match) =>
|
||||
match.captures
|
||||
.filter((capture) => capture.name === 'name')
|
||||
.map((capture) => capture.node.text),
|
||||
);
|
||||
expect(definitionNames).toEqual([
|
||||
'zero',
|
||||
'guarded',
|
||||
'guarded_zero',
|
||||
'guarded_private',
|
||||
'guarded_macro',
|
||||
'guarded_private_macro',
|
||||
'guarded_guard',
|
||||
'guarded_private_guard',
|
||||
]);
|
||||
expect(
|
||||
matches
|
||||
.flatMap((match) => match.captures)
|
||||
.filter((capture) => capture.name === 'import.source')
|
||||
.map((capture) => capture.node.text),
|
||||
).toEqual(['Prefix.{One, Two}']);
|
||||
|
||||
const parsed = extractParsedFile(elixirProvider, source, 'captures.ex');
|
||||
const bindings = Object.fromEntries(
|
||||
parsed?.scopes.flatMap((scope) =>
|
||||
[...scope.typeBindings].map(([name, ref]) => [name, ref.rawName]),
|
||||
) ?? [],
|
||||
);
|
||||
expect(bindings).toMatchObject({ One: 'Prefix.One', Two: 'Prefix.Two' });
|
||||
});
|
||||
|
||||
it('does not turn quoted definitions into local declarations', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'quoted.ex');
|
||||
const parsed = extractParsedFile(
|
||||
elixirProvider,
|
||||
`defmodule M do
|
||||
def real, do: :ok
|
||||
quote do
|
||||
def generated(value), do: value
|
||||
end
|
||||
end`,
|
||||
'quoted.ex',
|
||||
);
|
||||
expect(parsed?.localDefs.map((def) => def.qualifiedName).sort()).toEqual(['M', 'M.real']);
|
||||
});
|
||||
|
||||
it('captures aliases, constrained imports, pipelines, captures, delegates, and macro identity', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'lib/nonconventional_name.ex');
|
||||
const parsed = extractParsedFile(
|
||||
elixirProvider,
|
||||
`
|
||||
defmodule Actual.Name do
|
||||
alias External.Service, as: Service
|
||||
alias Actual.Name.Nested
|
||||
alias Prefix.{One, Two}
|
||||
import Helpers, only: [allowed: 1]
|
||||
import Filtered, except: [hidden: 1]
|
||||
require Required
|
||||
use Dynamic
|
||||
def run(value), do: value |> allowed() |> Service.call(:flag)
|
||||
def captured, do: &local/1
|
||||
def qualified_capture, do: &Remote.call/1
|
||||
defdelegate forwarded(value), to: Remote, as: :call
|
||||
defmacro same(value), do: value
|
||||
def same(value), do: value
|
||||
end
|
||||
`,
|
||||
'lib/filename_does_not_define_the_module.ex',
|
||||
);
|
||||
expect(
|
||||
parsed?.localDefs
|
||||
.filter((def) => def.qualifiedName === 'Actual.Name.same')
|
||||
.map((def) => def.type),
|
||||
).toEqual(expect.arrayContaining(['Function', 'Macro']));
|
||||
expect(parsed?.parsedImports.some((imp) => imp.targetRaw === 'Filtered')).toBe(false);
|
||||
expect(parsed?.parsedImports.some((imp) => imp.targetRaw === 'Helpers')).toBe(false);
|
||||
expect(
|
||||
elixirProvider.collectCaptureSideChannel?.('lib/filename_does_not_define_the_module.ex'),
|
||||
).toMatchObject({
|
||||
kind: 'elixir',
|
||||
importExcepts: [
|
||||
expect.objectContaining({ target: 'Filtered', excluded: [{ name: 'hidden', arity: 1 }] }),
|
||||
],
|
||||
});
|
||||
const calls = parsed?.referenceSites ?? [];
|
||||
expect(calls.filter((site) => site.name === 'allowed').map((site) => site.arity)).toEqual([1]);
|
||||
expect(
|
||||
calls.find((site) => site.name === 'call' && site.explicitReceiver?.name === 'Service'),
|
||||
).toMatchObject({ arity: 2 });
|
||||
expect(calls.find((site) => site.name === 'local')).toMatchObject({
|
||||
arity: 1,
|
||||
callForm: 'free',
|
||||
});
|
||||
expect(
|
||||
calls
|
||||
.filter((site) => site.name === 'call' && site.explicitReceiver?.name === 'Remote')
|
||||
.map((site) => site.arity),
|
||||
).toEqual([1, 1]);
|
||||
expect(calls.find((site) => site.name === 'forwarded')).toBeUndefined();
|
||||
const bindings = Object.fromEntries(
|
||||
parsed?.scopes.flatMap((scope) =>
|
||||
[...scope.typeBindings].map(([name, ref]) => [name, ref.rawName]),
|
||||
) ?? [],
|
||||
);
|
||||
expect(bindings).toMatchObject({
|
||||
Service: 'External.Service',
|
||||
Nested: 'Actual.Name.Nested',
|
||||
One: 'Prefix.One',
|
||||
Two: 'Prefix.Two',
|
||||
});
|
||||
});
|
||||
|
||||
it('records a `from p in Schema` query fact without relying on Repo calls', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'from-query.ex');
|
||||
extractParsedFile(
|
||||
elixirProvider,
|
||||
'defmodule Queries do\n def all, do: from p in MyApp.Post\nend',
|
||||
'lib/from_query.ex',
|
||||
);
|
||||
expect(elixirProvider.collectCaptureSideChannel?.('lib/from_query.ex')).toMatchObject({
|
||||
kind: 'elixir',
|
||||
frameworkFacts: [
|
||||
expect.objectContaining({ kind: 'query', method: 'from', model: 'MyApp.Post' }),
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('keeps literal atom resource filters while ignoring dynamic filters', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'routes.ex');
|
||||
extractParsedFile(
|
||||
elixirProvider,
|
||||
`defmodule Router do
|
||||
resources "/only", Controller, only: :index
|
||||
resources "/except", Controller, except: :show
|
||||
resources "/only-list", Controller, only: [:index, :show]
|
||||
resources "/except-list", Controller, except: [:show, :delete]
|
||||
resources "/dynamic", Controller, only: allowed_actions()
|
||||
end`,
|
||||
'routes.ex',
|
||||
);
|
||||
const routes =
|
||||
elixirProvider
|
||||
.collectCaptureSideChannel?.('routes.ex')
|
||||
?.frameworkFacts?.filter((fact) => fact.kind === 'route') ?? [];
|
||||
expect(routes.filter((fact) => fact.path === '/only')).toHaveLength(1);
|
||||
expect(routes.some((fact) => fact.path === '/only/:id')).toBe(false);
|
||||
expect(routes.some((fact) => fact.path === '/except/:id' && fact.action === 'show')).toBe(
|
||||
false,
|
||||
);
|
||||
expect(routes.filter((fact) => fact.path.startsWith('/only-list'))).toHaveLength(2);
|
||||
expect(routes.some((fact) => fact.path === '/only-list/new')).toBe(false);
|
||||
expect(routes.some((fact) => fact.path === '/except-list/:id' && fact.action === 'show')).toBe(
|
||||
false,
|
||||
);
|
||||
expect(
|
||||
routes.some((fact) => fact.path.startsWith('/except-list') && fact.action === 'delete'),
|
||||
).toBe(false);
|
||||
expect(routes.filter((fact) => fact.path.startsWith('/dynamic'))).toHaveLength(8);
|
||||
});
|
||||
|
||||
it('has identical cold and cached-tree ParsedFile output', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'cached.ex');
|
||||
const source =
|
||||
'defmodule Cached do\n import Filtered, except: [hidden: 1]\n import Helpers, only: [allowed: 1]\n alias Remote.Service, as: Service\n def run(v), do: v |> Service.call()\n end';
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
expect(
|
||||
extractParsedFile(elixirProvider, source, 'cached.ex', undefined, parser.parse(source)),
|
||||
).toEqual(extractParsedFile(elixirProvider, source, 'cached.ex'));
|
||||
});
|
||||
|
||||
it('treats category-only imports as wildcard imports, not empty allowlists', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'category-import.ex');
|
||||
const parsed = extractParsedFile(
|
||||
elixirProvider,
|
||||
'defmodule M do\n import Helpers, only: :functions\n import Macros, only: :macros\nend',
|
||||
'category-import.ex',
|
||||
);
|
||||
expect(parsed?.parsedImports).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ kind: 'wildcard', targetRaw: 'Helpers' }),
|
||||
expect.objectContaining({ kind: 'wildcard', targetRaw: 'Macros' }),
|
||||
]),
|
||||
);
|
||||
expect(elixirProvider.collectCaptureSideChannel?.('category-import.ex')).toMatchObject({
|
||||
kind: 'elixir',
|
||||
importOnly: [
|
||||
expect.objectContaining({ target: 'Helpers', category: 'functions' }),
|
||||
expect.objectContaining({ target: 'Macros', category: 'macros' }),
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('does not turn category-import text outside import AST nodes into facts or bindings', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'category-import-literals.ex');
|
||||
const parsed = extractParsedFile(
|
||||
elixirProvider,
|
||||
`defmodule M do
|
||||
# import Commented, only: :functions
|
||||
text = "import Stringified, only: :macros"
|
||||
heredoc = """
|
||||
import Heredoc, only: :functions
|
||||
"""
|
||||
quote do
|
||||
import Quoted, only: :macros
|
||||
end
|
||||
end`,
|
||||
'category-import-literals.ex',
|
||||
);
|
||||
expect(parsed?.parsedImports).toEqual([]);
|
||||
expect(
|
||||
elixirProvider.collectCaptureSideChannel?.('category-import-literals.ex'),
|
||||
).toBeUndefined();
|
||||
const bindings = parsed?.scopes.flatMap((scope) => [...scope.typeBindings.keys()]) ?? [];
|
||||
for (const forbidden of ['Commented', 'Stringified', 'Heredoc', 'Quoted'])
|
||||
expect(bindings).not.toContain(forbidden);
|
||||
});
|
||||
|
||||
it('extracts defmodule aliases as method owners and ordinary parameters', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'method-owner.ex');
|
||||
const source = 'defmodule App.Worker do\n def run(value), do: value\nend';
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(
|
||||
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
|
||||
);
|
||||
const extracted = elixirProvider.methodExtractor!.extract(
|
||||
parser.parse(source).rootNode.namedChild(0)!,
|
||||
{
|
||||
filePath: 'method-owner.ex',
|
||||
language: SupportedLanguages.Elixir,
|
||||
},
|
||||
);
|
||||
expect(extracted).toMatchObject({
|
||||
ownerName: 'App.Worker',
|
||||
methods: [
|
||||
expect.objectContaining({
|
||||
name: 'run',
|
||||
parameters: [expect.objectContaining({ name: 'value' })],
|
||||
}),
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('captures behaviours and protocol implementations as heritage facts without lifecycle calls', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'otp.ex');
|
||||
const parsed = extractParsedFile(
|
||||
elixirProvider,
|
||||
`
|
||||
defmodule Worker do
|
||||
@behaviour Runner
|
||||
use GenServer
|
||||
def init(state), do: {:ok, state}
|
||||
end
|
||||
defimpl Printable, for: User do
|
||||
def print(user), do: user
|
||||
end
|
||||
`,
|
||||
'lib/otp.ex',
|
||||
);
|
||||
const heritage = parsed?.referenceSites.filter((site) => site.kind === 'inherits') ?? [];
|
||||
expect(heritage).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ name: 'Runner' }),
|
||||
expect.objectContaining({
|
||||
name: 'Printable',
|
||||
explicitReceiver: expect.objectContaining({ name: 'User' }),
|
||||
}),
|
||||
]),
|
||||
);
|
||||
expect(parsed?.referenceSites.find((site) => site.name === 'init')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('classifies callback modules as interfaces and ignores quoted callbacks', async () => {
|
||||
await loadLanguage(SupportedLanguages.Elixir, 'behaviour.ex');
|
||||
const parsed = extractParsedFile(
|
||||
elixirProvider,
|
||||
`defmodule Runner do
|
||||
@callback run(term()) :: term()
|
||||
end
|
||||
defmodule Quoted do
|
||||
quote do
|
||||
@callback ignored(term()) :: term()
|
||||
end
|
||||
end`,
|
||||
'behaviour.ex',
|
||||
);
|
||||
expect(parsed?.localDefs).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ qualifiedName: 'Runner', type: 'Interface' }),
|
||||
]),
|
||||
);
|
||||
expect(parsed?.localDefs).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ qualifiedName: 'Quoted', type: 'Class' })]),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,242 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import type { ParsedFile, ScopeId, SymbolDefinition } from 'gitnexus-shared';
|
||||
import { elixirScopeResolver } from '../../../../src/core/ingestion/languages/elixir/scope-resolver.js';
|
||||
|
||||
const importerScope = 'scope:importer#0:0-9:0:Module' as ScopeId;
|
||||
const targetScope = 'scope:target#0:0-9:0:Module' as ScopeId;
|
||||
const callable = (name: string, arity: number, exported = true): SymbolDefinition => ({
|
||||
nodeId: `${name}/${arity}`,
|
||||
type: 'Function',
|
||||
name,
|
||||
qualifiedName: `Filtered.${name}`,
|
||||
parameterCount: arity,
|
||||
requiredParameterCount: arity,
|
||||
isExported: exported,
|
||||
filePath: 'filtered.ex',
|
||||
range: { startLine: 1, startCol: 0, endLine: 1, endCol: 1 },
|
||||
});
|
||||
|
||||
describe('Elixir import except augmentation', () => {
|
||||
it('adds only the exact public name and arity from worker-safe only facts', () => {
|
||||
const importer = {
|
||||
filePath: 'importer.ex',
|
||||
moduleScope: importerScope,
|
||||
parsedImports: [],
|
||||
localDefs: [],
|
||||
referenceSites: [],
|
||||
scopes: [{ id: importerScope, range: { startLine: 1, startCol: 0, endLine: 9, endCol: 0 } }],
|
||||
captureSideChannel: {
|
||||
kind: 'elixir',
|
||||
importExcepts: [],
|
||||
importOnly: [
|
||||
{ target: 'Filtered', allowed: [{ name: 'f', arity: 1 }], startLine: 1, startCol: 0 },
|
||||
],
|
||||
},
|
||||
} as unknown as ParsedFile;
|
||||
const target = {
|
||||
filePath: 'filtered.ex',
|
||||
moduleScope: targetScope,
|
||||
parsedImports: [],
|
||||
referenceSites: [],
|
||||
scopes: [],
|
||||
localDefs: [
|
||||
{ ...callable('Filtered', 0), type: 'Class', qualifiedName: 'Filtered' },
|
||||
callable('f', 1),
|
||||
callable('f', 2),
|
||||
callable('private', 1, false),
|
||||
],
|
||||
} as unknown as ParsedFile;
|
||||
const augmentations = new Map();
|
||||
elixirScopeResolver.populateNamespaceSiblings!(
|
||||
[importer, target],
|
||||
{ bindingAugmentations: augmentations } as never,
|
||||
{ fileContents: new Map() },
|
||||
);
|
||||
expect(
|
||||
augmentations
|
||||
.get(importerScope)!
|
||||
.get('f')
|
||||
.map((binding: { def: SymbolDefinition }) => binding.def.parameterCount),
|
||||
).toEqual([1]);
|
||||
expect(augmentations.get(importerScope)!.has('private')).toBe(false);
|
||||
});
|
||||
|
||||
it('adds only public non-excluded callable overloads at the lexical import scope', () => {
|
||||
const importer = {
|
||||
filePath: 'importer.ex',
|
||||
moduleScope: importerScope,
|
||||
parsedImports: [],
|
||||
localDefs: [],
|
||||
referenceSites: [],
|
||||
scopes: [{ id: importerScope, range: { startLine: 1, startCol: 0, endLine: 9, endCol: 0 } }],
|
||||
captureSideChannel: {
|
||||
kind: 'elixir',
|
||||
importExcepts: [
|
||||
{
|
||||
target: 'Filtered',
|
||||
excluded: [{ name: 'hidden', arity: 1 }],
|
||||
startLine: 2,
|
||||
startCol: 0,
|
||||
},
|
||||
],
|
||||
},
|
||||
} as unknown as ParsedFile;
|
||||
const target = {
|
||||
filePath: 'filtered.ex',
|
||||
moduleScope: targetScope,
|
||||
parsedImports: [],
|
||||
referenceSites: [],
|
||||
scopes: [],
|
||||
localDefs: [
|
||||
{ ...callable('Filtered', 0), type: 'Class', qualifiedName: 'Filtered' },
|
||||
{ ...callable('Sibling', 0), type: 'Class', qualifiedName: 'Sibling' },
|
||||
callable('visible', 1),
|
||||
callable('hidden', 1),
|
||||
callable('hidden', 2),
|
||||
{ ...callable('foreign', 1), qualifiedName: 'Sibling.foreign' },
|
||||
{ ...callable('nested', 0), qualifiedName: 'Filtered.Nested.nested' },
|
||||
callable('private', 0, false),
|
||||
],
|
||||
} as unknown as ParsedFile;
|
||||
const augmentations = new Map();
|
||||
elixirScopeResolver.populateNamespaceSiblings!(
|
||||
[importer, target],
|
||||
{
|
||||
bindingAugmentations: augmentations,
|
||||
} as never,
|
||||
{ fileContents: new Map() },
|
||||
);
|
||||
expect([...augmentations.get(importerScope)!.keys()].sort()).toEqual(['hidden', 'visible']);
|
||||
expect(
|
||||
augmentations
|
||||
.get(importerScope)!
|
||||
.get('hidden')
|
||||
.map((binding: { def: SymbolDefinition }) => binding.def.parameterCount),
|
||||
).toEqual([2]);
|
||||
expect(
|
||||
augmentations
|
||||
.get(importerScope)!
|
||||
.get('hidden')
|
||||
.some((binding: { def: SymbolDefinition }) => binding.def.parameterCount === 1),
|
||||
).toBe(false);
|
||||
expect(augmentations.get(importerScope)!.has('foreign')).toBe(false);
|
||||
expect(augmentations.get(importerScope)!.has('nested')).toBe(false);
|
||||
});
|
||||
|
||||
it('filters category-only imports by exported Function or Macro kind', () => {
|
||||
const importer = {
|
||||
filePath: 'importer.ex',
|
||||
moduleScope: importerScope,
|
||||
parsedImports: [{ kind: 'wildcard', targetRaw: 'Filtered', declaredAtScope: importerScope }],
|
||||
localDefs: [],
|
||||
referenceSites: [],
|
||||
scopes: [],
|
||||
captureSideChannel: {
|
||||
kind: 'elixir',
|
||||
importExcepts: [],
|
||||
importOnly: [
|
||||
{ target: 'Filtered', allowed: [], category: 'functions', startLine: 1, startCol: 0 },
|
||||
],
|
||||
},
|
||||
} as unknown as ParsedFile;
|
||||
const target = {
|
||||
filePath: 'filtered.ex',
|
||||
moduleScope: targetScope,
|
||||
parsedImports: [],
|
||||
referenceSites: [],
|
||||
scopes: [],
|
||||
localDefs: [
|
||||
{ ...callable('Filtered', 0), type: 'Class', qualifiedName: 'Filtered' },
|
||||
callable('function', 0),
|
||||
{ ...callable('macro', 0), type: 'Macro' },
|
||||
],
|
||||
} as unknown as ParsedFile;
|
||||
const augmentations = new Map();
|
||||
elixirScopeResolver.populateNamespaceSiblings!(
|
||||
[importer, target],
|
||||
{ bindingAugmentations: augmentations } as never,
|
||||
{ fileContents: new Map() },
|
||||
);
|
||||
expect([...augmentations.get(importerScope)!.keys()]).toEqual(['function']);
|
||||
});
|
||||
|
||||
it('imports only macros for only: :macros and imports nothing for only: []', () => {
|
||||
const importer = (only: {
|
||||
allowed: readonly { name: string; arity: number }[];
|
||||
category?: 'functions' | 'macros';
|
||||
}) =>
|
||||
({
|
||||
filePath: 'importer.ex',
|
||||
moduleScope: importerScope,
|
||||
parsedImports: [
|
||||
{ kind: 'wildcard', targetRaw: 'Filtered', declaredAtScope: importerScope },
|
||||
],
|
||||
localDefs: [],
|
||||
referenceSites: [],
|
||||
scopes: [],
|
||||
captureSideChannel: {
|
||||
kind: 'elixir',
|
||||
importExcepts: [],
|
||||
importOnly: [{ target: 'Filtered', startLine: 1, startCol: 0, ...only }],
|
||||
},
|
||||
}) as unknown as ParsedFile;
|
||||
const target = {
|
||||
filePath: 'filtered.ex',
|
||||
moduleScope: targetScope,
|
||||
parsedImports: [],
|
||||
referenceSites: [],
|
||||
scopes: [],
|
||||
localDefs: [
|
||||
{ ...callable('Filtered', 0), type: 'Class', qualifiedName: 'Filtered' },
|
||||
callable('function', 0),
|
||||
{ ...callable('macro', 0), type: 'Macro' },
|
||||
],
|
||||
} as unknown as ParsedFile;
|
||||
for (const [only, expected] of [
|
||||
[{ allowed: [], category: 'macros' as const }, ['macro']],
|
||||
[{ allowed: [] }, []],
|
||||
] as const) {
|
||||
const augmentations = new Map();
|
||||
elixirScopeResolver.populateNamespaceSiblings!(
|
||||
[importer(only), target],
|
||||
{ bindingAugmentations: augmentations } as never,
|
||||
{ fileContents: new Map() },
|
||||
);
|
||||
expect([...(augmentations.get(importerScope)?.keys() ?? [])]).toEqual(expected);
|
||||
}
|
||||
});
|
||||
|
||||
it('leaves an ambiguous module target unresolved', () => {
|
||||
const importer = {
|
||||
filePath: 'importer.ex',
|
||||
moduleScope: importerScope,
|
||||
parsedImports: [],
|
||||
localDefs: [],
|
||||
referenceSites: [],
|
||||
scopes: [],
|
||||
captureSideChannel: {
|
||||
kind: 'elixir',
|
||||
importExcepts: [{ target: 'Filtered', excluded: [], startLine: 1, startCol: 0 }],
|
||||
},
|
||||
} as unknown as ParsedFile;
|
||||
const module = (filePath: string) =>
|
||||
({
|
||||
filePath,
|
||||
moduleScope: targetScope,
|
||||
parsedImports: [],
|
||||
referenceSites: [],
|
||||
scopes: [],
|
||||
localDefs: [
|
||||
{ ...callable('Filtered', 0), type: 'Class', qualifiedName: 'Filtered' },
|
||||
callable('visible', 0),
|
||||
],
|
||||
}) as unknown as ParsedFile;
|
||||
const augmentations = new Map();
|
||||
elixirScopeResolver.populateNamespaceSiblings!(
|
||||
[importer, module('one.ex'), module('two.ex')],
|
||||
{ bindingAugmentations: augmentations } as never,
|
||||
{ fileContents: new Map() },
|
||||
);
|
||||
expect(augmentations).toEqual(new Map());
|
||||
});
|
||||
});
|
||||
|
|
@ -143,6 +143,25 @@ function kotlinParsedFile(filePath: string): ParsedFile {
|
|||
};
|
||||
}
|
||||
|
||||
function elixirParsedFile(filePath: string): ParsedFile {
|
||||
const name = filePath.includes('widget') ? 'Local.Widget' : 'App.Main';
|
||||
const moduleScope = `module:${filePath}` as ScopeId;
|
||||
const def: SymbolDefinition = {
|
||||
nodeId: `Class:${filePath}:${name}`,
|
||||
filePath,
|
||||
type: 'Class',
|
||||
qualifiedName: name,
|
||||
};
|
||||
return {
|
||||
filePath,
|
||||
moduleScope,
|
||||
scopes: [],
|
||||
parsedImports: [],
|
||||
localDefs: [def],
|
||||
referenceSites: [],
|
||||
};
|
||||
}
|
||||
|
||||
const PHP_FUNCTION_IMPORT = (targetRaw: string): ParsedImport => ({
|
||||
kind: 'named',
|
||||
localName: 'imported',
|
||||
|
|
@ -163,6 +182,19 @@ const CASES: ReadonlyMap<SupportedLanguages, ConformanceCase> = new Map([
|
|||
reachesDecoy: '../../../packages/inner/src/nest',
|
||||
},
|
||||
],
|
||||
[
|
||||
SupportedLanguages.Elixir,
|
||||
{
|
||||
files: ['lib/local/widget.ex', 'lib/app/main.ex'],
|
||||
fromFile: 'lib/app/main.ex',
|
||||
resolutionConfig: undefined,
|
||||
external: 'External.Widget',
|
||||
decoy: 'lib/local/widget.ex',
|
||||
reachesDecoy: 'Local.Widget',
|
||||
declare: () => {},
|
||||
parsedFile: elixirParsedFile,
|
||||
},
|
||||
],
|
||||
[
|
||||
SupportedLanguages.JavaScript,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -265,6 +265,25 @@ function kotlinParsedFile(filePath: string): ParsedFile {
|
|||
};
|
||||
}
|
||||
|
||||
function elixirParsedFile(filePath: string): ParsedFile {
|
||||
const name = filePath.includes('widget') ? 'Local.Widget' : 'App.Main';
|
||||
const moduleScope = `module:${filePath}` as ScopeId;
|
||||
const def: SymbolDefinition = {
|
||||
nodeId: `Class:${filePath}:${name}`,
|
||||
filePath,
|
||||
type: 'Class',
|
||||
qualifiedName: name,
|
||||
};
|
||||
return {
|
||||
filePath,
|
||||
moduleScope,
|
||||
scopes: [],
|
||||
parsedImports: [],
|
||||
localDefs: [def],
|
||||
referenceSites: [],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* `use function Vendor\Ghost\missing;` — the one PHP import shape that reaches
|
||||
* `filesByDirectory`. A `type` import (the default for `use X;`) returns before
|
||||
|
|
@ -309,6 +328,20 @@ const FIXTURES: ReadonlyMap<SupportedLanguages, ImportTargetFixture> = new Map<
|
|||
minimumParsedFileReads: 1,
|
||||
},
|
||||
],
|
||||
[
|
||||
SupportedLanguages.Elixir,
|
||||
{
|
||||
files: ['lib/local/widget.ex', 'lib/app/main.ex'],
|
||||
fromFile: 'lib/app/main.ex',
|
||||
resolutionConfig: undefined,
|
||||
missTarget: (i) => `External.Ghost${i}`,
|
||||
hitTarget: 'Local.Widget',
|
||||
parsedImport: IGNORES_CONTEXT,
|
||||
parsedFile: elixirParsedFile,
|
||||
minimumScans: 0,
|
||||
minimumParsedFileReads: 1,
|
||||
},
|
||||
],
|
||||
[
|
||||
SupportedLanguages.CSharp,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -329,10 +329,11 @@ function f(x) { exec(escape(x)); }`);
|
|||
describe('registry + model identity', () => {
|
||||
beforeEach(() => clearSourceSinkRegistry());
|
||||
|
||||
it('registerBuiltinTaintModels registers TS, JS, and Python (idempotent); others stay undefined', () => {
|
||||
it('registerBuiltinTaintModels registers every built-in model idempotently', () => {
|
||||
registerBuiltinTaintModels();
|
||||
registerBuiltinTaintModels(); // idempotent — last-write-wins on the same ids
|
||||
expect(registeredTaintLanguages().sort()).toEqual([
|
||||
'elixir',
|
||||
'java',
|
||||
'javascript',
|
||||
'python',
|
||||
|
|
@ -342,6 +343,7 @@ describe('registry + model identity', () => {
|
|||
expect(getSourceSinkConfig('typescript')).toBe(TS_JS_TAINT_MODEL);
|
||||
expect(getSourceSinkConfig('javascript')).toBe(TS_JS_TAINT_MODEL);
|
||||
expect(getSourceSinkConfig('python')).toBe(BUILTIN_TAINT_MODELS.python);
|
||||
expect(getSourceSinkConfig('elixir')).toBe(BUILTIN_TAINT_MODELS.elixir);
|
||||
expect(getSourceSinkConfig('ruby')).toBeUndefined();
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue