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
193 lines (161 loc) · 4.69 KB
/
helpers.js
File metadata and controls
193 lines (161 loc) · 4.69 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* 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";
import type { SymbolDeclaration } from "../index";
import generate from "@babel/generator";
import flatten from "lodash/flatten";
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 isObjectShorthand(parent: Node): boolean {
return (
t.isObjectProperty(parent) &&
parent.value &&
parent.key.start == parent.value.start &&
parent.key.loc.identifierName === parent.value.loc.identifierName
);
}
export function getObjectExpressionValue(node: Node) {
const { value } = node;
if (t.isIdentifier(value)) {
return value.name;
}
if (t.isCallExpression(value)) {
return "";
}
const code = generate(value).code;
const shouldWrap = t.isObjectExpression(value);
return shouldWrap ? `(${code})` : code;
}
export function getCode(node: Node) {
return generate(node).code;
}
export function getVariableNames(path: SimplePath): SymbolDeclaration[] {
if (t.isObjectProperty(path.node) && !isFunction(path.node.value)) {
if (path.node.key.type === "StringLiteral") {
return [
{
name: path.node.key.value,
location: path.node.loc
}
];
} else if (path.node.value.type === "Identifier") {
return [{ name: path.node.value.name, location: path.node.loc }];
} else if (path.node.value.type === "AssignmentPattern") {
return [{ name: path.node.value.left.name, location: path.node.loc }];
}
return [
{
name: path.node.key.name,
location: path.node.loc
}
];
}
if (!path.node.declarations) {
return path.node.params.map(dec => ({
name: dec.name,
location: dec.loc
}));
}
const declarations = path.node.declarations
.filter(dec => dec.id.type !== "ObjectPattern")
.map(getVariables);
return flatten(declarations);
}
export function getComments(ast: any) {
if (!ast || !ast.comments) {
return [];
}
return ast.comments.map(comment => ({
name: comment.location,
location: comment.loc
}));
}
export function getSpecifiers(specifiers: any) {
if (!specifiers) {
return [];
}
return specifiers.map(specifier => specifier.local && specifier.local.name);
}
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
}
];
}
// Top Level checks the number of "body" nodes in the ancestor chain
// if the node is top-level, then it shoul only have one body.
export function isTopLevel(ancestors: Node[]) {
return ancestors.filter(ancestor => ancestor.key == "body").length == 1;
}