Skip to content

Commit 763e280

Browse files
omonienclaude
andcommitted
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 <[email protected]>
1 parent 482cbee commit 763e280

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

__tests__/extraction.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,16 @@ describe('Pascal / Delphi Extraction', () => {
18501850
expect(moduleNode).toBeDefined();
18511851
expect(moduleNode?.name).toBe('MyApp');
18521852
});
1853+
1854+
it('should fallback to filename when module name is empty', () => {
1855+
// Some .dpr templates use "program;" without a name
1856+
const code = `program;\nuses SysUtils;\nbegin\nend.`;
1857+
const result = extractFromSource('Console.dpr', code);
1858+
1859+
const moduleNode = result.nodes.find((n) => n.kind === 'module');
1860+
expect(moduleNode).toBeDefined();
1861+
expect(moduleNode?.name).toBe('Console');
1862+
});
18531863
});
18541864

18551865
describe('Uses clause (imports)', () => {

src/extraction/tree-sitter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,10 +2056,10 @@ export class TreeSitterExtractor {
20562056
const moduleNameNode = node.namedChildren.find(
20572057
(c: SyntaxNode) => c.type === 'moduleName'
20582058
);
2059-
if (moduleNameNode) {
2060-
const name = getNodeText(moduleNameNode, this.source);
2061-
this.createNode('module', name, node);
2062-
}
2059+
const name = moduleNameNode ? getNodeText(moduleNameNode, this.source) : '';
2060+
// Fallback to filename without extension if module name is empty
2061+
const moduleName = name || path.basename(this.filePath).replace(/\.[^.]+$/, '');
2062+
this.createNode('module', moduleName, node);
20632063
// Continue visiting children (interface/implementation sections)
20642064
for (let i = 0; i < node.namedChildCount; i++) {
20652065
const child = node.namedChild(i);

0 commit comments

Comments
 (0)