UNPKG

2.57 kBTypeScriptView Raw
1type AnyFunction = (...args: any[]) => any;
2
3export interface CallSite {
4 /**
5 Returns the value of `this`.
6 */
7 getThis(): unknown | undefined;
8
9 /**
10 Returns the type of `this` as a string. This is the name of the function stored in the constructor field of `this`, if available, otherwise the object's `[[Class]]` internal property.
11 */
12 getTypeName(): string | null;
13
14 /**
15 Returns the current function.
16 */
17 getFunction(): AnyFunction | undefined;
18
19 /**
20 Returns the name of the current function, typically its `name` property. If a name property is not available an attempt will be made to try to infer a name from the function's context.
21 */
22 getFunctionName(): string | null;
23
24 /**
25 Returns the name of the property of `this` or one of its prototypes that holds the current function.
26 */
27 getMethodName(): string | undefined;
28
29 /**
30 Returns the name of the script if this function was defined in a script.
31 */
32 getFileName(): string | null;
33
34 /**
35 Returns the current line number if this function was defined in a script.
36 */
37 getLineNumber(): number | null;
38
39 /**
40 Returns the current column number if this function was defined in a script.
41 */
42 getColumnNumber(): number | null;
43
44 /**
45 Returns a string representing the location where `eval` was called if this function was created using a call to `eval`.
46 */
47 getEvalOrigin(): string | undefined;
48
49 /**
50 Returns `true` if this is a top-level invocation, that is, if it's a global object.
51 */
52 isToplevel(): boolean;
53
54 /**
55 Returns `true` if this call takes place in code defined by a call to `eval`.
56 */
57 isEval(): boolean;
58
59 /**
60 Returns `true` if this call is in native V8 code.
61 */
62 isNative(): boolean;
63
64 /**
65 Returns `true` if this is a constructor call.
66 */
67 isConstructor(): boolean;
68
69 /**
70 Returns `true` if this call is asynchronous (i.e. `await`, `Promise.all()`, or `Promise.any()`).
71 */
72 isAsync(): boolean;
73
74 /**
75 Returns `true` if this is an asynchronous call to `Promise.all()`.
76 */
77 isPromiseAll(): boolean;
78
79 /**
80 Returns the index of the promise element that was followed in `Promise.all()` or `Promise.any()` for async stack traces, or `null` if the `CallSite` is not an asynchronous `Promise.all()` or `Promise.any()` call.
81 */
82 getPromiseIndex(): number | null;
83}
84
85/**
86Get callsites from the V8 stack trace API.
87
88@returns An array of `CallSite` objects.
89
90@example
91```
92import callsites from 'callsites';
93
94function unicorn() {
95 console.log(callsites()[0].getFileName());
96 //=> '/Users/sindresorhus/dev/callsites/test.js'
97}
98
99unicorn();
100```
101*/
102export default function callsites(): CallSite[];