astronomical 1.0.0 → 1.0.1

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.
@@ -1,55 +1,58 @@
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.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
- function isNode(candidate) {
3
+ 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
+ exports.isScope = isScope;
5
+ exports.isScopable = isScopable;
6
+ exports.isExportSpecifier = isExportSpecifier;
7
+ const isNode = (candidate) => {
5
8
  return typeof candidate === "object" && candidate != null && "type" in candidate;
6
- }
9
+ };
7
10
  exports.isNode = isNode;
8
- function isNodePath(candidate) {
11
+ const isNodePath = (candidate) => {
9
12
  return typeof candidate === "object" && candidate != null && "node" in candidate;
10
- }
13
+ };
11
14
  exports.isNodePath = isNodePath;
12
- function isLiteral(candidate) {
13
- return isNode(candidate) && candidate.type === "Literal";
14
- }
15
+ const isLiteral = (candidate) => {
16
+ return (0, exports.isNode)(candidate) && candidate.type === "Literal";
17
+ };
15
18
  exports.isLiteral = isLiteral;
16
- function isPrimitive(value) {
19
+ const isPrimitive = (value) => {
17
20
  return typeof value == "string" || typeof value == "number" || typeof value == "boolean";
18
- }
21
+ };
19
22
  exports.isPrimitive = isPrimitive;
20
- function isUpdateExpression(value) {
21
- return isNode(value) && value.type === "UpdateExpression";
22
- }
23
+ const isUpdateExpression = (value) => {
24
+ return (0, exports.isNode)(value) && value.type === "UpdateExpression";
25
+ };
23
26
  exports.isUpdateExpression = isUpdateExpression;
24
- function isAssignmentExpression(node) {
27
+ const isAssignmentExpression = (node) => {
25
28
  return node.type === "AssignmentExpression";
26
- }
29
+ };
27
30
  exports.isAssignmentExpression = isAssignmentExpression;
28
- function isMemberExpression(node) {
31
+ const isMemberExpression = (node) => {
29
32
  return node.type === "MemberExpression";
30
- }
33
+ };
31
34
  exports.isMemberExpression = isMemberExpression;
32
- function isIdentifier(node) {
35
+ const isIdentifier = (node) => {
33
36
  return node.type === "Identifier";
34
- }
37
+ };
35
38
  exports.isIdentifier = isIdentifier;
36
- function isFunctionDeclaration(node) {
39
+ const isFunctionDeclaration = (node) => {
37
40
  return node.type === "FunctionDeclaration";
38
- }
41
+ };
39
42
  exports.isFunctionDeclaration = isFunctionDeclaration;
40
- function isFunctionExpression(node) {
43
+ const isFunctionExpression = (node) => {
41
44
  return node.type === "FunctionExpression";
42
- }
45
+ };
43
46
  exports.isFunctionExpression = isFunctionExpression;
44
- function isVariableDeclarator(node) {
47
+ const isVariableDeclarator = (node) => {
45
48
  return node.type === "VariableDeclarator";
46
- }
49
+ };
47
50
  exports.isVariableDeclarator = isVariableDeclarator;
48
- function isVariableDeclaration(node) {
51
+ const isVariableDeclaration = (node) => {
49
52
  return node.type === "VariableDeclaration";
50
- }
53
+ };
51
54
  exports.isVariableDeclaration = isVariableDeclaration;
52
- function isBinding(node, parentNode, grandParentNode) {
55
+ const isBinding = (node, parentNode, grandParentNode) => {
53
56
  if (grandParentNode &&
54
57
  node.type === "Identifier" &&
55
58
  parentNode.type === "Property" &&
@@ -72,7 +75,7 @@ function isBinding(node, parentNode, grandParentNode) {
72
75
  }
73
76
  }
74
77
  return false;
75
- }
78
+ };
76
79
  exports.isBinding = isBinding;
77
80
  const bindingIdentifiersKeys = {
78
81
  DeclareClass: ["id"],
@@ -148,6 +151,7 @@ exports.VISITOR_KEYS = {
148
151
  FunctionExpression: ["id", "params", "body"],
149
152
  Identifier: [],
150
153
  IfStatement: ["test", "consequent", "alternate"],
154
+ ImportAttribute: ["key", "value"],
151
155
  ImportDeclaration: ["specifiers", "source"],
152
156
  ImportDefaultSpecifier: ["local"],
153
157
  ImportNamespaceSpecifier: ["local"],
@@ -227,9 +231,8 @@ function isScope(node, parentNode) {
227
231
  if (isPattern(node) && (isFunction(parentNode) || isCatchClause(parentNode))) {
228
232
  return true;
229
233
  }
230
- return isFunctionDeclaration(parentNode) || isFunctionExpression(parentNode) || isScopable(node);
234
+ return (0, exports.isFunctionDeclaration)(parentNode) || (0, exports.isFunctionExpression)(parentNode) || isScopable(node);
231
235
  }
232
- exports.isScope = isScope;
233
236
  function isScopable(node) {
234
237
  switch (node.type) {
235
238
  case "BlockStatement":
@@ -252,8 +255,6 @@ function isScopable(node) {
252
255
  }
253
256
  return false;
254
257
  }
255
- exports.isScopable = isScopable;
256
258
  function isExportSpecifier(node) {
257
259
  return node.type === "ExportSpecifier";
258
260
  }
259
- exports.isExportSpecifier = isExportSpecifier;
@@ -1,14 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parse = exports.tokenize = void 0;
3
+ exports.tokenize = tokenize;
4
+ exports.parse = parse;
4
5
  const _1 = require(".");
5
6
  const nodeutils_1 = require("./nodeutils");
6
7
  const debugLogEnabled = false;
7
8
  const log = {
8
- debug: (...args) => {
9
+ debug: debugLogEnabled ? (...args) => {
9
10
  if (debugLogEnabled)
10
11
  console.debug(...args);
11
- }
12
+ } : () => { }
12
13
  };
13
14
  const supportedIdentifiers = Object.fromEntries(Object.keys(nodeutils_1.VISITOR_KEYS).map(k => [k, k]));
14
15
  function isIdentifierToken(token) {
@@ -149,7 +150,6 @@ function tokenize(input) {
149
150
  }
150
151
  return result;
151
152
  }
152
- exports.tokenize = tokenize;
153
153
  function buildFilter(tokens) {
154
154
  log.debug("BUILD FILTER", tokens);
155
155
  tokens.shift();
@@ -295,4 +295,3 @@ function parse(input) {
295
295
  throw new Error("No root element found");
296
296
  return result;
297
297
  }
298
- exports.parse = parse;
@@ -1,4 +1,4 @@
1
- import { ASTNode } from "./traverse";
1
+ import { ESTree } from "meriyah";
2
2
  export declare const functions: {
3
3
  join: {
4
4
  fn: (result: Result[][]) => Result[];
@@ -24,5 +24,40 @@ export declare function multiQuery<T extends Record<string, string>>(code: strin
24
24
  __AST?: ASTNode;
25
25
  };
26
26
  export declare function parseSource(source: string): ASTNode;
27
+ export type Binding = {
28
+ path: NodePath;
29
+ };
30
+ export type Scope = {
31
+ bindings: Record<string, Binding>;
32
+ parentScopeId?: number;
33
+ id: number;
34
+ };
35
+ export type ASTNode = ESTree.Node & {
36
+ extra?: {
37
+ scopeId?: number;
38
+ functionScopeId?: number;
39
+ nodePath?: NodePath;
40
+ };
41
+ };
42
+ export type NodePath = {
43
+ node: ASTNode;
44
+ key?: string;
45
+ parentPath?: NodePath;
46
+ parentKey?: string;
47
+ scopeId: number;
48
+ functionScopeId: number;
49
+ };
50
+ type Visitor<T> = {
51
+ enter: (path: NodePath, state: T) => void;
52
+ exit: (path: NodePath, state: T) => void;
53
+ };
54
+ export default function createTraverser(): {
55
+ traverse: <T>(node: ASTNode, visitor: Visitor<T>, scopeId: number | undefined, state: T, path?: NodePath) => void;
56
+ createNodePath: (node: ASTNode, key: string | undefined | number, parentKey: string | undefined, scopeId: number | undefined, functionScopeId: number | undefined, nodePath?: NodePath) => NodePath;
57
+ getChildren: (key: string, path: NodePath) => NodePath[];
58
+ getPrimitiveChildren: (key: string, path: NodePath) => PrimitiveValue[];
59
+ getPrimitiveChildrenOrNodePaths: (key: string, path: NodePath) => Array<PrimitiveValue | NodePath>;
60
+ getBinding: (scopeId: number, name: string) => Binding | undefined;
61
+ };
27
62
  export {};
28
63
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mapp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
3
3
  import { PrimitiveValue } from ".";
4
- export declare function isNode(candidate: unknown): candidate is ASTNode;
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;
9
- export declare function isAssignmentExpression(node: ESTree.Node): node is ESTree.AssignmentExpression;
10
- export declare function isMemberExpression(node: ESTree.Node): node is ESTree.MemberExpression;
11
- export declare function isIdentifier(node: ESTree.Node): node is ESTree.Identifier;
12
- export declare function isFunctionDeclaration(node: ESTree.Node): node is ESTree.FunctionDeclaration;
13
- export declare function isFunctionExpression(node: ESTree.Node): node is ESTree.FunctionExpression;
14
- export declare function isVariableDeclarator(node: ESTree.Node): node is ESTree.VariableDeclarator;
15
- export declare function isVariableDeclaration(node: ESTree.Node): node is ESTree.VariableDeclaration;
16
- export declare function isBinding(node: ESTree.Node, parentNode: ESTree.Node, grandParentNode: ESTree.Node | undefined): boolean;
4
+ export declare const isNode: (candidate: unknown) => candidate is ASTNode;
5
+ export declare const isNodePath: (candidate: unknown) => candidate is NodePath;
6
+ export declare const isLiteral: (candidate: unknown) => candidate is ESTree.Literal;
7
+ export declare const isPrimitive: (value: unknown) => value is PrimitiveValue;
8
+ export declare const isUpdateExpression: (value: unknown) => value is ESTree.UpdateExpression;
9
+ export declare const isAssignmentExpression: (node: ESTree.Node) => node is ESTree.AssignmentExpression;
10
+ export declare const isMemberExpression: (node: ESTree.Node) => node is ESTree.MemberExpression;
11
+ export declare const isIdentifier: (node: ESTree.Node) => node is ESTree.Identifier;
12
+ export declare const isFunctionDeclaration: (node: ESTree.Node) => node is ESTree.FunctionDeclaration;
13
+ export declare const isFunctionExpression: (node: ESTree.Node) => node is ESTree.FunctionExpression;
14
+ export declare const isVariableDeclarator: (node: ESTree.Node) => node is ESTree.VariableDeclarator;
15
+ export declare const isVariableDeclaration: (node: ESTree.Node) => node is ESTree.VariableDeclaration;
16
+ export declare const isBinding: (node: ESTree.Node, parentNode: ESTree.Node, grandParentNode: ESTree.Node | undefined) => boolean;
17
17
  export declare const VISITOR_KEYS: Record<ESTree.Node["type"], string[]>;
18
18
  export declare function isScope(node: ESTree.Node, parentNode: ESTree.Node): boolean;
19
19
  export declare function isScopable(node: 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,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,GAAG,CAAC;AAEnC,wBAAgB,MAAM,CAAC,SAAS,EAAE,OAAO,GAAI,SAAS,IAAI,OAAO,CAEhE;AAED,wBAAgB,UAAU,CAAC,SAAS,EAAE,OAAO,GAAI,SAAS,IAAI,QAAQ,CAErE;AAED,wBAAgB,SAAS,CAAC,SAAS,EAAE,OAAO,GAAI,SAAS,IAAI,MAAM,CAAC,OAAO,CAE1E;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAI,KAAK,IAAI,cAAc,CAEpE;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAI,KAAK,IAAI,MAAM,CAAC,gBAAgB,CAEpF;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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M,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"}
package/lib/esm/utils.js CHANGED
@@ -1,11 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isDefined = exports.toArray = void 0;
3
+ exports.toArray = toArray;
4
+ exports.isDefined = isDefined;
4
5
  function toArray(value) {
5
6
  return Array.isArray(value) ? value : [value];
6
7
  }
7
- exports.toArray = toArray;
8
8
  function isDefined(value) {
9
9
  return value != undefined && value != null;
10
10
  }
11
- exports.isDefined = isDefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astronomical",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
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",
@@ -33,7 +33,7 @@
33
33
  "typescript": "^5.3.3"
34
34
  },
35
35
  "dependencies": {
36
- "meriyah": "^4.3.9"
36
+ "meriyah": "^6.0.3"
37
37
  },
38
38
  "files": [
39
39
  "lib/**/*"
@@ -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;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