diff --git a/esprima-walk.js b/esprima-walk.js index df63c73..7432a24 100644 --- a/esprima-walk.js +++ b/esprima-walk.js @@ -15,7 +15,7 @@ function walk( ast, fn ) { child = node[ key ] - if ( child instanceof Array ) { + if ( child instanceof Array && key != 'range') { for ( j = 0, len = child.length; j < len; j += 1 ) { stack.push( child[ j ] ) @@ -51,13 +51,13 @@ walk.walkAddParent = function ( ast, fn ) { child = node[ key ] - if ( child instanceof Array ) { + if ( child instanceof Array && key != 'range') { for ( j = 0, len = child.length; j < len; j += 1 ) { subchild = child[ j ] - if( subchild instanceof Object ) { + if( subchild instanceof Object ) { subchild.parent = node diff --git a/package.json b/package.json index 700d57c..9d7021b 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "devDependencies": { "mocha": "~1.19.0", "chai": "~1.9.1", - "eslint": "~0.6.2" + "eslint": "~0.6.2", + "esprima": "~2.7.2" }, "scripts": { "test": "node_modules/eslint/bin/eslint.js . && node_modules/mocha/bin/mocha --reporter spec test.js" diff --git a/test.js b/test.js index 6b7968d..5e0c850 100644 --- a/test.js +++ b/test.js @@ -8,8 +8,64 @@ var expect = require( 'chai' ).expect var walk = require( './esprima-walk' ) -describe( 'esprima-walk', function () { +var esprima = require('esprima') + +var source = 'var i=0;' + + + + + + +describe('esprima-walk with range', function(){ + it('should work', function(){ + var ast = esprima.parse(source, { + range: true + }) + + var types = [] + + walk( ast, function ( node ) { + types.push( node.type ) + } ) + + var expectedTypes = [ 'Program', + 'VariableDeclaration', + 'VariableDeclarator', + 'Identifier', + 'Literal' ] + + expect( types ).to.deep.equal( expectedTypes ) + + }) +}) + +describe('esprima-walkAddParent with range', function(){ + walk = walk.walkAddParent + it('should work', function(){ + var ast = esprima.parse(source, { + range: true + }) + + var types = [] + + walk( ast, function ( node ) { + types.push( node.type ) + } ) + + var expectedTypes = [ 'Program', + 'VariableDeclaration', + 'VariableDeclarator', + 'Identifier', + 'Literal' ] + + expect( types ).to.deep.equal( expectedTypes ) + }) +}) + + +describe( 'esprima-walk', function () { it( 'should work', function () { @@ -68,3 +124,4 @@ describe( 'esprima-walk', function () { } ) +