This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
83 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,36 @@ | ||
var estraverse = require('estraverse'); | ||
|
||
module.exports = { | ||
iterate: iterate | ||
}; | ||
|
||
var iterableProperties = { | ||
'body': true, | ||
'expression': true, | ||
|
||
// if | ||
'test': true, | ||
'consequent': true, | ||
'alternate': true, | ||
|
||
'object': true, | ||
|
||
//switch | ||
'discriminant': true, | ||
'cases': true, | ||
|
||
// return | ||
'argument': true, | ||
'arguments': true, | ||
|
||
// try | ||
'block': true, | ||
'guardedHandlers': true, | ||
'handlers': true, | ||
'finalizer': true, | ||
|
||
// catch | ||
'handler': true, | ||
|
||
// for | ||
'init': true, | ||
'update': true, | ||
|
||
// for in | ||
'left': true, | ||
'right': true, | ||
|
||
// var | ||
'declarations': true, | ||
|
||
// array | ||
'elements': true, | ||
|
||
// object | ||
'properties': true, | ||
'key': true, | ||
'value': true, | ||
|
||
// new | ||
'callee': true, | ||
|
||
// export (ES6) | ||
'declaration': true, | ||
|
||
// xxx.yyy | ||
'property': true | ||
}; | ||
|
||
function iterate(node, cb, parentNode, parentCollection) { | ||
if (cb(node, parentNode, parentCollection) === false) { | ||
return; | ||
} | ||
|
||
for (var propName in node) { | ||
if (node.hasOwnProperty(propName)) { | ||
if (iterableProperties[propName]) { | ||
var contents = node[propName]; | ||
if (typeof contents === 'object') { | ||
if (Array.isArray(contents)) { | ||
for (var i = 0, l = contents.length; i < l; i++) { | ||
iterate(contents[i], cb, node, contents); | ||
function iterate(node, cb) { | ||
if ('type' in node) { | ||
estraverse.traverse(node, { | ||
enter: function(node, parent) { | ||
var parentCollection = []; | ||
|
||
// parentCollection support | ||
var path = this.path(); | ||
if (path) { | ||
var collectionKey; | ||
while (path.length > 0) { | ||
var pathElement = path.pop(); | ||
if (typeof pathElement === 'string') { | ||
collectionKey = pathElement; | ||
} | ||
} else { | ||
iterate(contents, cb, node, [contents]); | ||
} | ||
|
||
parentCollection = parent[collectionKey]; | ||
if (!Array.isArray(parentCollection)) { | ||
parentCollection = [parentCollection]; | ||
} | ||
} | ||
|
||
if (cb(node, parent, parentCollection) === false) { | ||
return estraverse.VisitorOption.Skip; | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
var assert = require('assert'); | ||
var esprima = require('esprima'); | ||
var sinon = require('sinon'); | ||
var iterate = require('../lib/tree-iterator').iterate; | ||
|
||
describe('modules/tree-iterator', function() { | ||
|
||
it('should pass parent and parentCollection', function() { | ||
var spy = sinon.spy(); | ||
iterate(esprima.parse('1;', {loc: true, range: true, comment: true, tokens: true}), spy); | ||
assert.equal(spy.callCount, 3); | ||
assert.equal(spy.getCall(0).args[0].type, 'Program'); | ||
assert.equal(spy.getCall(0).args[1], null); | ||
assert.equal(spy.getCall(1).args[0].type, 'ExpressionStatement'); | ||
assert.equal(spy.getCall(2).args[0].type, 'Literal'); | ||
}); | ||
|
||
it('should not fail for empty object', function() { | ||
var spy = sinon.spy(); | ||
iterate({}, spy); | ||
assert.equal(spy.callCount, 0); | ||
}); | ||
|
||
it('should exit thread on false result', function() { | ||
var spy = sinon.spy(function() { | ||
return false; | ||
}); | ||
iterate(esprima.parse('1;', {loc: true, range: true, comment: true, tokens: true}), spy); | ||
assert.equal(spy.callCount, 1); | ||
assert.equal(spy.getCall(0).args[0].type, 'Program'); | ||
}); | ||
|
||
}); |