@ethersphere/bee-js 13.0.0-upcoming.gc78f60a → 13.0.0-upcoming.gf9d33e6
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.
- package/dist/codemod/transforms/v13.js +241 -0
- package/package.json +1 -1
|
@@ -237,7 +237,199 @@ function transform(sourceFile, checker) {
|
|
|
237
237
|
return /[/\\]bee-js[/\\]/.test(file) || /[/\\]bee\.(d\.)?ts$/.test(file);
|
|
238
238
|
});
|
|
239
239
|
};
|
|
240
|
+
// ChunkBuilder.hash() now returns a Reference, not a raw Uint8Array.
|
|
241
|
+
const isChunkBuilderReceiver = (expr) => {
|
|
242
|
+
const symbol = checker.getTypeAtLocation(expr).getSymbol();
|
|
243
|
+
if (!symbol || symbol.getName() !== 'ChunkBuilder') {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
return (symbol.getDeclarations() ?? []).some(decl => {
|
|
247
|
+
if (!ts.isClassDeclaration(decl)) {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
const file = decl.getSourceFile().fileName;
|
|
251
|
+
return /[/\\]core-sdk[/\\]/.test(file) || /[/\\]splitter\.(d\.)?ts$/.test(file);
|
|
252
|
+
});
|
|
253
|
+
};
|
|
240
254
|
const replacements = [];
|
|
255
|
+
// MerkleTree was removed in v13; ChunkSplitter is the replacement.
|
|
256
|
+
let merkleTreeLocalName = null;
|
|
257
|
+
let merkleTreeImportNameNode = null;
|
|
258
|
+
for (const stmt of sourceFile.statements) {
|
|
259
|
+
if (ts.isImportDeclaration(stmt) &&
|
|
260
|
+
ts.isStringLiteral(stmt.moduleSpecifier) &&
|
|
261
|
+
stmt.moduleSpecifier.text === '@ethersphere/bee-js' &&
|
|
262
|
+
stmt.importClause?.namedBindings &&
|
|
263
|
+
ts.isNamedImports(stmt.importClause.namedBindings)) {
|
|
264
|
+
for (const element of stmt.importClause.namedBindings.elements) {
|
|
265
|
+
const importedNameNode = element.propertyName ?? element.name;
|
|
266
|
+
if (importedNameNode.text === 'MerkleTree') {
|
|
267
|
+
// Aliased imports keep their local name unchanged.
|
|
268
|
+
replacements.push({
|
|
269
|
+
start: importedNameNode.getStart(sourceFile),
|
|
270
|
+
end: importedNameNode.getEnd(),
|
|
271
|
+
text: 'ChunkSplitter',
|
|
272
|
+
});
|
|
273
|
+
if (!element.propertyName) {
|
|
274
|
+
merkleTreeLocalName = element.name.text;
|
|
275
|
+
merkleTreeImportNameNode = element.name;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
// @upcoming/swarm-core is now @ethersphere/core-sdk.
|
|
281
|
+
if ((ts.isImportDeclaration(stmt) || ts.isExportDeclaration(stmt)) &&
|
|
282
|
+
stmt.moduleSpecifier &&
|
|
283
|
+
ts.isStringLiteral(stmt.moduleSpecifier) &&
|
|
284
|
+
stmt.moduleSpecifier.text === '@upcoming/swarm-core') {
|
|
285
|
+
const quote = stmt.moduleSpecifier.getText(sourceFile)[0];
|
|
286
|
+
replacements.push({
|
|
287
|
+
start: stmt.moduleSpecifier.getStart(sourceFile),
|
|
288
|
+
end: stmt.moduleSpecifier.getEnd(),
|
|
289
|
+
text: `${quote}@ethersphere/core-sdk${quote}`,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
// Syntactic fallback: MerkleTree no longer type-checks, so isChunkBuilderReceiver alone
|
|
294
|
+
// won't find it.
|
|
295
|
+
const unwrap = (expr) => {
|
|
296
|
+
while (ts.isParenthesizedExpression(expr) || ts.isAwaitExpression(expr)) {
|
|
297
|
+
expr = expr.expression;
|
|
298
|
+
}
|
|
299
|
+
return expr;
|
|
300
|
+
};
|
|
301
|
+
const isChunkSplitterRootCall = (expr) => {
|
|
302
|
+
const inner = unwrap(expr);
|
|
303
|
+
return (merkleTreeLocalName !== null &&
|
|
304
|
+
ts.isCallExpression(inner) &&
|
|
305
|
+
ts.isPropertyAccessExpression(inner.expression) &&
|
|
306
|
+
ts.isIdentifier(inner.expression.expression) &&
|
|
307
|
+
inner.expression.expression.text === merkleTreeLocalName &&
|
|
308
|
+
(inner.expression.name.text === 'root' || inner.expression.name.text === 'finalize'));
|
|
309
|
+
};
|
|
310
|
+
const chunkBuilderVarNames = new Set();
|
|
311
|
+
const collectChunkBuilderVars = (node) => {
|
|
312
|
+
if (ts.isVariableDeclaration(node) &&
|
|
313
|
+
ts.isIdentifier(node.name) &&
|
|
314
|
+
node.initializer &&
|
|
315
|
+
isChunkSplitterRootCall(node.initializer)) {
|
|
316
|
+
chunkBuilderVarNames.add(node.name.text);
|
|
317
|
+
}
|
|
318
|
+
ts.forEachChild(node, collectChunkBuilderVars);
|
|
319
|
+
};
|
|
320
|
+
if (merkleTreeLocalName) {
|
|
321
|
+
collectChunkBuilderVars(sourceFile);
|
|
322
|
+
}
|
|
323
|
+
// bee-js's MantarayNode.collect()/.find()/.findClosest() return core-sdk's MantarayNode,
|
|
324
|
+
// not its own.
|
|
325
|
+
const isMantarayNodeType = (type, fileTest) => {
|
|
326
|
+
const symbol = type.getSymbol();
|
|
327
|
+
if (!symbol || symbol.getName() !== 'MantarayNode') {
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
return (symbol.getDeclarations() ?? []).some(decl => ts.isClassDeclaration(decl) && fileTest.test(decl.getSourceFile().fileName));
|
|
331
|
+
};
|
|
332
|
+
const isBeeMantarayNodeType = (type) => isMantarayNodeType(type, /[/\\]bee-js[/\\]/);
|
|
333
|
+
const isCoreMantarayNodeType = (type) => isMantarayNodeType(type, /[/\\]core-sdk[/\\]/);
|
|
334
|
+
// Tracks for-of loop vars whose element type is core-sdk's MantarayNode.
|
|
335
|
+
const coreFlavoredNames = new Set();
|
|
336
|
+
const collectCoreFlavoredBindings = (node) => {
|
|
337
|
+
if (ts.isForOfStatement(node) && ts.isVariableDeclarationList(node.initializer)) {
|
|
338
|
+
const decl = node.initializer.declarations[0];
|
|
339
|
+
if (decl && ts.isIdentifier(decl.name) && isCoreMantarayNodeType(checker.getTypeAtLocation(decl.name))) {
|
|
340
|
+
coreFlavoredNames.add(decl.name.text);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
ts.forEachChild(node, collectCoreFlavoredBindings);
|
|
344
|
+
};
|
|
345
|
+
collectCoreFlavoredBindings(sourceFile);
|
|
346
|
+
let needsCoreMantarayNodeImport = false;
|
|
347
|
+
// Rewrites a parameter's type when called with a core-flavored argument.
|
|
348
|
+
const rewriteMantarayNodeParameters = (node) => {
|
|
349
|
+
if (ts.isCallExpression(node)) {
|
|
350
|
+
node.arguments.forEach((arg, index) => {
|
|
351
|
+
if (!ts.isIdentifier(arg) || !coreFlavoredNames.has(arg.text)) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
const signature = checker.getResolvedSignature(node);
|
|
355
|
+
const decl = signature?.getDeclaration();
|
|
356
|
+
const param = decl && 'parameters' in decl ? decl.parameters[index] : undefined;
|
|
357
|
+
if (param?.type &&
|
|
358
|
+
ts.isTypeReferenceNode(param.type) &&
|
|
359
|
+
isBeeMantarayNodeType(checker.getTypeFromTypeNode(param.type))) {
|
|
360
|
+
replacements.push({
|
|
361
|
+
start: param.type.getStart(sourceFile),
|
|
362
|
+
end: param.type.getEnd(),
|
|
363
|
+
text: 'CoreMantarayNode',
|
|
364
|
+
});
|
|
365
|
+
needsCoreMantarayNodeImport = true;
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
ts.forEachChild(node, rewriteMantarayNodeParameters);
|
|
370
|
+
};
|
|
371
|
+
// Rewrites a Map/Array/Set's value type when a core-flavored value is inserted.
|
|
372
|
+
const rewriteMantarayNodeContainers = (node) => {
|
|
373
|
+
if (ts.isCallExpression(node) &&
|
|
374
|
+
ts.isPropertyAccessExpression(node.expression) &&
|
|
375
|
+
['set', 'push', 'add'].includes(node.expression.name.text)) {
|
|
376
|
+
const valueArg = node.expression.name.text === 'set' ? node.arguments[1] : node.arguments[0];
|
|
377
|
+
if (valueArg && ts.isIdentifier(valueArg) && coreFlavoredNames.has(valueArg.text)) {
|
|
378
|
+
const receiverSymbol = checker.getSymbolAtLocation(node.expression.expression);
|
|
379
|
+
const receiverDecl = receiverSymbol?.declarations?.find(ts.isVariableDeclaration);
|
|
380
|
+
// Type args may be on the declaration or the constructor call.
|
|
381
|
+
const typeArguments = receiverDecl?.type && ts.isTypeReferenceNode(receiverDecl.type)
|
|
382
|
+
? receiverDecl.type.typeArguments
|
|
383
|
+
: receiverDecl?.initializer && ts.isNewExpression(receiverDecl.initializer)
|
|
384
|
+
? receiverDecl.initializer.typeArguments
|
|
385
|
+
: undefined;
|
|
386
|
+
if (typeArguments) {
|
|
387
|
+
for (const typeArg of typeArguments) {
|
|
388
|
+
if (ts.isTypeReferenceNode(typeArg) && isBeeMantarayNodeType(checker.getTypeFromTypeNode(typeArg))) {
|
|
389
|
+
replacements.push({
|
|
390
|
+
start: typeArg.getStart(sourceFile),
|
|
391
|
+
end: typeArg.getEnd(),
|
|
392
|
+
text: 'CoreMantarayNode',
|
|
393
|
+
});
|
|
394
|
+
needsCoreMantarayNodeImport = true;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
ts.forEachChild(node, rewriteMantarayNodeContainers);
|
|
401
|
+
};
|
|
402
|
+
if (coreFlavoredNames.size > 0) {
|
|
403
|
+
rewriteMantarayNodeParameters(sourceFile);
|
|
404
|
+
rewriteMantarayNodeContainers(sourceFile);
|
|
405
|
+
}
|
|
406
|
+
if (needsCoreMantarayNodeImport) {
|
|
407
|
+
const coreSdkImport = sourceFile.statements.find((stmt) => ts.isImportDeclaration(stmt) &&
|
|
408
|
+
ts.isStringLiteral(stmt.moduleSpecifier) &&
|
|
409
|
+
stmt.moduleSpecifier.text === '@ethersphere/core-sdk');
|
|
410
|
+
if (coreSdkImport?.importClause?.namedBindings && ts.isNamedImports(coreSdkImport.importClause.namedBindings)) {
|
|
411
|
+
const elements = coreSdkImport.importClause.namedBindings.elements;
|
|
412
|
+
const lastSpecifier = elements[elements.length - 1];
|
|
413
|
+
if (lastSpecifier) {
|
|
414
|
+
replacements.push({
|
|
415
|
+
start: lastSpecifier.getEnd(),
|
|
416
|
+
end: lastSpecifier.getEnd(),
|
|
417
|
+
text: ', MantarayNode as CoreMantarayNode',
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
else {
|
|
422
|
+
const beeJsImport = sourceFile.statements.find((stmt) => ts.isImportDeclaration(stmt) &&
|
|
423
|
+
ts.isStringLiteral(stmt.moduleSpecifier) &&
|
|
424
|
+
stmt.moduleSpecifier.text === '@ethersphere/bee-js');
|
|
425
|
+
const insertAt = beeJsImport ? beeJsImport.getEnd() : 0;
|
|
426
|
+
replacements.push({
|
|
427
|
+
start: insertAt,
|
|
428
|
+
end: insertAt,
|
|
429
|
+
text: `\nimport { MantarayNode as CoreMantarayNode } from '@ethersphere/core-sdk'`,
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
}
|
|
241
433
|
const visit = (node) => {
|
|
242
434
|
// Any `<bee>.<method>` access — called or not — so bare method references migrate too.
|
|
243
435
|
if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.name)) {
|
|
@@ -252,6 +444,55 @@ function transform(sourceFile, checker) {
|
|
|
252
444
|
});
|
|
253
445
|
}
|
|
254
446
|
}
|
|
447
|
+
// jest.spyOn uses a string literal, so the rewrite above doesn't catch it.
|
|
448
|
+
if (ts.isCallExpression(node) &&
|
|
449
|
+
ts.isPropertyAccessExpression(node.expression) &&
|
|
450
|
+
node.expression.name.text === 'spyOn' &&
|
|
451
|
+
node.arguments.length >= 2) {
|
|
452
|
+
const [receiverArg, methodArg] = node.arguments;
|
|
453
|
+
if (receiverArg && methodArg && ts.isStringLiteralLike(methodArg)) {
|
|
454
|
+
const mapping = METHOD_MAP[methodArg.text];
|
|
455
|
+
if (mapping && isBeeReceiver(receiverArg)) {
|
|
456
|
+
const quote = methodArg.getText(sourceFile)[0];
|
|
457
|
+
replacements.push({
|
|
458
|
+
start: receiverArg.getStart(sourceFile),
|
|
459
|
+
end: receiverArg.getEnd(),
|
|
460
|
+
text: `${receiverArg.getText(sourceFile)}.${mapping.namespace}`,
|
|
461
|
+
});
|
|
462
|
+
replacements.push({
|
|
463
|
+
start: methodArg.getStart(sourceFile),
|
|
464
|
+
end: methodArg.getEnd(),
|
|
465
|
+
text: `${quote}${mapping.newName}${quote}`,
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
// ChunkBuilder.hash() now returns a Reference; append the conversion back.
|
|
471
|
+
if (merkleTreeLocalName &&
|
|
472
|
+
ts.isCallExpression(node) &&
|
|
473
|
+
ts.isPropertyAccessExpression(node.expression) &&
|
|
474
|
+
node.expression.name.text === 'hash' &&
|
|
475
|
+
node.arguments.length === 0 &&
|
|
476
|
+
(isChunkSplitterRootCall(node.expression.expression) ||
|
|
477
|
+
(ts.isIdentifier(node.expression.expression) && chunkBuilderVarNames.has(node.expression.expression.text)) ||
|
|
478
|
+
isChunkBuilderReceiver(node.expression.expression))) {
|
|
479
|
+
replacements.push({
|
|
480
|
+
start: node.getEnd(),
|
|
481
|
+
end: node.getEnd(),
|
|
482
|
+
text: '.toUint8Array()',
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
// Renames remaining MerkleTree usages (import itself already handled above).
|
|
486
|
+
if (merkleTreeLocalName &&
|
|
487
|
+
ts.isIdentifier(node) &&
|
|
488
|
+
node.text === merkleTreeLocalName &&
|
|
489
|
+
node !== merkleTreeImportNameNode) {
|
|
490
|
+
replacements.push({
|
|
491
|
+
start: node.getStart(sourceFile),
|
|
492
|
+
end: node.getEnd(),
|
|
493
|
+
text: 'ChunkSplitter',
|
|
494
|
+
});
|
|
495
|
+
}
|
|
255
496
|
ts.forEachChild(node, visit);
|
|
256
497
|
};
|
|
257
498
|
visit(sourceFile);
|