pmndrs / swc-jotai

MIT License
90 stars 14 forks source link

Anonymous atoms cause an error in the debugLabel plugin #3

Closed kaaloo closed 1 year ago

kaaloo commented 1 year ago

This type of pattern will cause an error because although it will not be possible to deduce a debugLabel for the atom, the pattern is not skipped.

const Component = ({ value }) => {
  const valueAtom = useMemo(() => atom({ value }), [value])
  // ...
}

The workaround is to use a slightly more verbose form where the transform can do it's job.

const Component = ({ value }) => {
  const valueAtom = useMemo(() => {
    const myAtom = atom({ value })
    return myAtom;
  }), [value])
  // ...
}
Thisen commented 1 year ago

Thanks for the report! Will take a look.

Thisen commented 1 year ago

@kaaloo Do you have a reproduction? I tried creating a test case for this, it seems to work correctly.

Output:

var valueAtom = useMemo(
    function () {
      return atom({
        value: value,
      });
    },
    [value]
  );
  valueAtom.debugLabel = "valueAtom";
Thisen commented 1 year ago

I'll close due to inactivity. Feel free to re-open :)

kaaloo commented 1 year ago

@Thisen Sorry, I did not see your reply on this case. The issue comes up when using the debug label plugin. See:

https://jotai.org/docs/tools/swc#swc-jotai-debug-label

Your test case thus should not set the debug label because the purpose of the debug label plugin is to set it automatically through a code transform.