@fairfox/polly 0.3.1 → 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,10 +1647,27 @@ class HandlerExtractor {
1619
1647
  if (Node4.isIdentifier(funcExpr)) {
1620
1648
  funcName = funcExpr.getText();
1621
1649
  }
1622
- if (!funcName || !typeGuards.has(funcName)) {
1650
+ if (process.env.POLLY_DEBUG && funcName) {
1651
+ console.log(`[DEBUG] Processing if condition with function: ${funcName}`);
1652
+ }
1653
+ let messageType = undefined;
1654
+ if (funcName && typeGuards.has(funcName)) {
1655
+ messageType = typeGuards.get(funcName);
1656
+ if (process.env.POLLY_DEBUG) {
1657
+ console.log(`[DEBUG] Found in local type guards: ${funcName} → ${messageType}`);
1658
+ }
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
+ }
1663
+ messageType = this.resolveImportedTypeGuard(funcExpr);
1664
+ }
1665
+ if (!messageType) {
1666
+ if (process.env.POLLY_DEBUG && funcName) {
1667
+ console.log(`[DEBUG] Could not resolve message type for: ${funcName}`);
1668
+ }
1623
1669
  return null;
1624
1670
  }
1625
- const messageType = typeGuards.get(funcName);
1626
1671
  const line = ifNode.getStartLineNumber();
1627
1672
  return {
1628
1673
  messageType,
@@ -1683,6 +1728,55 @@ class HandlerExtractor {
1683
1728
  });
1684
1729
  return typeGuards;
1685
1730
  }
1731
+ resolveImportedTypeGuard(identifier) {
1732
+ try {
1733
+ const funcName = identifier.getText();
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
+ }
1741
+ for (const def of definitions) {
1742
+ if (Node4.isFunctionDeclaration(def) || Node4.isFunctionExpression(def) || Node4.isArrowFunction(def)) {
1743
+ const returnType = def.getReturnType();
1744
+ const returnTypeText = returnType.getText();
1745
+ if (process.env.POLLY_DEBUG) {
1746
+ console.log(`[DEBUG] Function ${funcName} return type: ${returnTypeText}`);
1747
+ }
1748
+ if (/is\s+\w+/.test(returnTypeText)) {
1749
+ const typeMatch = returnTypeText.match(/is\s+(\w+)/);
1750
+ if (typeMatch) {
1751
+ const typeName = typeMatch[1];
1752
+ const messageType = this.extractMessageTypeFromTypeName(typeName);
1753
+ if (process.env.POLLY_DEBUG) {
1754
+ console.log(`[DEBUG] Resolved ${funcName} → ${messageType}`);
1755
+ }
1756
+ return messageType;
1757
+ }
1758
+ const body = def.getBody();
1759
+ if (body) {
1760
+ const bodyText = body.getText();
1761
+ const typeValueMatch = bodyText.match(/\.type\s*===?\s*['"](\w+)['"]/);
1762
+ if (typeValueMatch) {
1763
+ const messageType = typeValueMatch[1];
1764
+ if (process.env.POLLY_DEBUG) {
1765
+ console.log(`[DEBUG] Resolved ${funcName} → ${messageType} (from body)`);
1766
+ }
1767
+ return messageType;
1768
+ }
1769
+ }
1770
+ }
1771
+ }
1772
+ }
1773
+ } catch (error) {
1774
+ if (process.env.POLLY_DEBUG) {
1775
+ console.log(`[DEBUG] Error resolving imported type guard: ${error}`);
1776
+ }
1777
+ }
1778
+ return null;
1779
+ }
1686
1780
  extractMessageTypeFromTypeName(typeName) {
1687
1781
  const messageType = typeName.replace(/Message$/, "").replace(/Event$/, "").replace(/Request$/, "").replace(/Command$/, "").replace(/Query$/, "").toLowerCase();
1688
1782
  return messageType;
@@ -2909,4 +3003,4 @@ Stack trace:`, COLORS.gray));
2909
3003
  process.exit(1);
2910
3004
  });
2911
3005
 
2912
- //# debugId=2DAA9097DE2E4CFB64756E2164756E21
3006
+ //# debugId=FFB153526798575464756E2164756E21