get_callsite.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. var _gracefulFs;
  6. function _load_gracefulFs() {
  7. return (_gracefulFs = _interopRequireDefault(require('graceful-fs')));
  8. }
  9. var _callsites;
  10. function _load_callsites() {
  11. return (_callsites = _interopRequireDefault(require('callsites')));
  12. }
  13. var _sourceMap;
  14. function _load_sourceMap() {
  15. return (_sourceMap = require('source-map'));
  16. }
  17. function _interopRequireDefault(obj) {
  18. return obj && obj.__esModule ? obj : {default: obj};
  19. }
  20. // Copied from https://github.com/rexxars/sourcemap-decorate-callsites/blob/5b9735a156964973a75dc62fd2c7f0c1975458e8/lib/index.js#L113-L158
  21. /**
  22. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  23. *
  24. * This source code is licensed under the MIT license found in the
  25. * LICENSE file in the root directory of this source tree.
  26. *
  27. *
  28. */
  29. const addSourceMapConsumer = (callsite, consumer) => {
  30. const getLineNumber = callsite.getLineNumber;
  31. const getColumnNumber = callsite.getColumnNumber;
  32. let position = null;
  33. function getPosition() {
  34. if (!position) {
  35. position = consumer.originalPositionFor({
  36. column: getColumnNumber.call(callsite),
  37. line: getLineNumber.call(callsite)
  38. });
  39. }
  40. return position;
  41. }
  42. Object.defineProperties(callsite, {
  43. getColumnNumber: {
  44. value: function() {
  45. return getPosition().column || getColumnNumber.call(callsite);
  46. },
  47. writable: false
  48. },
  49. getLineNumber: {
  50. value: function() {
  51. return getPosition().line || getLineNumber.call(callsite);
  52. },
  53. writable: false
  54. }
  55. });
  56. };
  57. exports.default = (level, sourceMaps) => {
  58. const levelAfterThisCall = level + 1;
  59. const stack = (0, (_callsites || _load_callsites()).default)()[
  60. levelAfterThisCall
  61. ];
  62. const sourceMapFileName = sourceMaps && sourceMaps[stack.getFileName()];
  63. if (sourceMapFileName) {
  64. try {
  65. const sourceMap = (
  66. _gracefulFs || _load_gracefulFs()
  67. ).default.readFileSync(sourceMapFileName, 'utf8');
  68. addSourceMapConsumer(
  69. stack,
  70. new (_sourceMap || _load_sourceMap()).SourceMapConsumer(sourceMap)
  71. );
  72. } catch (e) {
  73. // ignore
  74. }
  75. }
  76. return stack;
  77. };