1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.inferLabel = void 0;
|
4 | const fs = require("fs");
|
5 | const is_valid_identifier_1 = require("./is-valid-identifier");
|
6 | const is_node_1 = require("./node/is-node");
|
7 |
|
8 | const labelRegex = /^.*?\((?<label>.*?)[,)]/;
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const inferLabel = (callsites) => {
|
17 | var _a;
|
18 | if (!is_node_1.default) {
|
19 |
|
20 | return;
|
21 | }
|
22 |
|
23 | const functionCallStackFrame = callsites[1];
|
24 | if (!functionCallStackFrame) {
|
25 | return;
|
26 | }
|
27 | const fileName = functionCallStackFrame.getFileName();
|
28 | const lineNumber = functionCallStackFrame.getLineNumber();
|
29 | const columnNumber = functionCallStackFrame.getColumnNumber();
|
30 | if (fileName === null || lineNumber === null || columnNumber === null) {
|
31 | return;
|
32 | }
|
33 | let content = [];
|
34 | try {
|
35 | content = fs.readFileSync(fileName, 'utf8').split('\n');
|
36 | }
|
37 | catch (_b) {
|
38 | return;
|
39 | }
|
40 | let line = content[lineNumber - 1];
|
41 | if (!line) {
|
42 |
|
43 | return;
|
44 | }
|
45 | line = line.slice(columnNumber - 1);
|
46 | const match = labelRegex.exec(line);
|
47 | if (!((_a = match === null || match === void 0 ? void 0 : match.groups) === null || _a === void 0 ? void 0 : _a.label)) {
|
48 |
|
49 | return;
|
50 | }
|
51 | const token = match.groups.label;
|
52 | if (is_valid_identifier_1.default(token) || is_valid_identifier_1.default(token.split('.').pop())) {
|
53 | return token;
|
54 | }
|
55 | return;
|
56 | };
|
57 | exports.inferLabel = inferLabel;
|