astronomical 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/index.js +351 -61
- package/lib/cjs/nodeutils.js +34 -33
- package/lib/cjs/parseQuery.js +10 -12
- package/lib/cjs/types/index.d.ts +37 -2
- package/lib/cjs/types/index.d.ts.map +1 -1
- package/lib/cjs/types/nodeutils.d.ts +14 -14
- package/lib/cjs/types/nodeutils.d.ts.map +1 -1
- package/lib/cjs/utils.js +2 -3
- package/lib/esm/index.mjs +351 -61
- package/lib/esm/nodeutils.js +34 -33
- package/lib/esm/parseQuery.js +10 -12
- package/lib/esm/types/index.d.ts +37 -2
- package/lib/esm/types/index.d.ts.map +1 -1
- package/lib/esm/types/nodeutils.d.ts +14 -14
- package/lib/esm/types/nodeutils.d.ts.map +1 -1
- package/lib/esm/utils.js +2 -3
- package/package.json +3 -2
- package/lib/cjs/traverse.js +0 -239
- package/lib/cjs/types/traverse.d.ts +0 -39
- package/lib/cjs/types/traverse.d.ts.map +0 -1
- package/lib/esm/traverse.js +0 -239
- package/lib/esm/types/traverse.d.ts +0 -39
- package/lib/esm/types/traverse.d.ts.map +0 -1
package/lib/esm/traverse.js
DELETED
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const nodeutils_1 = require("./nodeutils");
|
|
4
|
-
const utils_1 = require("./utils");
|
|
5
|
-
const debugLogEnabled = false;
|
|
6
|
-
const log = {
|
|
7
|
-
debug: (...args) => {
|
|
8
|
-
if (debugLogEnabled)
|
|
9
|
-
console.debug(...args);
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
const scopes = new Array(100000);
|
|
13
|
-
function createTraverser() {
|
|
14
|
-
let scopeIdCounter = 0;
|
|
15
|
-
let removedScopes = 0;
|
|
16
|
-
const nodePathsCreated = {};
|
|
17
|
-
function createScope(parentScopeId) {
|
|
18
|
-
const id = scopeIdCounter++;
|
|
19
|
-
scopes[id] = parentScopeId ?? -1;
|
|
20
|
-
return id;
|
|
21
|
-
}
|
|
22
|
-
function getBinding(scopeId, name) {
|
|
23
|
-
const scope = scopes[scopeId];
|
|
24
|
-
if (typeof scope == "number") {
|
|
25
|
-
if (scope == -1)
|
|
26
|
-
return undefined;
|
|
27
|
-
return getBinding(scope, name);
|
|
28
|
-
}
|
|
29
|
-
const s = scope.bindings[name];
|
|
30
|
-
if (s != undefined)
|
|
31
|
-
return s;
|
|
32
|
-
if (scope.parentScopeId != undefined && scope.parentScopeId >= 0) {
|
|
33
|
-
return getBinding(scope.parentScopeId, name);
|
|
34
|
-
}
|
|
35
|
-
return undefined;
|
|
36
|
-
}
|
|
37
|
-
function setBinding(scopeId, name, binding) {
|
|
38
|
-
let scope;
|
|
39
|
-
const s = scopes[scopeId];
|
|
40
|
-
if (typeof s == "number") {
|
|
41
|
-
scope = {
|
|
42
|
-
bindings: {},
|
|
43
|
-
id: scopeId,
|
|
44
|
-
parentScopeId: s == -1 ? undefined : s,
|
|
45
|
-
};
|
|
46
|
-
scopes[scopeId] = scope;
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
scope = s;
|
|
50
|
-
}
|
|
51
|
-
scope.bindings[name] = binding;
|
|
52
|
-
}
|
|
53
|
-
let pathsCreated = 0;
|
|
54
|
-
function getChildren(key, path) {
|
|
55
|
-
if (key in path.node) {
|
|
56
|
-
const r = path.node[key];
|
|
57
|
-
if (Array.isArray(r)) {
|
|
58
|
-
return r.map((n, i) => createNodePath(n, i, key, path.scopeId, path.functionScopeId, path));
|
|
59
|
-
}
|
|
60
|
-
else if (r != undefined) {
|
|
61
|
-
return [createNodePath(r, key, key, path.scopeId, path.functionScopeId, path)];
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return [];
|
|
65
|
-
}
|
|
66
|
-
function getPrimitiveChildren(key, path) {
|
|
67
|
-
if (key in path.node) {
|
|
68
|
-
const r = path.node[key];
|
|
69
|
-
return (0, utils_1.toArray)(r).filter(utils_1.isDefined).filter(nodeutils_1.isPrimitive);
|
|
70
|
-
}
|
|
71
|
-
return [];
|
|
72
|
-
}
|
|
73
|
-
function getPrimitiveChildrenOrNodePaths(key, path) {
|
|
74
|
-
if (key in path.node) {
|
|
75
|
-
const r = path.node[key];
|
|
76
|
-
if (Array.isArray(r)) {
|
|
77
|
-
return r.map((n, i) => (0, nodeutils_1.isPrimitive)(n) ? n :
|
|
78
|
-
// isLiteral(n) ? n.value as PrimitiveValue :
|
|
79
|
-
createNodePath(n, i, key, path.scopeId, path.functionScopeId, path));
|
|
80
|
-
}
|
|
81
|
-
else if (r != undefined) {
|
|
82
|
-
return [
|
|
83
|
-
(0, nodeutils_1.isPrimitive)(r) ? r :
|
|
84
|
-
// isLiteral(r) ? r.value as PrimitiveValue :
|
|
85
|
-
createNodePath(r, key, key, path.scopeId, path.functionScopeId, path)
|
|
86
|
-
];
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return [];
|
|
90
|
-
}
|
|
91
|
-
function createNodePath(node, key, parentKey, scopeId, functionScopeId, nodePath) {
|
|
92
|
-
if (node.extra?.nodePath) {
|
|
93
|
-
const path = node.extra.nodePath;
|
|
94
|
-
if (nodePath && (0, nodeutils_1.isExportSpecifier)(nodePath.node) && key == "exported" && path.key == "local") {
|
|
95
|
-
//Special handling for "export { someName }" as id is both local and exported
|
|
96
|
-
path.key = "exported";
|
|
97
|
-
path.parentPath = nodePath;
|
|
98
|
-
return path;
|
|
99
|
-
}
|
|
100
|
-
if (key != undefined)
|
|
101
|
-
path.key = typeof (key) == "number" ? key.toString() : key;
|
|
102
|
-
if (parentKey != undefined)
|
|
103
|
-
path.parentKey = parentKey;
|
|
104
|
-
if (nodePath != undefined)
|
|
105
|
-
path.parentPath = nodePath;
|
|
106
|
-
return path;
|
|
107
|
-
}
|
|
108
|
-
const finalScope = ((node.extra && node.extra.scopeId != undefined) ? node.extra.scopeId : scopeId) ?? createScope();
|
|
109
|
-
const finalFScope = ((node.extra && node.extra.functionScopeId != undefined) ? node.extra.functionScopeId : functionScopeId) ?? finalScope;
|
|
110
|
-
const path = {
|
|
111
|
-
node,
|
|
112
|
-
scopeId: finalScope,
|
|
113
|
-
functionScopeId: finalFScope,
|
|
114
|
-
parentPath: nodePath,
|
|
115
|
-
key: typeof (key) == "number" ? key.toString() : key,
|
|
116
|
-
parentKey
|
|
117
|
-
};
|
|
118
|
-
if ((0, nodeutils_1.isNode)(node)) {
|
|
119
|
-
node.extra = node.extra ?? {};
|
|
120
|
-
node.extra.nodePath = path;
|
|
121
|
-
Object.defineProperty(node.extra, "nodePath", { enumerable: false });
|
|
122
|
-
}
|
|
123
|
-
nodePathsCreated[node.type] = (nodePathsCreated[node.type] ?? 0) + 1;
|
|
124
|
-
pathsCreated++;
|
|
125
|
-
return path;
|
|
126
|
-
}
|
|
127
|
-
function registerBinding(stack, scopeId, functionScopeId, key, parentKey) {
|
|
128
|
-
//console.log("x registerBinding?", isIdentifier(node) ? node.name : node.type, parentNode.type, grandParentNode?.type, scopeId, isBinding(node, parentNode, grandParentNode));
|
|
129
|
-
const node = stack[stack.length - 1];
|
|
130
|
-
if (!(0, nodeutils_1.isIdentifier)(node))
|
|
131
|
-
return;
|
|
132
|
-
const parentNode = stack[stack.length - 2];
|
|
133
|
-
if ((0, nodeutils_1.isAssignmentExpression)(parentNode) || (0, nodeutils_1.isMemberExpression)(parentNode) || (0, nodeutils_1.isUpdateExpression)(parentNode) || (0, nodeutils_1.isExportSpecifier)(parentNode))
|
|
134
|
-
return;
|
|
135
|
-
const grandParentNode = stack[stack.length - 3];
|
|
136
|
-
if (!(0, nodeutils_1.isBinding)(node, parentNode, grandParentNode))
|
|
137
|
-
return;
|
|
138
|
-
if (key == "id" && !(0, nodeutils_1.isVariableDeclarator)(parentNode)) {
|
|
139
|
-
setBinding(functionScopeId, node.name, { path: createNodePath(node, undefined, undefined, scopeId, functionScopeId) });
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
if ((0, nodeutils_1.isVariableDeclarator)(parentNode) && (0, nodeutils_1.isVariableDeclaration)(grandParentNode)) {
|
|
143
|
-
if (grandParentNode.kind == "var") {
|
|
144
|
-
setBinding(functionScopeId, node.name, { path: createNodePath(parentNode, undefined, undefined, scopeId, functionScopeId) });
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
else {
|
|
148
|
-
setBinding(scopeId, node.name, { path: createNodePath(parentNode, undefined, undefined, scopeId, functionScopeId) });
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
if ((0, nodeutils_1.isScope)(node, parentNode)) {
|
|
153
|
-
setBinding(scopeId, node.name, { path: createNodePath(node, key, parentKey, scopeId, functionScopeId) });
|
|
154
|
-
} /*else {
|
|
155
|
-
console.log(node.type, parentNode.type, grandParentNode?.type);
|
|
156
|
-
}*/
|
|
157
|
-
}
|
|
158
|
-
let bindingNodesVisited = 0;
|
|
159
|
-
function registerBindings(stack, scopeId, functionScopeId) {
|
|
160
|
-
const node = stack[stack.length - 1];
|
|
161
|
-
if (!(0, nodeutils_1.isNode)(node))
|
|
162
|
-
return;
|
|
163
|
-
if (node.extra?.scopeId != undefined)
|
|
164
|
-
return;
|
|
165
|
-
node.extra = node.extra ?? {};
|
|
166
|
-
node.extra.scopeId = scopeId;
|
|
167
|
-
bindingNodesVisited++;
|
|
168
|
-
const keys = nodeutils_1.VISITOR_KEYS[node.type];
|
|
169
|
-
if (keys.length == 0)
|
|
170
|
-
return;
|
|
171
|
-
let childScopeId = scopeId;
|
|
172
|
-
if ((0, nodeutils_1.isScopable)(node)) {
|
|
173
|
-
childScopeId = createScope(scopeId);
|
|
174
|
-
}
|
|
175
|
-
for (const key of keys) {
|
|
176
|
-
const childNodes = node[key];
|
|
177
|
-
const children = (0, utils_1.toArray)(childNodes).filter(utils_1.isDefined);
|
|
178
|
-
children.forEach((child, i) => {
|
|
179
|
-
if (!(0, nodeutils_1.isNode)(child))
|
|
180
|
-
return;
|
|
181
|
-
const f = key == "body" && ((0, nodeutils_1.isFunctionDeclaration)(node) || (0, nodeutils_1.isFunctionExpression)(node)) ? childScopeId : functionScopeId;
|
|
182
|
-
stack.push(child);
|
|
183
|
-
if ((0, nodeutils_1.isIdentifier)(child)) {
|
|
184
|
-
const k = Array.isArray(childNodes) ? i : key;
|
|
185
|
-
registerBinding(stack, childScopeId, f, k, key);
|
|
186
|
-
}
|
|
187
|
-
else {
|
|
188
|
-
registerBindings(stack, childScopeId, f);
|
|
189
|
-
}
|
|
190
|
-
stack.pop();
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
if (childScopeId != scopeId && typeof scopes[childScopeId] == "number") { // Scope has not been populated
|
|
194
|
-
scopes[childScopeId] = scopes[scopeId];
|
|
195
|
-
removedScopes++;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
function traverseInner(node, visitor, scopeId, functionScopeId, state, path) {
|
|
199
|
-
const nodePath = path ?? createNodePath(node, undefined, undefined, scopeId, functionScopeId);
|
|
200
|
-
const keys = nodeutils_1.VISITOR_KEYS[node.type] ?? [];
|
|
201
|
-
if (nodePath.parentPath)
|
|
202
|
-
registerBindings([nodePath.parentPath.parentPath?.node, nodePath.parentPath.node, nodePath.node].filter(utils_1.isDefined), nodePath.scopeId, nodePath.functionScopeId);
|
|
203
|
-
for (const key of keys) {
|
|
204
|
-
const childNodes = node[key];
|
|
205
|
-
const children = Array.isArray(childNodes) ? childNodes : childNodes ? [childNodes] : [];
|
|
206
|
-
const nodePaths = children.map((child, i) => {
|
|
207
|
-
if ((0, nodeutils_1.isNode)(child)) {
|
|
208
|
-
return createNodePath(child, Array.isArray(childNodes) ? i : key, key, nodePath.scopeId, nodePath.functionScopeId, nodePath);
|
|
209
|
-
}
|
|
210
|
-
return undefined;
|
|
211
|
-
}).filter(x => x != undefined);
|
|
212
|
-
nodePaths.forEach((childPath) => {
|
|
213
|
-
visitor.enter(childPath, state);
|
|
214
|
-
traverseInner(childPath.node, visitor, nodePath.scopeId, nodePath.functionScopeId, state, childPath);
|
|
215
|
-
visitor.exit(childPath, state);
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
const sOut = [];
|
|
220
|
-
function traverse(node, visitor, scopeId, state, path) {
|
|
221
|
-
const fscope = path?.functionScopeId ?? node.extra?.functionScopeId ?? scopeId;
|
|
222
|
-
traverseInner(node, visitor, scopeId, fscope, state, path);
|
|
223
|
-
if (!sOut.includes(scopeIdCounter)) {
|
|
224
|
-
log.debug("Scopes created", scopeIdCounter, " Scopes removed", removedScopes, "Paths created", pathsCreated, bindingNodesVisited);
|
|
225
|
-
sOut.push(scopeIdCounter);
|
|
226
|
-
const k = Object.fromEntries(Object.entries(nodePathsCreated).sort((a, b) => a[1] - b[1]));
|
|
227
|
-
log.debug("Node paths created", k);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
return {
|
|
231
|
-
traverse,
|
|
232
|
-
createNodePath,
|
|
233
|
-
getChildren,
|
|
234
|
-
getPrimitiveChildren,
|
|
235
|
-
getPrimitiveChildrenOrNodePaths,
|
|
236
|
-
getBinding
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
exports.default = createTraverser;
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { ESTree } from "meriyah";
|
|
2
|
-
import { PrimitiveValue } from ".";
|
|
3
|
-
export type Binding = {
|
|
4
|
-
path: NodePath;
|
|
5
|
-
};
|
|
6
|
-
export type Scope = {
|
|
7
|
-
bindings: Record<string, Binding>;
|
|
8
|
-
parentScopeId?: number;
|
|
9
|
-
id: number;
|
|
10
|
-
};
|
|
11
|
-
export type ASTNode = ESTree.Node & {
|
|
12
|
-
extra?: {
|
|
13
|
-
scopeId?: number;
|
|
14
|
-
functionScopeId?: number;
|
|
15
|
-
nodePath?: NodePath;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
export type NodePath = {
|
|
19
|
-
node: ASTNode;
|
|
20
|
-
key?: string;
|
|
21
|
-
parentPath?: NodePath;
|
|
22
|
-
parentKey?: string;
|
|
23
|
-
scopeId: number;
|
|
24
|
-
functionScopeId: number;
|
|
25
|
-
};
|
|
26
|
-
type Visitor<T> = {
|
|
27
|
-
enter: (path: NodePath, state: T) => void;
|
|
28
|
-
exit: (path: NodePath, state: T) => void;
|
|
29
|
-
};
|
|
30
|
-
export default function createTraverser(): {
|
|
31
|
-
traverse: <T>(node: ASTNode, visitor: Visitor<T>, scopeId: number | undefined, state: T, path?: NodePath) => void;
|
|
32
|
-
createNodePath: (node: ASTNode, key: string | undefined | number, parentKey: string | undefined, scopeId: number | undefined, functionScopeId: number | undefined, nodePath?: NodePath) => NodePath;
|
|
33
|
-
getChildren: (key: string, path: NodePath) => NodePath[];
|
|
34
|
-
getPrimitiveChildren: (key: string, path: NodePath) => PrimitiveValue[];
|
|
35
|
-
getPrimitiveChildrenOrNodePaths: (key: string, path: NodePath) => Array<PrimitiveValue | NodePath>;
|
|
36
|
-
getBinding: (scopeId: number, name: string) => Binding | undefined;
|
|
37
|
-
};
|
|
38
|
-
export {};
|
|
39
|
-
//# sourceMappingURL=traverse.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"traverse.d.ts","sourceRoot":"","sources":["../../../src/traverse.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC,OAAO,EAAE,cAAc,EAAE,MAAM,GAAG,CAAC;AASnC,MAAM,MAAM,OAAO,GAAG;IACpB,IAAI,EAAE,QAAQ,CAAC;CAChB,CAAA;AAED,MAAM,MAAM,KAAK,GAAG;IAClB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,EAAE,EAAE,MAAM,CAAC;CACZ,CAAC;AAIF,MAAM,MAAM,OAAO,GAAG,MAAM,CAA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