@fairfox/polly 0.3.4 → 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.
@@ -1321,11 +1321,7 @@ class HandlerExtractor {
1321
1321
  typeGuardCache;
1322
1322
  constructor(tsConfigPath) {
1323
1323
  this.project = new Project4({
1324
- tsConfigFilePath: tsConfigPath,
1325
- compilerOptions: {
1326
- allowImportingTsExtensions: true,
1327
- moduleResolution: 99
1328
- }
1324
+ tsConfigFilePath: tsConfigPath
1329
1325
  });
1330
1326
  this.typeGuardCache = new WeakMap;
1331
1327
  }
@@ -1382,8 +1378,12 @@ class HandlerExtractor {
1382
1378
  handlers.push(...mapHandlers);
1383
1379
  }
1384
1380
  if (Node4.isIfStatement(node)) {
1385
- const typeGuardHandlers = this.extractTypeGuardHandlers(node, context, filePath);
1386
- 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
+ }
1387
1387
  }
1388
1388
  });
1389
1389
  return handlers;
@@ -1685,9 +1685,8 @@ class HandlerExtractor {
1685
1685
  const typeGuards = new Map;
1686
1686
  sourceFile.forEachDescendant((node) => {
1687
1687
  if (Node4.isFunctionDeclaration(node) || Node4.isFunctionExpression(node) || Node4.isArrowFunction(node)) {
1688
- const returnType = node.getReturnType();
1689
- const returnTypeText = returnType.getText();
1690
- if (/is\s+\w+/.test(returnTypeText)) {
1688
+ const returnTypeNode = node.getReturnTypeNode();
1689
+ if (returnTypeNode && Node4.isTypePredicate(returnTypeNode)) {
1691
1690
  let functionName;
1692
1691
  if (Node4.isFunctionDeclaration(node)) {
1693
1692
  functionName = node.getName();
@@ -1703,10 +1702,10 @@ class HandlerExtractor {
1703
1702
  }
1704
1703
  }
1705
1704
  if (functionName) {
1705
+ const typeNode = returnTypeNode.getTypeNode();
1706
1706
  let messageType = null;
1707
- const typeMatch = returnTypeText.match(/is\s+(\w+)/);
1708
- if (typeMatch) {
1709
- const typeName = typeMatch[1];
1707
+ if (typeNode) {
1708
+ const typeName = typeNode.getText();
1710
1709
  messageType = this.extractMessageTypeFromTypeName(typeName);
1711
1710
  }
1712
1711
  if (!messageType) {
@@ -1740,34 +1739,38 @@ class HandlerExtractor {
1740
1739
  }
1741
1740
  for (const def of definitions) {
1742
1741
  if (Node4.isFunctionDeclaration(def) || Node4.isFunctionExpression(def) || Node4.isArrowFunction(def)) {
1743
- const returnType = def.getReturnType();
1744
- const returnTypeText = returnType.getText();
1742
+ const returnTypeNode = def.getReturnTypeNode();
1745
1743
  if (process.env.POLLY_DEBUG) {
1746
- console.log(`[DEBUG] Function ${funcName} return type: ${returnTypeText}`);
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)}`);
1747
1748
  }
1748
- if (/is\s+\w+/.test(returnTypeText)) {
1749
- const typeMatch = returnTypeText.match(/is\s+(\w+)/);
1750
- if (typeMatch) {
1751
- const typeName = typeMatch[1];
1749
+ if (returnTypeNode && Node4.isTypePredicate(returnTypeNode)) {
1750
+ const typeNode = returnTypeNode.getTypeNode();
1751
+ if (typeNode) {
1752
+ const typeName = typeNode.getText();
1752
1753
  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];
1754
+ if (messageType) {
1764
1755
  if (process.env.POLLY_DEBUG) {
1765
- console.log(`[DEBUG] Resolved ${funcName} → ${messageType} (from body)`);
1756
+ console.log(`[DEBUG] Resolved ${funcName} → ${messageType} (from AST type predicate)`);
1766
1757
  }
1767
1758
  return messageType;
1768
1759
  }
1769
1760
  }
1770
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)`);
1770
+ }
1771
+ return messageType;
1772
+ }
1773
+ }
1771
1774
  }
1772
1775
  }
1773
1776
  } catch (error) {
@@ -3003,4 +3006,4 @@ Stack trace:`, COLORS.gray));
3003
3006
  process.exit(1);
3004
3007
  });
3005
3008
 
3006
- //# debugId=FFB153526798575464756E2164756E21
3009
+ //# debugId=A5E7982E558C442F64756E2164756E21