@fairfox/polly 0.3.1 → 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,10 +1056,27 @@ class HandlerExtractor {
1028
1056
  if (Node.isIdentifier(funcExpr)) {
1029
1057
  funcName = funcExpr.getText();
1030
1058
  }
1031
- if (!funcName || !typeGuards.has(funcName)) {
1059
+ if (process.env.POLLY_DEBUG && funcName) {
1060
+ console.log(`[DEBUG] Processing if condition with function: ${funcName}`);
1061
+ }
1062
+ let messageType = undefined;
1063
+ if (funcName && typeGuards.has(funcName)) {
1064
+ messageType = typeGuards.get(funcName);
1065
+ if (process.env.POLLY_DEBUG) {
1066
+ console.log(`[DEBUG] Found in local type guards: ${funcName} → ${messageType}`);
1067
+ }
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
+ }
1072
+ messageType = this.resolveImportedTypeGuard(funcExpr);
1073
+ }
1074
+ if (!messageType) {
1075
+ if (process.env.POLLY_DEBUG && funcName) {
1076
+ console.log(`[DEBUG] Could not resolve message type for: ${funcName}`);
1077
+ }
1032
1078
  return null;
1033
1079
  }
1034
- const messageType = typeGuards.get(funcName);
1035
1080
  const line = ifNode.getStartLineNumber();
1036
1081
  return {
1037
1082
  messageType,
@@ -1092,6 +1137,55 @@ class HandlerExtractor {
1092
1137
  });
1093
1138
  return typeGuards;
1094
1139
  }
1140
+ resolveImportedTypeGuard(identifier) {
1141
+ try {
1142
+ const funcName = identifier.getText();
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
+ }
1150
+ for (const def of definitions) {
1151
+ if (Node.isFunctionDeclaration(def) || Node.isFunctionExpression(def) || Node.isArrowFunction(def)) {
1152
+ const returnType = def.getReturnType();
1153
+ const returnTypeText = returnType.getText();
1154
+ if (process.env.POLLY_DEBUG) {
1155
+ console.log(`[DEBUG] Function ${funcName} return type: ${returnTypeText}`);
1156
+ }
1157
+ if (/is\s+\w+/.test(returnTypeText)) {
1158
+ const typeMatch = returnTypeText.match(/is\s+(\w+)/);
1159
+ if (typeMatch) {
1160
+ const typeName = typeMatch[1];
1161
+ const messageType = this.extractMessageTypeFromTypeName(typeName);
1162
+ if (process.env.POLLY_DEBUG) {
1163
+ console.log(`[DEBUG] Resolved ${funcName} → ${messageType}`);
1164
+ }
1165
+ return messageType;
1166
+ }
1167
+ const body = def.getBody();
1168
+ if (body) {
1169
+ const bodyText = body.getText();
1170
+ const typeValueMatch = bodyText.match(/\.type\s*===?\s*['"](\w+)['"]/);
1171
+ if (typeValueMatch) {
1172
+ const messageType = typeValueMatch[1];
1173
+ if (process.env.POLLY_DEBUG) {
1174
+ console.log(`[DEBUG] Resolved ${funcName} → ${messageType} (from body)`);
1175
+ }
1176
+ return messageType;
1177
+ }
1178
+ }
1179
+ }
1180
+ }
1181
+ }
1182
+ } catch (error) {
1183
+ if (process.env.POLLY_DEBUG) {
1184
+ console.log(`[DEBUG] Error resolving imported type guard: ${error}`);
1185
+ }
1186
+ }
1187
+ return null;
1188
+ }
1095
1189
  extractMessageTypeFromTypeName(typeName) {
1096
1190
  const messageType = typeName.replace(/Message$/, "").replace(/Event$/, "").replace(/Request$/, "").replace(/Command$/, "").replace(/Query$/, "").toLowerCase();
1097
1191
  return messageType;
@@ -2288,4 +2382,4 @@ Stack trace:`, COLORS.gray));
2288
2382
  process.exit(1);
2289
2383
  });
2290
2384
 
2291
- //# debugId=5E943FE5FB56B58764756E2164756E21
2385
+ //# debugId=51714DF41A65879964756E2164756E21