@thinksharpe/react-compiler-unmemo 0.5.6 → 0.6.0

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.
@@ -325,6 +325,8 @@ export function processFile(filePath, { dryRun = false } = {}) {
325
325
  const transformations = [];
326
326
 
327
327
  // Phase 1: Strip generic type params like useMemo<ColumnsType<Foo>>( -> useMemo(
328
+ // Store stripped generics so Phase 2 can apply them as type annotations
329
+ const strippedGenerics = new Map();
328
330
  const hookNames = [
329
331
  "React.useMemo",
330
332
  "React.useCallback",
@@ -373,6 +375,8 @@ export function processFile(filePath, { dryRun = false } = {}) {
373
375
  const genericType = content.slice(angleStart + 1, angleEnd);
374
376
  content =
375
377
  content.slice(0, angleStart) + content.slice(angleEnd + 1);
378
+ // After stripping, the hook call starts at hookIdx — store the generic
379
+ strippedGenerics.set(hookIdx, genericType);
376
380
  changed = true;
377
381
  transformations.push({
378
382
  type: "strip-generic",
@@ -469,15 +473,23 @@ export function processFile(filePath, { dryRun = false } = {}) {
469
473
  replacement = withoutDeps;
470
474
  }
471
475
 
472
- const beforeMatch = content.slice(
473
- Math.max(0, idx - 50),
474
- idx,
475
- );
476
476
  const lineNum =
477
477
  content.slice(0, idx).split("\n").length;
478
478
 
479
+ // If Phase 1 stripped a generic type, inject it as a type annotation
480
+ const genericType = strippedGenerics.get(idx);
481
+ let prefix = content.slice(0, idx);
482
+ if (genericType) {
483
+ const declMatch = prefix.match(/((?:const|let|var)\s+\w+)\s*=\s*$/);
484
+ if (declMatch) {
485
+ const insertPos = prefix.length - declMatch[0].length + declMatch[1].length;
486
+ prefix = prefix.slice(0, insertPos) + ": " + genericType + prefix.slice(insertPos);
487
+ }
488
+ strippedGenerics.delete(idx);
489
+ }
490
+
479
491
  content =
480
- content.slice(0, idx) +
492
+ prefix +
481
493
  replacement +
482
494
  content.slice(closeParenIdx + 1);
483
495
  changed = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thinksharpe/react-compiler-unmemo",
3
- "version": "0.5.6",
3
+ "version": "0.6.0",
4
4
  "description": "Remove useMemo and useCallback hooks from React codebases to leverage React Compiler automatic optimization",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -25,7 +25,8 @@
25
25
  "automatic-memoization"
26
26
  ],
27
27
  "bin": {
28
- "react-compiler-unmemo": "react-compiler-unmemo.mjs"
28
+ "react-compiler-unmemo": "react-compiler-unmemo.mjs",
29
+ "unmemo": "react-compiler-unmemo.mjs"
29
30
  },
30
31
  "main": "./react-compiler-unmemo.mjs",
31
32
  "exports": {