@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.
@@ -730,11 +730,7 @@ class HandlerExtractor {
730
730
  typeGuardCache;
731
731
  constructor(tsConfigPath) {
732
732
  this.project = new Project({
733
- tsConfigFilePath: tsConfigPath,
734
- compilerOptions: {
735
- allowImportingTsExtensions: true,
736
- moduleResolution: 99
737
- }
733
+ tsConfigFilePath: tsConfigPath
738
734
  });
739
735
  this.typeGuardCache = new WeakMap;
740
736
  }
@@ -791,8 +787,12 @@ class HandlerExtractor {
791
787
  handlers.push(...mapHandlers);
792
788
  }
793
789
  if (Node.isIfStatement(node)) {
794
- const typeGuardHandlers = this.extractTypeGuardHandlers(node, context, filePath);
795
- 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
+ }
796
796
  }
797
797
  });
798
798
  return handlers;
@@ -1094,9 +1094,8 @@ class HandlerExtractor {
1094
1094
  const typeGuards = new Map;
1095
1095
  sourceFile.forEachDescendant((node) => {
1096
1096
  if (Node.isFunctionDeclaration(node) || Node.isFunctionExpression(node) || Node.isArrowFunction(node)) {
1097
- const returnType = node.getReturnType();
1098
- const returnTypeText = returnType.getText();
1099
- if (/is\s+\w+/.test(returnTypeText)) {
1097
+ const returnTypeNode = node.getReturnTypeNode();
1098
+ if (returnTypeNode && Node.isTypePredicate(returnTypeNode)) {
1100
1099
  let functionName;
1101
1100
  if (Node.isFunctionDeclaration(node)) {
1102
1101
  functionName = node.getName();
@@ -1112,10 +1111,10 @@ class HandlerExtractor {
1112
1111
  }
1113
1112
  }
1114
1113
  if (functionName) {
1114
+ const typeNode = returnTypeNode.getTypeNode();
1115
1115
  let messageType = null;
1116
- const typeMatch = returnTypeText.match(/is\s+(\w+)/);
1117
- if (typeMatch) {
1118
- const typeName = typeMatch[1];
1116
+ if (typeNode) {
1117
+ const typeName = typeNode.getText();
1119
1118
  messageType = this.extractMessageTypeFromTypeName(typeName);
1120
1119
  }
1121
1120
  if (!messageType) {
@@ -1149,34 +1148,38 @@ class HandlerExtractor {
1149
1148
  }
1150
1149
  for (const def of definitions) {
1151
1150
  if (Node.isFunctionDeclaration(def) || Node.isFunctionExpression(def) || Node.isArrowFunction(def)) {
1152
- const returnType = def.getReturnType();
1153
- const returnTypeText = returnType.getText();
1151
+ const returnTypeNode = def.getReturnTypeNode();
1154
1152
  if (process.env.POLLY_DEBUG) {
1155
- console.log(`[DEBUG] Function ${funcName} return type: ${returnTypeText}`);
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)}`);
1156
1157
  }
1157
- if (/is\s+\w+/.test(returnTypeText)) {
1158
- const typeMatch = returnTypeText.match(/is\s+(\w+)/);
1159
- if (typeMatch) {
1160
- const typeName = typeMatch[1];
1158
+ if (returnTypeNode && Node.isTypePredicate(returnTypeNode)) {
1159
+ const typeNode = returnTypeNode.getTypeNode();
1160
+ if (typeNode) {
1161
+ const typeName = typeNode.getText();
1161
1162
  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];
1163
+ if (messageType) {
1173
1164
  if (process.env.POLLY_DEBUG) {
1174
- console.log(`[DEBUG] Resolved ${funcName} → ${messageType} (from body)`);
1165
+ console.log(`[DEBUG] Resolved ${funcName} → ${messageType} (from AST type predicate)`);
1175
1166
  }
1176
1167
  return messageType;
1177
1168
  }
1178
1169
  }
1179
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)`);
1179
+ }
1180
+ return messageType;
1181
+ }
1182
+ }
1180
1183
  }
1181
1184
  }
1182
1185
  } catch (error) {
@@ -2382,4 +2385,4 @@ Stack trace:`, COLORS.gray));
2382
2385
  process.exit(1);
2383
2386
  });
2384
2387
 
2385
- //# debugId=51714DF41A65879964756E2164756E21
2388
+ //# debugId=EE34F5E282168E7C64756E2164756E21