-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
infer-label.ts
66 lines (50 loc) · 1.43 KB
/
infer-label.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import * as fs from 'node:fs';
import {CallSite} from 'callsites';
import isValidIdentifier from './is-valid-identifier.js';
import isNode from './node/is-node.js';
// Regex to extract the label out of the `ow` function call
const labelRegex = /^.*?\((?<label>.*?)[,)]/;
/**
Infer the label of the caller.
@hidden
@param callsites - List of stack frames.
*/
export const inferLabel = (callsites: readonly CallSite[]): void | string => {
if (!isNode) {
// Exit if we are not running in a Node.js environment
return;
}
// Grab the stackframe with the `ow` function call
const functionCallStackFrame = callsites[1];
if (!functionCallStackFrame) {
return;
}
const fileName = functionCallStackFrame.getFileName();
const lineNumber = functionCallStackFrame.getLineNumber();
const columnNumber = functionCallStackFrame.getColumnNumber();
if (fileName === null || lineNumber === null || columnNumber === null) {
return;
}
let content: string[] = [];
try {
content = fs.readFileSync(fileName, 'utf8').split('\n');
} catch {
return;
}
let line = content[lineNumber - 1];
if (!line) {
// Exit if the line number couldn't be found
return;
}
line = line.slice(columnNumber - 1);
const match = labelRegex.exec(line);
const token = match?.groups?.['label'];
if (!token) {
// Exit if we didn't find a label
return;
}
if (isValidIdentifier(token) || isValidIdentifier(token.split('.').pop())) {
return token;
}
return;
};