@fairfox/polly 0.3.2 → 0.3.6

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.
@@ -1329,6 +1329,14 @@ class HandlerExtractor {
1329
1329
  const handlers = [];
1330
1330
  const messageTypes = new Set;
1331
1331
  const sourceFiles = this.project.getSourceFiles();
1332
+ if (process.env.POLLY_DEBUG) {
1333
+ console.log(`[DEBUG] Loaded ${sourceFiles.length} source files`);
1334
+ if (sourceFiles.length <= 20) {
1335
+ for (const sf of sourceFiles) {
1336
+ console.log(`[DEBUG] - ${sf.getFilePath()}`);
1337
+ }
1338
+ }
1339
+ }
1332
1340
  for (const sourceFile of sourceFiles) {
1333
1341
  const fileHandlers = this.extractFromFile(sourceFile);
1334
1342
  handlers.push(...fileHandlers);
@@ -1336,6 +1344,9 @@ class HandlerExtractor {
1336
1344
  messageTypes.add(handler.messageType);
1337
1345
  }
1338
1346
  }
1347
+ if (process.env.POLLY_DEBUG) {
1348
+ console.log(`[DEBUG] Total handlers extracted: ${handlers.length}`);
1349
+ }
1339
1350
  return {
1340
1351
  handlers,
1341
1352
  messageTypes
@@ -1367,8 +1378,12 @@ class HandlerExtractor {
1367
1378
  handlers.push(...mapHandlers);
1368
1379
  }
1369
1380
  if (Node4.isIfStatement(node)) {
1370
- const typeGuardHandlers = this.extractTypeGuardHandlers(node, context, filePath);
1371
- handlers.push(...typeGuardHandlers);
1381
+ const parent = node.getParent();
1382
+ const isElseIf = parent && Node4.isIfStatement(parent);
1383
+ if (!isElseIf) {
1384
+ const typeGuardHandlers = this.extractTypeGuardHandlers(node, context, filePath);
1385
+ handlers.push(...typeGuardHandlers);
1386
+ }
1372
1387
  }
1373
1388
  });
1374
1389
  return handlers;
@@ -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();
@@ -1645,9 +1685,8 @@ class HandlerExtractor {
1645
1685
  const typeGuards = new Map;
1646
1686
  sourceFile.forEachDescendant((node) => {
1647
1687
  if (Node4.isFunctionDeclaration(node) || Node4.isFunctionExpression(node) || Node4.isArrowFunction(node)) {
1648
- const returnType = node.getReturnType();
1649
- const returnTypeText = returnType.getText();
1650
- if (/is\s+\w+/.test(returnTypeText)) {
1688
+ const returnTypeNode = node.getReturnTypeNode();
1689
+ if (returnTypeNode && Node4.isTypePredicate(returnTypeNode)) {
1651
1690
  let functionName;
1652
1691
  if (Node4.isFunctionDeclaration(node)) {
1653
1692
  functionName = node.getName();
@@ -1663,10 +1702,10 @@ class HandlerExtractor {
1663
1702
  }
1664
1703
  }
1665
1704
  if (functionName) {
1705
+ const typeNode = returnTypeNode.getTypeNode();
1666
1706
  let messageType = null;
1667
- const typeMatch = returnTypeText.match(/is\s+(\w+)/);
1668
- if (typeMatch) {
1669
- const typeName = typeMatch[1];
1707
+ if (typeNode) {
1708
+ const typeName = typeNode.getText();
1670
1709
  messageType = this.extractMessageTypeFromTypeName(typeName);
1671
1710
  }
1672
1711
  if (!messageType) {
@@ -1690,29 +1729,55 @@ class HandlerExtractor {
1690
1729
  }
1691
1730
  resolveImportedTypeGuard(identifier) {
1692
1731
  try {
1732
+ const funcName = identifier.getText();
1693
1733
  const definitions = identifier.getDefinitionNodes();
1734
+ if (definitions.length === 0) {
1735
+ if (process.env.POLLY_DEBUG) {
1736
+ console.log(`[DEBUG] No definitions found for imported function: ${funcName}`);
1737
+ }
1738
+ return null;
1739
+ }
1694
1740
  for (const def of definitions) {
1695
1741
  if (Node4.isFunctionDeclaration(def) || Node4.isFunctionExpression(def) || Node4.isArrowFunction(def)) {
1696
- const returnType = def.getReturnType();
1697
- const returnTypeText = returnType.getText();
1698
- if (/is\s+\w+/.test(returnTypeText)) {
1699
- const typeMatch = returnTypeText.match(/is\s+(\w+)/);
1700
- if (typeMatch) {
1701
- const typeName = typeMatch[1];
1702
- return this.extractMessageTypeFromTypeName(typeName);
1742
+ const returnTypeNode = def.getReturnTypeNode();
1743
+ if (process.env.POLLY_DEBUG) {
1744
+ const returnType = def.getReturnType().getText();
1745
+ console.log(`[DEBUG] Function ${funcName} return type (resolved): ${returnType}`);
1746
+ console.log(`[DEBUG] Has return type node: ${!!returnTypeNode}`);
1747
+ console.log(`[DEBUG] Is type predicate node: ${returnTypeNode && Node4.isTypePredicate(returnTypeNode)}`);
1748
+ }
1749
+ if (returnTypeNode && Node4.isTypePredicate(returnTypeNode)) {
1750
+ const typeNode = returnTypeNode.getTypeNode();
1751
+ if (typeNode) {
1752
+ const typeName = typeNode.getText();
1753
+ const messageType = this.extractMessageTypeFromTypeName(typeName);
1754
+ if (messageType) {
1755
+ if (process.env.POLLY_DEBUG) {
1756
+ console.log(`[DEBUG] Resolved ${funcName} → ${messageType} (from AST type predicate)`);
1757
+ }
1758
+ return messageType;
1759
+ }
1703
1760
  }
1704
- const body = def.getBody();
1705
- if (body) {
1706
- const bodyText = body.getText();
1707
- const typeValueMatch = bodyText.match(/\.type\s*===?\s*['"](\w+)['"]/);
1708
- if (typeValueMatch) {
1709
- return typeValueMatch[1];
1761
+ }
1762
+ const body = def.getBody();
1763
+ if (body) {
1764
+ const bodyText = body.getText();
1765
+ const typeValueMatch = bodyText.match(/\.type\s*===?\s*['"](\w+)['"]/);
1766
+ if (typeValueMatch) {
1767
+ const messageType = typeValueMatch[1];
1768
+ if (process.env.POLLY_DEBUG) {
1769
+ console.log(`[DEBUG] Resolved ${funcName} → ${messageType} (from body)`);
1710
1770
  }
1771
+ return messageType;
1711
1772
  }
1712
1773
  }
1713
1774
  }
1714
1775
  }
1715
- } catch (error) {}
1776
+ } catch (error) {
1777
+ if (process.env.POLLY_DEBUG) {
1778
+ console.log(`[DEBUG] Error resolving imported type guard: ${error}`);
1779
+ }
1780
+ }
1716
1781
  return null;
1717
1782
  }
1718
1783
  extractMessageTypeFromTypeName(typeName) {
@@ -2941,4 +3006,4 @@ Stack trace:`, COLORS.gray));
2941
3006
  process.exit(1);
2942
3007
  });
2943
3008
 
2944
- //# debugId=0C75E6084607493864756E2164756E21
3009
+ //# debugId=A5E7982E558C442F64756E2164756E21