feat(extraction): index Erlang escripts and OTP app resource files (#635, #648) (#1169)

escripts (.escript) index like any module — the ELP grammar has a
first-class shebang node, so no source transform is needed; main/1 and its
helpers get full function/call extraction.

OTP application resource files (<app>.app.src and compiled <app>.app) join
the graph as Erlang terms the grammar parses natively. They route by full
suffix (their last-dot extension, .src, is far too generic for the
extension map). The application tuple yields structure: {mod, {Mod, _}}
links the app to its callback module — the app's entry point — and
{applications, [...]} / {included_applications, [...]} connect umbrella
sibling apps, resolving through the OTP app-name == module-name convention;
kernel/stdlib and other out-of-repo apps stay unresolved.

App-file refs resolve only ever to MODULES: validation on emqx caught the
ssl OTP-app dependency resolving to a test helper FUNCTION named ssl (the
same defect class as the earlier -behaviour gate), so the matchReference
module-only gate now covers every ref an .app/.app.src file emits.

Validated on emqx: 2 app.src + 6 escripts indexed, entry-module and
umbrella-dependency edges all namespace-targeted post-gate, escript
functions extracted; a stray legacy/module.src stays unknown.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Colby Mchenry
2026-07-03 15:32:59 -05:00
committed by GitHub
co-authored by Claude Fable 5
parent a5b8cd8e25
commit a0208feaac
7 changed files with 173 additions and 4 deletions
+58
View File
@@ -213,6 +213,52 @@ function handleBehaviour(node: SyntaxNode, ctx: ExtractorContext): boolean {
return true;
}
/**
* OTP application resource file (`<app>.app.src` / `<app>.app`): a single
* `{application, Name, Props}.` term the grammar parses as a top-level
* expression. Two properties carry graph structure — `{mod, {Mod, _Args}}`
* names the application-callback module (the app's entry point), and
* `{applications, [...]}` / `{included_applications, [...]}` declare the apps
* this one depends on. In an umbrella repo those resolve to the sibling app's
* module of the same name (the OTP convention); kernel/stdlib and other
* out-of-repo apps stay unresolved.
*/
function handleAppResourceTuple(node: SyntaxNode, ctx: ExtractorContext): boolean {
const parentId = ctx.nodeStack[ctx.nodeStack.length - 1];
const props = node.namedChildren[2];
if (!parentId || props?.type !== 'list') return true;
const ref = (nameNode: SyntaxNode, kind: 'references' | 'imports'): void => {
const name = atomText(nameNode, ctx.source);
if (!name) return;
ctx.addUnresolvedReference({
fromNodeId: parentId,
referenceName: name,
referenceKind: kind,
line: nameNode.startPosition.row + 1,
column: nameNode.startPosition.column,
});
};
for (const prop of props.namedChildren) {
if (prop.type !== 'tuple' || prop.namedChildren.length < 2) continue;
const key = prop.namedChildren[0];
const value = prop.namedChildren[1];
if (!key || key.type !== 'atom' || !value) continue;
const keyName = atomText(key, ctx.source);
if (keyName === 'mod' && value.type === 'tuple') {
const mod = value.namedChildren[0];
if (mod?.type === 'atom') ref(mod, 'references');
} else if (
(keyName === 'applications' || keyName === 'included_applications') &&
value.type === 'list'
) {
for (const app of value.namedChildren) {
if (app.type === 'atom') ref(app, 'imports');
}
}
}
return true; // nothing else in an app term carries graph structure
}
export const erlangExtractor: LanguageExtractor = {
functionTypes: ['fun_decl'], // dispatched via visitNode (name lives on the clause)
classTypes: [],
@@ -282,6 +328,18 @@ export const erlangExtractor: LanguageExtractor = {
case 'spec':
case 'callback':
return true;
// `{application, Name, Props}.` at the top of an .app/.app.src resource
// file (never a valid form in a module, so the gate is file + position).
case 'tuple':
if (
node.parent?.type === 'source_file' &&
/\.app(?:\.src)?$/i.test(ctx.filePath) &&
node.namedChildren[0]?.type === 'atom' &&
atomText(node.namedChildren[0]!, ctx.source) === 'application'
) {
return handleAppResourceTuple(node, ctx);
}
return false;
default:
return false;
}