forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetFunctionName.js
More file actions
88 lines (76 loc) · 2.29 KB
/
Copy pathgetFunctionName.js
File metadata and controls
88 lines (76 loc) · 2.29 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* 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
import * as t from "@babel/types";
import type { Node } from "@babel/types";
// Perform ES6's anonymous function name inference for all
// locations where static analysis is possible.
// eslint-disable-next-line complexity
export default function getFunctionName(node: Node, parent: Node): string {
if (t.isIdentifier(node.id)) {
return node.id.name;
}
if (
t.isObjectMethod(node, { computed: false }) ||
t.isClassMethod(node, { computed: false })
) {
const key = node.key;
if (t.isIdentifier(key)) {
return key.name;
}
if (t.isStringLiteral(key)) {
return key.value;
}
if (t.isNumericLiteral(key)) {
return `${key.value}`;
}
}
if (
t.isObjectProperty(parent, { computed: false, value: node }) ||
// TODO: Babylon 6 doesn't support computed class props. It is included
// here so that it is most flexible. Once Babylon 7 is used, this
// can change to use computed: false like ObjectProperty.
(t.isClassProperty(parent, { value: node }) && !parent.computed)
) {
const key = parent.key;
if (t.isIdentifier(key)) {
return key.name;
}
if (t.isStringLiteral(key)) {
return key.value;
}
if (t.isNumericLiteral(key)) {
return `${key.value}`;
}
}
if (t.isAssignmentExpression(parent, { operator: "=", right: node })) {
if (t.isIdentifier(parent.left)) {
return parent.left.name;
}
// This case is not supported in standard ES6 name inference, but it
// is included here since it is still a helpful case during debugging.
if (t.isMemberExpression(parent.left, { computed: false })) {
return parent.left.property.name;
}
}
if (
t.isAssignmentPattern(parent, { right: node }) &&
t.isIdentifier(parent.left)
) {
return parent.left.name;
}
if (
t.isVariableDeclarator(parent, { init: node }) &&
t.isIdentifier(parent.id)
) {
return parent.id.name;
}
if (
t.isExportDefaultDeclaration(parent, { declaration: node }) &&
t.isFunctionDeclaration(node)
) {
return "default";
}
return "anonymous";
}