Comparing version 2.1.0-canary-4 to 3.0.0-canary-5
@@ -46,5 +46,2 @@ "use strict"; | ||
function explode(input) { | ||
if (input._babel_walk_exploded) { | ||
return input._babel_walk_exploded; | ||
} | ||
const results = {}; | ||
@@ -110,3 +107,2 @@ for (const key in input) { | ||
} | ||
input._babel_walk_exploded = results; | ||
return results; | ||
@@ -113,0 +109,0 @@ } |
@@ -12,3 +12,3 @@ import * as t from '@babel/types'; | ||
}; | ||
export declare function simple<TState>(node: t.Node, visitors: SimpleVisitors<TState>, state: TState): void; | ||
export declare function simple<TState = void>(visitors: SimpleVisitors<TState>): (node: t.Node, state: TState) => void; | ||
export declare type AncestorFunction<TKey extends string, TState> = (node: NodeType<TKey>, state: TState, ancestors: t.Node[]) => void; | ||
@@ -21,6 +21,6 @@ export declare type AncestorVisitor<TState = void> = { | ||
}; | ||
export declare function ancestor<TState = void>(node: t.Node, visitors: AncestorVisitor<TState>, state: TState): void; | ||
export declare function ancestor<TState = void>(visitors: AncestorVisitor<TState>): (node: t.Node, state: TState) => void; | ||
export declare type RecursiveVisitors<TState = void> = { | ||
[key in keyof t.Aliases | t.Node['type']]?: (node: NodeType<key>, state: TState, recurse: (node: t.Node) => void) => void; | ||
}; | ||
export declare function recursive<TState>(node: t.Node, visitors: RecursiveVisitors<TState>, state: TState): void; | ||
export declare function recursive<TState = void>(visitors: RecursiveVisitors<TState>): (node: t.Node, state: TState) => void; |
146
lib/index.js
@@ -37,80 +37,50 @@ "use strict"; | ||
} | ||
function simple(node, visitors, state) { | ||
function simple(visitors) { | ||
const vis = explode_1.default(visitors); | ||
(function recurse(node) { | ||
if (!node) | ||
return; | ||
const visitor = vis[node.type]; | ||
if (visitor === null || visitor === void 0 ? void 0 : visitor.enter) { | ||
for (const v of visitor.enter) { | ||
v(node, state); | ||
return (node, state) => { | ||
(function recurse(node) { | ||
if (!node) | ||
return; | ||
const visitor = vis[node.type]; | ||
if (visitor === null || visitor === void 0 ? void 0 : visitor.enter) { | ||
for (const v of visitor.enter) { | ||
v(node, state); | ||
} | ||
} | ||
} | ||
for (const key of VISITOR_KEYS[node.type] || []) { | ||
const subNode = node[key]; | ||
if (Array.isArray(subNode)) { | ||
for (const subSubNode of subNode) { | ||
recurse(subSubNode); | ||
for (const key of VISITOR_KEYS[node.type] || []) { | ||
const subNode = node[key]; | ||
if (Array.isArray(subNode)) { | ||
for (const subSubNode of subNode) { | ||
recurse(subSubNode); | ||
} | ||
} | ||
else { | ||
recurse(subNode); | ||
} | ||
} | ||
else { | ||
recurse(subNode); | ||
if (visitor === null || visitor === void 0 ? void 0 : visitor.exit) { | ||
for (const v of visitor.exit) { | ||
v(node, state); | ||
} | ||
} | ||
} | ||
if (visitor === null || visitor === void 0 ? void 0 : visitor.exit) { | ||
for (const v of visitor.exit) { | ||
v(node, state); | ||
} | ||
} | ||
})(node); | ||
})(node); | ||
}; | ||
} | ||
exports.simple = simple; | ||
function ancestor(node, visitors, state) { | ||
function ancestor(visitors) { | ||
const vis = explode_1.default(visitors); | ||
const ancestors = []; | ||
(function recurse(node) { | ||
if (!node) | ||
return; | ||
const visitor = vis[node.type]; | ||
const isNew = node !== ancestors[ancestors.length - 1]; | ||
if (isNew) | ||
ancestors.push(node); | ||
if (visitor === null || visitor === void 0 ? void 0 : visitor.enter) { | ||
for (const v of visitor.enter) { | ||
v(node, state, ancestors); | ||
} | ||
} | ||
for (const key of VISITOR_KEYS[node.type] || []) { | ||
const subNode = node[key]; | ||
if (Array.isArray(subNode)) { | ||
for (const subSubNode of subNode) { | ||
recurse(subSubNode); | ||
return (node, state) => { | ||
const ancestors = []; | ||
(function recurse(node) { | ||
if (!node) | ||
return; | ||
const visitor = vis[node.type]; | ||
const isNew = node !== ancestors[ancestors.length - 1]; | ||
if (isNew) | ||
ancestors.push(node); | ||
if (visitor === null || visitor === void 0 ? void 0 : visitor.enter) { | ||
for (const v of visitor.enter) { | ||
v(node, state, ancestors); | ||
} | ||
} | ||
else { | ||
recurse(subNode); | ||
} | ||
} | ||
if (visitor === null || visitor === void 0 ? void 0 : visitor.exit) { | ||
for (const v of visitor.exit) { | ||
v(node, state, ancestors); | ||
} | ||
} | ||
if (isNew) | ||
ancestors.pop(); | ||
})(node); | ||
} | ||
exports.ancestor = ancestor; | ||
function recursive(node, visitors, state) { | ||
const vis = explode_1.default(visitors); | ||
(function recurse(node) { | ||
if (!node) | ||
return; | ||
const visitor = vis[node.type]; | ||
if (visitor === null || visitor === void 0 ? void 0 : visitor.enter) { | ||
for (const v of visitor.enter) { | ||
v(node, state, recurse); | ||
} | ||
} | ||
else { | ||
for (const key of VISITOR_KEYS[node.type] || []) { | ||
@@ -127,6 +97,42 @@ const subNode = node[key]; | ||
} | ||
} | ||
})(node); | ||
if (visitor === null || visitor === void 0 ? void 0 : visitor.exit) { | ||
for (const v of visitor.exit) { | ||
v(node, state, ancestors); | ||
} | ||
} | ||
if (isNew) | ||
ancestors.pop(); | ||
})(node); | ||
}; | ||
} | ||
exports.ancestor = ancestor; | ||
function recursive(visitors) { | ||
const vis = explode_1.default(visitors); | ||
return (node, state) => { | ||
(function recurse(node) { | ||
if (!node) | ||
return; | ||
const visitor = vis[node.type]; | ||
if (visitor === null || visitor === void 0 ? void 0 : visitor.enter) { | ||
for (const v of visitor.enter) { | ||
v(node, state, recurse); | ||
} | ||
} | ||
else { | ||
for (const key of VISITOR_KEYS[node.type] || []) { | ||
const subNode = node[key]; | ||
if (Array.isArray(subNode)) { | ||
for (const subSubNode of subNode) { | ||
recurse(subSubNode); | ||
} | ||
} | ||
else { | ||
recurse(subNode); | ||
} | ||
} | ||
} | ||
})(node); | ||
}; | ||
} | ||
exports.recursive = recursive; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "babel-walk", | ||
"version": "2.1.0-canary-4", | ||
"version": "3.0.0-canary-5", | ||
"description": "Lightweight Babel AST traversal", | ||
@@ -30,2 +30,3 @@ "main": "lib/index.js", | ||
"@forbeslindesay/tsconfig": "^2.0.0", | ||
"@types/node": "^14.0.5", | ||
"prettier": "^2.0.5", | ||
@@ -32,0 +33,0 @@ "rimraf": "^3.0.2", |
@@ -31,23 +31,23 @@ # babel-walk | ||
### walk.simple(node, visitors, state) | ||
### walk.simple(visitors)(node, state) | ||
Do a simple walk over the AST. `node` should be the AST node to walk, and `visitors` an object containing Babel [visitors]. Each visitor function will be called as `(node, state)`, where `node` is the AST node, and `state` is the same `state` passed to `walk.simple`. | ||
When `walk.simple` is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding `Visitor(node, state) {}` to `Visitor() { enter(node, state) {} }`). This exploding process can take some time, so it is recommended to [cache your visitors] and communicate state leveraging the `state` parameter. (One difference between the linked article and babel-walk is that the state is only accessible through the `state` variable, never as `this`.) | ||
When `walk.simple` is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding `Visitor(node, state) {}` to `Visitor() { enter(node, state) {} }`). This exploding process can take some time, so it is recommended to cache the result of calling `walk.simple(visitors)` and communicate state leveraging the `state` parameter. | ||
All [babel-types] aliases (e.g. `Expression`) work, but the union syntax (e.g. `'Identifier|AssignmentPattern'(node, state) {}`) does not. | ||
### walk.ancestor(node, visitors, state) | ||
### walk.ancestor(visitors)(node, state) | ||
Do a simple walk over the AST, but memoizing the ancestors of the node and making them available to the visitors. `node` should be the AST node to walk, and `visitors` an object containing Babel [visitors]. Each visitor function will be called as `(node, state, ancestors)`, where `node` is the AST node, `state` is the same `state` passed to `walk.ancestor`, and `ancestors` is an array of ancestors to the node (with the outermost node being `[0]` and the current node being `[ancestors.length - 1]`). If `state` is not specified in the call to `walk.ancestor`, the `state` parameter will be set to `ancestors`. | ||
When `walk.ancestor` is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding `Visitor(node, state) {}` to `Visitor() { enter(node, state) {} }`). This exploding process can take some time, so it is recommended to [cache your visitors] and communicate state leveraging the `state` parameter. (One difference between the linked article and babel-walk is that the state is only accessible through the `state` variable, never as `this`.) | ||
When `walk.ancestor` is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding `Visitor(node, state) {}` to `Visitor() { enter(node, state) {} }`). This exploding process can take some time, so it is recommended to cache the result of calling `walk.ancestor(visitors)` and communicate state leveraging the `state` parameter. | ||
All [babel-types] aliases (e.g. `Expression`) work, but the union syntax (e.g. `'Identifier|AssignmentPattern'(node, state) {}`) does not. | ||
### walk.recursive(node, visitors, state) | ||
### walk.recursive(visitors)(node, state) | ||
Do a recursive walk over the AST, where the visitors are responsible for continuing the walk on the child nodes of their target node. `node` should be the AST node to walk, and `visitors` an object containing Babel [visitors]. Each visitor function will be called as `(node, state, c)`, where `node` is the AST node, `state` is the same `state` passed to `walk.recursive`, and `c` is a function that takes a single node as argument and continues walking _that_ node. If no visitor for a node is provided, the default walker algorithm will still be used. | ||
When `walk.recursive` is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding `Visitor(node, state) {}` to `Visitor() { enter(node, state) {} }`). This exploding process can take some time, so it is recommended to [cache your visitors] and communicate state leveraging the `state` parameter. (One difference between the linked article and babel-walk is that the state is only accessible through the `state` variable, never as `this`.) | ||
When `walk.recursive` is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding `Visitor(node, state) {}` to `Visitor() { enter(node, state) {} }`). This exploding process can take some time, so it is recommended to cache the result of calling `walk.recursive(visitors)` and communicate state leveraging the `state` parameter. | ||
@@ -65,3 +65,3 @@ Unlike other babel-walk walkers, `walk.recursive` does not call the `exit` visitor, only the `enter` (the default) visitor, of a specific node type. | ||
const visitors = { | ||
const visitors = walk.recursive({ | ||
Statement(node, state, c) { | ||
@@ -83,3 +83,3 @@ if (t.isVariableDeclaration(node)) { | ||
}, | ||
}; | ||
}); | ||
@@ -90,3 +90,3 @@ function countFunctions(node) { | ||
}; | ||
walk.recursive(node, visitors, state); | ||
visitors(node, state); | ||
return state.counter; | ||
@@ -93,0 +93,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
110137
13
310
6