An open API service indexing awesome lists of open source software.

https://github.com/venkatperi/coffee-ast-walk

Walk Coffeescript's AST nodes
https://github.com/venkatperi/coffee-ast-walk

Last synced: 2 months ago
JSON representation

Walk Coffeescript's AST nodes

Awesome Lists containing this project

README

        

# coffee-ast-walk
Walk Coffeescript's AST nodes

[![Build Status](https://travis-ci.org/venkatperi/coffee-ast-walk.svg?branch=master)](https://travis-ci.org/venkatperi/coffee-ast-walk)

# Installation

Install with npm

```shell
npm install coffee-ast-walk --save
```

# Examples

Create a ast walker

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

## Dump Every Node

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

## AST Node Count

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

## Find the First Class

```coffeescript
walk.findFirstByType 'Class'
```

## Get Parent of Type

```coffeescript
klass = walk.findFirstByType 'Class'

# fire up another walker
astwalk(klass).findParent ( x ) -> x.__type is 'Assign'
```

# API

Class | Summary
------| ------------
[Walk](#class-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)



Create AST Walker


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.








:: 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.



Performs a walk -- calls the visitor for every node.


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).








:: 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.



Finds all nodes that satisfy the callback



Returns



  • Returns array of Object ast nodes or .







:: 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.



Finds the first node that satisfies the callback



Returns



  • Returns Object, the ast node or .







:: findByType( t[, depth] )
public
instance
Walk





  • t String the node type to search for.


  • depth Number limits the traversal depth



Finds all nodes with the given CoffeeScript node type.



Returns



  • Returns array of Object ast nodes or .







:: findFirstByType( t[, depth] )
public
instance
Walk





  • t String the node type to search for.


  • depth Number limits the traversal depth



Finds the first node with the given CoffeeScript node type.



Returns



  • Returns Object ast node or .







:: reduce( )
public
instance
Walk





  • acc The initial value of the accumulator


  • f Function called with each node: f(node, acc).



Performs a reduce operation on the AST tree


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



Returns



  • Returns the final accumulator value.







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