Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose svelte.walk #2672

Merged
merged 1 commit into from
May 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions site/content/docs/04-compile-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

---
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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__';