-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding documentation and information (#5717) [skip ci]
- Loading branch information
1 parent
dd82d7a
commit 1b29ab1
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,33 @@ | ||
# babel-traverse | ||
|
||
> babel-traverse maintains the overall tree state, and is responsible for replacing, removing, and adding nodes. | ||
## Install | ||
|
||
```sh | ||
$ npm install --save babel-traverse | ||
``` | ||
|
||
## Usage | ||
|
||
We can use it alongside Babylon to traverse and update nodes: | ||
|
||
```js | ||
import * as babylon from "babylon"; | ||
import traverse from "babel-traverse"; | ||
|
||
const code = `function square(n) { | ||
return n * n; | ||
}`; | ||
|
||
const ast = babylon.parse(code); | ||
|
||
traverse(ast, { | ||
enter(path) { | ||
if (path.isIdentifier({ name: "n" })) { | ||
path.node.name = "x"; | ||
} | ||
} | ||
}); | ||
``` | ||
[:book: **Read the full docs here**](https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#babel-traverse) |