@openrewrite/rewrite 8.62.0 → 8.62.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.
Files changed (97) hide show
  1. package/dist/java/rpc.d.ts +3 -3
  2. package/dist/java/rpc.d.ts.map +1 -1
  3. package/dist/java/rpc.js +6 -6
  4. package/dist/java/rpc.js.map +1 -1
  5. package/dist/java/tree.d.ts +31 -33
  6. package/dist/java/tree.d.ts.map +1 -1
  7. package/dist/java/tree.js +125 -7
  8. package/dist/java/tree.js.map +1 -1
  9. package/dist/java/type.d.ts +70 -57
  10. package/dist/java/type.d.ts.map +1 -1
  11. package/dist/java/type.js +454 -25
  12. package/dist/java/type.js.map +1 -1
  13. package/dist/java/visitor.d.ts +4 -4
  14. package/dist/java/visitor.d.ts.map +1 -1
  15. package/dist/java/visitor.js +34 -28
  16. package/dist/java/visitor.js.map +1 -1
  17. package/dist/javascript/assertions.d.ts +3 -0
  18. package/dist/javascript/assertions.d.ts.map +1 -1
  19. package/dist/javascript/assertions.js +78 -1
  20. package/dist/javascript/assertions.js.map +1 -1
  21. package/dist/javascript/format.js +4 -4
  22. package/dist/javascript/format.js.map +1 -1
  23. package/dist/javascript/parser.d.ts +1 -1
  24. package/dist/javascript/parser.d.ts.map +1 -1
  25. package/dist/javascript/parser.js +38 -11
  26. package/dist/javascript/parser.js.map +1 -1
  27. package/dist/javascript/rpc.js +1 -4
  28. package/dist/javascript/rpc.js.map +1 -1
  29. package/dist/javascript/tree.d.ts +33 -35
  30. package/dist/javascript/tree.d.ts.map +1 -1
  31. package/dist/javascript/tree.js +70 -17
  32. package/dist/javascript/tree.js.map +1 -1
  33. package/dist/javascript/type-mapping.d.ts +44 -8
  34. package/dist/javascript/type-mapping.d.ts.map +1 -1
  35. package/dist/javascript/type-mapping.js +571 -101
  36. package/dist/javascript/type-mapping.js.map +1 -1
  37. package/dist/javascript/visitor.d.ts +2 -2
  38. package/dist/javascript/visitor.d.ts.map +1 -1
  39. package/dist/javascript/visitor.js.map +1 -1
  40. package/dist/json/tree.d.ts +0 -2
  41. package/dist/json/tree.d.ts.map +1 -1
  42. package/dist/json/tree.js +12 -2
  43. package/dist/json/tree.js.map +1 -1
  44. package/dist/reference.js +1 -1
  45. package/dist/reference.js.map +1 -1
  46. package/dist/rpc/chrome-profiler.d.ts +25 -0
  47. package/dist/rpc/chrome-profiler.d.ts.map +1 -0
  48. package/dist/rpc/chrome-profiler.js +405 -0
  49. package/dist/rpc/chrome-profiler.js.map +1 -0
  50. package/dist/rpc/queue.d.ts +0 -3
  51. package/dist/rpc/queue.d.ts.map +1 -1
  52. package/dist/rpc/queue.js +6 -6
  53. package/dist/rpc/queue.js.map +1 -1
  54. package/dist/rpc/request/parse.d.ts.map +1 -1
  55. package/dist/rpc/request/parse.js.map +1 -1
  56. package/dist/rpc/rewrite-rpc.d.ts +1 -0
  57. package/dist/rpc/rewrite-rpc.d.ts.map +1 -1
  58. package/dist/rpc/rewrite-rpc.js +1 -1
  59. package/dist/rpc/rewrite-rpc.js.map +1 -1
  60. package/dist/rpc/server.d.ts.map +1 -1
  61. package/dist/rpc/server.js +26 -1
  62. package/dist/rpc/server.js.map +1 -1
  63. package/dist/test/rewrite-test.d.ts +1 -1
  64. package/dist/test/rewrite-test.d.ts.map +1 -1
  65. package/dist/test/rewrite-test.js +18 -2
  66. package/dist/test/rewrite-test.js.map +1 -1
  67. package/dist/text/tree.d.ts +0 -2
  68. package/dist/text/tree.d.ts.map +1 -1
  69. package/dist/text/tree.js +4 -17
  70. package/dist/text/tree.js.map +1 -1
  71. package/dist/tree.d.ts +1 -0
  72. package/dist/tree.d.ts.map +1 -1
  73. package/dist/tree.js +14 -0
  74. package/dist/tree.js.map +1 -1
  75. package/dist/version.txt +1 -1
  76. package/package.json +3 -3
  77. package/src/java/rpc.ts +18 -15
  78. package/src/java/tree.ts +68 -38
  79. package/src/java/type.ts +475 -74
  80. package/src/java/visitor.ts +45 -39
  81. package/src/javascript/assertions.ts +52 -1
  82. package/src/javascript/format.ts +4 -4
  83. package/src/javascript/parser.ts +51 -18
  84. package/src/javascript/rpc.ts +8 -10
  85. package/src/javascript/tree.ts +34 -35
  86. package/src/javascript/type-mapping.ts +582 -47
  87. package/src/javascript/visitor.ts +5 -5
  88. package/src/json/tree.ts +1 -2
  89. package/src/reference.ts +1 -1
  90. package/src/rpc/chrome-profiler.ts +373 -0
  91. package/src/rpc/queue.ts +8 -12
  92. package/src/rpc/request/parse.ts +1 -1
  93. package/src/rpc/rewrite-rpc.ts +3 -2
  94. package/src/rpc/server.ts +30 -1
  95. package/src/test/rewrite-test.ts +19 -3
  96. package/src/text/tree.ts +0 -1
  97. package/src/tree.ts +15 -0
@@ -1,46 +1,8 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
- return new (P || (P = Promise))(function (resolve, reject) {
38
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
- step((generator = generator.apply(thisArg, _arguments || [])).next());
42
- });
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
43
4
  };
5
+ var _a;
44
6
  Object.defineProperty(exports, "__esModule", { value: true });
45
7
  exports.JavaScriptTypeMapping = void 0;
46
8
  /*
@@ -58,125 +20,633 @@ exports.JavaScriptTypeMapping = void 0;
58
20
  * See the License for the specific language governing permissions and
59
21
  * limitations under the License.
60
22
  */
61
- const ts = __importStar(require("typescript"));
23
+ const typescript_1 = __importDefault(require("typescript"));
62
24
  const java_1 = require("../java");
63
- const rpc_1 = require("../rpc");
25
+ const immer_1 = require("immer");
26
+ // Helper class to create Type objects that immer won't traverse
27
+ class NonDraftableType {
28
+ constructor() {
29
+ this[_a] = false;
30
+ }
31
+ }
32
+ _a = immer_1.immerable;
33
+ const builtInTypes = new Set([
34
+ 'Array', 'Object', 'Function', 'String', 'Number', 'Boolean',
35
+ 'Date', 'RegExp', 'Error', 'Promise', 'Map', 'Set', 'WeakMap',
36
+ 'WeakSet', 'Symbol', 'BigInt', 'HTMLElement', 'Document',
37
+ 'Window', 'Console', 'JSON', 'Math', 'Reflect', 'Proxy'
38
+ ]);
64
39
  class JavaScriptTypeMapping {
65
- constructor(checker) {
40
+ constructor(checker, projectRoot = process.cwd()) {
66
41
  this.checker = checker;
42
+ this.projectRoot = projectRoot;
67
43
  this.typeCache = new Map();
68
- this.regExpSymbol = checker.resolveName("RegExp", undefined, ts.SymbolFlags.Type, false);
44
+ this.regExpSymbol = checker.resolveName("RegExp", undefined, typescript_1.default.SymbolFlags.Type, false);
69
45
  }
70
46
  type(node) {
71
47
  let type;
72
- if (ts.isExpression(node)) {
48
+ if (typescript_1.default.isExpression(node)) {
73
49
  type = this.checker.getTypeAtLocation(node);
74
50
  }
75
- else if (ts.isTypeNode(node)) {
51
+ else if (typescript_1.default.isTypeNode(node)) {
76
52
  type = this.checker.getTypeFromTypeNode(node);
77
53
  }
78
54
  return type && this.getType(type);
79
55
  }
80
56
  getType(type) {
57
+ var _b;
81
58
  const signature = this.getSignature(type);
82
59
  const existing = this.typeCache.get(signature);
83
60
  if (existing) {
84
61
  return existing;
85
62
  }
86
- const result = this.createType(type, signature);
63
+ // Check if this is an array type BEFORE checking for classes
64
+ // TypeScript represents Array<T> as a reference to the Array interface
65
+ if (this.checker.isArrayType(type)) {
66
+ const arrayType = this.createArrayType(type);
67
+ this.typeCache.set(signature, arrayType);
68
+ return arrayType;
69
+ }
70
+ // Check for class/interface/enum types (but not arrays)
71
+ const symbol = (_b = type.getSymbol) === null || _b === void 0 ? void 0 : _b.call(type);
72
+ if (symbol) {
73
+ if (symbol.flags & (typescript_1.default.SymbolFlags.Class | typescript_1.default.SymbolFlags.Interface | typescript_1.default.SymbolFlags.Enum | typescript_1.default.SymbolFlags.TypeAlias)) {
74
+ // Create and cache shell first to handle circular references
75
+ const classType = this.createEmptyClassType(type);
76
+ this.typeCache.set(signature, classType);
77
+ this.populateClassType(classType, type);
78
+ return classType;
79
+ }
80
+ }
81
+ // For anonymous object types that could have circular references
82
+ if (type.flags & typescript_1.default.TypeFlags.Object) {
83
+ const objectFlags = type.objectFlags;
84
+ if (objectFlags & typescript_1.default.ObjectFlags.Anonymous) {
85
+ // Anonymous object type - create and cache shell, then populate
86
+ const classType = this.createEmptyClassType(type);
87
+ this.typeCache.set(signature, classType);
88
+ this.populateClassType(classType, type);
89
+ return classType;
90
+ }
91
+ }
92
+ // For non-object types, we can create them directly without recursion concerns
93
+ const result = this.createPrimitiveOrUnknownType(type);
87
94
  this.typeCache.set(signature, result);
88
95
  return result;
89
96
  }
90
97
  getSignature(type) {
91
- // FIXME for classes we need to include the containing module / package in the signature and probably include in the qualified name
92
- if ("id" in type) { // a private field returned by the type checker
98
+ var _b, _c;
99
+ // Try to use TypeScript's internal id if available
100
+ if ("id" in type && type.id !== undefined) {
93
101
  return type.id;
94
102
  }
95
- else {
96
- throw new Error("no id property in type: " + JSON.stringify(type));
103
+ // Fallback: Generate a string signature based on type characteristics
104
+ const typeString = this.checker.typeToString(type);
105
+ const symbol = (_b = type.getSymbol) === null || _b === void 0 ? void 0 : _b.call(type);
106
+ if (symbol) {
107
+ const declaration = symbol.valueDeclaration || ((_c = symbol.declarations) === null || _c === void 0 ? void 0 : _c[0]);
108
+ if (declaration) {
109
+ const sourceFile = declaration.getSourceFile();
110
+ const fileName = sourceFile.fileName;
111
+ const pos = declaration.pos;
112
+ // Create unique signature from file + position + type string
113
+ // This ensures types from different modules are distinguished
114
+ return `${fileName}:${pos}:${typeString}`;
115
+ }
97
116
  }
117
+ // Last resort: use type string with a prefix to distinguish from numeric IDs
118
+ // This might happen for synthetic types or types without declarations
119
+ return `synthetic:${typeString}`;
98
120
  }
99
121
  primitiveType(node) {
100
122
  const type = this.type(node);
101
- return java_1.JavaType.isPrimitive(type) ? type : java_1.JavaType.Primitive.None;
123
+ if (java_1.Type.isClass(type) && type.fullyQualifiedName === 'lib.RegExp') {
124
+ return java_1.Type.Primitive.String;
125
+ }
126
+ return java_1.Type.isPrimitive(type) ? type : java_1.Type.Primitive.None;
102
127
  }
103
128
  variableType(node) {
104
- if (ts.isVariableDeclaration(node)) {
129
+ if (typescript_1.default.isVariableDeclaration(node)) {
105
130
  const symbol = this.checker.getSymbolAtLocation(node.name);
106
131
  if (symbol) {
107
- const type = this.checker.getTypeOfSymbolAtLocation(symbol, node);
132
+ // TODO: Implement in Phase 6
133
+ // const type = this.checker.getTypeOfSymbolAtLocation(symbol, node);
134
+ // return JavaType.Variable with proper mapping
108
135
  }
109
136
  }
110
137
  return undefined;
111
138
  }
112
139
  methodType(node) {
113
- return undefined;
140
+ let signature;
141
+ let methodName;
142
+ let declaringType;
143
+ let declaredFormalTypeNames = [];
144
+ // Handle different kinds of nodes that represent methods or method invocations
145
+ if (typescript_1.default.isCallExpression(node)) {
146
+ // For method invocations (e.g., _.map(...))
147
+ signature = this.checker.getResolvedSignature(node);
148
+ if (!signature) {
149
+ return undefined;
150
+ }
151
+ const symbol = this.checker.getSymbolAtLocation(node.expression);
152
+ if (!symbol) {
153
+ return undefined;
154
+ }
155
+ // Get the method name
156
+ if (typescript_1.default.isPropertyAccessExpression(node.expression)) {
157
+ methodName = node.expression.name.getText();
158
+ const exprType = this.checker.getTypeAtLocation(node.expression.expression);
159
+ declaringType = this.getType(exprType);
160
+ }
161
+ else if (typescript_1.default.isIdentifier(node.expression)) {
162
+ methodName = node.expression.getText();
163
+ // For standalone functions, we need to determine the appropriate declaring type
164
+ const exprType = this.checker.getTypeAtLocation(node.expression);
165
+ const funcType = this.getType(exprType);
166
+ if (funcType && funcType.kind === java_1.Type.Kind.Class) {
167
+ const fqn = funcType.fullyQualifiedName;
168
+ const lastDot = fqn.lastIndexOf('.');
169
+ if (lastDot > 0) {
170
+ // For functions from modules, use the module part as declaring type
171
+ // Examples:
172
+ // - "node.assert" -> declaring type: "node"
173
+ // - "@types/lodash.map" -> declaring type: "@types/lodash"
174
+ // - "@types/express.express" -> declaring type: "@types/express"
175
+ declaringType = {
176
+ kind: java_1.Type.Kind.Class,
177
+ fullyQualifiedName: fqn.substring(0, lastDot)
178
+ };
179
+ }
180
+ else {
181
+ // No dots in the name - the type IS the module itself
182
+ // This handles single-name modules like "axios", "lodash" etc.
183
+ declaringType = funcType;
184
+ }
185
+ }
186
+ else {
187
+ // Try to use the symbol's parent or module
188
+ const parent = symbol.parent;
189
+ if (parent) {
190
+ const parentType = this.checker.getDeclaredTypeOfSymbol(parent);
191
+ declaringType = this.getType(parentType);
192
+ }
193
+ else {
194
+ declaringType = java_1.Type.unknownType;
195
+ }
196
+ }
197
+ }
198
+ else {
199
+ methodName = symbol.getName();
200
+ declaringType = java_1.Type.unknownType;
201
+ }
202
+ // Get type parameters from signature
203
+ const typeParameters = signature.getTypeParameters();
204
+ if (typeParameters) {
205
+ for (const tp of typeParameters) {
206
+ declaredFormalTypeNames.push(tp.symbol.getName());
207
+ }
208
+ }
209
+ }
210
+ else if (typescript_1.default.isMethodDeclaration(node) || typescript_1.default.isMethodSignature(node)) {
211
+ // For method declarations
212
+ const symbol = this.checker.getSymbolAtLocation(node.name);
213
+ if (!symbol) {
214
+ return undefined;
215
+ }
216
+ signature = this.checker.getSignatureFromDeclaration(node);
217
+ if (!signature) {
218
+ return undefined;
219
+ }
220
+ methodName = symbol.getName();
221
+ // Get the declaring type (the class/interface that contains this method)
222
+ const parent = node.parent;
223
+ if (typescript_1.default.isClassDeclaration(parent) || typescript_1.default.isInterfaceDeclaration(parent) || typescript_1.default.isObjectLiteralExpression(parent)) {
224
+ const parentType = this.checker.getTypeAtLocation(parent);
225
+ declaringType = this.getType(parentType);
226
+ }
227
+ else {
228
+ declaringType = java_1.Type.unknownType;
229
+ }
230
+ // Get type parameters from node
231
+ if (node.typeParameters) {
232
+ for (const tp of node.typeParameters) {
233
+ declaredFormalTypeNames.push(tp.name.getText());
234
+ }
235
+ }
236
+ }
237
+ else if (typescript_1.default.isFunctionDeclaration(node) || typescript_1.default.isFunctionExpression(node)) {
238
+ // For function declarations/expressions
239
+ signature = this.checker.getSignatureFromDeclaration(node);
240
+ if (!signature) {
241
+ return undefined;
242
+ }
243
+ methodName = node.name ? node.name.getText() : "<anonymous>";
244
+ declaringType = java_1.Type.unknownType;
245
+ // Get type parameters from node
246
+ if (node.typeParameters) {
247
+ for (const tp of node.typeParameters) {
248
+ declaredFormalTypeNames.push(tp.name.getText());
249
+ }
250
+ }
251
+ }
252
+ else {
253
+ // For other node types, return undefined
254
+ return undefined;
255
+ }
256
+ // Common logic for all method types
257
+ const returnType = signature.getReturnType();
258
+ const parameters = signature.getParameters();
259
+ const parameterTypes = [];
260
+ const parameterNames = [];
261
+ for (const param of parameters) {
262
+ parameterNames.push(param.getName());
263
+ const paramType = this.checker.getTypeOfSymbolAtLocation(param, node);
264
+ parameterTypes.push(this.getType(paramType));
265
+ }
266
+ // Create the Type.Method object
267
+ return Object.assign(new NonDraftableType(), {
268
+ kind: java_1.Type.Kind.Method,
269
+ declaringType: declaringType,
270
+ name: methodName,
271
+ returnType: this.getType(returnType),
272
+ parameterNames: parameterNames,
273
+ parameterTypes: parameterTypes,
274
+ thrownExceptions: [], // JavaScript doesn't have checked exceptions
275
+ annotations: [],
276
+ defaultValue: undefined,
277
+ declaredFormalTypeNames: declaredFormalTypeNames,
278
+ toJSON: function () {
279
+ return java_1.Type.signature(this);
280
+ }
281
+ });
282
+ }
283
+ /**
284
+ * Create a JavaType.Array from a TypeScript array type
285
+ */
286
+ createArrayType(type) {
287
+ // Get the element type (type argument of Array<T>)
288
+ const typeArgs = this.checker.getTypeArguments(type);
289
+ const elemType = typeArgs.length > 0 ? this.getType(typeArgs[0]) : java_1.Type.unknownType;
290
+ return Object.assign(new NonDraftableType(), {
291
+ kind: java_1.Type.Kind.Array,
292
+ elemType: elemType,
293
+ annotations: [],
294
+ toJSON: function () {
295
+ return java_1.Type.signature(this);
296
+ }
297
+ });
114
298
  }
115
- createType(type, cacheKey) {
116
- const signature = this.checker.typeToString(type);
299
+ /**
300
+ * Get the fully qualified name for a TypeScript type.
301
+ * Format: "module-specifier.TypeName" (e.g., "@mui/material.Button", "src/components/Button.Button")
302
+ */
303
+ getFullyQualifiedName(type) {
304
+ var _b, _c;
305
+ const symbol = (_b = type.getSymbol) === null || _b === void 0 ? void 0 : _b.call(type);
306
+ if (!symbol) {
307
+ return "unknown";
308
+ }
309
+ const typeName = symbol.getName();
310
+ const declaration = symbol.valueDeclaration || ((_c = symbol.declarations) === null || _c === void 0 ? void 0 : _c[0]);
311
+ if (!declaration) {
312
+ // No declaration - might be a built-in or synthetic type
313
+ if (builtInTypes.has(typeName)) {
314
+ return `lib.${typeName}`;
315
+ }
316
+ return typeName;
317
+ }
318
+ const sourceFile = declaration.getSourceFile();
319
+ const fileName = sourceFile.fileName;
320
+ // Check if this is a test file (snowflake ID as filename)
321
+ // Test files are generated with numeric IDs like "672087069480189952.ts"
322
+ if (/^\d+\.(ts|tsx|js|jsx)$/.test(fileName)) {
323
+ // For test files, just return the type name without module prefix
324
+ return typeName;
325
+ }
326
+ // Check if this is from TypeScript's lib files (lib.d.ts, lib.dom.d.ts, etc.)
327
+ if (fileName.includes("/typescript/lib/lib.") || fileName.includes("\\typescript\\lib\\lib.")) {
328
+ return `lib.${typeName}`;
329
+ }
330
+ // Check if this is from an external module (node_modules or .d.ts)
331
+ if (sourceFile.isDeclarationFile || fileName.includes("node_modules")) {
332
+ const packageName = this.extractPackageName(fileName);
333
+ if (packageName) {
334
+ // Special handling for @types/node - these are Node.js built-in modules
335
+ // and should be mapped to "node.*" instead of "@types/node.*"
336
+ if (packageName === "@types/node") {
337
+ // Extract the module name from the file path
338
+ // e.g., /node_modules/@types/node/assert.d.ts -> assert
339
+ // e.g., /node_modules/@types/node/fs/promises.d.ts -> fs/promises
340
+ const nodeMatch = fileName.match(/node_modules\/@types\/node\/([^.]+)\.d\.ts/);
341
+ if (nodeMatch) {
342
+ const modulePath = nodeMatch[1];
343
+ // For default exports from Node modules, we want the module to be the "class"
344
+ // But we still need to include the type name for proper identification
345
+ if (typeName === "default" || typeName === modulePath) {
346
+ // This is likely the default export, just use the module name
347
+ return `node.${modulePath}`;
348
+ }
349
+ // For named exports, include both module and type name
350
+ if (modulePath.includes('/')) {
351
+ return `node.${modulePath.replace(/\//g, '.')}.${typeName}`;
352
+ }
353
+ return `node.${modulePath}.${typeName}`;
354
+ }
355
+ // Fallback for @types/node types that don't match the pattern
356
+ return `node.${typeName}`;
357
+ }
358
+ return `${packageName}.${typeName}`;
359
+ }
360
+ }
361
+ // For local files, use relative path from project root
362
+ const relativePath = this.getRelativeModulePath(fileName);
363
+ return `${relativePath}.${typeName}`;
364
+ }
365
+ /**
366
+ * Extract package name from a node_modules path.
367
+ * Examples:
368
+ * - /path/to/project/node_modules/react/index.d.ts -> "react"
369
+ * - /path/to/project/node_modules/@mui/material/Button/index.d.ts -> "@mui/material"
370
+ */
371
+ extractPackageName(fileName) {
372
+ const match = fileName.match(/node_modules\/(@[^\/]+\/[^\/]+|[^\/]+)/);
373
+ return match ? match[1] : null;
374
+ }
375
+ /**
376
+ * Get relative module path from project root.
377
+ * Removes file extension and uses forward slashes.
378
+ */
379
+ getRelativeModulePath(fileName) {
380
+ // Remove project root and normalize path
381
+ let relativePath = fileName;
382
+ if (fileName.startsWith(this.projectRoot)) {
383
+ relativePath = fileName.slice(this.projectRoot.length);
384
+ }
385
+ // Remove leading slash and file extension
386
+ relativePath = relativePath.replace(/^\//, '').replace(/\.[^/.]+$/, '');
387
+ // Convert backslashes to forward slashes (for Windows)
388
+ relativePath = relativePath.replace(/\\/g, '/');
389
+ return relativePath;
390
+ }
391
+ /**
392
+ * Create an empty JavaType.Class shell from a TypeScript type.
393
+ * The shell will be populated later to handle circular references.
394
+ */
395
+ createEmptyClassType(type) {
396
+ var _b;
397
+ // Use our custom getFullyQualifiedName method for consistent naming
398
+ let fullyQualifiedName = this.getFullyQualifiedName(type);
399
+ // If getFullyQualifiedName returned unknown, fall back to TypeScript's method
400
+ if (fullyQualifiedName === "unknown") {
401
+ const symbol = type.symbol;
402
+ fullyQualifiedName = symbol ? this.checker.getFullyQualifiedName(symbol) : `<anonymous>${this.checker.typeToString(type)}`;
403
+ // Fix FQN for types from @types packages
404
+ // TypeScript returns "_.LoDashStatic" but we want "@types/lodash.LoDashStatic"
405
+ if (symbol && symbol.declarations && symbol.declarations.length > 0) {
406
+ const sourceFile = symbol.declarations[0].getSourceFile();
407
+ const fileName = sourceFile.fileName;
408
+ // Check if this is from @types package
409
+ const typesMatch = fileName.match(/node_modules\/@types\/([^/]+)/);
410
+ if (typesMatch) {
411
+ const packageName = typesMatch[1];
412
+ // Special handling for @types/node - use "node" prefix instead
413
+ if (packageName === "node") {
414
+ // Extract the module name from the file path if possible
415
+ const nodeMatch = fileName.match(/node_modules\/@types\/node\/([^.]+)\.d\.ts/);
416
+ if (nodeMatch) {
417
+ const modulePath = nodeMatch[1];
418
+ // Replace the module specifier with node.module
419
+ fullyQualifiedName = fullyQualifiedName.replace(/^[^.]+\./, `node.${modulePath}.`);
420
+ }
421
+ else {
422
+ // Fallback: just use "node" prefix
423
+ fullyQualifiedName = fullyQualifiedName.replace(/^[^.]+\./, `node.`);
424
+ }
425
+ }
426
+ else {
427
+ // Replace the module specifier part with @types/package
428
+ fullyQualifiedName = fullyQualifiedName.replace(/^[^.]+\./, `@types/${packageName}.`);
429
+ }
430
+ }
431
+ }
432
+ }
433
+ // Determine the class kind based on symbol flags
434
+ let classKind = java_1.Type.Class.Kind.Interface; // Default to interface
435
+ const symbol = (_b = type.getSymbol) === null || _b === void 0 ? void 0 : _b.call(type);
436
+ if (symbol) {
437
+ if (symbol.flags & typescript_1.default.SymbolFlags.Class) {
438
+ classKind = java_1.Type.Class.Kind.Class;
439
+ }
440
+ else if (symbol.flags & typescript_1.default.SymbolFlags.Enum) {
441
+ classKind = java_1.Type.Class.Kind.Enum;
442
+ }
443
+ else if (symbol.flags & typescript_1.default.SymbolFlags.Interface) {
444
+ classKind = java_1.Type.Class.Kind.Interface;
445
+ }
446
+ }
447
+ // Create empty class type shell (no members yet to avoid recursion)
448
+ return Object.assign(new NonDraftableType(), {
449
+ kind: java_1.Type.Kind.Class,
450
+ classKind: classKind,
451
+ fullyQualifiedName: fullyQualifiedName,
452
+ typeParameters: [],
453
+ annotations: [],
454
+ interfaces: [],
455
+ members: [],
456
+ methods: [],
457
+ toJSON: function () {
458
+ return java_1.Type.signature(this);
459
+ }
460
+ });
461
+ }
462
+ /**
463
+ * Populates the class type with members, methods, heritage, and type parameters
464
+ * Since the shell is already in the cache, any recursive references will find it
465
+ */
466
+ populateClassType(classType, type) {
467
+ var _b, _c, _d, _e, _f, _g, _h, _j;
468
+ const symbol = (_b = type.getSymbol) === null || _b === void 0 ? void 0 : _b.call(type);
469
+ // Try to get base types using TypeScript's getBaseTypes API
470
+ // This works for both local and external types (from node_modules)
471
+ if (type.flags & typescript_1.default.TypeFlags.Object) {
472
+ let baseTypes;
473
+ // Check if this is a class or interface type that supports getBaseTypes
474
+ const objectType = type;
475
+ if (objectType.objectFlags & (typescript_1.default.ObjectFlags.Class | typescript_1.default.ObjectFlags.Interface)) {
476
+ try {
477
+ baseTypes = (_d = (_c = this.checker).getBaseTypes) === null || _d === void 0 ? void 0 : _d.call(_c, type);
478
+ }
479
+ catch (e) {
480
+ // getBaseTypes might fail for some types, fall back to declaration-based extraction
481
+ }
482
+ }
483
+ else if (symbol) {
484
+ // For constructor functions or type references, we need to get the actual class type
485
+ // Try to get the type of the class itself (not the constructor or instance)
486
+ const classSymbol = symbol.flags & typescript_1.default.SymbolFlags.Alias ?
487
+ this.checker.getAliasedSymbol(symbol) : symbol;
488
+ if (classSymbol && classSymbol.flags & (typescript_1.default.SymbolFlags.Class | typescript_1.default.SymbolFlags.Interface)) {
489
+ // Get the type of the class declaration itself
490
+ const declaredType = this.checker.getDeclaredTypeOfSymbol(classSymbol);
491
+ if (declaredType && declaredType !== type) {
492
+ try {
493
+ baseTypes = (_f = (_e = this.checker).getBaseTypes) === null || _f === void 0 ? void 0 : _f.call(_e, declaredType);
494
+ }
495
+ catch (e) {
496
+ // getBaseTypes might fail, fall back to declaration-based extraction
497
+ }
498
+ }
499
+ }
500
+ else if (classSymbol && classSymbol.valueDeclaration && typescript_1.default.isClassDeclaration(classSymbol.valueDeclaration)) {
501
+ // Handle the case where the symbol is for a class value (constructor function)
502
+ // Get the instance type of the class
503
+ const instanceType = this.checker.getDeclaredTypeOfSymbol(classSymbol);
504
+ if (instanceType && instanceType !== type) {
505
+ try {
506
+ baseTypes = (_h = (_g = this.checker).getBaseTypes) === null || _h === void 0 ? void 0 : _h.call(_g, instanceType);
507
+ }
508
+ catch (e) {
509
+ // getBaseTypes might fail, fall back to declaration-based extraction
510
+ }
511
+ }
512
+ }
513
+ }
514
+ if (baseTypes && baseTypes.length > 0) {
515
+ // For classes, the first base type is usually the superclass
516
+ // Additional base types are interfaces
517
+ if (classType.classKind === java_1.Type.Class.Kind.Class) {
518
+ const firstBase = this.getType(baseTypes[0]);
519
+ if (java_1.Type.isClass(firstBase)) {
520
+ classType.supertype = firstBase;
521
+ }
522
+ // Rest are interfaces
523
+ for (let i = 1; i < baseTypes.length; i++) {
524
+ const interfaceType = this.getType(baseTypes[i]);
525
+ if (java_1.Type.isClass(interfaceType)) {
526
+ classType.interfaces.push(interfaceType);
527
+ }
528
+ }
529
+ }
530
+ else {
531
+ // For interfaces, all base types are extended interfaces
532
+ for (const baseType of baseTypes) {
533
+ const interfaceType = this.getType(baseType);
534
+ if (java_1.Type.isClass(interfaceType)) {
535
+ classType.interfaces.push(interfaceType);
536
+ }
537
+ }
538
+ }
539
+ }
540
+ }
541
+ // Extract type parameters from declarations (not provided by getBaseTypes)
542
+ if (symbol === null || symbol === void 0 ? void 0 : symbol.declarations) {
543
+ for (const declaration of symbol.declarations) {
544
+ if (typescript_1.default.isClassDeclaration(declaration) || typescript_1.default.isInterfaceDeclaration(declaration)) {
545
+ // Extract type parameters
546
+ if (declaration.typeParameters) {
547
+ for (const tp of declaration.typeParameters) {
548
+ const tpType = this.checker.getTypeAtLocation(tp);
549
+ classType.typeParameters.push(this.getType(tpType));
550
+ }
551
+ }
552
+ break; // Only process the first declaration
553
+ }
554
+ }
555
+ }
556
+ // Get properties and methods
557
+ const properties = this.checker.getPropertiesOfType(type);
558
+ for (const prop of properties) {
559
+ const declaration = prop.valueDeclaration || ((_j = prop.declarations) === null || _j === void 0 ? void 0 : _j[0]);
560
+ if (!declaration) {
561
+ // Skip properties without declarations (synthetic/built-in properties)
562
+ continue;
563
+ }
564
+ if (prop.flags & typescript_1.default.SymbolFlags.Method) {
565
+ // TODO: Create Type.Method when method support is added
566
+ // For now, skip methods
567
+ continue;
568
+ }
569
+ else {
570
+ // Create Type.Variable for fields/properties
571
+ const propType = this.checker.getTypeOfSymbolAtLocation(prop, declaration);
572
+ const variable = Object.assign(new NonDraftableType(), {
573
+ kind: java_1.Type.Kind.Variable,
574
+ name: prop.getName(),
575
+ owner: classType, // Cyclic reference to the containing class (already in cache)
576
+ type: this.getType(propType), // This will find classType in cache if it's recursive
577
+ annotations: [],
578
+ toJSON: function () {
579
+ return java_1.Type.signature(this);
580
+ }
581
+ });
582
+ classType.members.push(variable);
583
+ }
584
+ }
585
+ }
586
+ /**
587
+ * Note: Object/Class/Interface types are handled in getType() to properly manage circular references
588
+ * This method should only be called for primitive and unknown types
589
+ */
590
+ createPrimitiveOrUnknownType(type) {
591
+ var _b;
592
+ // Check for literals first
117
593
  if (type.isLiteral()) {
118
594
  if (type.isNumberLiteral()) {
119
- return java_1.JavaType.Primitive.Double;
595
+ return java_1.Type.Primitive.Double;
120
596
  }
121
597
  else if (type.isStringLiteral()) {
122
- return java_1.JavaType.Primitive.String;
598
+ return java_1.Type.Primitive.String;
123
599
  }
124
600
  }
125
- if (type.flags === ts.TypeFlags.Null) {
126
- return java_1.JavaType.Primitive.Null;
601
+ // Check for primitive types
602
+ if (type.flags === typescript_1.default.TypeFlags.Null) {
603
+ return java_1.Type.Primitive.Null;
127
604
  }
128
- else if (type.flags === ts.TypeFlags.Undefined) {
129
- return java_1.JavaType.Primitive.None;
605
+ else if (type.flags === typescript_1.default.TypeFlags.Undefined) {
606
+ return java_1.Type.Primitive.None;
130
607
  }
131
- else if (type.flags === ts.TypeFlags.Number ||
132
- type.flags === ts.TypeFlags.NumberLiteral ||
133
- type.flags === ts.TypeFlags.NumberLike) {
134
- return java_1.JavaType.Primitive.Double;
608
+ else if (type.flags === typescript_1.default.TypeFlags.Number ||
609
+ type.flags === typescript_1.default.TypeFlags.NumberLiteral ||
610
+ type.flags === typescript_1.default.TypeFlags.NumberLike) {
611
+ return java_1.Type.Primitive.Double;
135
612
  }
136
- else if (type.flags === ts.TypeFlags.String ||
137
- type.flags === ts.TypeFlags.StringLiteral ||
138
- type.flags === ts.TypeFlags.StringLike) {
139
- return java_1.JavaType.Primitive.String;
613
+ else if (type.flags === typescript_1.default.TypeFlags.String ||
614
+ type.flags === typescript_1.default.TypeFlags.StringLiteral ||
615
+ type.flags === typescript_1.default.TypeFlags.StringLike) {
616
+ return java_1.Type.Primitive.String;
140
617
  }
141
- else if (type.flags === ts.TypeFlags.Void) {
142
- return java_1.JavaType.Primitive.Void;
618
+ else if (type.flags === typescript_1.default.TypeFlags.Void) {
619
+ return java_1.Type.Primitive.Void;
143
620
  }
144
- else if (type.flags === ts.TypeFlags.BigInt ||
145
- type.flags === ts.TypeFlags.BigIntLiteral ||
146
- type.flags === ts.TypeFlags.BigIntLike) {
147
- return java_1.JavaType.Primitive.Long;
621
+ else if (type.flags === typescript_1.default.TypeFlags.BigInt ||
622
+ type.flags === typescript_1.default.TypeFlags.BigIntLiteral ||
623
+ type.flags === typescript_1.default.TypeFlags.BigIntLike) {
624
+ return java_1.Type.Primitive.Long;
148
625
  }
149
626
  else if ((type.symbol !== undefined && type.symbol === this.regExpSymbol) ||
150
627
  this.checker.typeToString(type) === "RegExp") {
151
- return java_1.JavaType.Primitive.String;
628
+ return java_1.Type.Primitive.String;
152
629
  }
153
630
  /**
154
631
  * TypeScript may assign multiple flags to a single type (e.g., Boolean + Union).
155
632
  * Using a bitwise check ensures we detect Boolean even if other flags are set.
156
633
  */
157
- if (type.flags & ts.TypeFlags.Boolean ||
158
- type.flags & ts.TypeFlags.BooleanLiteral ||
159
- type.flags & ts.TypeFlags.BooleanLike) {
160
- return java_1.JavaType.Primitive.Boolean;
161
- }
162
- // if (ts.isRegularExpressionLiteral(node)) {
163
- // return JavaType.Primitive.String;
164
- // }
165
- return java_1.JavaType.unknownType;
634
+ if (type.flags & typescript_1.default.TypeFlags.Boolean ||
635
+ type.flags & typescript_1.default.TypeFlags.BooleanLiteral ||
636
+ type.flags & typescript_1.default.TypeFlags.BooleanLike) {
637
+ return java_1.Type.Primitive.Boolean;
638
+ }
639
+ // Check for type aliases that may resolve to primitives
640
+ const symbol = (_b = type.getSymbol) === null || _b === void 0 ? void 0 : _b.call(type);
641
+ if (symbol && symbol.flags & typescript_1.default.SymbolFlags.TypeAlias) {
642
+ // Type aliases may resolve to primitive types
643
+ const aliasedType = this.checker.getDeclaredTypeOfSymbol(symbol);
644
+ if (aliasedType !== type) {
645
+ return this.getType(aliasedType);
646
+ }
647
+ }
648
+ return java_1.Type.unknownType;
166
649
  }
167
650
  }
168
651
  exports.JavaScriptTypeMapping = JavaScriptTypeMapping;
169
- rpc_1.RpcCodecs.registerCodec(java_1.JavaType.Kind.Primitive, {
170
- rpcSend(after, q) {
171
- return __awaiter(this, void 0, void 0, function* () {
172
- yield q.getAndSend(after, p => p.keyword);
173
- });
174
- },
175
- rpcReceive(before, q) {
176
- return __awaiter(this, void 0, void 0, function* () {
177
- const keyword = yield q.receive(before.keyword);
178
- return java_1.JavaType.Primitive.fromKeyword(keyword);
179
- });
180
- }
181
- });
182
652
  //# sourceMappingURL=type-mapping.js.map