Skip to content

venkatperi/coffee-ast-walk

Repository files navigation

coffee-ast-walk

Walk Coffeescript's AST nodes

Build Status

Installation

Install with npm

npm install coffee-ast-walk --save

Examples

Create a ast walker

astwalk = require 'coffee-ast-walk'
root = coffee.nodes source
walk = astwalk(root)

Dump Every Node

walk.walk (x) ->
  console.log x

AST Node Count

walk.reduce 0, (x, acc) ->
	if @isAstNode then acc + 1 else acc

Find the First Class

walk.findFirstByType 'Class'

Get Parent of Type

klass = walk.findFirstByType 'Class'

# fire up another walker
astwalk(klass).findParent ( x ) -> x.__type is 'Assign'
Class Summary
Walk Walk Coffeescript's AST nodes

WalkCLASS Back to Class List

Walk Coffeescript's AST nodes

Methods

:: constructor( __id, __type ) public instance Walk
  • __id A unique id (monotonically increasing count).
  • __type The type of AST node (node.constructor.name)
  <p>Create AST Walker</p>

If init is set, astwalk will perform an initial walk over the AST tree and append a meta object as well as two properties to each AST node:

AST Node Meta Information

astwalk adds a property meta to Base which is the base class for Coffeescript AST node classes. meta provides meta-information about the node and in many cases, it rolls up information from child nodes which can ease navigation and manipulation of the AST.

</td>
:: walk( [depth][, context] ) public instance Walk
  • depth limits the walk up to depth levels from the current node (i.e. relative to the current node).
  • context Object is available to the visitor callback.
  <p>Performs a walk -- calls the visitor for every node.</p>

About the Visitor: visitor(node)

The current node is passed ot the visitor. Additionaly, the following properties are available in the this context of the visitor callback:

  • @path The path to the current node as an array of strings.
  • @depth The depth of the current node.
  • @isRoot True if the node is the root node.
  • @isLeaf True of the node is a leaf node.
  • @id The id used to invoke the current node, also the last element of the path array.
  • @isAstNode True if the node is a coffeescript AST node, or a scalar (string etc).
  • @context An optional context, passed to walk.
  • @parent The node's parent (undefined for root node).

The visitor callback can invoke @abort() to terminate the walk early.

Visitor Return Value: If a visitor returns an Object, the values from visiting the current AST node's child nodes are added to the object with the same key names as the original node. This can be viewed as a mapping operation on the AST tree (see example at the end).

</td>
:: findAll( [depth], f ) public instance Walk
  • depth Number limits the traversal depth
  • f Function is called with each node. The node is returned if f returns true.
  <p>Finds all nodes that satisfy the callback</p>

  <p>  <em>Returns</em></p>
  • Returns array of Object ast nodes or .
</td>
:: findFirst( [depth], f ) public instance Walk
  • depth Number limits the traversal depth
  • f Function is called with each node. The node is returned if f returns true.
  <p>Finds the first node that satisfies the callback</p>

  <p>  <em>Returns</em></p>
  • Returns Object, the ast node or .
</td>
:: findByType( t[, depth] ) public instance Walk
  • t String the node type to search for.
  • depth Number limits the traversal depth
  <p>Finds all nodes with the given CoffeeScript node type.</p>

  <p>  <em>Returns</em></p>
  • Returns array of Object ast nodes or .
</td>
:: findFirstByType( t[, depth] ) public instance Walk
  • t String the node type to search for.
  • depth Number limits the traversal depth
  <p>Finds the first node with the given CoffeeScript node type.</p>

  <p>  <em>Returns</em></p>
  • Returns Object ast node or .
</td>
:: reduce( ) public instance Walk
  • acc The initial value of the accumulator
  • f Function called with each node: f(node, acc).
  <p>Performs a <code>reduce</code> operation on the AST tree</p>

Updates an internal accumulator to the value returned by f(node, acc) and returns the final value of acc.

  <p>  <em>Returns</em></p>
  • Returns the final accumulator value.
</td>

Markdown generated by [atomdoc-md](https://github.com/venkatperi/atomdoc-md).

About

Walk Coffeescript's AST nodes

Resources

Stars

Watchers

Forks

Packages

No packages published