Przeglądaj źródła

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>
Olaf Monien 4 miesięcy temu
rodzic
commit
763e280829
2 zmienionych plików z 14 dodań i 4 usunięć
  1. 10 0
      __tests__/extraction.test.ts
  2. 4 4
      src/extraction/tree-sitter.ts

+ 10 - 0
__tests__/extraction.test.ts

@@ -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
src/extraction/tree-sitter.ts

@@ -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);