lxsmnsyc / forgetti

Solve your hook spaghetti (with more spaghetti). Inspired by React Forget.
https://forgetti.vercel.app/
MIT License
352 stars 7 forks source link

Fix memoizeIdentifier where id could be undefined #4

Closed SukkaW closed 1 year ago

SukkaW commented 1 year ago

I was playing with forgetti in some of my hobby react projects when this error happens:

TypeError: unknown file: Cannot read properties of undefined (reading 'name')
    at Optimizer.memoizeIdentifier (/Users/sukka/[redacted]/node_modules/forgetti/dist/cjs/development/index.cjs:557:34)
    at Optimizer.optimizeObjectExpression (/Users/sukka/[redacted]/node_modules/forgetti/dist/cjs/development/index.cjs:964:34)
    at Optimizer.optimizeExpression (/Users/sukka/[redacted]/node_modules/forgetti/dist/cjs/development/index.cjs:1185:19)
    at Optimizer.createDependency (/Users/sukka/[redacted]/node_modules/forgetti/dist/cjs/development/index.cjs:541:28)
    at Optimizer.optimizeCallExpression (/Users/sukka/[redacted]/node_modules/forgetti/dist/cjs/development/index.cjs:808:36)
    at Optimizer.optimizeExpression (/Users/sukka/[redacted]/node_modules/forgetti/dist/cjs/development/index.cjs:1170:19)
    at Optimizer.optimizeVariableDeclaration (/Users/sukka/[redacted]/node_modules/forgetti/dist/cjs/development/index.cjs:1216:49)
    at Optimizer.optimizeStatement (/Users/sukka/[redacted]/node_modules/forgetti/dist/cjs/development/index.cjs:1358:12)
    at Optimizer.optimizeBlock (/Users/sukka/[redacted]/node_modules/forgetti/dist/cjs/development/index.cjs:1247:12)
    at Optimizer.optimizeFunctionComponent (/Users/sukka/[redacted]/node_modules/forgetti/dist/cjs/development/index.cjs:1394:10)
    at Optimizer.optimize (/Users/sukka/[redacted]/node_modules/forgetti/dist/cjs/development/index.cjs:1402:12)

It turns out that the second parameter of memoizeIdentifier could be undefined. I don't know why and I am not able to grab a minimum re-production (yet!). For now, I just make memoizeIdentifier skip when id is falsy.

lxsmnsyc commented 1 year ago

I'd like to know the code that triggers this behavior, so that I'll know what the exact behavior causing this, but thanks for submitting this!

SukkaW commented 1 year ago

I'd like to know the code that triggers this behavior, so that I'll know what the exact behavior causing this, but thanks for submitting this!

I am using forgetti in dashflare, one of my hobby react projects, with forgetti-loader. As I said, I am not able to grab a minimum re-production yet.

lxsmnsyc commented 1 year ago

@SukkaW I spot the issue now, it was from this line: https://github.com/lxsmnsyc/forgetti/blob/main/packages/forgetti/src/core/optimizer.ts#L725

I'll release a hotfix

lxsmnsyc commented 1 year ago

Should be fixed in 0.4.3. Thank you for bringing up this issue!