fix: Fallback to filename when Pascal module name is empty

Some .dpr templates use "program;" without a name, which produces an
empty moduleName in the AST. Fall back to the filename (without extension)
to prevent nodes with empty names that cause downstream FK constraint errors.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Olaf Monien
2026-02-11 12:50:58 +01:00
co-authored by Claude Opus 4.6
parent 482cbee0d0
commit 763e280829
2 changed files with 14 additions and 4 deletions
+10
View File
@@ -1850,6 +1850,16 @@ describe('Pascal / Delphi Extraction', () => {
expect(moduleNode).toBeDefined();
expect(moduleNode?.name).toBe('MyApp');
});
it('should fallback to filename when module name is empty', () => {
// Some .dpr templates use "program;" without a name
const code = `program;\nuses SysUtils;\nbegin\nend.`;
const result = extractFromSource('Console.dpr', code);
const moduleNode = result.nodes.find((n) => n.kind === 'module');
expect(moduleNode).toBeDefined();
expect(moduleNode?.name).toBe('Console');
});
});
describe('Uses clause (imports)', () => {
+4 -4
View File
@@ -2056,10 +2056,10 @@ export class TreeSitterExtractor {
const moduleNameNode = node.namedChildren.find(
(c: SyntaxNode) => c.type === 'moduleName'
);
if (moduleNameNode) {
const name = getNodeText(moduleNameNode, this.source);
this.createNode('module', name, node);
}
const name = moduleNameNode ? getNodeText(moduleNameNode, this.source) : '';
// Fallback to filename without extension if module name is empty
const moduleName = name || path.basename(this.filePath).replace(/\.[^.]+$/, '');
this.createNode('module', moduleName, node);
// Continue visiting children (interface/implementation sections)
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);