@fairfox/polly 0.3.2 → 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.
@@ -730,7 +730,11 @@ class HandlerExtractor {
730
730
  typeGuardCache;
731
731
  constructor(tsConfigPath) {
732
732
  this.project = new Project({
733
- tsConfigFilePath: tsConfigPath
733
+ tsConfigFilePath: tsConfigPath,
734
+ compilerOptions: {
735
+ allowImportingTsExtensions: true,
736
+ moduleResolution: 99
737
+ }
734
738
  });
735
739
  this.typeGuardCache = new WeakMap;
736
740
  }
@@ -738,6 +742,14 @@ class HandlerExtractor {
738
742
  const handlers = [];
739
743
  const messageTypes = new Set;
740
744
  const sourceFiles = this.project.getSourceFiles();
745
+ if (process.env.POLLY_DEBUG) {
746
+ console.log(`[DEBUG] Loaded ${sourceFiles.length} source files`);
747
+ if (sourceFiles.length <= 20) {
748
+ for (const sf of sourceFiles) {
749
+ console.log(`[DEBUG] - ${sf.getFilePath()}`);
750
+ }
751
+ }
752
+ }
741
753
  for (const sourceFile of sourceFiles) {
742
754
  const fileHandlers = this.extractFromFile(sourceFile);
743
755
  handlers.push(...fileHandlers);
@@ -745,6 +757,9 @@ class HandlerExtractor {
745
757
  messageTypes.add(handler.messageType);
746
758
  }
747
759
  }
760
+ if (process.env.POLLY_DEBUG) {
761
+ console.log(`[DEBUG] Total handlers extracted: ${handlers.length}`);
762
+ }
748
763
  return {
749
764
  handlers,
750
765
  messageTypes
@@ -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();
@@ -1099,29 +1139,51 @@ class HandlerExtractor {
1099
1139
  }
1100
1140
  resolveImportedTypeGuard(identifier) {
1101
1141
  try {
1142
+ const funcName = identifier.getText();
1102
1143
  const definitions = identifier.getDefinitionNodes();
1144
+ if (definitions.length === 0) {
1145
+ if (process.env.POLLY_DEBUG) {
1146
+ console.log(`[DEBUG] No definitions found for imported function: ${funcName}`);
1147
+ }
1148
+ return null;
1149
+ }
1103
1150
  for (const def of definitions) {
1104
1151
  if (Node.isFunctionDeclaration(def) || Node.isFunctionExpression(def) || Node.isArrowFunction(def)) {
1105
1152
  const returnType = def.getReturnType();
1106
1153
  const returnTypeText = returnType.getText();
1154
+ if (process.env.POLLY_DEBUG) {
1155
+ console.log(`[DEBUG] Function ${funcName} return type: ${returnTypeText}`);
1156
+ }
1107
1157
  if (/is\s+\w+/.test(returnTypeText)) {
1108
1158
  const typeMatch = returnTypeText.match(/is\s+(\w+)/);
1109
1159
  if (typeMatch) {
1110
1160
  const typeName = typeMatch[1];
1111
- return this.extractMessageTypeFromTypeName(typeName);
1161
+ const messageType = this.extractMessageTypeFromTypeName(typeName);
1162
+ if (process.env.POLLY_DEBUG) {
1163
+ console.log(`[DEBUG] Resolved ${funcName} → ${messageType}`);
1164
+ }
1165
+ return messageType;
1112
1166
  }
1113
1167
  const body = def.getBody();
1114
1168
  if (body) {
1115
1169
  const bodyText = body.getText();
1116
1170
  const typeValueMatch = bodyText.match(/\.type\s*===?\s*['"](\w+)['"]/);
1117
1171
  if (typeValueMatch) {
1118
- return typeValueMatch[1];
1172
+ const messageType = typeValueMatch[1];
1173
+ if (process.env.POLLY_DEBUG) {
1174
+ console.log(`[DEBUG] Resolved ${funcName} → ${messageType} (from body)`);
1175
+ }
1176
+ return messageType;
1119
1177
  }
1120
1178
  }
1121
1179
  }
1122
1180
  }
1123
1181
  }
1124
- } catch (error) {}
1182
+ } catch (error) {
1183
+ if (process.env.POLLY_DEBUG) {
1184
+ console.log(`[DEBUG] Error resolving imported type guard: ${error}`);
1185
+ }
1186
+ }
1125
1187
  return null;
1126
1188
  }
1127
1189
  extractMessageTypeFromTypeName(typeName) {
@@ -2320,4 +2382,4 @@ Stack trace:`, COLORS.gray));
2320
2382
  process.exit(1);
2321
2383
  });
2322
2384
 
2323
- //# debugId=45501E6F6BA7319064756E2164756E21
2385
+ //# debugId=51714DF41A65879964756E2164756E21