index.js 641 B

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. const callsites = require('callsites');
  3. module.exports = filepath => {
  4. const stacks = callsites();
  5. if (!filepath) {
  6. return stacks[2].getFileName();
  7. }
  8. let seenVal = false;
  9. // Skip the first stack as it's this function
  10. stacks.shift();
  11. for (const stack of stacks) {
  12. const parentFilepath = stack.getFileName();
  13. if (typeof parentFilepath !== 'string') {
  14. continue;
  15. }
  16. if (parentFilepath === filepath) {
  17. seenVal = true;
  18. continue;
  19. }
  20. // Skip native modules
  21. if (parentFilepath === 'module.js') {
  22. continue;
  23. }
  24. if (seenVal && parentFilepath !== filepath) {
  25. return parentFilepath;
  26. }
  27. }
  28. };