@babel/traverse 7.21.4-esm → 7.21.4-esm.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.
Potentially problematic release.
This version of @babel/traverse might be problematic. Click here for more details.
- package/lib/cache.js +7 -18
- package/lib/cache.js.map +1 -1
- package/lib/context.js +4 -11
- package/lib/context.js.map +1 -1
- package/lib/hub.js +1 -8
- package/lib/hub.js.map +1 -1
- package/lib/index.js +11 -36
- package/lib/index.js.map +1 -1
- package/lib/path/ancestry.js +11 -26
- package/lib/path/ancestry.js.map +1 -1
- package/lib/path/comments.js +4 -12
- package/lib/path/comments.js.map +1 -1
- package/lib/path/context.js +25 -49
- package/lib/path/context.js.map +1 -1
- package/lib/path/conversion.js +16 -26
- package/lib/path/conversion.js.map +1 -1
- package/lib/path/evaluation.js +2 -9
- package/lib/path/evaluation.js.map +1 -1
- package/lib/path/family.js +19 -33
- package/lib/path/family.js.map +1 -1
- package/lib/path/index.js +30 -40
- package/lib/path/index.js.map +1 -1
- package/lib/path/inference/index.js +8 -19
- package/lib/path/inference/index.js.map +1 -1
- package/lib/path/inference/inferer-reference.js +5 -11
- package/lib/path/inference/inferer-reference.js.map +1 -1
- package/lib/path/inference/inferers.js +30 -65
- package/lib/path/inference/inferers.js.map +1 -1
- package/lib/path/inference/util.js +2 -8
- package/lib/path/inference/util.js.map +1 -1
- package/lib/path/introspection.js +20 -45
- package/lib/path/introspection.js.map +1 -1
- package/lib/path/lib/hoister.js +3 -10
- package/lib/path/lib/hoister.js.map +1 -1
- package/lib/path/lib/removal-hooks.js +1 -8
- package/lib/path/lib/removal-hooks.js.map +1 -1
- package/lib/path/lib/virtual-types-validator.js +19 -42
- package/lib/path/lib/virtual-types-validator.js.map +1 -1
- package/lib/path/lib/virtual-types.js +18 -42
- package/lib/path/lib/virtual-types.js.map +1 -1
- package/lib/path/modification.js +19 -34
- package/lib/path/modification.js.map +1 -1
- package/lib/path/removal.js +12 -23
- package/lib/path/removal.js.map +1 -1
- package/lib/path/replacement.js +22 -33
- package/lib/path/replacement.js.map +1 -1
- package/lib/scope/binding.js +1 -8
- package/lib/scope/binding.js.map +1 -1
- package/lib/scope/index.js +13 -20
- package/lib/scope/index.js.map +1 -1
- package/lib/scope/lib/renamer.js +9 -16
- package/lib/scope/lib/renamer.js.map +1 -1
- package/lib/traverse-node.js +4 -10
- package/lib/traverse-node.js.map +1 -1
- package/lib/visitors.js +5 -13
- package/lib/visitors.js.map +1 -1
- package/package.json +11 -11
package/lib/cache.js
CHANGED
@@ -1,25 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.clear = clear;
|
7
|
-
exports.clearPath = clearPath;
|
8
|
-
exports.clearScope = clearScope;
|
9
|
-
exports.scope = exports.path = void 0;
|
10
|
-
let path = new WeakMap();
|
11
|
-
exports.path = path;
|
12
|
-
let scope = new WeakMap();
|
13
|
-
exports.scope = scope;
|
14
|
-
function clear() {
|
1
|
+
export let path = new WeakMap();
|
2
|
+
export let scope = new WeakMap();
|
3
|
+
export function clear() {
|
15
4
|
clearPath();
|
16
5
|
clearScope();
|
17
6
|
}
|
18
|
-
function clearPath() {
|
19
|
-
|
7
|
+
export function clearPath() {
|
8
|
+
path = new WeakMap();
|
20
9
|
}
|
21
|
-
function clearScope() {
|
22
|
-
|
10
|
+
export function clearScope() {
|
11
|
+
scope = new WeakMap();
|
23
12
|
}
|
24
13
|
|
25
14
|
//# sourceMappingURL=cache.js.map
|
package/lib/cache.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["path","WeakMap","
|
1
|
+
{"version":3,"names":["path","WeakMap","scope","clear","clearPath","clearScope"],"sources":["../src/cache.ts"],"sourcesContent":["export let path = new WeakMap();\nexport let scope = new WeakMap();\n\nexport function clear() {\n clearPath();\n clearScope();\n}\n\nexport function clearPath() {\n path = new WeakMap();\n}\n\nexport function clearScope() {\n scope = new WeakMap();\n}\n"],"mappings":"AAAA,OAAO,IAAIA,IAAI,GAAG,IAAIC,OAAO,EAAE;AAC/B,OAAO,IAAIC,KAAK,GAAG,IAAID,OAAO,EAAE;AAEhC,OAAO,SAASE,KAAKA,CAAA,EAAG;EACtBC,SAAS,EAAE;EACXC,UAAU,EAAE;AACd;AAEA,OAAO,SAASD,SAASA,CAAA,EAAG;EAC1BJ,IAAI,GAAG,IAAIC,OAAO,EAAE;AACtB;AAEA,OAAO,SAASI,UAAUA,CAAA,EAAG;EAC3BH,KAAK,GAAG,IAAID,OAAO,EAAE;AACvB"}
|
package/lib/context.js
CHANGED
@@ -1,15 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.default = void 0;
|
7
|
-
var _path = require("./path");
|
8
|
-
var _t = require("@babel/types");
|
1
|
+
import NodePath from "./path/index.js";
|
2
|
+
import * as _t from "@babel/types";
|
9
3
|
const {
|
10
4
|
VISITOR_KEYS
|
11
5
|
} = _t;
|
12
|
-
class TraversalContext {
|
6
|
+
export default class TraversalContext {
|
13
7
|
constructor(scope, opts, state, parentPath) {
|
14
8
|
this.queue = null;
|
15
9
|
this.priorityQueue = null;
|
@@ -32,7 +26,7 @@ class TraversalContext {
|
|
32
26
|
return false;
|
33
27
|
}
|
34
28
|
create(node, container, key, listKey) {
|
35
|
-
return
|
29
|
+
return NodePath.get({
|
36
30
|
parentPath: this.parentPath,
|
37
31
|
parent: node,
|
38
32
|
container,
|
@@ -110,6 +104,5 @@ class TraversalContext {
|
|
110
104
|
}
|
111
105
|
}
|
112
106
|
}
|
113
|
-
exports.default = TraversalContext;
|
114
107
|
|
115
108
|
//# sourceMappingURL=context.js.map
|
package/lib/context.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["
|
1
|
+
{"version":3,"names":["NodePath","_t","VISITOR_KEYS","TraversalContext","constructor","scope","opts","state","parentPath","queue","priorityQueue","shouldVisit","node","enter","exit","type","keys","length","key","create","container","listKey","get","parent","maybeQueue","path","notPriority","push","visitMultiple","visitQueue","visitSingle","visited","WeakSet","stop","resync","contexts","pushContext","has","add","visit","popContext","nodes","Array","isArray"],"sources":["../src/context.ts"],"sourcesContent":["import NodePath from \"./path\";\nimport { VISITOR_KEYS } from \"@babel/types\";\nimport type Scope from \"./scope\";\nimport type { TraverseOptions } from \".\";\nimport type * as t from \"@babel/types\";\nimport type { Visitor } from \"./types\";\n\nexport default class TraversalContext<S = unknown> {\n constructor(\n scope: Scope,\n opts: TraverseOptions,\n state: S,\n parentPath: NodePath,\n ) {\n this.parentPath = parentPath;\n this.scope = scope;\n this.state = state;\n this.opts = opts;\n }\n\n declare parentPath: NodePath;\n declare scope: Scope;\n declare state: S;\n declare opts: TraverseOptions;\n queue: Array<NodePath> | null = null;\n priorityQueue: Array<NodePath> | null = null;\n\n /**\n * This method does a simple check to determine whether or not we really need to attempt\n * visit a node. This will prevent us from constructing a NodePath.\n */\n\n shouldVisit(node: t.Node): boolean {\n const opts = this.opts as Visitor;\n if (opts.enter || opts.exit) return true;\n\n // check if we have a visitor for this node\n if (opts[node.type]) return true;\n\n // check if we're going to traverse into this node\n const keys: Array<string> | undefined = VISITOR_KEYS[node.type];\n if (!keys?.length) return false;\n\n // we need to traverse into this node so ensure that it has children to traverse into!\n for (const key of keys) {\n if (\n // @ts-expect-error key is from visitor keys\n node[key]\n ) {\n return true;\n }\n }\n\n return false;\n }\n\n create(\n node: t.Node,\n container: t.Node | t.Node[],\n key: string | number,\n listKey?: string,\n ): NodePath {\n // We don't need to `.setContext()` here, since `.visitQueue()` already\n // calls `.pushContext`.\n return NodePath.get({\n parentPath: this.parentPath,\n parent: node,\n container,\n key: key,\n listKey,\n });\n }\n\n maybeQueue(path: NodePath, notPriority?: boolean) {\n if (this.queue) {\n if (notPriority) {\n this.queue.push(path);\n } else {\n this.priorityQueue.push(path);\n }\n }\n }\n\n visitMultiple(container: t.Node[], parent: t.Node, listKey: string) {\n // nothing to traverse!\n if (container.length === 0) return false;\n\n const queue = [];\n\n // build up initial queue\n for (let key = 0; key < container.length; key++) {\n const node = container[key];\n if (node && this.shouldVisit(node)) {\n queue.push(this.create(parent, container, key, listKey));\n }\n }\n\n return this.visitQueue(queue);\n }\n\n visitSingle(node: t.Node, key: string): boolean {\n if (\n this.shouldVisit(\n // @ts-expect-error key may not index node\n node[key],\n )\n ) {\n return this.visitQueue([this.create(node, node, key)]);\n } else {\n return false;\n }\n }\n\n visitQueue(queue: Array<NodePath>): boolean {\n // set queue\n this.queue = queue;\n this.priorityQueue = [];\n\n const visited = new WeakSet();\n let stop = false;\n\n // visit the queue\n for (const path of queue) {\n path.resync();\n\n if (\n path.contexts.length === 0 ||\n path.contexts[path.contexts.length - 1] !== this\n ) {\n // The context might already have been pushed when this path was inserted and queued.\n // If we always re-pushed here, we could get duplicates and risk leaving contexts\n // on the stack after the traversal has completed, which could break things.\n path.pushContext(this);\n }\n\n // this path no longer belongs to the tree\n if (path.key === null) continue;\n\n // ensure we don't visit the same node twice\n const { node } = path;\n if (visited.has(node)) continue;\n if (node) visited.add(node);\n\n if (path.visit()) {\n stop = true;\n break;\n }\n\n if (this.priorityQueue.length) {\n stop = this.visitQueue(this.priorityQueue);\n this.priorityQueue = [];\n this.queue = queue;\n if (stop) break;\n }\n }\n\n // clear queue\n for (const path of queue) {\n path.popContext();\n }\n\n // clear queue\n this.queue = null;\n\n return stop;\n }\n\n visit(node: t.Node, key: string) {\n // @ts-expect-error key may not index node\n const nodes = node[key] as t.Node | t.Node[] | null;\n if (!nodes) return false;\n\n if (Array.isArray(nodes)) {\n return this.visitMultiple(nodes, node, key);\n } else {\n return this.visitSingle(node, key);\n }\n }\n}\n"],"mappings":"AAAA,OAAOA,QAAQ,MAAM,iBAAQ;AAC7B,YAAAC,EAAA,MAA6B,cAAc;AAAC;EAAnCC;AAAY,IAAAD,EAAA;AAMrB,eAAe,MAAME,gBAAgB,CAAc;EACjDC,W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
|
|
1
|
-
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.default = void 0;
|
7
|
-
class Hub {
|
1
|
+
export default class Hub {
|
8
2
|
getCode() {}
|
9
3
|
getScope() {}
|
10
4
|
addHelper() {
|
@@ -14,6 +8,5 @@ class Hub {
|
|
14
8
|
return new Error(msg);
|
15
9
|
}
|
16
10
|
}
|
17
|
-
exports.default = Hub;
|
18
11
|
|
19
12
|
//# sourceMappingURL=hub.js.map
|
package/lib/hub.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["Hub","getCode","getScope","addHelper","Error","buildError","node","msg","TypeError"
|
1
|
+
{"version":3,"names":["Hub","getCode","getScope","addHelper","Error","buildError","node","msg","TypeError"],"sources":["../src/hub.ts"],"sourcesContent":["import type Scope from \"./scope\";\nimport type { Node } from \"@babel/types\";\n\nexport interface HubInterface {\n getCode(): string | void;\n getScope(): Scope | void;\n addHelper(name: string): any;\n buildError(node: Node, msg: string, Error: new () => Error): Error;\n}\n\nexport default class Hub implements HubInterface {\n getCode() {}\n\n getScope() {}\n\n addHelper() {\n throw new Error(\"Helpers are not supported by the default hub.\");\n }\n\n buildError(node: Node, msg: string, Error = TypeError): Error {\n return new Error(msg);\n }\n}\n"],"mappings":"AAUA,eAAe,MAAMA,GAAG,CAAyB;EAC/CC,OAAOA,CAAA,EAAG,CAAC;EAEXC,QAAQA,CAAA,EAAG,CAAC;EAEZC,SAASA,CAAA,EAAG;IACV,MAAM,IAAIC,KAAK,CAAC,+CAA+C,CAAC;EAClE;EAEAC,UAAUA,CAACC,IAAU,EAAEC,GAAW,EAAEH,KAAK,GAAGI,SAAS,EAAS;IAC5D,OAAO,IAAIJ,KAAK,CAACG,GAAG,CAAC;EACvB;AACF"}
|
package/lib/index.js
CHANGED
@@ -1,40 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
Object.defineProperty(exports, "Hub", {
|
7
|
-
enumerable: true,
|
8
|
-
get: function () {
|
9
|
-
return _hub.default;
|
10
|
-
}
|
11
|
-
});
|
12
|
-
Object.defineProperty(exports, "NodePath", {
|
13
|
-
enumerable: true,
|
14
|
-
get: function () {
|
15
|
-
return _path.default;
|
16
|
-
}
|
17
|
-
});
|
18
|
-
Object.defineProperty(exports, "Scope", {
|
19
|
-
enumerable: true,
|
20
|
-
get: function () {
|
21
|
-
return _scope.default;
|
22
|
-
}
|
23
|
-
});
|
24
|
-
exports.visitors = exports.default = void 0;
|
25
|
-
var visitors = require("./visitors");
|
26
|
-
exports.visitors = visitors;
|
27
|
-
var _t = require("@babel/types");
|
28
|
-
var cache = require("./cache");
|
29
|
-
var _traverseNode = require("./traverse-node");
|
30
|
-
var _path = require("./path");
|
31
|
-
var _scope = require("./scope");
|
32
|
-
var _hub = require("./hub");
|
1
|
+
import * as visitors from "./visitors.js";
|
2
|
+
import * as _t from "@babel/types";
|
33
3
|
const {
|
34
4
|
VISITOR_KEYS,
|
35
5
|
removeProperties,
|
36
6
|
traverseFast
|
37
7
|
} = _t;
|
8
|
+
import * as cache from "./cache.js";
|
9
|
+
import { traverseNode } from "./traverse-node.js";
|
10
|
+
export { default as NodePath } from "./path/index.js";
|
11
|
+
export { default as Scope } from "./scope/index.js";
|
12
|
+
export { default as Hub } from "./hub.js";
|
13
|
+
export { visitors };
|
38
14
|
function traverse(parent, opts = {}, scope, state, parentPath) {
|
39
15
|
if (!parent) return;
|
40
16
|
if (!opts.noScope && !scope) {
|
@@ -46,10 +22,9 @@ function traverse(parent, opts = {}, scope, state, parentPath) {
|
|
46
22
|
return;
|
47
23
|
}
|
48
24
|
visitors.explode(opts);
|
49
|
-
|
25
|
+
traverseNode(parent, opts, scope, state, parentPath);
|
50
26
|
}
|
51
|
-
|
52
|
-
exports.default = _default;
|
27
|
+
export default traverse;
|
53
28
|
traverse.visitors = visitors;
|
54
29
|
traverse.verify = visitors.verify;
|
55
30
|
traverse.explode = visitors.explode;
|
@@ -58,7 +33,7 @@ traverse.cheap = function (node, enter) {
|
|
58
33
|
return;
|
59
34
|
};
|
60
35
|
traverse.node = function (node, opts, scope, state, path, skipKeys) {
|
61
|
-
|
36
|
+
traverseNode(node, opts, scope, state, path, skipKeys);
|
62
37
|
};
|
63
38
|
traverse.clearNode = function (node, opts) {
|
64
39
|
removeProperties(node, opts);
|
package/lib/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["visitors","
|
1
|
+
{"version":3,"names":["visitors","_t","VISITOR_KEYS","removeProperties","traverseFast","cache","traverseNode","default","NodePath","Scope","Hub","traverse","parent","opts","scope","state","parentPath","noScope","type","Error","explode","verify","cheap","node","enter","path","skipKeys","clearNode","delete","tree","hasDenylistedType","has","stop","hasType","denylistTypes","includes","denylist"],"sources":["../src/index.ts"],"sourcesContent":["import * as visitors from \"./visitors\";\nimport {\n VISITOR_KEYS,\n removeProperties,\n type RemovePropertiesOptions,\n traverseFast,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport * as cache from \"./cache\";\nimport type NodePath from \"./path\";\nimport type { default as Scope, Binding } from \"./scope\";\nimport type { Visitor } from \"./types\";\nimport { traverseNode } from \"./traverse-node\";\n\nexport type { Visitor, Binding };\nexport { default as NodePath } from \"./path\";\nexport { default as Scope } from \"./scope\";\nexport { default as Hub } from \"./hub\";\nexport type { HubInterface } from \"./hub\";\n\nexport { visitors };\n\nexport type TraverseOptions<S = t.Node> = {\n scope?: Scope;\n noScope?: boolean;\n denylist?: string[];\n} & Visitor<S>;\n\nfunction traverse<S>(\n parent: t.Node,\n opts: TraverseOptions<S>,\n scope: Scope | undefined,\n state: S,\n parentPath?: NodePath,\n): void;\n\nfunction traverse(\n parent: t.Node,\n opts: TraverseOptions,\n scope?: Scope,\n state?: any,\n parentPath?: NodePath,\n): void;\n\nfunction traverse<Options extends TraverseOptions>(\n parent: t.Node,\n // @ts-expect-error provide {} as default value for Options\n opts: Options = {},\n scope?: Scope,\n state?: any,\n parentPath?: NodePath,\n) {\n if (!parent) return;\n\n if (!opts.noScope && !scope) {\n if (parent.type !== \"Program\" && parent.type !== \"File\") {\n throw new Error(\n \"You must pass a scope and parentPath unless traversing a Program/File. \" +\n `Instead of that you tried to traverse a ${parent.type} node without ` +\n \"passing scope and parentPath.\",\n );\n }\n }\n\n if (!VISITOR_KEYS[parent.type]) {\n return;\n }\n\n visitors.explode(opts as Visitor);\n\n traverseNode(parent, opts, scope, state, parentPath);\n}\n\nexport default traverse;\n\ntraverse.visitors = visitors;\ntraverse.verify = visitors.verify;\ntraverse.explode = visitors.explode;\n\ntraverse.cheap = function (node: t.Node, enter: (node: t.Node) => void) {\n traverseFast(node, enter);\n return;\n};\n\ntraverse.node = function (\n node: t.Node,\n opts: TraverseOptions,\n scope?: Scope,\n state?: any,\n path?: NodePath,\n skipKeys?: Record<string, boolean>,\n) {\n traverseNode(node, opts, scope, state, path, skipKeys);\n // traverse.node always returns undefined\n};\n\ntraverse.clearNode = function (node: t.Node, opts?: RemovePropertiesOptions) {\n removeProperties(node, opts);\n\n cache.path.delete(node);\n};\n\ntraverse.removeProperties = function (\n tree: t.Node,\n opts?: RemovePropertiesOptions,\n) {\n traverseFast(tree, traverse.clearNode, opts);\n return tree;\n};\n\ntype HasDenylistedTypeState = {\n has: boolean;\n type: t.Node[\"type\"];\n};\nfunction hasDenylistedType(path: NodePath, state: HasDenylistedTypeState) {\n if (path.node.type === state.type) {\n state.has = true;\n path.stop();\n }\n}\n\ntraverse.hasType = function (\n tree: t.Node,\n type: t.Node[\"type\"],\n denylistTypes?: Array<string>,\n): boolean {\n // the node we're searching in is denylisted\n if (denylistTypes?.includes(tree.type)) return false;\n\n // the type we're looking for is the same as the passed node\n if (tree.type === type) return true;\n\n const state: HasDenylistedTypeState = {\n has: false,\n type: type,\n };\n\n traverse(\n tree,\n {\n noScope: true,\n denylist: denylistTypes,\n enter: has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;EACR,CAAC;EAEDP,QAAQ,CACNkB,IAAI,EACJ;IACEZ,OAAO,EAAE,IAAI;IACbmB,QAAQ,EAAEF,aAAa;IACvBV,KAAK,EAAEM;EACT,CAAC,EACD,IAAI,EACJf,KAAK,CACN;EAED,OAAOA,KAAK,CAACgB,GAAG;AAClB,CAAC;AAEDpB,QAAQ,CAACN,KAAK,GAAGA,KAAK"}
|
package/lib/path/ancestry.js
CHANGED
@@ -1,40 +1,25 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.find = find;
|
7
|
-
exports.findParent = findParent;
|
8
|
-
exports.getAncestry = getAncestry;
|
9
|
-
exports.getDeepestCommonAncestorFrom = getDeepestCommonAncestorFrom;
|
10
|
-
exports.getEarliestCommonAncestorFrom = getEarliestCommonAncestorFrom;
|
11
|
-
exports.getFunctionParent = getFunctionParent;
|
12
|
-
exports.getStatementParent = getStatementParent;
|
13
|
-
exports.inType = inType;
|
14
|
-
exports.isAncestor = isAncestor;
|
15
|
-
exports.isDescendant = isDescendant;
|
16
|
-
var _t = require("@babel/types");
|
1
|
+
import * as _t from "@babel/types";
|
17
2
|
const {
|
18
3
|
VISITOR_KEYS
|
19
4
|
} = _t;
|
20
|
-
function findParent(callback) {
|
5
|
+
export function findParent(callback) {
|
21
6
|
let path = this;
|
22
7
|
while (path = path.parentPath) {
|
23
8
|
if (callback(path)) return path;
|
24
9
|
}
|
25
10
|
return null;
|
26
11
|
}
|
27
|
-
function find(callback) {
|
12
|
+
export function find(callback) {
|
28
13
|
let path = this;
|
29
14
|
do {
|
30
15
|
if (callback(path)) return path;
|
31
16
|
} while (path = path.parentPath);
|
32
17
|
return null;
|
33
18
|
}
|
34
|
-
function getFunctionParent() {
|
19
|
+
export function getFunctionParent() {
|
35
20
|
return this.findParent(p => p.isFunction());
|
36
21
|
}
|
37
|
-
function getStatementParent() {
|
22
|
+
export function getStatementParent() {
|
38
23
|
let path = this;
|
39
24
|
do {
|
40
25
|
if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
|
@@ -48,7 +33,7 @@ function getStatementParent() {
|
|
48
33
|
}
|
49
34
|
return path;
|
50
35
|
}
|
51
|
-
function getEarliestCommonAncestorFrom(paths) {
|
36
|
+
export function getEarliestCommonAncestorFrom(paths) {
|
52
37
|
return this.getDeepestCommonAncestorFrom(paths, function (deepest, i, ancestries) {
|
53
38
|
let earliest;
|
54
39
|
const keys = VISITOR_KEYS[deepest.type];
|
@@ -73,7 +58,7 @@ function getEarliestCommonAncestorFrom(paths) {
|
|
73
58
|
return earliest;
|
74
59
|
});
|
75
60
|
}
|
76
|
-
function getDeepestCommonAncestorFrom(paths, filter) {
|
61
|
+
export function getDeepestCommonAncestorFrom(paths, filter) {
|
77
62
|
if (!paths.length) {
|
78
63
|
return this;
|
79
64
|
}
|
@@ -113,7 +98,7 @@ function getDeepestCommonAncestorFrom(paths, filter) {
|
|
113
98
|
throw new Error("Couldn't find intersection");
|
114
99
|
}
|
115
100
|
}
|
116
|
-
function getAncestry() {
|
101
|
+
export function getAncestry() {
|
117
102
|
let path = this;
|
118
103
|
const paths = [];
|
119
104
|
do {
|
@@ -121,13 +106,13 @@ function getAncestry() {
|
|
121
106
|
} while (path = path.parentPath);
|
122
107
|
return paths;
|
123
108
|
}
|
124
|
-
function isAncestor(maybeDescendant) {
|
109
|
+
export function isAncestor(maybeDescendant) {
|
125
110
|
return maybeDescendant.isDescendant(this);
|
126
111
|
}
|
127
|
-
function isDescendant(maybeAncestor) {
|
112
|
+
export function isDescendant(maybeAncestor) {
|
128
113
|
return !!this.findParent(parent => parent === maybeAncestor);
|
129
114
|
}
|
130
|
-
function inType(...candidateTypes) {
|
115
|
+
export function inType(...candidateTypes) {
|
131
116
|
let path = this;
|
132
117
|
while (path) {
|
133
118
|
for (const type of candidateTypes) {
|
package/lib/path/ancestry.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_t","require","VISITOR_KEYS","findParent","callback","path","parentPath","find","getFunctionParent","p","isFunction","getStatementParent","Array","isArray","container","isStatement","isProgram","isFile","Error","getEarliestCommonAncestorFrom","paths","getDeepestCommonAncestorFrom","deepest","i","ancestries","earliest","keys","type","ancestry","listKey","key","earliestKeyIndex","indexOf","parentKey","currentKeyIndex","filter","length","minDepth","Infinity","lastCommonIndex","lastCommon","map","unshift","first","depthLoop","shouldMatch","getAncestry","push","isAncestor","maybeDescendant","isDescendant","maybeAncestor","parent","inType","candidateTypes","node"],"sources":["../../src/path/ancestry.ts"],"sourcesContent":["// This file contains that retrieve or validate anything related to the current paths ancestry.\n\nimport { VISITOR_KEYS } from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport type NodePath from \"./index\";\n\n/**\n * Starting at the parent path of the current `NodePath` and going up the\n * tree, return the first `NodePath` that causes the provided `callback`\n * to return a truthy value, or `null` if the `callback` never returns a\n * truthy value.\n */\n\nexport function findParent(\n this: NodePath,\n callback: (path: NodePath) => boolean,\n): NodePath | null {\n let path = this;\n while ((path = path.parentPath)) {\n if (callback(path)) return path;\n }\n return null;\n}\n\n/**\n * Starting at current `NodePath` and going up the tree, return the first\n * `NodePath` that causes the provided `callback` to return a truthy value,\n * or `null` if the `callback` never returns a truthy value.\n */\n\nexport function find(\n this: NodePath,\n callback: (path: NodePath) => boolean,\n): NodePath | null {\n let path = this;\n do {\n if (callback(path)) return path;\n } while ((path = path.parentPath));\n return null;\n}\n\n/**\n * Get the parent function of the current path.\n */\n\nexport function getFunctionParent(this: NodePath): NodePath<t.Function> | null {\n return this.findParent(p => p.isFunction()) as NodePath<t.Function> | null;\n}\n\n/**\n * Walk up the tree until we hit a parent node path in a list.\n */\n\nexport function getStatementParent(this: NodePath): NodePath<t.Statement> {\n let path = this;\n\n do {\n if (\n !path.parentPath ||\n (Array.isArray(path.container) && path.isStatement())\n ) {\n break;\n } else {\n path = path.parentPath;\n }\n } while (path);\n\n if (path && (path.isProgram() || path.isFile())) {\n throw new Error(\n \"File/Program node, we can't possibly find a statement parent to this\",\n );\n }\n\n return path as NodePath<t.Statement>;\n}\n\n/**\n * Get the deepest common ancestor and then from it, get the earliest relationship path\n * to that ancestor.\n *\n * Earliest is defined as being \"before\" all the other nodes in terms of list container\n * position and visiting key.\n */\n\nexport function getEarliestCommonAncestorFrom(\n this: NodePath,\n paths: Array<NodePath>,\n): NodePath {\n return this.getDeepestCommonAncestorFrom(\n paths,\n function (deepest, i, ancestries) {\n let earliest;\n const keys = VISITOR_KEYS[deepest.type];\n\n for (const ancestry of ancestries) {\n const path = ancestry[i + 1];\n\n // first path\n if (!earliest) {\n earliest = path;\n continue;\n }\n\n // handle containers\n if (path.listKey && earliest.listKey === path.listKey) {\n // we're in the same container so check if we're earlier\n if (path.key < earliest.key) {\n earliest = path;\n continue;\n }\n }\n\n // handle keys\n const earliestKeyIndex = keys.indexOf(earliest.parentKey);\n const currentKeyIndex = keys.indexOf(path.parentKey);\n if (earliestKeyIndex > currentKeyIndex) {\n // key appears before so it's earlier\n earliest = path;\n }\n }\n\n return earliest;\n },\n );\n}\n\n/**\n * Get the earliest path in the tree where the provided `paths` intersect.\n *\n * TODO: Possible optimisation target.\n */\n\nexport function getDeepestCommonAncestorFrom(\n this: NodePath,\n paths: Array<NodePath>,\n filter?: (deepest: NodePath, i: number, ancestries: NodePath[][]) => NodePath,\n): NodePath {\n if (!paths.length) {\n return this;\n }\n\n if (paths.length === 1) {\n return paths[0];\n }\n\n // minimum depth of the tree so we know the highest node\n let minDepth = Infinity;\n\n // last common ancestor\n let lastCommonIndex, lastCommon;\n\n // get the ancestors of the path, breaking when the parent exceeds ourselves\n const ancestries = paths.map(path => {\n const ancestry: NodePath[] = [];\n\n do {\n ancestry.unshift(path);\n } while ((path = path.parentPath) && path !== this);\n\n // save min depth to avoid going too far in\n if (ancestry.length < minDepth) {\n minDepth = ancestry.length;\n }\n\n return ancestry;\n });\n\n // get the first ancestry so we have a seed to assess all other ancestries with\n const first = ancestries[0];\n\n // check ancestor equality\n depthLoop: for (let i = 0; i < minDepth; i++) {\n const shouldMatch = first[i];\n\n for (const ancestry of ancestries) {\n if (ancestry[i] !== shouldMatch) {\n // we've hit a snag\n break depthLoop;\n }\n }\n\n // next iteration may break so store these so they can be returned\n lastCommonIndex = i;\n lastCommon = shouldMatch;\n }\n\n if (lastCommon) {\n if (filter) {\n return filter(lastCommon, lastCommonIndex, ancestries);\n } else {\n return lastCommon;\n }\n } else {\n throw new Error(\"Couldn't find intersection\");\n }\n}\n\n/**\n * Build an array of node paths containing the entire ancestry of the current node path.\n *\n * NOTE: The current node path is included in this.\n */\n\nexport function getAncestry(this: NodePath): Array<NodePath> {\n let path = this;\n const paths = [];\n do {\n paths.push(path);\n } while ((path = path.parentPath));\n return paths;\n}\n\n/**\n * A helper to find if `this` path is an ancestor of @param maybeDescendant\n */\nexport function isAncestor(this: NodePath, maybeDescendant: NodePath): boolean {\n return maybeDescendant.isDescendant(this);\n}\n\n/**\n * A helper to find if `this` path is a descendant of @param maybeAncestor\n */\nexport function isDescendant(this: NodePath, maybeAncestor: NodePath): boolean {\n return !!this.findParent(parent => parent === maybeAncestor);\n}\n\nexport function inType(this: NodePath, ...candidateTypes: string[]): boolean {\n let path = this;\n while (path) {\n for (const type of candidateTypes) {\n if (path.node.type === type) return true;\n }\n path = path.parentPath;\n }\n\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAEA,IAAAA,EAAA,GAAAC,OAAA;AAA4C;EAAnCC;AAAY,IAAAF,EAAA;AAWd,SAASG,UAAUA,CAExBC,QAAqC,EACpB;EACjB,IAAIC,IAAI,GAAG,IAAI;EACf,OAAQA,IAAI,GAAGA,IAAI,CAACC,UAAU,EAAG;IAC/B,IAAIF,QAAQ,CAACC,IAAI,CAAC,EAAE,OAAOA,IAAI;EACjC;EACA,OAAO,IAAI;AACb;AAQO,SAASE,IAAIA,CAElBH,QAAqC,EACpB;EACjB,IAAIC,IAAI,GAAG,IAAI;EACf,GAAG;I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ACd"}
|
1
|
+
{"version":3,"names":["_t","VISITOR_KEYS","findParent","callback","path","parentPath","find","getFunctionParent","p","isFunction","getStatementParent","Array","isArray","container","isStatement","isProgram","isFile","Error","getEarliestCommonAncestorFrom","paths","getDeepestCommonAncestorFrom","deepest","i","ancestries","earliest","keys","type","ancestry","listKey","key","earliestKeyIndex","indexOf","parentKey","currentKeyIndex","filter","length","minDepth","Infinity","lastCommonIndex","lastCommon","map","unshift","first","depthLoop","shouldMatch","getAncestry","push","isAncestor","maybeDescendant","isDescendant","maybeAncestor","parent","inType","candidateTypes","node"],"sources":["../../src/path/ancestry.ts"],"sourcesContent":["// This file contains that retrieve or validate anything related to the current paths ancestry.\n\nimport { VISITOR_KEYS } from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport type NodePath from \"./index\";\n\n/**\n * Starting at the parent path of the current `NodePath` and going up the\n * tree, return the first `NodePath` that causes the provided `callback`\n * to return a truthy value, or `null` if the `callback` never returns a\n * truthy value.\n */\n\nexport function findParent(\n this: NodePath,\n callback: (path: NodePath) => boolean,\n): NodePath | null {\n let path = this;\n while ((path = path.parentPath)) {\n if (callback(path)) return path;\n }\n return null;\n}\n\n/**\n * Starting at current `NodePath` and going up the tree, return the first\n * `NodePath` that causes the provided `callback` to return a truthy value,\n * or `null` if the `callback` never returns a truthy value.\n */\n\nexport function find(\n this: NodePath,\n callback: (path: NodePath) => boolean,\n): NodePath | null {\n let path = this;\n do {\n if (callback(path)) return path;\n } while ((path = path.parentPath));\n return null;\n}\n\n/**\n * Get the parent function of the current path.\n */\n\nexport function getFunctionParent(this: NodePath): NodePath<t.Function> | null {\n return this.findParent(p => p.isFunction()) as NodePath<t.Function> | null;\n}\n\n/**\n * Walk up the tree until we hit a parent node path in a list.\n */\n\nexport function getStatementParent(this: NodePath): NodePath<t.Statement> {\n let path = this;\n\n do {\n if (\n !path.parentPath ||\n (Array.isArray(path.container) && path.isStatement())\n ) {\n break;\n } else {\n path = path.parentPath;\n }\n } while (path);\n\n if (path && (path.isProgram() || path.isFile())) {\n throw new Error(\n \"File/Program node, we can't possibly find a statement parent to this\",\n );\n }\n\n return path as NodePath<t.Statement>;\n}\n\n/**\n * Get the deepest common ancestor and then from it, get the earliest relationship path\n * to that ancestor.\n *\n * Earliest is defined as being \"before\" all the other nodes in terms of list container\n * position and visiting key.\n */\n\nexport function getEarliestCommonAncestorFrom(\n this: NodePath,\n paths: Array<NodePath>,\n): NodePath {\n return this.getDeepestCommonAncestorFrom(\n paths,\n function (deepest, i, ancestries) {\n let earliest;\n const keys = VISITOR_KEYS[deepest.type];\n\n for (const ancestry of ancestries) {\n const path = ancestry[i + 1];\n\n // first path\n if (!earliest) {\n earliest = path;\n continue;\n }\n\n // handle containers\n if (path.listKey && earliest.listKey === path.listKey) {\n // we're in the same container so check if we're earlier\n if (path.key < earliest.key) {\n earliest = path;\n continue;\n }\n }\n\n // handle keys\n const earliestKeyIndex = keys.indexOf(earliest.parentKey);\n const currentKeyIndex = keys.indexOf(path.parentKey);\n if (earliestKeyIndex > currentKeyIndex) {\n // key appears before so it's earlier\n earliest = path;\n }\n }\n\n return earliest;\n },\n );\n}\n\n/**\n * Get the earliest path in the tree where the provided `paths` intersect.\n *\n * TODO: Possible optimisation target.\n */\n\nexport function getDeepestCommonAncestorFrom(\n this: NodePath,\n paths: Array<NodePath>,\n filter?: (deepest: NodePath, i: number, ancestries: NodePath[][]) => NodePath,\n): NodePath {\n if (!paths.length) {\n return this;\n }\n\n if (paths.length === 1) {\n return paths[0];\n }\n\n // minimum depth of the tree so we know the highest node\n let minDepth = Infinity;\n\n // last common ancestor\n let lastCommonIndex, lastCommon;\n\n // get the ancestors of the path, breaking when the parent exceeds ourselves\n const ancestries = paths.map(path => {\n const ancestry: NodePath[] = [];\n\n do {\n ancestry.unshift(path);\n } while ((path = path.parentPath) && path !== this);\n\n // save min depth to avoid going too far in\n if (ancestry.length < minDepth) {\n minDepth = ancestry.length;\n }\n\n return ancestry;\n });\n\n // get the first ancestry so we have a seed to assess all other ancestries with\n const first = ancestries[0];\n\n // check ancestor equality\n depthLoop: for (let i = 0; i < minDepth; i++) {\n const shouldMatch = first[i];\n\n for (const ancestry of ancestries) {\n if (ancestry[i] !== shouldMatch) {\n // we've hit a snag\n break depthLoop;\n }\n }\n\n // next iteration may break so store these so they can be returned\n lastCommonIndex = i;\n lastCommon = shouldMatch;\n }\n\n if (lastCommon) {\n if (filter) {\n return filter(lastCommon, lastCommonIndex, ancestries);\n } else {\n return lastCommon;\n }\n } else {\n throw new Error(\"Couldn't find intersection\");\n }\n}\n\n/**\n * Build an array of node paths containing the entire ancestry of the current node path.\n *\n * NOTE: The current node path is included in this.\n */\n\nexport function getAncestry(this: NodePath): Array<NodePath> {\n let path = this;\n const paths = [];\n do {\n paths.push(path);\n } while ((path = path.parentPath));\n return paths;\n}\n\n/**\n * A helper to find if `this` path is an ancestor of @param maybeDescendant\n */\nexport function isAncestor(this: NodePath, maybeDescendant: NodePath): boolean {\n return maybeDescendant.isDescendant(this);\n}\n\n/**\n * A helper to find if `this` path is a descendant of @param maybeAncestor\n */\nexport function isDescendant(this: NodePath, maybeAncestor: NodePath): boolean {\n return !!this.findParent(parent => parent === maybeAncestor);\n}\n\nexport function inType(this: NodePath, ...candidateTypes: string[]): boolean {\n let path = this;\n while (path) {\n for (const type of candidateTypes) {\n if (path.node.type === type) return true;\n }\n path = path.parentPath;\n }\n\n return false;\n}\n"],"mappings":"AAEA,YAAAA,EAAA,MAA6B,cAAc;AAAC;EAAnCC;AAAY,IAAAD,EAAA;AAWrB,OAAO,SAASE,UAAUA,CAExBC,QAAqC,EACpB;EACjB,IAAIC,IAAI,GAAG,IAAI;EACf,OAAQA,IAAI,GAAGA,IAAI,CAACC,UAAU,EAAG;IAC/B,IAAIF,QAAQ,CAACC,IAAI,CAAC,EAAE,OAAOA,IAAI;EACjC;EACA,OAAO,IAAI;AACb;AAQA,OAAO,SAASE,IAAIA,CAElBH,QAAqC,EACpB;EACjB,IAAIC,IAAI,GAAG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1C;IACAtB,IAAI,GAAGA,IAAI,CAACC,UAAU;EACxB;EAEA,OAAO,KAAK;AACd"}
|
package/lib/path/comments.js
CHANGED
@@ -1,17 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.addComment = addComment;
|
7
|
-
exports.addComments = addComments;
|
8
|
-
exports.shareCommentsWithSiblings = shareCommentsWithSiblings;
|
9
|
-
var _t = require("@babel/types");
|
1
|
+
import * as _t from "@babel/types";
|
10
2
|
const {
|
11
3
|
addComment: _addComment,
|
12
4
|
addComments: _addComments
|
13
5
|
} = _t;
|
14
|
-
function shareCommentsWithSiblings() {
|
6
|
+
export function shareCommentsWithSiblings() {
|
15
7
|
if (typeof this.key === "string") return;
|
16
8
|
const node = this.node;
|
17
9
|
if (!node) return;
|
@@ -44,10 +36,10 @@ function removeIfExisting(list, toRemove) {
|
|
44
36
|
lastFoundIndex = i;
|
45
37
|
});
|
46
38
|
}
|
47
|
-
function addComment(type, content, line) {
|
39
|
+
export function addComment(type, content, line) {
|
48
40
|
_addComment(this.node, type, content, line);
|
49
41
|
}
|
50
|
-
function addComments(type, comments) {
|
42
|
+
export function addComments(type, comments) {
|
51
43
|
_addComments(this.node, type, comments);
|
52
44
|
}
|
53
45
|
|
package/lib/path/comments.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_t","
|
1
|
+
{"version":3,"names":["_t","addComment","_addComment","addComments","_addComments","shareCommentsWithSiblings","key","node","trailing","trailingComments","leading","leadingComments","prev","getSibling","next","hasPrev","Boolean","hasNext","removeIfExisting","list","toRemove","lastFoundIndex","filter","el","i","indexOf","type","content","line","comments"],"sources":["../../src/path/comments.ts"],"sourcesContent":["// This file contains methods responsible for dealing with comments.\nimport type * as t from \"@babel/types\";\nimport type NodePath from \"./index\";\nimport {\n addComment as _addComment,\n addComments as _addComments,\n} from \"@babel/types\";\n\n/**\n * Share comments amongst siblings.\n */\n\nexport function shareCommentsWithSiblings(this: NodePath) {\n // NOTE: this assumes numbered keys\n if (typeof this.key === \"string\") return;\n\n const node = this.node;\n if (!node) return;\n\n const trailing = node.trailingComments;\n const leading = node.leadingComments;\n if (!trailing && !leading) return;\n\n const prev = this.getSibling(this.key - 1);\n const next = this.getSibling(this.key + 1);\n const hasPrev = Boolean(prev.node);\n const hasNext = Boolean(next.node);\n\n if (hasPrev) {\n if (leading) {\n prev.addComments(\n \"trailing\",\n removeIfExisting(leading, prev.node.trailingComments),\n );\n }\n if (trailing && !hasNext) prev.addComments(\"trailing\", trailing);\n }\n if (hasNext) {\n if (trailing) {\n next.addComments(\n \"leading\",\n removeIfExisting(trailing, next.node.leadingComments),\n );\n }\n if (leading && !hasPrev) next.addComments(\"leading\", leading);\n }\n}\n\nfunction removeIfExisting<T>(list: T[], toRemove?: T[]): T[] {\n if (!toRemove) return list;\n let lastFoundIndex = -1;\n return list.filter(el => {\n const i = toRemove.indexOf(el, lastFoundIndex);\n if (i === -1) return true;\n lastFoundIndex = i;\n });\n}\n\nexport function addComment(\n this: NodePath,\n type: t.CommentTypeShorthand,\n content: string,\n line?: boolean,\n) {\n _addComment(this.node, type, content, line);\n}\n\n/**\n * Give node `comments` of the specified `type`.\n */\n\nexport function addComments(\n this: NodePath,\n type: t.CommentTypeShorthand,\n comments: t.Comment[],\n) {\n _addComments(this.node, type, comments);\n}\n"],"mappings":"AAGA,YAAAA,EAAA,MAGO,cAAc;AAAC;EAFpBC,UAAU,EAAIC,WAAW;EACzBC,WAAW,EAAIC;AAAY,IAAAJ,EAAA;AAO7B,OAAO,SAASK,yBAAyBA,CAAA,EAAiB;EAExD,IAAI,OAAO,IAAI,CAACC,GAAG,KAAK,QAAQ,EAAE;EAElC,MAAMC,IAAI,GAAG,IAAI,CAACA,IAAI;EACtB,IAAI,CAACA,IAAI,EAAE;EAEX,MAAMC,QAAQ,GAAGD,IAAI,CAACE,gBAAgB;EACtC,MAAMC,OAAO,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AI;IACzBH,cAAc,GAAGG,CAAC;EACpB,CAAC,CAAC;AACJ;AAEA,OAAO,SAASvB,UAAUA,CAExByB,IAA4B,EAC5BC,OAAe,EACfC,IAAc,EACd;EACA1B,WAAW,CAAC,IAAI,CAACK,IAAI,EAAEmB,IAAI,EAAEC,OAAO,EAAEC,IAAI,CAAC;AAC7C;AAMA,OAAO,SAASzB,WAAWA,CAEzBuB,IAA4B,EAC5BG,QAAqB,EACrB;EACAzB,YAAY,CAAC,IAAI,CAACG,IAAI,EAAEmB,IAAI,EAAEG,QAAQ,CAAC;AACzC"}
|
package/lib/path/context.js
CHANGED
@@ -1,31 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports._call = _call;
|
7
|
-
exports._getQueueContexts = _getQueueContexts;
|
8
|
-
exports._resyncKey = _resyncKey;
|
9
|
-
exports._resyncList = _resyncList;
|
10
|
-
exports._resyncParent = _resyncParent;
|
11
|
-
exports._resyncRemoved = _resyncRemoved;
|
12
|
-
exports.call = call;
|
13
|
-
exports.isBlacklisted = exports.isDenylisted = isDenylisted;
|
14
|
-
exports.popContext = popContext;
|
15
|
-
exports.pushContext = pushContext;
|
16
|
-
exports.requeue = requeue;
|
17
|
-
exports.resync = resync;
|
18
|
-
exports.setContext = setContext;
|
19
|
-
exports.setKey = setKey;
|
20
|
-
exports.setScope = setScope;
|
21
|
-
exports.setup = setup;
|
22
|
-
exports.skip = skip;
|
23
|
-
exports.skipKey = skipKey;
|
24
|
-
exports.stop = stop;
|
25
|
-
exports.visit = visit;
|
26
|
-
var _traverseNode = require("../traverse-node");
|
27
|
-
var _index = require("./index");
|
28
|
-
function call(key) {
|
1
|
+
import { traverseNode } from "../traverse-node.js";
|
2
|
+
import { SHOULD_SKIP, SHOULD_STOP } from "./index.js";
|
3
|
+
export function call(key) {
|
29
4
|
const opts = this.opts;
|
30
5
|
this.debug(key);
|
31
6
|
if (this.node) {
|
@@ -36,7 +11,7 @@ function call(key) {
|
|
36
11
|
}
|
37
12
|
return false;
|
38
13
|
}
|
39
|
-
function _call(fns) {
|
14
|
+
export function _call(fns) {
|
40
15
|
if (!fns) return false;
|
41
16
|
for (const fn of fns) {
|
42
17
|
if (!fn) continue;
|
@@ -54,11 +29,12 @@ function _call(fns) {
|
|
54
29
|
}
|
55
30
|
return false;
|
56
31
|
}
|
57
|
-
function isDenylisted() {
|
32
|
+
export function isDenylisted() {
|
58
33
|
var _this$opts$denylist;
|
59
34
|
const denylist = (_this$opts$denylist = this.opts.denylist) != null ? _this$opts$denylist : this.opts.blacklist;
|
60
35
|
return denylist && denylist.indexOf(this.node.type) > -1;
|
61
36
|
}
|
37
|
+
export { isDenylisted as isBlacklisted };
|
62
38
|
function restoreContext(path, context) {
|
63
39
|
if (path.context !== context) {
|
64
40
|
path.context = context;
|
@@ -66,7 +42,7 @@ function restoreContext(path, context) {
|
|
66
42
|
path.opts = context.opts;
|
67
43
|
}
|
68
44
|
}
|
69
|
-
function visit() {
|
45
|
+
export function visit() {
|
70
46
|
if (!this.node) {
|
71
47
|
return false;
|
72
48
|
}
|
@@ -83,24 +59,24 @@ function visit() {
|
|
83
59
|
}
|
84
60
|
restoreContext(this, currentContext);
|
85
61
|
this.debug("Recursing into...");
|
86
|
-
this.shouldStop =
|
62
|
+
this.shouldStop = traverseNode(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
|
87
63
|
restoreContext(this, currentContext);
|
88
64
|
this.call("exit");
|
89
65
|
return this.shouldStop;
|
90
66
|
}
|
91
|
-
function skip() {
|
67
|
+
export function skip() {
|
92
68
|
this.shouldSkip = true;
|
93
69
|
}
|
94
|
-
function skipKey(key) {
|
70
|
+
export function skipKey(key) {
|
95
71
|
if (this.skipKeys == null) {
|
96
72
|
this.skipKeys = {};
|
97
73
|
}
|
98
74
|
this.skipKeys[key] = true;
|
99
75
|
}
|
100
|
-
function stop() {
|
101
|
-
this._traverseFlags |=
|
76
|
+
export function stop() {
|
77
|
+
this._traverseFlags |= SHOULD_SKIP | SHOULD_STOP;
|
102
78
|
}
|
103
|
-
function setScope() {
|
79
|
+
export function setScope() {
|
104
80
|
if (this.opts && this.opts.noScope) return;
|
105
81
|
let path = this.parentPath;
|
106
82
|
if ((this.key === "key" || this.listKey === "decorators") && path.isMethod() || this.key === "discriminant" && path.isSwitchStatement()) {
|
@@ -115,7 +91,7 @@ function setScope() {
|
|
115
91
|
this.scope = this.getScope(target);
|
116
92
|
if (this.scope) this.scope.init();
|
117
93
|
}
|
118
|
-
function setContext(context) {
|
94
|
+
export function setContext(context) {
|
119
95
|
if (this.skipKeys != null) {
|
120
96
|
this.skipKeys = {};
|
121
97
|
}
|
@@ -128,18 +104,18 @@ function setContext(context) {
|
|
128
104
|
this.setScope();
|
129
105
|
return this;
|
130
106
|
}
|
131
|
-
function resync() {
|
107
|
+
export function resync() {
|
132
108
|
if (this.removed) return;
|
133
109
|
this._resyncParent();
|
134
110
|
this._resyncList();
|
135
111
|
this._resyncKey();
|
136
112
|
}
|
137
|
-
function _resyncParent() {
|
113
|
+
export function _resyncParent() {
|
138
114
|
if (this.parentPath) {
|
139
115
|
this.parent = this.parentPath.node;
|
140
116
|
}
|
141
117
|
}
|
142
|
-
function _resyncKey() {
|
118
|
+
export function _resyncKey() {
|
143
119
|
if (!this.container) return;
|
144
120
|
if (this.node === this.container[this.key]) {
|
145
121
|
return;
|
@@ -161,18 +137,18 @@ function _resyncKey() {
|
|
161
137
|
}
|
162
138
|
this.key = null;
|
163
139
|
}
|
164
|
-
function _resyncList() {
|
140
|
+
export function _resyncList() {
|
165
141
|
if (!this.parent || !this.inList) return;
|
166
142
|
const newContainer = this.parent[this.listKey];
|
167
143
|
if (this.container === newContainer) return;
|
168
144
|
this.container = newContainer || null;
|
169
145
|
}
|
170
|
-
function _resyncRemoved() {
|
146
|
+
export function _resyncRemoved() {
|
171
147
|
if (this.key == null || !this.container || this.container[this.key] !== this.node) {
|
172
148
|
this._markRemoved();
|
173
149
|
}
|
174
150
|
}
|
175
|
-
function popContext() {
|
151
|
+
export function popContext() {
|
176
152
|
this.contexts.pop();
|
177
153
|
if (this.contexts.length > 0) {
|
178
154
|
this.setContext(this.contexts[this.contexts.length - 1]);
|
@@ -180,23 +156,23 @@ function popContext() {
|
|
180
156
|
this.setContext(undefined);
|
181
157
|
}
|
182
158
|
}
|
183
|
-
function pushContext(context) {
|
159
|
+
export function pushContext(context) {
|
184
160
|
this.contexts.push(context);
|
185
161
|
this.setContext(context);
|
186
162
|
}
|
187
|
-
function setup(parentPath, container, listKey, key) {
|
163
|
+
export function setup(parentPath, container, listKey, key) {
|
188
164
|
this.listKey = listKey;
|
189
165
|
this.container = container;
|
190
166
|
this.parentPath = parentPath || this.parentPath;
|
191
167
|
this.setKey(key);
|
192
168
|
}
|
193
|
-
function setKey(key) {
|
169
|
+
export function setKey(key) {
|
194
170
|
var _this$node;
|
195
171
|
this.key = key;
|
196
172
|
this.node = this.container[this.key];
|
197
173
|
this.type = (_this$node = this.node) == null ? void 0 : _this$node.type;
|
198
174
|
}
|
199
|
-
function requeue(pathToQueue = this) {
|
175
|
+
export function requeue(pathToQueue = this) {
|
200
176
|
if (pathToQueue.removed) return;
|
201
177
|
;
|
202
178
|
const contexts = this.contexts;
|
@@ -204,7 +180,7 @@ function requeue(pathToQueue = this) {
|
|
204
180
|
context.maybeQueue(pathToQueue);
|
205
181
|
}
|
206
182
|
}
|
207
|
-
function _getQueueContexts() {
|
183
|
+
export function _getQueueContexts() {
|
208
184
|
let path = this;
|
209
185
|
let contexts = this.contexts;
|
210
186
|
while (!contexts.length) {
|