You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
641 B

4 years ago
  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. };