astronomical 1.0.0-beta.14 → 1.0.0-beta.16

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 CHANGED
@@ -75,7 +75,7 @@ function breadCrumb(path) {
75
75
  }
76
76
  function createQuerier() {
77
77
  const traverser = (0, traverse_1.default)();
78
- const { getChildren, getBinding, createNodePath, traverse } = traverser;
78
+ const { getChildren, getPrimitiveChildren, getPrimitiveChildrenOrNodePaths, getBinding, createNodePath, traverse } = traverser;
79
79
  function createFilter(filter, filterResult) {
80
80
  if (filter.type == "and" || filter.type == "or" || filter.type == "equals") {
81
81
  return {
@@ -190,20 +190,17 @@ function createQuerier() {
190
190
  return functionNode;
191
191
  }
192
192
  function addPrimitiveAttributeIfMatch(fnode, path) {
193
- if (!fnode.node.attribute || !fnode.node.value)
193
+ if (!fnode.node.attribute || fnode.node.value == undefined)
194
194
  return;
195
195
  if (fnode.node.child || fnode.node.filter)
196
196
  return;
197
197
  if (!Object.hasOwn(path.node, fnode.node.value))
198
198
  return;
199
- const lookup = getChildren(fnode.node.value, path);
200
- const nodes = toArray(lookup)
201
- .filter(n => n.node != undefined)
202
- .filter(n => (0, nodeutils_1.isPrimitive)(n.node));
199
+ const nodes = getPrimitiveChildren(fnode.node.value, path);
203
200
  if (nodes.length == 0)
204
201
  return;
205
- log.debug("PRIMITIVE", fnode.node.value, nodes.map(n => n.node));
206
- fnode.result.push(...nodes.map(n => n.node));
202
+ log.debug("PRIMITIVE", fnode.node.value, nodes);
203
+ fnode.result.push(...nodes);
207
204
  }
208
205
  function evaluateFilter(filter, path) {
209
206
  log.debug("EVALUATING FILTER", filter, breadCrumb(path));
@@ -260,7 +257,6 @@ function createQuerier() {
260
257
  }
261
258
  return resolveDirectly(startNode, startPath);
262
259
  }
263
- const toArray = (value) => Array.isArray(value) ? value : [value];
264
260
  function isDefined(value) {
265
261
  return value != undefined && value != null;
266
262
  }
@@ -273,31 +269,32 @@ function createQuerier() {
273
269
  const lookup = startNode.value;
274
270
  if (!lookup)
275
271
  throw new Error("Selector must have a value");
276
- log.debug("STEP IN ", lookup, paths.map(p => breadCrumb(p)));
277
- const nodes = paths.map(n => getChildren(lookup, n)).map(toArray).flat().filter(n => n.node != undefined);
278
- log.debug("LOOKUP", lookup, path.node.type, nodes.map(n => n.node), nodes.filter(n => n.node == undefined));
272
+ //log.debug("STEP IN ", lookup, paths.map(p => breadCrumb(p)));
273
+ const nodes = paths.filter(nodeutils_1.isNodePath).map(n => getPrimitiveChildrenOrNodePaths(lookup, n)).flat();
274
+ //log.debug("LOOKUP", lookup, path.node.type, nodes.map(n => n.node));
275
+ //console.log(nodes);
279
276
  if (nodes.length == 0)
280
277
  return [];
281
278
  paths = nodes;
282
279
  if (startNode.resolve) {
283
- const resolved = paths.map(p => resolveBinding(p)).filter(isDefined).map(p => getChildren("init", p)).flatMap(toArray).filter(p => p.node != undefined).filter(isDefined);
280
+ const resolved = paths.filter(nodeutils_1.isNodePath).map(p => resolveBinding(p)).filter(isDefined).map(p => getChildren("init", p)).flat();
284
281
  if (resolved.length > 0)
285
282
  paths = resolved;
286
283
  }
287
284
  else if (startNode.binding) {
288
- paths = paths.map(p => resolveBinding(p)).filter(isDefined);
285
+ paths = paths.filter(nodeutils_1.isNodePath).map(p => resolveBinding(p)).filter(isDefined);
289
286
  }
290
287
  const filter = startNode.filter;
291
288
  if (filter) {
292
- paths = paths.filter(p => travHandle({ subquery: filter }, p).subquery.length > 0);
289
+ paths = paths.filter(nodeutils_1.isNodePath).filter(p => travHandle({ subquery: filter }, p).subquery.length > 0);
293
290
  }
294
291
  if (!startNode.child) {
295
- return paths.map(p => p.node);
292
+ return paths.map(p => (0, nodeutils_1.isPrimitive)(p) ? p : p.node);
296
293
  }
297
294
  startNode = startNode.child;
298
295
  }
299
- log.debug("DIRECT TRAV RESOLVE", startNode, paths.map(p => breadCrumb(p)));
300
- const result = paths.flatMap(path => {
296
+ //log.debug("DIRECT TRAV RESOLVE", startNode, paths.map(p => breadCrumb(p)));
297
+ const result = paths.filter(nodeutils_1.isNodePath).flatMap(path => {
301
298
  const subQueryKey = "subquery-" + subQueryCounter++;
302
299
  return travHandle({ [subQueryKey]: startNode }, path)[subQueryKey];
303
300
  });
@@ -406,7 +403,7 @@ function createQuerier() {
406
403
  return results;
407
404
  }
408
405
  function beginHandle(queries, path) {
409
- const rootPath = createNodePath(path, undefined, undefined, undefined);
406
+ const rootPath = createNodePath(path, undefined, undefined, undefined, undefined);
410
407
  return travHandle(queries, rootPath);
411
408
  }
412
409
  return {
@@ -1,14 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isExportSpecifier = exports.isScopable = exports.isScope = exports.VISITOR_KEYS = exports.isBinding = exports.isVariableDeclarator = exports.isFunctionExpression = exports.isFunctionDeclaration = exports.isIdentifier = exports.isMemberExpression = exports.isAssignmentExpression = exports.isPrimitive = exports.isNode = void 0;
3
+ exports.isExportSpecifier = exports.isScopable = exports.isScope = exports.VISITOR_KEYS = exports.isBinding = exports.isVariableDeclaration = exports.isVariableDeclarator = exports.isFunctionExpression = exports.isFunctionDeclaration = exports.isIdentifier = exports.isMemberExpression = exports.isAssignmentExpression = exports.isUpdateExpression = exports.isPrimitive = exports.isLiteral = exports.isNodePath = exports.isNode = void 0;
4
4
  function isNode(candidate) {
5
5
  return typeof candidate === "object" && candidate != null && "type" in candidate;
6
6
  }
7
7
  exports.isNode = isNode;
8
+ function isNodePath(candidate) {
9
+ return typeof candidate === "object" && candidate != null && "node" in candidate;
10
+ }
11
+ exports.isNodePath = isNodePath;
12
+ function isLiteral(candidate) {
13
+ return isNode(candidate) && candidate.type === "Literal";
14
+ }
15
+ exports.isLiteral = isLiteral;
8
16
  function isPrimitive(value) {
9
17
  return typeof value == "string" || typeof value == "number" || typeof value == "boolean";
10
18
  }
11
19
  exports.isPrimitive = isPrimitive;
20
+ function isUpdateExpression(value) {
21
+ return isNode(value) && value.type === "UpdateExpression";
22
+ }
23
+ exports.isUpdateExpression = isUpdateExpression;
12
24
  function isAssignmentExpression(node) {
13
25
  return node.type === "AssignmentExpression";
14
26
  }
@@ -33,6 +45,10 @@ function isVariableDeclarator(node) {
33
45
  return node.type === "VariableDeclarator";
34
46
  }
35
47
  exports.isVariableDeclarator = isVariableDeclarator;
48
+ function isVariableDeclaration(node) {
49
+ return node.type === "VariableDeclaration";
50
+ }
51
+ exports.isVariableDeclaration = isVariableDeclaration;
36
52
  function isBinding(node, parentNode, grandParentNode) {
37
53
  if (grandParentNode &&
38
54
  node.type === "Identifier" &&
@@ -41,20 +57,18 @@ function isBinding(node, parentNode, grandParentNode) {
41
57
  return false;
42
58
  }
43
59
  const keys = bindingIdentifiersKeys[parentNode.type] ?? [];
44
- if (keys) {
45
- for (let i = 0; i < keys.length; i++) {
46
- const key = keys[i];
47
- const val =
48
- // @ts-expect-error key must present in parent
49
- parentNode[key];
50
- if (Array.isArray(val)) {
51
- if (val.indexOf(node) >= 0)
52
- return true;
53
- }
54
- else {
55
- if (val === node)
56
- return true;
57
- }
60
+ for (let i = 0; i < keys.length; i++) {
61
+ const key = keys[i];
62
+ const val =
63
+ // @ts-expect-error key must present in parent
64
+ parentNode[key];
65
+ if (Array.isArray(val)) {
66
+ if (val.indexOf(node) >= 0)
67
+ return true;
68
+ }
69
+ else {
70
+ if (val === node)
71
+ return true;
58
72
  }
59
73
  }
60
74
  return false;
@@ -13,6 +13,7 @@ const scopes = new Array(100000);
13
13
  function createTraverser() {
14
14
  let scopeIdCounter = 0;
15
15
  let removedScopes = 0;
16
+ const nodePathsCreated = {};
16
17
  function createScope(parentScopeId) {
17
18
  const id = scopeIdCounter++;
18
19
  scopes[id] = parentScopeId ?? -1;
@@ -26,7 +27,7 @@ function createTraverser() {
26
27
  return getBinding(scope, name);
27
28
  }
28
29
  const s = scope.bindings[name];
29
- if (s)
30
+ if (s != undefined)
30
31
  return s;
31
32
  if (scope.parentScopeId != undefined && scope.parentScopeId >= 0) {
32
33
  return getBinding(scope.parentScopeId, name);
@@ -54,28 +55,64 @@ function createTraverser() {
54
55
  if (key in path.node) {
55
56
  const r = path.node[key];
56
57
  if (Array.isArray(r)) {
57
- return r.map((n, i) => createNodePath(n, i.toString(), key, path.scopeId, path));
58
+ return r.map((n, i) => createNodePath(n, i, key, path.scopeId, path.functionScopeId, path));
58
59
  }
59
60
  else if (r != undefined) {
60
- return [createNodePath(r, key, key, path.scopeId, path)];
61
+ return [createNodePath(r, key, key, path.scopeId, path.functionScopeId, path)];
61
62
  }
62
63
  }
63
64
  return [];
64
65
  }
65
- function createNodePath(node, key, parentKey, scopeId, nodePath) {
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) {
66
92
  if (node.extra?.nodePath) {
67
93
  const path = node.extra.nodePath;
68
- path.key = key;
69
- path.parentKey = parentKey;
70
- path.parentPath = 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;
71
106
  return path;
72
107
  }
73
- const finalScope = ((node.extra && node.extra["scopeId"]) ? node.extra["scopeId"] : scopeId) ?? createScope();
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;
74
110
  const path = {
75
111
  node,
76
112
  scopeId: finalScope,
113
+ functionScopeId: finalFScope,
77
114
  parentPath: nodePath,
78
- key,
115
+ key: typeof (key) == "number" ? key.toString() : key,
79
116
  parentKey
80
117
  };
81
118
  if ((0, nodeutils_1.isNode)(node)) {
@@ -83,51 +120,74 @@ function createTraverser() {
83
120
  node.extra.nodePath = path;
84
121
  Object.defineProperty(node.extra, "nodePath", { enumerable: false });
85
122
  }
123
+ nodePathsCreated[node.type] = (nodePathsCreated[node.type] ?? 0) + 1;
86
124
  pathsCreated++;
87
125
  return path;
88
126
  }
89
- function registerBinding(node, parentNode, grandParentNode, scopeId) {
127
+ function registerBinding(stack, scopeId, functionScopeId, key, parentKey) {
90
128
  //console.log("x registerBinding?", isIdentifier(node) ? node.name : node.type, parentNode.type, grandParentNode?.type, scopeId, isBinding(node, parentNode, grandParentNode));
91
- if ((0, nodeutils_1.isBinding)(node, parentNode, grandParentNode)) {
92
- if ((0, nodeutils_1.isIdentifier)(node) && !(0, nodeutils_1.isAssignmentExpression)(parentNode) && !(0, nodeutils_1.isMemberExpression)(parentNode)) {
93
- //console.log("x registerBinding!", node.name, parentNode.type, grandParentNode?.type, scopeId);
94
- //A bit of a hack here as well. Needs some further investigation
95
- if ((0, nodeutils_1.isScope)(node, parentNode)) {
96
- setBinding(scopeId, node.name, { path: createNodePath(node, undefined, undefined, scopeId) });
97
- }
98
- else {
99
- setBinding(scopeId, node.name, { path: createNodePath(parentNode, undefined, undefined, scopeId) });
100
- }
101
- }
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;
102
141
  }
103
- }
104
- function registerBindings(node, parentNode, grandParentNode, scopeId) {
105
- if (typeof node == "object" && node != null) {
106
- node.extra = node.extra ?? {};
107
- if (node.extra["scopeId"])
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) });
108
145
  return;
109
- node.extra["scopeId"] = scopeId;
146
+ }
147
+ else {
148
+ setBinding(scopeId, node.name, { path: createNodePath(parentNode, undefined, undefined, scopeId, functionScopeId) });
149
+ return;
150
+ }
110
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++;
111
168
  const keys = nodeutils_1.VISITOR_KEYS[node.type];
112
- //console.log(keys, node);
113
169
  if (keys.length == 0)
114
170
  return;
115
171
  let childScopeId = scopeId;
116
- // This is also buggy. Need to investigate what creates a new scope
117
172
  if ((0, nodeutils_1.isScopable)(node)) {
118
173
  childScopeId = createScope(scopeId);
119
174
  }
120
175
  for (const key of keys) {
121
176
  const childNodes = node[key];
122
177
  const children = (0, utils_1.toArray)(childNodes).filter(utils_1.isDefined);
123
- children.forEach((child) => {
124
- if ((0, nodeutils_1.isNode)(child)) {
125
- // This feels like a hack. Need to figure out how to make this work
126
- // for other types of scopes as well (classes, etc.)
127
- const s = key == "id" ? scopeId : childScopeId;
128
- registerBinding(child, node, parentNode, s);
129
- registerBindings(child, node, parentNode, s);
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);
130
189
  }
190
+ stack.pop();
131
191
  });
132
192
  }
133
193
  if (childScopeId != scopeId && typeof scopes[childScopeId] == "number") { // Scope has not been populated
@@ -135,39 +195,44 @@ function createTraverser() {
135
195
  removedScopes++;
136
196
  }
137
197
  }
138
- function traverseInner(node, visitor, scopeId, state, path) {
139
- const nodePath = path ?? createNodePath(node, undefined, undefined, scopeId);
198
+ function traverseInner(node, visitor, scopeId, functionScopeId, state, path) {
199
+ const nodePath = path ?? createNodePath(node, undefined, undefined, scopeId, functionScopeId);
140
200
  const keys = nodeutils_1.VISITOR_KEYS[node.type] ?? [];
141
201
  if (nodePath.parentPath)
142
- registerBindings(nodePath.node, nodePath.parentPath.node, nodePath.parentPath.parentPath?.node, nodePath.scopeId);
202
+ registerBindings([nodePath.parentPath.parentPath?.node, nodePath.parentPath.node, nodePath.node].filter(utils_1.isDefined), nodePath.scopeId, nodePath.functionScopeId);
143
203
  for (const key of keys) {
144
204
  const childNodes = node[key];
145
205
  const children = Array.isArray(childNodes) ? childNodes : childNodes ? [childNodes] : [];
146
206
  const nodePaths = children.map((child, i) => {
147
207
  if ((0, nodeutils_1.isNode)(child)) {
148
- return createNodePath(child, key, Array.isArray(childNodes) ? i.toString() : key, nodePath.scopeId, nodePath);
208
+ return createNodePath(child, Array.isArray(childNodes) ? i : key, key, nodePath.scopeId, nodePath.functionScopeId, nodePath);
149
209
  }
150
210
  return undefined;
151
211
  }).filter(x => x != undefined);
152
212
  nodePaths.forEach((childPath) => {
153
213
  visitor.enter(childPath, state);
154
- traverseInner(childPath.node, visitor, nodePath.scopeId, state, childPath);
214
+ traverseInner(childPath.node, visitor, nodePath.scopeId, nodePath.functionScopeId, state, childPath);
155
215
  visitor.exit(childPath, state);
156
216
  });
157
217
  }
158
218
  }
159
219
  const sOut = [];
160
220
  function traverse(node, visitor, scopeId, state, path) {
161
- traverseInner(node, visitor, scopeId, state, path);
221
+ const fscope = path?.functionScopeId ?? node.extra?.functionScopeId ?? scopeId;
222
+ traverseInner(node, visitor, scopeId, fscope, state, path);
162
223
  if (!sOut.includes(scopeIdCounter)) {
163
- log.debug("Scopes created", scopeIdCounter, " Scopes removed", removedScopes, "Paths created", pathsCreated);
224
+ log.debug("Scopes created", scopeIdCounter, " Scopes removed", removedScopes, "Paths created", pathsCreated, bindingNodesVisited);
164
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);
165
228
  }
166
229
  }
167
230
  return {
168
231
  traverse,
169
232
  createNodePath,
170
233
  getChildren,
234
+ getPrimitiveChildren,
235
+ getPrimitiveChildrenOrNodePaths,
171
236
  getBinding
172
237
  };
173
238
  }
@@ -15,7 +15,8 @@ export declare const functions: {
15
15
  };
16
16
  export type AvailableFunction = keyof typeof functions;
17
17
  export declare function isAvailableFunction(name: string): name is AvailableFunction;
18
- type Result = ASTNode | string | number | boolean;
18
+ export type PrimitiveValue = string | number | boolean;
19
+ type Result = ASTNode | PrimitiveValue;
19
20
  export declare function query(code: string | ASTNode, query: string, returnAST?: boolean): Result[] & {
20
21
  __AST?: ASTNode;
21
22
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAwB,EAAG,OAAO,EAAY,MAAM,YAAY,CAAC;AAajE,eAAO,MAAM,SAAS;;qBAEL,MAAM,EAAE,EAAE,KAAG,MAAM,EAAE;;;qBAWrB,MAAM,EAAE,EAAE,KAAG,MAAM,EAAE;;;qBAMrB,MAAM,EAAE,EAAE,KAAG,MAAM,EAAE;;;qBAOrB,MAAM,EAAE,EAAE,KAAG,MAAM,EAAE;;CASrC,CAAA;AAED,MAAM,MAAM,iBAAiB,GAAG,MAAM,OAAO,SAAS,CAAC;AACvD,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAI,IAAI,IAAI,iBAAiB,CAE5E;AAID,KAAK,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AA+XlD,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,OAAO,GAAI,MAAM,EAAE,GAAG;IAAE,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,CAQjH;AAED,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,GAA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GAAG;IAAE,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,CAY3K;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAI,OAAO,CAMpD"}
@@ -1,13 +1,18 @@
1
1
  import { ESTree } from "meriyah";
2
- import { ASTNode } from "./traverse";
2
+ import { ASTNode, NodePath } from "./traverse";
3
+ import { PrimitiveValue } from ".";
3
4
  export declare function isNode(candidate: unknown): candidate is ASTNode;
4
- export declare function isPrimitive(value: unknown): boolean;
5
+ export declare function isNodePath(candidate: unknown): candidate is NodePath;
6
+ export declare function isLiteral(candidate: unknown): candidate is ESTree.Literal;
7
+ export declare function isPrimitive(value: unknown): value is PrimitiveValue;
8
+ export declare function isUpdateExpression(value: unknown): value is ESTree.UpdateExpression;
5
9
  export declare function isAssignmentExpression(node: ESTree.Node): node is ESTree.AssignmentExpression;
6
10
  export declare function isMemberExpression(node: ESTree.Node): node is ESTree.MemberExpression;
7
11
  export declare function isIdentifier(node: ESTree.Node): node is ESTree.Identifier;
8
12
  export declare function isFunctionDeclaration(node: ESTree.Node): node is ESTree.FunctionDeclaration;
9
13
  export declare function isFunctionExpression(node: ESTree.Node): node is ESTree.FunctionExpression;
10
14
  export declare function isVariableDeclarator(node: ESTree.Node): node is ESTree.VariableDeclarator;
15
+ export declare function isVariableDeclaration(node: ESTree.Node): node is ESTree.VariableDeclaration;
11
16
  export declare function isBinding(node: ESTree.Node, parentNode: ESTree.Node, grandParentNode: ESTree.Node | undefined): boolean;
12
17
  export declare const VISITOR_KEYS: Record<ESTree.Node["type"], string[]>;
13
18
  export declare function isScope(node: ESTree.Node, parentNode: ESTree.Node): boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"nodeutils.d.ts","sourceRoot":"","sources":["../../../src/nodeutils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAErC,wBAAgB,MAAM,CAA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B,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,GAAG,IAAI,IAAI,MAAM,CAAC,kBAAkB,CAEzF;AACD,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,GAAG,IAAI,IAAI,MAAM,CAAC,mBAAmB,CAE3F;AACD,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,CAAC,IAAI,EAAE,eAAe,EAAE,MAAM,CAAC,IAAI,GAAG,SAAS,GAAG,OAAO,CAwBvH;AAuDD,eAAO,MAAM,YAAY,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,CA0F9D,CAAC;AAqBF,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,CAAC,IAAI,GAAG,OAAO,CAS3E;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,GAAG,OAAO,CAqBrD;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,GAAG,IAAI,IAAI,MAAM,CAAC,eAAe,CAEnF"}
@@ -1,4 +1,5 @@
1
1
  import { ESTree } from "meriyah";
2
+ import { PrimitiveValue } from ".";
2
3
  export type Binding = {
3
4
  path: NodePath;
4
5
  };
@@ -10,6 +11,7 @@ export type Scope = {
10
11
  export type ASTNode = ESTree.Node & {
11
12
  extra?: {
12
13
  scopeId?: number;
14
+ functionScopeId?: number;
13
15
  nodePath?: NodePath;
14
16
  };
15
17
  };
@@ -19,6 +21,7 @@ export type NodePath = {
19
21
  parentPath?: NodePath;
20
22
  parentKey?: string;
21
23
  scopeId: number;
24
+ functionScopeId: number;
22
25
  };
23
26
  type Visitor<T> = {
24
27
  enter: (path: NodePath, state: T) => void;
@@ -26,8 +29,10 @@ type Visitor<T> = {
26
29
  };
27
30
  export default function createTraverser(): {
28
31
  traverse: <T>(node: ASTNode, visitor: Visitor<T>, scopeId: number | undefined, state: T, path?: NodePath) => void;
29
- createNodePath: (node: ASTNode, key: string | undefined, parentKey: string | undefined, scopeId: number | undefined, nodePath?: NodePath) => NodePath;
32
+ createNodePath: (node: ASTNode, key: string | undefined | number, parentKey: string | undefined, scopeId: number | undefined, functionScopeId: number | undefined, nodePath?: NodePath) => NodePath;
30
33
  getChildren: (key: string, path: NodePath) => NodePath[];
34
+ getPrimitiveChildren: (key: string, path: NodePath) => PrimitiveValue[];
35
+ getPrimitiveChildrenOrNodePaths: (key: string, path: NodePath) => Array<PrimitiveValue | NodePath>;
31
36
  getBinding: (scopeId: number, name: string) => Binding | undefined;
32
37
  };
33
38
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"traverse.d.ts","sourceRoot":"","sources":["../../../src/traverse.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAUjC,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,CAAC,IAAI,GAAG;IAClC,KAAK,CAAC,EAAE;QACN,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,QAAQ,CAAC,EAAE,QAAQ,CAAC;KACrB,CAAA;CACF,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IACrB,IAAI,EAAE,OAAO,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,QAAQ,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,KAAK,OAAO,CAAC,CAA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CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,KAAK,OAAO,CAAC,CAAC,IAAI;IAChB,KAAK,EAAE,CAAC,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;IAC1C,IAAI,EAAE,CAAC,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;CAC1C,CAAA;AAED,MAAM,CAAC,OAAO,UAAU,eAAe;wBA4NR,OAAO,gCAEzB,MAAM,GAAG,SAAS,mBAEpB,QAAQ;2BAhJa,OAAO,OAAO,MAAM,GAAG,SAAS,GAAG,MAAM,aAAa,MAAM,GAAG,SAAS,WAAW,MAAM,GAAG,SAAS,mBAAmB,MAAM,GAAG,SAAS,aAAa,QAAQ,KAAI,QAAQ;uBAtC/K,MAAM,QAAQ,QAAQ,KAAI,QAAQ,EAAE;gCAW3B,MAAM,QAAQ,QAAQ,KAAI,cAAc,EAAE;2CAO/B,MAAM,QAAQ,QAAQ,KAAI,MAAM,cAAc,GAAG,QAAQ,CAAC;0BAjD3E,MAAM,QAAQ,MAAM;EAyOlD"}
package/lib/esm/index.mjs CHANGED
@@ -75,7 +75,7 @@ function breadCrumb(path) {
75
75
  }
76
76
  function createQuerier() {
77
77
  const traverser = (0, traverse_1.default)();
78
- const { getChildren, getBinding, createNodePath, traverse } = traverser;
78
+ const { getChildren, getPrimitiveChildren, getPrimitiveChildrenOrNodePaths, getBinding, createNodePath, traverse } = traverser;
79
79
  function createFilter(filter, filterResult) {
80
80
  if (filter.type == "and" || filter.type == "or" || filter.type == "equals") {
81
81
  return {
@@ -190,20 +190,17 @@ function createQuerier() {
190
190
  return functionNode;
191
191
  }
192
192
  function addPrimitiveAttributeIfMatch(fnode, path) {
193
- if (!fnode.node.attribute || !fnode.node.value)
193
+ if (!fnode.node.attribute || fnode.node.value == undefined)
194
194
  return;
195
195
  if (fnode.node.child || fnode.node.filter)
196
196
  return;
197
197
  if (!Object.hasOwn(path.node, fnode.node.value))
198
198
  return;
199
- const lookup = getChildren(fnode.node.value, path);
200
- const nodes = toArray(lookup)
201
- .filter(n => n.node != undefined)
202
- .filter(n => (0, nodeutils_1.isPrimitive)(n.node));
199
+ const nodes = getPrimitiveChildren(fnode.node.value, path);
203
200
  if (nodes.length == 0)
204
201
  return;
205
- log.debug("PRIMITIVE", fnode.node.value, nodes.map(n => n.node));
206
- fnode.result.push(...nodes.map(n => n.node));
202
+ log.debug("PRIMITIVE", fnode.node.value, nodes);
203
+ fnode.result.push(...nodes);
207
204
  }
208
205
  function evaluateFilter(filter, path) {
209
206
  log.debug("EVALUATING FILTER", filter, breadCrumb(path));
@@ -260,7 +257,6 @@ function createQuerier() {
260
257
  }
261
258
  return resolveDirectly(startNode, startPath);
262
259
  }
263
- const toArray = (value) => Array.isArray(value) ? value : [value];
264
260
  function isDefined(value) {
265
261
  return value != undefined && value != null;
266
262
  }
@@ -273,31 +269,32 @@ function createQuerier() {
273
269
  const lookup = startNode.value;
274
270
  if (!lookup)
275
271
  throw new Error("Selector must have a value");
276
- log.debug("STEP IN ", lookup, paths.map(p => breadCrumb(p)));
277
- const nodes = paths.map(n => getChildren(lookup, n)).map(toArray).flat().filter(n => n.node != undefined);
278
- log.debug("LOOKUP", lookup, path.node.type, nodes.map(n => n.node), nodes.filter(n => n.node == undefined));
272
+ //log.debug("STEP IN ", lookup, paths.map(p => breadCrumb(p)));
273
+ const nodes = paths.filter(nodeutils_1.isNodePath).map(n => getPrimitiveChildrenOrNodePaths(lookup, n)).flat();
274
+ //log.debug("LOOKUP", lookup, path.node.type, nodes.map(n => n.node));
275
+ //console.log(nodes);
279
276
  if (nodes.length == 0)
280
277
  return [];
281
278
  paths = nodes;
282
279
  if (startNode.resolve) {
283
- const resolved = paths.map(p => resolveBinding(p)).filter(isDefined).map(p => getChildren("init", p)).flatMap(toArray).filter(p => p.node != undefined).filter(isDefined);
280
+ const resolved = paths.filter(nodeutils_1.isNodePath).map(p => resolveBinding(p)).filter(isDefined).map(p => getChildren("init", p)).flat();
284
281
  if (resolved.length > 0)
285
282
  paths = resolved;
286
283
  }
287
284
  else if (startNode.binding) {
288
- paths = paths.map(p => resolveBinding(p)).filter(isDefined);
285
+ paths = paths.filter(nodeutils_1.isNodePath).map(p => resolveBinding(p)).filter(isDefined);
289
286
  }
290
287
  const filter = startNode.filter;
291
288
  if (filter) {
292
- paths = paths.filter(p => travHandle({ subquery: filter }, p).subquery.length > 0);
289
+ paths = paths.filter(nodeutils_1.isNodePath).filter(p => travHandle({ subquery: filter }, p).subquery.length > 0);
293
290
  }
294
291
  if (!startNode.child) {
295
- return paths.map(p => p.node);
292
+ return paths.map(p => (0, nodeutils_1.isPrimitive)(p) ? p : p.node);
296
293
  }
297
294
  startNode = startNode.child;
298
295
  }
299
- log.debug("DIRECT TRAV RESOLVE", startNode, paths.map(p => breadCrumb(p)));
300
- const result = paths.flatMap(path => {
296
+ //log.debug("DIRECT TRAV RESOLVE", startNode, paths.map(p => breadCrumb(p)));
297
+ const result = paths.filter(nodeutils_1.isNodePath).flatMap(path => {
301
298
  const subQueryKey = "subquery-" + subQueryCounter++;
302
299
  return travHandle({ [subQueryKey]: startNode }, path)[subQueryKey];
303
300
  });
@@ -406,7 +403,7 @@ function createQuerier() {
406
403
  return results;
407
404
  }
408
405
  function beginHandle(queries, path) {
409
- const rootPath = createNodePath(path, undefined, undefined, undefined);
406
+ const rootPath = createNodePath(path, undefined, undefined, undefined, undefined);
410
407
  return travHandle(queries, rootPath);
411
408
  }
412
409
  return {
@@ -1,14 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isExportSpecifier = exports.isScopable = exports.isScope = exports.VISITOR_KEYS = exports.isBinding = exports.isVariableDeclarator = exports.isFunctionExpression = exports.isFunctionDeclaration = exports.isIdentifier = exports.isMemberExpression = exports.isAssignmentExpression = exports.isPrimitive = exports.isNode = void 0;
3
+ exports.isExportSpecifier = exports.isScopable = exports.isScope = exports.VISITOR_KEYS = exports.isBinding = exports.isVariableDeclaration = exports.isVariableDeclarator = exports.isFunctionExpression = exports.isFunctionDeclaration = exports.isIdentifier = exports.isMemberExpression = exports.isAssignmentExpression = exports.isUpdateExpression = exports.isPrimitive = exports.isLiteral = exports.isNodePath = exports.isNode = void 0;
4
4
  function isNode(candidate) {
5
5
  return typeof candidate === "object" && candidate != null && "type" in candidate;
6
6
  }
7
7
  exports.isNode = isNode;
8
+ function isNodePath(candidate) {
9
+ return typeof candidate === "object" && candidate != null && "node" in candidate;
10
+ }
11
+ exports.isNodePath = isNodePath;
12
+ function isLiteral(candidate) {
13
+ return isNode(candidate) && candidate.type === "Literal";
14
+ }
15
+ exports.isLiteral = isLiteral;
8
16
  function isPrimitive(value) {
9
17
  return typeof value == "string" || typeof value == "number" || typeof value == "boolean";
10
18
  }
11
19
  exports.isPrimitive = isPrimitive;
20
+ function isUpdateExpression(value) {
21
+ return isNode(value) && value.type === "UpdateExpression";
22
+ }
23
+ exports.isUpdateExpression = isUpdateExpression;
12
24
  function isAssignmentExpression(node) {
13
25
  return node.type === "AssignmentExpression";
14
26
  }
@@ -33,6 +45,10 @@ function isVariableDeclarator(node) {
33
45
  return node.type === "VariableDeclarator";
34
46
  }
35
47
  exports.isVariableDeclarator = isVariableDeclarator;
48
+ function isVariableDeclaration(node) {
49
+ return node.type === "VariableDeclaration";
50
+ }
51
+ exports.isVariableDeclaration = isVariableDeclaration;
36
52
  function isBinding(node, parentNode, grandParentNode) {
37
53
  if (grandParentNode &&
38
54
  node.type === "Identifier" &&
@@ -41,20 +57,18 @@ function isBinding(node, parentNode, grandParentNode) {
41
57
  return false;
42
58
  }
43
59
  const keys = bindingIdentifiersKeys[parentNode.type] ?? [];
44
- if (keys) {
45
- for (let i = 0; i < keys.length; i++) {
46
- const key = keys[i];
47
- const val =
48
- // @ts-expect-error key must present in parent
49
- parentNode[key];
50
- if (Array.isArray(val)) {
51
- if (val.indexOf(node) >= 0)
52
- return true;
53
- }
54
- else {
55
- if (val === node)
56
- return true;
57
- }
60
+ for (let i = 0; i < keys.length; i++) {
61
+ const key = keys[i];
62
+ const val =
63
+ // @ts-expect-error key must present in parent
64
+ parentNode[key];
65
+ if (Array.isArray(val)) {
66
+ if (val.indexOf(node) >= 0)
67
+ return true;
68
+ }
69
+ else {
70
+ if (val === node)
71
+ return true;
58
72
  }
59
73
  }
60
74
  return false;
@@ -13,6 +13,7 @@ const scopes = new Array(100000);
13
13
  function createTraverser() {
14
14
  let scopeIdCounter = 0;
15
15
  let removedScopes = 0;
16
+ const nodePathsCreated = {};
16
17
  function createScope(parentScopeId) {
17
18
  const id = scopeIdCounter++;
18
19
  scopes[id] = parentScopeId ?? -1;
@@ -26,7 +27,7 @@ function createTraverser() {
26
27
  return getBinding(scope, name);
27
28
  }
28
29
  const s = scope.bindings[name];
29
- if (s)
30
+ if (s != undefined)
30
31
  return s;
31
32
  if (scope.parentScopeId != undefined && scope.parentScopeId >= 0) {
32
33
  return getBinding(scope.parentScopeId, name);
@@ -54,28 +55,64 @@ function createTraverser() {
54
55
  if (key in path.node) {
55
56
  const r = path.node[key];
56
57
  if (Array.isArray(r)) {
57
- return r.map((n, i) => createNodePath(n, i.toString(), key, path.scopeId, path));
58
+ return r.map((n, i) => createNodePath(n, i, key, path.scopeId, path.functionScopeId, path));
58
59
  }
59
60
  else if (r != undefined) {
60
- return [createNodePath(r, key, key, path.scopeId, path)];
61
+ return [createNodePath(r, key, key, path.scopeId, path.functionScopeId, path)];
61
62
  }
62
63
  }
63
64
  return [];
64
65
  }
65
- function createNodePath(node, key, parentKey, scopeId, nodePath) {
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) {
66
92
  if (node.extra?.nodePath) {
67
93
  const path = node.extra.nodePath;
68
- path.key = key;
69
- path.parentKey = parentKey;
70
- path.parentPath = 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;
71
106
  return path;
72
107
  }
73
- const finalScope = ((node.extra && node.extra["scopeId"]) ? node.extra["scopeId"] : scopeId) ?? createScope();
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;
74
110
  const path = {
75
111
  node,
76
112
  scopeId: finalScope,
113
+ functionScopeId: finalFScope,
77
114
  parentPath: nodePath,
78
- key,
115
+ key: typeof (key) == "number" ? key.toString() : key,
79
116
  parentKey
80
117
  };
81
118
  if ((0, nodeutils_1.isNode)(node)) {
@@ -83,51 +120,74 @@ function createTraverser() {
83
120
  node.extra.nodePath = path;
84
121
  Object.defineProperty(node.extra, "nodePath", { enumerable: false });
85
122
  }
123
+ nodePathsCreated[node.type] = (nodePathsCreated[node.type] ?? 0) + 1;
86
124
  pathsCreated++;
87
125
  return path;
88
126
  }
89
- function registerBinding(node, parentNode, grandParentNode, scopeId) {
127
+ function registerBinding(stack, scopeId, functionScopeId, key, parentKey) {
90
128
  //console.log("x registerBinding?", isIdentifier(node) ? node.name : node.type, parentNode.type, grandParentNode?.type, scopeId, isBinding(node, parentNode, grandParentNode));
91
- if ((0, nodeutils_1.isBinding)(node, parentNode, grandParentNode)) {
92
- if ((0, nodeutils_1.isIdentifier)(node) && !(0, nodeutils_1.isAssignmentExpression)(parentNode) && !(0, nodeutils_1.isMemberExpression)(parentNode)) {
93
- //console.log("x registerBinding!", node.name, parentNode.type, grandParentNode?.type, scopeId);
94
- //A bit of a hack here as well. Needs some further investigation
95
- if ((0, nodeutils_1.isScope)(node, parentNode)) {
96
- setBinding(scopeId, node.name, { path: createNodePath(node, undefined, undefined, scopeId) });
97
- }
98
- else {
99
- setBinding(scopeId, node.name, { path: createNodePath(parentNode, undefined, undefined, scopeId) });
100
- }
101
- }
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;
102
141
  }
103
- }
104
- function registerBindings(node, parentNode, grandParentNode, scopeId) {
105
- if (typeof node == "object" && node != null) {
106
- node.extra = node.extra ?? {};
107
- if (node.extra["scopeId"])
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) });
108
145
  return;
109
- node.extra["scopeId"] = scopeId;
146
+ }
147
+ else {
148
+ setBinding(scopeId, node.name, { path: createNodePath(parentNode, undefined, undefined, scopeId, functionScopeId) });
149
+ return;
150
+ }
110
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++;
111
168
  const keys = nodeutils_1.VISITOR_KEYS[node.type];
112
- //console.log(keys, node);
113
169
  if (keys.length == 0)
114
170
  return;
115
171
  let childScopeId = scopeId;
116
- // This is also buggy. Need to investigate what creates a new scope
117
172
  if ((0, nodeutils_1.isScopable)(node)) {
118
173
  childScopeId = createScope(scopeId);
119
174
  }
120
175
  for (const key of keys) {
121
176
  const childNodes = node[key];
122
177
  const children = (0, utils_1.toArray)(childNodes).filter(utils_1.isDefined);
123
- children.forEach((child) => {
124
- if ((0, nodeutils_1.isNode)(child)) {
125
- // This feels like a hack. Need to figure out how to make this work
126
- // for other types of scopes as well (classes, etc.)
127
- const s = key == "id" ? scopeId : childScopeId;
128
- registerBinding(child, node, parentNode, s);
129
- registerBindings(child, node, parentNode, s);
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);
130
189
  }
190
+ stack.pop();
131
191
  });
132
192
  }
133
193
  if (childScopeId != scopeId && typeof scopes[childScopeId] == "number") { // Scope has not been populated
@@ -135,39 +195,44 @@ function createTraverser() {
135
195
  removedScopes++;
136
196
  }
137
197
  }
138
- function traverseInner(node, visitor, scopeId, state, path) {
139
- const nodePath = path ?? createNodePath(node, undefined, undefined, scopeId);
198
+ function traverseInner(node, visitor, scopeId, functionScopeId, state, path) {
199
+ const nodePath = path ?? createNodePath(node, undefined, undefined, scopeId, functionScopeId);
140
200
  const keys = nodeutils_1.VISITOR_KEYS[node.type] ?? [];
141
201
  if (nodePath.parentPath)
142
- registerBindings(nodePath.node, nodePath.parentPath.node, nodePath.parentPath.parentPath?.node, nodePath.scopeId);
202
+ registerBindings([nodePath.parentPath.parentPath?.node, nodePath.parentPath.node, nodePath.node].filter(utils_1.isDefined), nodePath.scopeId, nodePath.functionScopeId);
143
203
  for (const key of keys) {
144
204
  const childNodes = node[key];
145
205
  const children = Array.isArray(childNodes) ? childNodes : childNodes ? [childNodes] : [];
146
206
  const nodePaths = children.map((child, i) => {
147
207
  if ((0, nodeutils_1.isNode)(child)) {
148
- return createNodePath(child, key, Array.isArray(childNodes) ? i.toString() : key, nodePath.scopeId, nodePath);
208
+ return createNodePath(child, Array.isArray(childNodes) ? i : key, key, nodePath.scopeId, nodePath.functionScopeId, nodePath);
149
209
  }
150
210
  return undefined;
151
211
  }).filter(x => x != undefined);
152
212
  nodePaths.forEach((childPath) => {
153
213
  visitor.enter(childPath, state);
154
- traverseInner(childPath.node, visitor, nodePath.scopeId, state, childPath);
214
+ traverseInner(childPath.node, visitor, nodePath.scopeId, nodePath.functionScopeId, state, childPath);
155
215
  visitor.exit(childPath, state);
156
216
  });
157
217
  }
158
218
  }
159
219
  const sOut = [];
160
220
  function traverse(node, visitor, scopeId, state, path) {
161
- traverseInner(node, visitor, scopeId, state, path);
221
+ const fscope = path?.functionScopeId ?? node.extra?.functionScopeId ?? scopeId;
222
+ traverseInner(node, visitor, scopeId, fscope, state, path);
162
223
  if (!sOut.includes(scopeIdCounter)) {
163
- log.debug("Scopes created", scopeIdCounter, " Scopes removed", removedScopes, "Paths created", pathsCreated);
224
+ log.debug("Scopes created", scopeIdCounter, " Scopes removed", removedScopes, "Paths created", pathsCreated, bindingNodesVisited);
164
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);
165
228
  }
166
229
  }
167
230
  return {
168
231
  traverse,
169
232
  createNodePath,
170
233
  getChildren,
234
+ getPrimitiveChildren,
235
+ getPrimitiveChildrenOrNodePaths,
171
236
  getBinding
172
237
  };
173
238
  }
@@ -15,7 +15,8 @@ export declare const functions: {
15
15
  };
16
16
  export type AvailableFunction = keyof typeof functions;
17
17
  export declare function isAvailableFunction(name: string): name is AvailableFunction;
18
- type Result = ASTNode | string | number | boolean;
18
+ export type PrimitiveValue = string | number | boolean;
19
+ type Result = ASTNode | PrimitiveValue;
19
20
  export declare function query(code: string | ASTNode, query: string, returnAST?: boolean): Result[] & {
20
21
  __AST?: ASTNode;
21
22
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAwB,EAAG,OAAO,EAAY,MAAM,YAAY,CAAC;AAajE,eAAO,MAAM,SAAS;;qBAEL,MAAM,EAAE,EAAE,KA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C,CAAA;AAED,MAAM,MAAM,iBAAiB,GAAG,MAAM,OAAO,SAAS,CAAC;AACvD,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAI,IAAI,IAAI,iBAAiB,CAE5E;AAGD,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAEvD,KAAK,MAAM,GAAG,OAAO,GAAG,cAAc,CAAC;AA4XvC,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,OAAO,GAAI,MAAM,EAAE,GAAG;IAAE,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,CAQjH;AAED,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,EAAE,YAAY,EAAE,CAAC,EAAE,SAAS,CAAC,EAAE,OAAO,GAAI,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG;IAAE,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,CAY3K;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAI,OAAO,CAMpD"}
@@ -1,13 +1,18 @@
1
1
  import { ESTree } from "meriyah";
2
- import { ASTNode } from "./traverse";
2
+ import { ASTNode, NodePath } from "./traverse";
3
+ import { PrimitiveValue } from ".";
3
4
  export declare function isNode(candidate: unknown): candidate is ASTNode;
4
- export declare function isPrimitive(value: unknown): boolean;
5
+ export declare function isNodePath(candidate: unknown): candidate is NodePath;
6
+ export declare function isLiteral(candidate: unknown): candidate is ESTree.Literal;
7
+ export declare function isPrimitive(value: unknown): value is PrimitiveValue;
8
+ export declare function isUpdateExpression(value: unknown): value is ESTree.UpdateExpression;
5
9
  export declare function isAssignmentExpression(node: ESTree.Node): node is ESTree.AssignmentExpression;
6
10
  export declare function isMemberExpression(node: ESTree.Node): node is ESTree.MemberExpression;
7
11
  export declare function isIdentifier(node: ESTree.Node): node is ESTree.Identifier;
8
12
  export declare function isFunctionDeclaration(node: ESTree.Node): node is ESTree.FunctionDeclaration;
9
13
  export declare function isFunctionExpression(node: ESTree.Node): node is ESTree.FunctionExpression;
10
14
  export declare function isVariableDeclarator(node: ESTree.Node): node is ESTree.VariableDeclarator;
15
+ export declare function isVariableDeclaration(node: ESTree.Node): node is ESTree.VariableDeclaration;
11
16
  export declare function isBinding(node: ESTree.Node, parentNode: ESTree.Node, grandParentNode: ESTree.Node | undefined): boolean;
12
17
  export declare const VISITOR_KEYS: Record<ESTree.Node["type"], string[]>;
13
18
  export declare function isScope(node: ESTree.Node, parentNode: ESTree.Node): boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"nodeutils.d.ts","sourceRoot":"","sources":["../../../src/nodeutils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAErC,wBAAgB,MAAM,CAAC,SAAS,EAAE,OAAO,GAAI,SAAS,IAAI,OAAO,CAEhE;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAI,OAAO,CAEpD;AAED,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,GAAG,IAAI,IAAI,MAAM,CAAC,oBAAoB,CAE7F;AAED,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,GAAG,IAAI,IAAI,MAAM,CAAC,gBAAgB,CAErF;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,GAAG,IAAI,IAAI,MAAM,CAAC,UAAU,CAEzE;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,GAAG,IAAI,IAAI,MAAM,CAAC,mBAAmB,CAE3F;AACD,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,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S3E;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,GAAG,OAAO,CAqBrD;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,GAAG,IAAI,IAAI,MAAM,CAAC,eAAe,CAEnF"}
@@ -1,4 +1,5 @@
1
1
  import { ESTree } from "meriyah";
2
+ import { PrimitiveValue } from ".";
2
3
  export type Binding = {
3
4
  path: NodePath;
4
5
  };
@@ -10,6 +11,7 @@ export type Scope = {
10
11
  export type ASTNode = ESTree.Node & {
11
12
  extra?: {
12
13
  scopeId?: number;
14
+ functionScopeId?: number;
13
15
  nodePath?: NodePath;
14
16
  };
15
17
  };
@@ -19,6 +21,7 @@ export type NodePath = {
19
21
  parentPath?: NodePath;
20
22
  parentKey?: string;
21
23
  scopeId: number;
24
+ functionScopeId: number;
22
25
  };
23
26
  type Visitor<T> = {
24
27
  enter: (path: NodePath, state: T) => void;
@@ -26,8 +29,10 @@ type Visitor<T> = {
26
29
  };
27
30
  export default function createTraverser(): {
28
31
  traverse: <T>(node: ASTNode, visitor: Visitor<T>, scopeId: number | undefined, state: T, path?: NodePath) => void;
29
- createNodePath: (node: ASTNode, key: string | undefined, parentKey: string | undefined, scopeId: number | undefined, nodePath?: NodePath) => NodePath;
32
+ createNodePath: (node: ASTNode, key: string | undefined | number, parentKey: string | undefined, scopeId: number | undefined, functionScopeId: number | undefined, nodePath?: NodePath) => NodePath;
30
33
  getChildren: (key: string, path: NodePath) => NodePath[];
34
+ getPrimitiveChildren: (key: string, path: NodePath) => PrimitiveValue[];
35
+ getPrimitiveChildrenOrNodePaths: (key: string, path: NodePath) => Array<PrimitiveValue | NodePath>;
31
36
  getBinding: (scopeId: number, name: string) => Binding | undefined;
32
37
  };
33
38
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"traverse.d.ts","sourceRoot":"","sources":["../../../src/traverse.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,SAA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AI,QAAQ;uBAtC/K,MAAM,QAAQ,QAAQ,KAAI,QAAQ,EAAE;gCAW3B,MAAM,QAAQ,QAAQ,KAAI,cAAc,EAAE;2CAO/B,MAAM,QAAQ,QAAQ,KAAI,MAAM,cAAc,GAAG,QAAQ,CAAC;0BAjD3E,MAAM,QAAQ,MAAM;EAyOlD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astronomical",
3
- "version": "1.0.0-beta.14",
3
+ "version": "1.0.0-beta.16",
4
4
  "description": "offers a way to query a Javascript AST to find specific patterns using a syntax somewhat similar to XPath.",
5
5
  "scripts": {
6
6
  "lint": "eslint . --ext .ts --fix --ignore-path .gitignore",