@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.
@@ -738,6 +738,14 @@ class HandlerExtractor {
738
738
  const handlers = [];
739
739
  const messageTypes = new Set;
740
740
  const sourceFiles = this.project.getSourceFiles();
741
+ if (process.env.POLLY_DEBUG) {
742
+ console.log(`[DEBUG] Loaded ${sourceFiles.length} source files`);
743
+ if (sourceFiles.length <= 20) {
744
+ for (const sf of sourceFiles) {
745
+ console.log(`[DEBUG] - ${sf.getFilePath()}`);
746
+ }
747
+ }
748
+ }
741
749
  for (const sourceFile of sourceFiles) {
742
750
  const fileHandlers = this.extractFromFile(sourceFile);
743
751
  handlers.push(...fileHandlers);
@@ -745,6 +753,9 @@ class HandlerExtractor {
745
753
  messageTypes.add(handler.messageType);
746
754
  }
747
755
  }
756
+ if (process.env.POLLY_DEBUG) {
757
+ console.log(`[DEBUG] Total handlers extracted: ${handlers.length}`);
758
+ }
748
759
  return {
749
760
  handlers,
750
761
  messageTypes
@@ -776,8 +787,12 @@ class HandlerExtractor {
776
787
  handlers.push(...mapHandlers);
777
788
  }
778
789
  if (Node.isIfStatement(node)) {
779
- const typeGuardHandlers = this.extractTypeGuardHandlers(node, context, filePath);
780
- handlers.push(...typeGuardHandlers);
790
+ const parent = node.getParent();
791
+ const isElseIf = parent && Node.isIfStatement(parent);
792
+ if (!isElseIf) {
793
+ const typeGuardHandlers = this.extractTypeGuardHandlers(node, context, filePath);
794
+ handlers.push(...typeGuardHandlers);
795
+ }
781
796
  }
782
797
  });
783
798
  return handlers;
@@ -998,14 +1013,23 @@ class HandlerExtractor {
998
1013
  typeGuards = this.findTypePredicateFunctions(sourceFile);
999
1014
  this.typeGuardCache.set(sourceFile, typeGuards);
1000
1015
  }
1001
- if (typeGuards.size === 0) {
1002
- return handlers;
1016
+ if (process.env.POLLY_DEBUG) {
1017
+ console.log(`[DEBUG] File: ${sourceFile.getBaseName()}`);
1018
+ console.log(`[DEBUG] Local type guards found: ${typeGuards.size}`);
1019
+ if (typeGuards.size > 0) {
1020
+ for (const [name, type] of typeGuards.entries()) {
1021
+ console.log(`[DEBUG] - ${name} → ${type}`);
1022
+ }
1023
+ }
1003
1024
  }
1004
1025
  let currentIf = ifNode;
1005
1026
  while (currentIf) {
1006
1027
  const handler = this.extractHandlerFromIfClause(currentIf, typeGuards, context, filePath);
1007
1028
  if (handler) {
1008
1029
  handlers.push(handler);
1030
+ if (process.env.POLLY_DEBUG) {
1031
+ console.log(`[DEBUG] Found handler: ${handler.messageType} at line ${handler.location.line}`);
1032
+ }
1009
1033
  }
1010
1034
  const elseStatement = currentIf.getElseStatement();
1011
1035
  if (elseStatement && Node.isIfStatement(elseStatement)) {
@@ -1014,7 +1038,11 @@ class HandlerExtractor {
1014
1038
  break;
1015
1039
  }
1016
1040
  }
1017
- } catch (error) {}
1041
+ } catch (error) {
1042
+ if (process.env.POLLY_DEBUG) {
1043
+ console.log(`[DEBUG] Error in extractTypeGuardHandlers: ${error}`);
1044
+ }
1045
+ }
1018
1046
  return handlers;
1019
1047
  }
1020
1048
  extractHandlerFromIfClause(ifNode, typeGuards, context, filePath) {
@@ -1028,13 +1056,25 @@ class HandlerExtractor {
1028
1056
  if (Node.isIdentifier(funcExpr)) {
1029
1057
  funcName = funcExpr.getText();
1030
1058
  }
1059
+ if (process.env.POLLY_DEBUG && funcName) {
1060
+ console.log(`[DEBUG] Processing if condition with function: ${funcName}`);
1061
+ }
1031
1062
  let messageType = undefined;
1032
1063
  if (funcName && typeGuards.has(funcName)) {
1033
1064
  messageType = typeGuards.get(funcName);
1065
+ if (process.env.POLLY_DEBUG) {
1066
+ console.log(`[DEBUG] Found in local type guards: ${funcName} → ${messageType}`);
1067
+ }
1034
1068
  } else if (Node.isIdentifier(funcExpr)) {
1069
+ if (process.env.POLLY_DEBUG) {
1070
+ console.log(`[DEBUG] Not found locally, trying import resolution for: ${funcName}`);
1071
+ }
1035
1072
  messageType = this.resolveImportedTypeGuard(funcExpr);
1036
1073
  }
1037
1074
  if (!messageType) {
1075
+ if (process.env.POLLY_DEBUG && funcName) {
1076
+ console.log(`[DEBUG] Could not resolve message type for: ${funcName}`);
1077
+ }
1038
1078
  return null;
1039
1079
  }
1040
1080
  const line = ifNode.getStartLineNumber();
@@ -1054,9 +1094,8 @@ class HandlerExtractor {
1054
1094
  const typeGuards = new Map;
1055
1095
  sourceFile.forEachDescendant((node) => {
1056
1096
  if (Node.isFunctionDeclaration(node) || Node.isFunctionExpression(node) || Node.isArrowFunction(node)) {
1057
- const returnType = node.getReturnType();
1058
- const returnTypeText = returnType.getText();
1059
- if (/is\s+\w+/.test(returnTypeText)) {
1097
+ const returnTypeNode = node.getReturnTypeNode();
1098
+ if (returnTypeNode && Node.isTypePredicate(returnTypeNode)) {
1060
1099
  let functionName;
1061
1100
  if (Node.isFunctionDeclaration(node)) {
1062
1101
  functionName = node.getName();
@@ -1072,10 +1111,10 @@ class HandlerExtractor {
1072
1111
  }
1073
1112
  }
1074
1113
  if (functionName) {
1114
+ const typeNode = returnTypeNode.getTypeNode();
1075
1115
  let messageType = null;
1076
- const typeMatch = returnTypeText.match(/is\s+(\w+)/);
1077
- if (typeMatch) {
1078
- const typeName = typeMatch[1];
1116
+ if (typeNode) {
1117
+ const typeName = typeNode.getText();
1079
1118
  messageType = this.extractMessageTypeFromTypeName(typeName);
1080
1119
  }
1081
1120
  if (!messageType) {
@@ -1099,29 +1138,55 @@ class HandlerExtractor {
1099
1138
  }
1100
1139
  resolveImportedTypeGuard(identifier) {
1101
1140
  try {
1141
+ const funcName = identifier.getText();
1102
1142
  const definitions = identifier.getDefinitionNodes();
1143
+ if (definitions.length === 0) {
1144
+ if (process.env.POLLY_DEBUG) {
1145
+ console.log(`[DEBUG] No definitions found for imported function: ${funcName}`);
1146
+ }
1147
+ return null;
1148
+ }
1103
1149
  for (const def of definitions) {
1104
1150
  if (Node.isFunctionDeclaration(def) || Node.isFunctionExpression(def) || Node.isArrowFunction(def)) {
1105
- const returnType = def.getReturnType();
1106
- const returnTypeText = returnType.getText();
1107
- if (/is\s+\w+/.test(returnTypeText)) {
1108
- const typeMatch = returnTypeText.match(/is\s+(\w+)/);
1109
- if (typeMatch) {
1110
- const typeName = typeMatch[1];
1111
- return this.extractMessageTypeFromTypeName(typeName);
1151
+ const returnTypeNode = def.getReturnTypeNode();
1152
+ if (process.env.POLLY_DEBUG) {
1153
+ const returnType = def.getReturnType().getText();
1154
+ console.log(`[DEBUG] Function ${funcName} return type (resolved): ${returnType}`);
1155
+ console.log(`[DEBUG] Has return type node: ${!!returnTypeNode}`);
1156
+ console.log(`[DEBUG] Is type predicate node: ${returnTypeNode && Node.isTypePredicate(returnTypeNode)}`);
1157
+ }
1158
+ if (returnTypeNode && Node.isTypePredicate(returnTypeNode)) {
1159
+ const typeNode = returnTypeNode.getTypeNode();
1160
+ if (typeNode) {
1161
+ const typeName = typeNode.getText();
1162
+ const messageType = this.extractMessageTypeFromTypeName(typeName);
1163
+ if (messageType) {
1164
+ if (process.env.POLLY_DEBUG) {
1165
+ console.log(`[DEBUG] Resolved ${funcName} → ${messageType} (from AST type predicate)`);
1166
+ }
1167
+ return messageType;
1168
+ }
1112
1169
  }
1113
- const body = def.getBody();
1114
- if (body) {
1115
- const bodyText = body.getText();
1116
- const typeValueMatch = bodyText.match(/\.type\s*===?\s*['"](\w+)['"]/);
1117
- if (typeValueMatch) {
1118
- return typeValueMatch[1];
1170
+ }
1171
+ const body = def.getBody();
1172
+ if (body) {
1173
+ const bodyText = body.getText();
1174
+ const typeValueMatch = bodyText.match(/\.type\s*===?\s*['"](\w+)['"]/);
1175
+ if (typeValueMatch) {
1176
+ const messageType = typeValueMatch[1];
1177
+ if (process.env.POLLY_DEBUG) {
1178
+ console.log(`[DEBUG] Resolved ${funcName} → ${messageType} (from body)`);
1119
1179
  }
1180
+ return messageType;
1120
1181
  }
1121
1182
  }
1122
1183
  }
1123
1184
  }
1124
- } catch (error) {}
1185
+ } catch (error) {
1186
+ if (process.env.POLLY_DEBUG) {
1187
+ console.log(`[DEBUG] Error resolving imported type guard: ${error}`);
1188
+ }
1189
+ }
1125
1190
  return null;
1126
1191
  }
1127
1192
  extractMessageTypeFromTypeName(typeName) {
@@ -2320,4 +2385,4 @@ Stack trace:`, COLORS.gray));
2320
2385
  process.exit(1);
2321
2386
  });
2322
2387
 
2323
- //# debugId=45501E6F6BA7319064756E2164756E21
2388
+ //# debugId=EE34F5E282168E7C64756E2164756E21