UNPKG

1.23 kBTypeScriptView Raw
1declare namespace callerPath {
2 interface Options {
3 /**
4 The caller path depth, meaning how many levels we follow back on the stack trace.
5
6 @default 0
7
8 @example
9 ```
10 // foo.ts
11 import callerPath = require('caller-path');
12
13 module.exports = () => {
14 console.log(callerPath());
15 //=> '/Users/sindresorhus/dev/unicorn/foobar.ts'
16 console.log(callerPath({depth: 1}));
17 //=> '/Users/sindresorhus/dev/unicorn/bar.ts'
18 console.log(callerPath({depth: 2}));
19 //=> '/Users/sindresorhus/dev/unicorn/foo.ts'
20 }
21
22 // bar.ts
23 import foo = require('./foo');
24
25 module.exports = () => {
26 foo();
27 }
28
29 // foobar.ts
30 import bar = require('./bar');
31 bar();
32 ```
33 */
34 readonly depth?: number;
35 }
36}
37
38/**
39Get the path of the caller function.
40
41If the caller's [callsite](https://github.com/sindresorhus/callsites#api) object `getFileName` was not defined for some reason, it will return `undefined`.
42
43@example
44```
45// foo.ts
46import callerPath = require('caller-path');
47
48export default () => {
49 console.log(callerPath());
50 //=> '/Users/sindresorhus/dev/unicorn/bar.ts'
51}
52
53// bar.ts
54import foo from './foo';
55foo();
56```
57*/
58declare function callerPath(options?: callerPath.Options): string | undefined;
59
60export = callerPath;