UNPKG

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