From 83e7352b0a8e03aabb6120ee4b4865112b82f198 Mon Sep 17 00:00:00 2001 From: franklife Date: Thu, 16 Jun 2016 09:25:34 +0800 Subject: [PATCH 1/3] fix: fix range properties --- esprima-walk.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esprima-walk.js b/esprima-walk.js index df63c73..95487a6 100644 --- a/esprima-walk.js +++ b/esprima-walk.js @@ -57,7 +57,7 @@ walk.walkAddParent = function ( ast, fn ) { subchild = child[ j ] - if( subchild instanceof Object ) { + if( subchild instanceof Object && key != 'range' ) { subchild.parent = node From 8c312affd7ddfec34223c818626a4cc72f5ca01d Mon Sep 17 00:00:00 2001 From: franklife Date: Fri, 24 Jun 2016 14:30:42 +0800 Subject: [PATCH 2/3] fix: walk function add range judge --- esprima-walk.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/esprima-walk.js b/esprima-walk.js index 95487a6..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 && key != 'range' ) { + if( subchild instanceof Object ) { subchild.parent = node From 8d2745cbd19c94ead882c6ea7982d59367f51458 Mon Sep 17 00:00:00 2001 From: franklife Date: Wed, 29 Jun 2016 21:32:19 +0800 Subject: [PATCH 3/3] test --- package.json | 3 ++- test.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) 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 () { } ) +