forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
99 lines (83 loc) · 2.36 KB
/
helpers.js
File metadata and controls
99 lines (83 loc) · 2.36 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
89
90
91
92
93
94
95
96
97
98
99
/* 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";
import type { SimplePath } from "./simple-path";
export function isFunction(node: Node) {
return (
t.isFunction(node) ||
t.isArrowFunctionExpression(node) ||
t.isObjectMethod(node) ||
t.isClassMethod(node)
);
}
export function isAwaitExpression(path: SimplePath) {
const { node, parent } = path;
return (
t.isAwaitExpression(node) ||
(t.isAwaitExpression(parent.init) || t.isAwaitExpression(parent))
);
}
export function isYieldExpression(path: SimplePath) {
const { node, parent } = path;
return (
t.isYieldExpression(node) ||
(t.isYieldExpression(parent.init) || t.isYieldExpression(parent))
);
}
export function isVariable(path: SimplePath) {
const node = path.node;
return (
t.isVariableDeclaration(node) ||
(isFunction(path) && path.node.params != null && path.node.params.length) ||
(t.isObjectProperty(node) && !isFunction(path.node.value))
);
}
export function isComputedExpression(expression: string): boolean {
return /^\[/m.test(expression);
}
export function getMemberExpression(root: Node) {
function _getMemberExpression(node, expr) {
if (t.isMemberExpression(node)) {
expr = [node.property.name].concat(expr);
return _getMemberExpression(node.object, expr);
}
if (t.isCallExpression(node)) {
return [];
}
if (t.isThisExpression(node)) {
return ["this"].concat(expr);
}
return [node.name].concat(expr);
}
const expr = _getMemberExpression(root, []);
return expr.join(".");
}
export function getVariables(dec: Node) {
if (!dec.id) {
return [];
}
if (t.isArrayPattern(dec.id)) {
if (!dec.id.elements) {
return [];
}
// NOTE: it's possible that an element is empty
// e.g. const [, a] = arr
return dec.id.elements.filter(element => element).map(element => {
return {
name: t.isAssignmentPattern(element)
? element.left.name
: element.name || element.argument.name,
location: element.loc
};
});
}
return [
{
name: dec.id.name,
location: dec.loc
}
];
}