@fairfox/polly 0.3.0 → 0.3.2

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.
@@ -1318,10 +1318,12 @@ import { Project as Project4, SyntaxKind as SyntaxKind2, Node as Node4 } from "t
1318
1318
 
1319
1319
  class HandlerExtractor {
1320
1320
  project;
1321
+ typeGuardCache;
1321
1322
  constructor(tsConfigPath) {
1322
1323
  this.project = new Project4({
1323
1324
  tsConfigFilePath: tsConfigPath
1324
1325
  });
1326
+ this.typeGuardCache = new WeakMap;
1325
1327
  }
1326
1328
  extractHandlers() {
1327
1329
  const handlers = [];
@@ -1364,6 +1366,10 @@ class HandlerExtractor {
1364
1366
  const mapHandlers = this.extractHandlerMapPattern(node, context, filePath);
1365
1367
  handlers.push(...mapHandlers);
1366
1368
  }
1369
+ if (Node4.isIfStatement(node)) {
1370
+ const typeGuardHandlers = this.extractTypeGuardHandlers(node, context, filePath);
1371
+ handlers.push(...typeGuardHandlers);
1372
+ }
1367
1373
  });
1368
1374
  return handlers;
1369
1375
  }
@@ -1574,6 +1580,145 @@ class HandlerExtractor {
1574
1580
  } catch (error) {}
1575
1581
  return handlers;
1576
1582
  }
1583
+ extractTypeGuardHandlers(ifNode, context, filePath) {
1584
+ const handlers = [];
1585
+ try {
1586
+ const sourceFile = ifNode.getSourceFile();
1587
+ let typeGuards = this.typeGuardCache.get(sourceFile);
1588
+ if (!typeGuards) {
1589
+ typeGuards = this.findTypePredicateFunctions(sourceFile);
1590
+ this.typeGuardCache.set(sourceFile, typeGuards);
1591
+ }
1592
+ if (typeGuards.size === 0) {
1593
+ return handlers;
1594
+ }
1595
+ let currentIf = ifNode;
1596
+ while (currentIf) {
1597
+ const handler = this.extractHandlerFromIfClause(currentIf, typeGuards, context, filePath);
1598
+ if (handler) {
1599
+ handlers.push(handler);
1600
+ }
1601
+ const elseStatement = currentIf.getElseStatement();
1602
+ if (elseStatement && Node4.isIfStatement(elseStatement)) {
1603
+ currentIf = elseStatement;
1604
+ } else {
1605
+ break;
1606
+ }
1607
+ }
1608
+ } catch (error) {}
1609
+ return handlers;
1610
+ }
1611
+ extractHandlerFromIfClause(ifNode, typeGuards, context, filePath) {
1612
+ try {
1613
+ const condition = ifNode.getExpression();
1614
+ if (!Node4.isCallExpression(condition)) {
1615
+ return null;
1616
+ }
1617
+ const funcExpr = condition.getExpression();
1618
+ let funcName;
1619
+ if (Node4.isIdentifier(funcExpr)) {
1620
+ funcName = funcExpr.getText();
1621
+ }
1622
+ let messageType = undefined;
1623
+ if (funcName && typeGuards.has(funcName)) {
1624
+ messageType = typeGuards.get(funcName);
1625
+ } else if (Node4.isIdentifier(funcExpr)) {
1626
+ messageType = this.resolveImportedTypeGuard(funcExpr);
1627
+ }
1628
+ if (!messageType) {
1629
+ return null;
1630
+ }
1631
+ const line = ifNode.getStartLineNumber();
1632
+ return {
1633
+ messageType,
1634
+ node: context,
1635
+ assignments: [],
1636
+ preconditions: [],
1637
+ postconditions: [],
1638
+ location: { file: filePath, line }
1639
+ };
1640
+ } catch (error) {
1641
+ return null;
1642
+ }
1643
+ }
1644
+ findTypePredicateFunctions(sourceFile) {
1645
+ const typeGuards = new Map;
1646
+ sourceFile.forEachDescendant((node) => {
1647
+ 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)) {
1651
+ let functionName;
1652
+ if (Node4.isFunctionDeclaration(node)) {
1653
+ functionName = node.getName();
1654
+ } else if (Node4.isFunctionExpression(node)) {
1655
+ const parent = node.getParent();
1656
+ if (Node4.isVariableDeclaration(parent)) {
1657
+ functionName = parent.getName();
1658
+ }
1659
+ } else if (Node4.isArrowFunction(node)) {
1660
+ const parent = node.getParent();
1661
+ if (Node4.isVariableDeclaration(parent)) {
1662
+ functionName = parent.getName();
1663
+ }
1664
+ }
1665
+ if (functionName) {
1666
+ let messageType = null;
1667
+ const typeMatch = returnTypeText.match(/is\s+(\w+)/);
1668
+ if (typeMatch) {
1669
+ const typeName = typeMatch[1];
1670
+ messageType = this.extractMessageTypeFromTypeName(typeName);
1671
+ }
1672
+ if (!messageType) {
1673
+ const body = node.getBody();
1674
+ if (body) {
1675
+ const bodyText = body.getText();
1676
+ const typeValueMatch = bodyText.match(/\.type\s*===?\s*['"](\w+)['"]/);
1677
+ if (typeValueMatch) {
1678
+ messageType = typeValueMatch[1];
1679
+ }
1680
+ }
1681
+ }
1682
+ if (messageType) {
1683
+ typeGuards.set(functionName, messageType);
1684
+ }
1685
+ }
1686
+ }
1687
+ }
1688
+ });
1689
+ return typeGuards;
1690
+ }
1691
+ resolveImportedTypeGuard(identifier) {
1692
+ try {
1693
+ const definitions = identifier.getDefinitionNodes();
1694
+ for (const def of definitions) {
1695
+ 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);
1703
+ }
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];
1710
+ }
1711
+ }
1712
+ }
1713
+ }
1714
+ }
1715
+ } catch (error) {}
1716
+ return null;
1717
+ }
1718
+ extractMessageTypeFromTypeName(typeName) {
1719
+ const messageType = typeName.replace(/Message$/, "").replace(/Event$/, "").replace(/Request$/, "").replace(/Command$/, "").replace(/Query$/, "").toLowerCase();
1720
+ return messageType;
1721
+ }
1577
1722
  inferContext(filePath) {
1578
1723
  const path2 = filePath.toLowerCase();
1579
1724
  if (path2.includes("/background/") || path2.includes("\\background\\")) {
@@ -2796,4 +2941,4 @@ Stack trace:`, COLORS.gray));
2796
2941
  process.exit(1);
2797
2942
  });
2798
2943
 
2799
- //# debugId=37FBF573C9C57A4C64756E2164756E21
2944
+ //# debugId=0C75E6084607493864756E2164756E21