Skip to content

Commit

Permalink
Parse into Node objects (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinbyers authored Sep 17, 2017
1 parent 7a23229 commit cae2103
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 220 deletions.
6 changes: 6 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[run]
source=.
omit=venv/*

[report]
fail_under=100
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
__pycache__/
.mypy_cache/

# Coverage
.coverage
htmlcov/

venv/
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: python
cache: pip
python: "3.6"
notifications:
email: false
install:
- pip3 install -r requirements.txt
script:
- coverage run visitor_test.py
- coverage report # Required coverage threshold in .coveragerc
- find . -name '*.py' -not -path './venv/*' -exec pylint '{}' +
- mypy .
after_success:
- coveralls
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Esprima AST Visitor
[![Build Status](https://travis-ci.org/austinbyers/esprima-ast-visitor.svg?branch=master)](https://travis-ci.org/austinbyers/esprima-ast-visitor)
[![Coverage Status](https://coveralls.io/repos/github/austinbyers/esprima-ast-visitor/badge.svg?branch=master)](https://coveralls.io/github/austinbyers/esprima-ast-visitor?branch=master)

This provides a simple Python3 module for pre-order traversal of an Esprima AST.

This is a Python3.5+ module for transforming an Esprima AST into a traversable Python object.

## JavaScript Parsing with Esprima
[Esprima](http://esprima.org/) is a popular state-of-the-art JavaScript parser.
Expand All @@ -13,24 +16,46 @@ JSON.stringify(esprima.parse(js_string), null, 2);
```

## AST Format
Esprima's AST follows a [standard format](https://github.com/estree/estree/blob/master/spec.md) specified by the [ESTree project](https://github.com/estree/estree).
Esprima's AST follows a [standard format](https://github.com/estree/estree/blob/master/es5.md) specified by the [ESTree project](https://github.com/estree/estree).
While there are other nodejs projects that provide Esprima AST traversal
(e.g. [estraverse](https://github.com/estools/estraverse)), I was unable
to find an equivalent Python tool. So I made one myself!

## Usage
```python
import json

import visitor

for node in visitor.traverse(json.loads(esprima_ast_string)):
print(node['type'])
# Do other work with node...
ast = json.loads(esprima_ast_string)
program = visitor.objectify(ast) # visitor.Node object

for node in program.traverse():
print(node.type)
# Replace all return statements with 'return null'
if node.type == 'ReturnStatement':
node.argument = None

# Save modified tree back to JSON format
with open('modified_ast.json', 'w') as f:
f.write(json.dumps(node.dict(), indent=2))
```


## Testing
The AST traversal has been tested with a dozen of the most complex real-world
JavaScript samples, including popular libraries like JQuery and Sugar and code
served by the Alexa top 10 sites.

`python3 visitor_test.py`
To run unit tests, test coverage, linting, and type-checking:

```bash

$ virtualenv -p python3 venv
$ source venv/bin/activate
$ pip3 install -r requirements.txt
$ coverage run visitor_test.py # Unit tests
$ coverage report # Should show 100%
$ find . -name '*.py' -not -path './venv/*' -exec pylint '{}' +
$ mypy . # Static type-checking
```
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coveralls
coverage
mypy==0.521
pylint==1.7.2
Loading

0 comments on commit cae2103

Please sign in to comment.