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

Introduce 'recur' callback #58

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ coverage/

npm-debug.log
.vimrc.local

/*.sublime-project
/*.sublime-workspace
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
### Estraverse [![Build Status](https://secure.travis-ci.org/estools/estraverse.png)](http://travis-ci.org/estools/estraverse)
### Estraverse Fork

This fork introduces a 'recur' callback beside 'enter' and 'leave'. recur is called with the parent
element between the traversal of its siblings.

Think of this simple graph:

```
Parent
/\
/ \
A B
```
With recur, the overall sequence of traversal will be: Enter Parent, Enter A, Leave A, Recur Parent, Enter B, Leave B, Leave Parent.
This can be useful to check, wether a node is actually a grandchild or a sibling of another node, which can be hard to detect with
enter and leave alone.

Recur only supports BREAK. Also, within replace, recur does not support replacing or removing, only BREAK.

The rest from here on is original documentation from estraverse.

Estraverse ([estraverse](http://github.com/estools/estraverse)) is
[ECMAScript](http://www.ecma-international.org/publications/standards/Ecma-262.htm)
Expand Down
18 changes: 18 additions & 0 deletions estraverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,15 @@
if (this.__state === BREAK || ret === BREAK) {
return;
}

if (worklist.length > 0 && worklist[worklist.length-1] !== sentinel) {
ret = this.__execute(visitor.recur, leavelist[leavelist.length-1]);
}

if (this.__state === BREAK || ret === BREAK) {
return;
}

continue;
}

Expand Down Expand Up @@ -625,6 +634,15 @@
if (this.__state === BREAK || target === BREAK) {
return outer.root;
}

if (worklist.length > 0 && worklist[worklist.length-1] !== sentinel) {
target = this.__execute(visitor.recur, leavelist[leavelist.length-1]);
}

if (this.__state === BREAK || target === BREAK) {
return;
}

continue;
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "estraverse",
"description": "ECMAScript JS AST traversal functions",
"homepage": "https://github.com/estools/estraverse",
"homepage": "https://github.com/ghost23/estraverse",
"main": "estraverse.js",
"version": "4.1.0",
"engines": {
Expand All @@ -16,7 +16,7 @@
],
"repository": {
"type": "git",
"url": "http://github.com/estools/estraverse.git"
"url": "http://github.com/ghost23/estraverse.git"
},
"devDependencies": {
"chai": "^2.1.1",
Expand Down
4 changes: 4 additions & 0 deletions test/controller.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ describe 'controller', ->
enter: (node) ->
dumper.log("enter - #{node.type}")

recur: (node) ->
dumper.log("recur - #{node.type}")

leave: (node) ->
dumper.log("leave - #{node.type}")

Expand All @@ -53,6 +56,7 @@ describe 'controller', ->
enter - undefined
enter - Identifier
leave - Identifier
recur - undefined
enter - Identifier
leave - Identifier
leave - undefined
Expand Down
4 changes: 4 additions & 0 deletions test/dumper.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ module.exports = class Dumper
dumper.log("enter - #{node.type}")
VisitorOption[node.$enter]

recur: (node) ->
dumper.log("recur - #{node.type}")
VisitorOption[node.$recur]

leave: (node) ->
dumper.log("leave - #{node.type}")
VisitorOption[node.$leave]
Expand Down
21 changes: 21 additions & 0 deletions test/es6.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,23 @@ describe 'class', ->
enter - ClassDeclaration
enter - Identifier
leave - Identifier
recur - ClassDeclaration
enter - Identifier
leave - Identifier
recur - ClassDeclaration
enter - ClassBody
enter - MethodDefinition
enter - Identifier
leave - Identifier
recur - MethodDefinition
enter - FunctionExpression
enter - BlockStatement
leave - BlockStatement
leave - FunctionExpression
leave - MethodDefinition
leave - ClassBody
leave - ClassDeclaration
recur - Program
enter - EmptyStatement
leave - EmptyStatement
leave - Program
Expand All @@ -97,21 +101,25 @@ describe 'class', ->
enter - ClassDeclaration
enter - Identifier
leave - Identifier
recur - ClassDeclaration
enter - CallExpression
enter - Identifier
leave - Identifier
leave - CallExpression
recur - ClassDeclaration
enter - ClassBody
enter - MethodDefinition
enter - Identifier
leave - Identifier
recur - MethodDefinition
enter - FunctionExpression
enter - BlockStatement
leave - BlockStatement
leave - FunctionExpression
leave - MethodDefinition
leave - ClassBody
leave - ClassDeclaration
recur - Program
enter - EmptyStatement
leave - EmptyStatement
leave - Program
Expand Down Expand Up @@ -142,6 +150,7 @@ describe 'export', ->
enter - VariableDeclarator
enter - Identifier
leave - Identifier
recur - VariableDeclarator
enter - Literal
leave - Literal
leave - VariableDeclarator
Expand Down Expand Up @@ -175,9 +184,11 @@ describe 'export', ->
enter - ExportSpecifier
enter - Identifier
leave - Identifier
recur - ExportSpecifier
enter - Identifier
leave - Identifier
leave - ExportSpecifier
recur - ExportNamedDeclaration
enter - Literal
leave - Literal
leave - ExportNamedDeclaration
Expand Down Expand Up @@ -208,6 +219,7 @@ describe 'export', ->
enter - ClassDeclaration
enter - Identifier
leave - Identifier
recur - ClassDeclaration
enter - ClassBody
leave - ClassBody
leave - ClassDeclaration
Expand All @@ -224,6 +236,7 @@ describe 'export', ->
enter - ClassDeclaration
enter - Identifier
leave - Identifier
recur - ClassDeclaration
enter - ClassBody
leave - ClassBody
leave - ClassDeclaration
Expand All @@ -247,6 +260,7 @@ describe 'import', ->
enter - Identifier
leave - Identifier
leave - ImportDefaultSpecifier
recur - ImportDeclaration
enter - Literal
leave - Literal
leave - ImportDeclaration
Expand All @@ -267,15 +281,19 @@ describe 'import', ->
enter - ImportSpecifier
enter - Identifier
leave - Identifier
recur - ImportSpecifier
enter - Identifier
leave - Identifier
leave - ImportSpecifier
recur - ImportDeclaration
enter - ImportSpecifier
enter - Identifier
leave - Identifier
recur - ImportSpecifier
enter - Identifier
leave - Identifier
leave - ImportSpecifier
recur - ImportDeclaration
enter - Literal
leave - Literal
leave - ImportDeclaration
Expand All @@ -297,6 +315,7 @@ describe 'import', ->
enter - Identifier
leave - Identifier
leave - ImportNamespaceSpecifier
recur - ImportDeclaration
enter - Literal
leave - Literal
leave - ImportDeclaration
Expand All @@ -320,6 +339,7 @@ describe 'pattern', ->
enter - AssignmentPattern
enter - Identifier
leave - Identifier
recur - AssignmentPattern
enter - Literal
leave - Literal
leave - AssignmentPattern
Expand Down Expand Up @@ -358,6 +378,7 @@ describe 'meta property', ->
enter - MetaProperty
enter - Identifier
leave - Identifier
recur - MetaProperty
enter - Identifier
leave - Identifier
leave - MetaProperty
Expand Down
30 changes: 30 additions & 0 deletions test/replace.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,33 @@ describe 'replace', ->
if node.type is 'Literal' and node.value is 2
VisitorOption.Remove
.to.throw('Unknown node type XXXExpression.')

it 'supports recur without remove/replace', ->
log = ""
tree =
type: 'ObjectExpression'
properties: [{
type: 'Property'
key:
type: 'Identifier'
name: 'a'
value:
type: 'BinaryExpression'
operator: '*'
left:
type: 'Literal'
value: 2
right:
type: 'Literal'
value: 3
}]

tree = replace tree,
recur: (node) ->
log += node.type + "\n"

expect(log).to.be.equal """
Property
BinaryExpression

"""
Loading