Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

callerCallsite() works incorrectly if there is a top-level await before it #12

Open
KostyaTretyak opened this issue Aug 3, 2024 · 1 comment

Comments

@KostyaTretyak
Copy link

Example:

await Promise.resolve();
const callsites = callerCallsite({ depth: 0 });
console.log(callsites); // undefined

In a real-life example, I import another library of yours before calling callerCallsite(), where there is a top-level await.

@KostyaTretyak
Copy link
Author

Maybe this will solve this bug:

export function callerCallsite({ depth = 0 } = {}) {
  const callers = [];
  const callerFileSet = new Set();
  const sliceOfCallsites = callsites();

  for (let i = 0; i < sliceOfCallsites.length; i++) {
    const callsite = sliceOfCallsites[i];
    const fileName = callsite.getFileName();
    const hasReceiver = callsite.getTypeName() !== null && fileName !== null;

    if (!callerFileSet.has(fileName) && !fileName?.startsWith('node:internal/')) {
      callerFileSet.add(fileName);
      callers.unshift(callsite);
    }

    if (hasReceiver || sliceOfCallsites.length == i + 1) {
      return callers[depth];
    }
  }
  return;
}

KostyaTretyak added a commit to ditsmod/ditsmod that referenced this issue Aug 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant