1. Compiler
  2. Walk

Compiler

Walk

        walk(ast: Node, {
	enter(node: Node, parent: Node, prop: string, index: number)?: void,
	leave(node: Node, parent: Node, prop: string, index: number)?: void
})

      

The walk function provides a way to walk the abstract syntax trees generated by the parser, using the compiler's own built-in instance of 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.

        const svelte = require('svelte/compiler');
svelte.walk(ast, {
  enter(node, parent, prop, index) {
    do_something(node);
    if (should_skip_children(node)) {
      this.skip();
    }
  },
  leave(node, parent, prop, index) {
    do_something_else(node);
  },
});