@fairfox/polly 0.3.2 → 0.3.4

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.
@@ -1321,7 +1321,11 @@ class HandlerExtractor {
1321
1321
  typeGuardCache;
1322
1322
  constructor(tsConfigPath) {
1323
1323
  this.project = new Project4({
1324
- tsConfigFilePath: tsConfigPath
1324
+ tsConfigFilePath: tsConfigPath,
1325
+ compilerOptions: {
1326
+ allowImportingTsExtensions: true,
1327
+ moduleResolution: 99
1328
+ }
1325
1329
  });
1326
1330
  this.typeGuardCache = new WeakMap;
1327
1331
  }
@@ -1329,6 +1333,14 @@ class HandlerExtractor {
1329
1333
  const handlers = [];
1330
1334
  const messageTypes = new Set;
1331
1335
  const sourceFiles = this.project.getSourceFiles();
1336
+ if (process.env.POLLY_DEBUG) {
1337
+ console.log(`[DEBUG] Loaded ${sourceFiles.length} source files`);
1338
+ if (sourceFiles.length <= 20) {
1339
+ for (const sf of sourceFiles) {
1340
+ console.log(`[DEBUG] - ${sf.getFilePath()}`);
1341
+ }
1342
+ }
1343
+ }
1332
1344
  for (const sourceFile of sourceFiles) {
1333
1345
  const fileHandlers = this.extractFromFile(sourceFile);
1334
1346
  handlers.push(...fileHandlers);
@@ -1336,6 +1348,9 @@ class HandlerExtractor {
1336
1348
  messageTypes.add(handler.messageType);
1337
1349
  }
1338
1350
  }
1351
+ if (process.env.POLLY_DEBUG) {
1352
+ console.log(`[DEBUG] Total handlers extracted: ${handlers.length}`);
1353
+ }
1339
1354
  return {
1340
1355
  handlers,
1341
1356
  messageTypes
@@ -1589,14 +1604,23 @@ class HandlerExtractor {
1589
1604
  typeGuards = this.findTypePredicateFunctions(sourceFile);
1590
1605
  this.typeGuardCache.set(sourceFile, typeGuards);
1591
1606
  }
1592
- if (typeGuards.size === 0) {
1593
- return handlers;
1607
+ if (process.env.POLLY_DEBUG) {
1608
+ console.log(`[DEBUG] File: ${sourceFile.getBaseName()}`);
1609
+ console.log(`[DEBUG] Local type guards found: ${typeGuards.size}`);
1610
+ if (typeGuards.size > 0) {
1611
+ for (const [name, type] of typeGuards.entries()) {
1612
+ console.log(`[DEBUG] - ${name} → ${type}`);
1613
+ }
1614
+ }
1594
1615
  }
1595
1616
  let currentIf = ifNode;
1596
1617
  while (currentIf) {
1597
1618
  const handler = this.extractHandlerFromIfClause(currentIf, typeGuards, context, filePath);
1598
1619
  if (handler) {
1599
1620
  handlers.push(handler);
1621
+ if (process.env.POLLY_DEBUG) {
1622
+ console.log(`[DEBUG] Found handler: ${handler.messageType} at line ${handler.location.line}`);
1623
+ }
1600
1624
  }
1601
1625
  const elseStatement = currentIf.getElseStatement();
1602
1626
  if (elseStatement && Node4.isIfStatement(elseStatement)) {
@@ -1605,7 +1629,11 @@ class HandlerExtractor {
1605
1629
  break;
1606
1630
  }
1607
1631
  }
1608
- } catch (error) {}
1632
+ } catch (error) {
1633
+ if (process.env.POLLY_DEBUG) {
1634
+ console.log(`[DEBUG] Error in extractTypeGuardHandlers: ${error}`);
1635
+ }
1636
+ }
1609
1637
  return handlers;
1610
1638
  }
1611
1639
  extractHandlerFromIfClause(ifNode, typeGuards, context, filePath) {
@@ -1619,13 +1647,25 @@ class HandlerExtractor {
1619
1647
  if (Node4.isIdentifier(funcExpr)) {
1620
1648
  funcName = funcExpr.getText();
1621
1649
  }
1650
+ if (process.env.POLLY_DEBUG && funcName) {
1651
+ console.log(`[DEBUG] Processing if condition with function: ${funcName}`);
1652
+ }
1622
1653
  let messageType = undefined;
1623
1654
  if (funcName && typeGuards.has(funcName)) {
1624
1655
  messageType = typeGuards.get(funcName);
1656
+ if (process.env.POLLY_DEBUG) {
1657
+ console.log(`[DEBUG] Found in local type guards: ${funcName} → ${messageType}`);
1658
+ }
1625
1659
  } else if (Node4.isIdentifier(funcExpr)) {
1660
+ if (process.env.POLLY_DEBUG) {
1661
+ console.log(`[DEBUG] Not found locally, trying import resolution for: ${funcName}`);
1662
+ }
1626
1663
  messageType = this.resolveImportedTypeGuard(funcExpr);
1627
1664
  }
1628
1665
  if (!messageType) {
1666
+ if (process.env.POLLY_DEBUG && funcName) {
1667
+ console.log(`[DEBUG] Could not resolve message type for: ${funcName}`);
1668
+ }
1629
1669
  return null;
1630
1670
  }
1631
1671
  const line = ifNode.getStartLineNumber();
@@ -1690,29 +1730,51 @@ class HandlerExtractor {
1690
1730
  }
1691
1731
  resolveImportedTypeGuard(identifier) {
1692
1732
  try {
1733
+ const funcName = identifier.getText();
1693
1734
  const definitions = identifier.getDefinitionNodes();
1735
+ if (definitions.length === 0) {
1736
+ if (process.env.POLLY_DEBUG) {
1737
+ console.log(`[DEBUG] No definitions found for imported function: ${funcName}`);
1738
+ }
1739
+ return null;
1740
+ }
1694
1741
  for (const def of definitions) {
1695
1742
  if (Node4.isFunctionDeclaration(def) || Node4.isFunctionExpression(def) || Node4.isArrowFunction(def)) {
1696
1743
  const returnType = def.getReturnType();
1697
1744
  const returnTypeText = returnType.getText();
1745
+ if (process.env.POLLY_DEBUG) {
1746
+ console.log(`[DEBUG] Function ${funcName} return type: ${returnTypeText}`);
1747
+ }
1698
1748
  if (/is\s+\w+/.test(returnTypeText)) {
1699
1749
  const typeMatch = returnTypeText.match(/is\s+(\w+)/);
1700
1750
  if (typeMatch) {
1701
1751
  const typeName = typeMatch[1];
1702
- return this.extractMessageTypeFromTypeName(typeName);
1752
+ const messageType = this.extractMessageTypeFromTypeName(typeName);
1753
+ if (process.env.POLLY_DEBUG) {
1754
+ console.log(`[DEBUG] Resolved ${funcName} → ${messageType}`);
1755
+ }
1756
+ return messageType;
1703
1757
  }
1704
1758
  const body = def.getBody();
1705
1759
  if (body) {
1706
1760
  const bodyText = body.getText();
1707
1761
  const typeValueMatch = bodyText.match(/\.type\s*===?\s*['"](\w+)['"]/);
1708
1762
  if (typeValueMatch) {
1709
- return typeValueMatch[1];
1763
+ const messageType = typeValueMatch[1];
1764
+ if (process.env.POLLY_DEBUG) {
1765
+ console.log(`[DEBUG] Resolved ${funcName} → ${messageType} (from body)`);
1766
+ }
1767
+ return messageType;
1710
1768
  }
1711
1769
  }
1712
1770
  }
1713
1771
  }
1714
1772
  }
1715
- } catch (error) {}
1773
+ } catch (error) {
1774
+ if (process.env.POLLY_DEBUG) {
1775
+ console.log(`[DEBUG] Error resolving imported type guard: ${error}`);
1776
+ }
1777
+ }
1716
1778
  return null;
1717
1779
  }
1718
1780
  extractMessageTypeFromTypeName(typeName) {
@@ -2941,4 +3003,4 @@ Stack trace:`, COLORS.gray));
2941
3003
  process.exit(1);
2942
3004
  });
2943
3005
 
2944
- //# debugId=0C75E6084607493864756E2164756E21
3006
+ //# debugId=FFB153526798575464756E2164756E21