0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

expose svelte.walk (#2661)

This commit is contained in:
Conduitry 2019-05-04 10:38:30 -04:00
parent ab711ac89c
commit 54a8eb9fd4
2 changed files with 33 additions and 0 deletions

View File

@ -280,6 +280,38 @@ const { code } = svelte.preprocess(source, [
```
### `svelte.walk`
```js
walk(ast: Node, {
enter(node: Node, parent: Node)?: void,
leave(node: Node, parent: Node)?: void
})
```
---
The `walk` function provides a way to walk to abstract syntax trees generated by the parser, using the compiler's own built-in instance of [estree-walker](https://github.com/Rich-Harris/estree-walker).
The walker takes an abstract syntax tree to walk and an object with two optional methods: `enter` and `leave`. For each node, `enter` is called (if present). Then, unless `this.skip()` is called during `enter`, each of the children are traversed, and then `leave` is called on the node.
```js
const svelte = require('svelte/compiler');
svelte.walk(ast, {
enter(node, parent) {
do_something(node);
if (should_skip_children(node)) {
this.skip();
}
},
leave(node, parent) {
do_something_else(node);
}
});
```
### `svelte.VERSION`
---

View File

@ -1,4 +1,5 @@
export { default as compile } from './compile/index';
export { default as preprocess } from './preprocess/index';
export { walk } from 'estree-walker';
export const VERSION = '__VERSION__';