@fairfox/polly 0.10.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1562,40 +1562,116 @@ class HandlerExtractor {
1562
1562
  project;
1563
1563
  typeGuardCache;
1564
1564
  relationshipExtractor;
1565
+ analyzedFiles;
1566
+ packageRoot;
1565
1567
  constructor(tsConfigPath) {
1566
1568
  this.project = new Project3({
1567
1569
  tsConfigFilePath: tsConfigPath
1568
1570
  });
1569
1571
  this.typeGuardCache = new WeakMap;
1570
1572
  this.relationshipExtractor = new RelationshipExtractor;
1573
+ this.analyzedFiles = new Set;
1574
+ this.packageRoot = this.findPackageRoot(tsConfigPath);
1575
+ }
1576
+ findPackageRoot(tsConfigPath) {
1577
+ let dir = tsConfigPath.substring(0, tsConfigPath.lastIndexOf("/"));
1578
+ while (dir.length > 1) {
1579
+ try {
1580
+ const packageJsonPath = `${dir}/package.json`;
1581
+ const file = Bun.file(packageJsonPath);
1582
+ if (file.size > 0) {
1583
+ return dir;
1584
+ }
1585
+ } catch {}
1586
+ const parentDir = dir.substring(0, dir.lastIndexOf("/"));
1587
+ if (parentDir === dir)
1588
+ break;
1589
+ dir = parentDir;
1590
+ }
1591
+ return tsConfigPath.substring(0, tsConfigPath.lastIndexOf("/"));
1571
1592
  }
1572
1593
  extractHandlers() {
1573
1594
  const handlers = [];
1574
1595
  const messageTypes = new Set;
1575
1596
  const invalidMessageTypes = new Set;
1576
1597
  const stateConstraints = [];
1577
- const sourceFiles = this.project.getSourceFiles();
1578
- this.debugLogSourceFiles(sourceFiles);
1579
- for (const sourceFile of sourceFiles) {
1580
- const fileHandlers = this.extractFromFile(sourceFile);
1581
- handlers.push(...fileHandlers);
1582
- this.categorizeHandlerMessageTypes(fileHandlers, messageTypes, invalidMessageTypes);
1583
- const fileConstraints = this.extractStateConstraintsFromFile(sourceFile);
1584
- stateConstraints.push(...fileConstraints);
1598
+ const allSourceFiles = this.project.getSourceFiles();
1599
+ const entryPoints = allSourceFiles.filter((f) => this.isWithinPackage(f.getFilePath()));
1600
+ this.debugLogSourceFiles(allSourceFiles, entryPoints);
1601
+ for (const entryPoint of entryPoints) {
1602
+ this.analyzeFileAndImports(entryPoint, handlers, messageTypes, invalidMessageTypes, stateConstraints);
1585
1603
  }
1586
1604
  this.debugLogExtractionResults(handlers.length, invalidMessageTypes.size);
1605
+ this.debugLogAnalysisStats(allSourceFiles.length, entryPoints.length);
1587
1606
  return {
1588
1607
  handlers,
1589
1608
  messageTypes,
1590
1609
  stateConstraints
1591
1610
  };
1592
1611
  }
1593
- debugLogSourceFiles(sourceFiles) {
1612
+ analyzeFileAndImports(sourceFile, handlers, messageTypes, invalidMessageTypes, stateConstraints) {
1613
+ const filePath = sourceFile.getFilePath();
1614
+ if (this.analyzedFiles.has(filePath)) {
1615
+ return;
1616
+ }
1617
+ this.analyzedFiles.add(filePath);
1618
+ if (process.env["POLLY_DEBUG"]) {
1619
+ console.log(`[DEBUG] Analyzing: ${filePath}`);
1620
+ }
1621
+ const fileHandlers = this.extractFromFile(sourceFile);
1622
+ handlers.push(...fileHandlers);
1623
+ this.categorizeHandlerMessageTypes(fileHandlers, messageTypes, invalidMessageTypes);
1624
+ const fileConstraints = this.extractStateConstraintsFromFile(sourceFile);
1625
+ stateConstraints.push(...fileConstraints);
1626
+ const importDeclarations = sourceFile.getImportDeclarations();
1627
+ for (const importDecl of importDeclarations) {
1628
+ const importedFile = importDecl.getModuleSpecifierSourceFile();
1629
+ if (importedFile) {
1630
+ const importedPath = importedFile.getFilePath();
1631
+ if (!this.isWithinPackage(importedPath)) {
1632
+ if (process.env["POLLY_DEBUG"]) {
1633
+ console.log(`[DEBUG] Skipping external import: ${importedPath}`);
1634
+ }
1635
+ continue;
1636
+ }
1637
+ this.analyzeFileAndImports(importedFile, handlers, messageTypes, invalidMessageTypes, stateConstraints);
1638
+ } else if (process.env["POLLY_DEBUG"]) {
1639
+ const specifier = importDecl.getModuleSpecifierValue();
1640
+ if (!specifier.startsWith("node:") && !this.isNodeModuleImport(specifier)) {
1641
+ console.log(`[DEBUG] Could not resolve import: ${specifier} in ${filePath}`);
1642
+ }
1643
+ }
1644
+ }
1645
+ }
1646
+ isWithinPackage(filePath) {
1647
+ if (!filePath.startsWith(this.packageRoot)) {
1648
+ return false;
1649
+ }
1650
+ if (filePath.includes("/node_modules/")) {
1651
+ return false;
1652
+ }
1653
+ return true;
1654
+ }
1655
+ isNodeModuleImport(specifier) {
1656
+ return !specifier.startsWith(".") && !specifier.startsWith("/");
1657
+ }
1658
+ debugLogAnalysisStats(totalSourceFiles, entryPointCount) {
1659
+ if (!process.env["POLLY_DEBUG"])
1660
+ return;
1661
+ console.log(`[DEBUG] Analysis Statistics:`);
1662
+ console.log(`[DEBUG] Package root: ${this.packageRoot}`);
1663
+ console.log(`[DEBUG] Source files from tsconfig: ${totalSourceFiles}`);
1664
+ console.log(`[DEBUG] Entry points (in package): ${entryPointCount}`);
1665
+ console.log(`[DEBUG] Files analyzed (including imports): ${this.analyzedFiles.size}`);
1666
+ console.log(`[DEBUG] Additional files discovered: ${this.analyzedFiles.size - entryPointCount}`);
1667
+ }
1668
+ debugLogSourceFiles(allSourceFiles, entryPoints) {
1594
1669
  if (!process.env["POLLY_DEBUG"])
1595
1670
  return;
1596
- console.log(`[DEBUG] Loaded ${sourceFiles.length} source files`);
1597
- if (sourceFiles.length <= 20) {
1598
- for (const sf of sourceFiles) {
1671
+ console.log(`[DEBUG] Loaded ${allSourceFiles.length} source files from tsconfig`);
1672
+ console.log(`[DEBUG] Filtered to ${entryPoints.length} entry points within package`);
1673
+ if (entryPoints.length <= 20) {
1674
+ for (const sf of entryPoints) {
1599
1675
  console.log(`[DEBUG] - ${sf.getFilePath()}`);
1600
1676
  }
1601
1677
  }
@@ -4799,4 +4875,4 @@ main().catch((_error) => {
4799
4875
  process.exit(1);
4800
4876
  });
4801
4877
 
4802
- //# debugId=4E2F22D76354977564756E2164756E21
4878
+ //# debugId=95CA9FB262DAF8A164756E2164756E21