forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function-info.js
65 lines (60 loc) · 1.95 KB
/
function-info.js
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
/**
* Strip any function arguments from the given string.
*
* If the function fails to determine that there are any parentheses to strip
* it will return the original string.
*/
export function stripFunctionArguments(functionCall: string): string {
// Remove known data that can appear at the end of the string
const s = functionCall.replace(/ \[clone [^]+\]$/, '').replace(/ const$/, '');
if (s[s.length - 1] !== ')') {
return functionCall;
}
// Start from the right parenthesis at the end of the string and
// then iterate towards the beginning until we find the matching
// left parenthesis.
let depth = 0;
for (let i = s.length - 1; i > 0; i--) {
if (s[i] === ')') {
depth++;
} else if (s[i] === '(') {
depth--;
if (depth === 0) {
return functionCall.substr(0, i);
}
}
}
return functionCall;
}
export function removeTemplateInformation(functionName: string) {
let result = '';
let depth = 0;
let start = 0; // Start of a segment we'd like to keep
for (let i = 0; i < functionName.length; i++) {
if (functionName[i] === '<') {
if (depth === 0) {
// Template information begins, save segment
result += functionName.substr(start, i - start);
// Start a new segment here to not lose the rest of the string
// should we find no matching '>'
start = i;
}
depth++;
} else if (functionName[i] === '>') {
depth--;
if (depth === 0) {
// Template information ends, start of new segment
start = i + 1;
}
}
}
result += functionName.substr(start);
return result;
}
export function getFunctionName(functionCall: string) {
return removeTemplateInformation(stripFunctionArguments(functionCall));
}