Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/discord/eslint-traverse
Create a sub-traversal of an AST node in your ESLint plugin
https://github.com/discord/eslint-traverse
ast babel eslint traverse visitor walk
Last synced: 2 days ago
JSON representation
Create a sub-traversal of an AST node in your ESLint plugin
- Host: GitHub
- URL: https://github.com/discord/eslint-traverse
- Owner: discord
- License: mit
- Created: 2020-04-04T00:12:44.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-15T18:37:19.000Z (about 2 years ago)
- Last Synced: 2024-10-01T03:40:55.665Z (4 months ago)
- Topics: ast, babel, eslint, traverse, visitor, walk
- Language: JavaScript
- Size: 3.91 KB
- Stars: 31
- Watchers: 4
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# eslint-traverse
> Create a sub-traversal of an AST node in your ESLint plugin
- Very fast
- Supports "Skipping" & "Stopping" (See below)
- Provides AST ancestor information for every node (Babel-style "Path" object)## Install
```sh
npm install --save eslint-traverse
```## Example
```js
import traverse from "eslint-traverse"export default function(context) {
return {
FunctionDeclaration(node) {
traverse(context, node, path => {
console.log(path)
// Path {
// node: Node,
// parent: Node | null,
// parentKey: string | null
// parentPath: Path | null
// }if (path.node.type === "FunctionDeclaration") {
return traverse.SKIP
}
})
}
}
}
```## Skipping
If you want to completely ignore a branch of the AST, without visiting any of its
children, you can return `traverse.SKIP` from the visitor.```js
traverse(context, node, path => {
if (path.node.type === "FunctionDeclaration") {
return traverse.SKIP
}
// ...
})
```## Stopping
If you want to stop the traversal completely, without visiting any more nodes
anywhere in the AST, you can return `traversal.STOP` from the visitor.```js
traverse(context, node, path => {
if (path.node.type === "Identifier" && path.node.name === "THING_I_WAS_SEARCHING_FOR") {
return traverse.STOP
}
// ...
})
```