From 8d093d07d4ff881e8403b0355ed5a181106bf597 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 3 Oct 2015 19:21:15 -0400 Subject: [PATCH] use estree-walker module --- package.json | 1 + src/Module.js | 4 +-- src/Statement.js | 2 +- src/ast/attachScopes.js | 2 +- src/ast/walk.js | 59 ----------------------------------------- 5 files changed, 4 insertions(+), 64 deletions(-) delete mode 100644 src/ast/walk.js diff --git a/package.json b/package.json index 1a4fe8b..3849a5f 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,7 @@ "dependencies": { "acorn": "^2.3.0", "chalk": "^1.0.0", + "estree-walker": "^0.1.3", "magic-string": "^0.7.0", "minimist": "^1.1.1", "sander": "^0.3.3", diff --git a/src/Module.js b/src/Module.js index f355026..55316e4 100644 --- a/src/Module.js +++ b/src/Module.js @@ -1,7 +1,7 @@ import { parse } from 'acorn'; import MagicString from 'magic-string'; +import { walk } from 'estree-walker'; import Statement from './Statement'; -import walk from './ast/walk'; import { blank, keys } from './utils/object'; import { basename, extname } from './utils/path'; import getLocation from './utils/getLocation'; @@ -20,8 +20,6 @@ class SyntheticDefaultDeclaration { addReference ( reference ) { reference.declaration = this; this.name = reference.name; - - console.log( 'this.name', this.name ) } } diff --git a/src/Statement.js b/src/Statement.js index 8ff6c77..b15d723 100644 --- a/src/Statement.js +++ b/src/Statement.js @@ -1,4 +1,4 @@ -import walk from './ast/walk'; +import { walk } from 'estree-walker'; import Scope from './ast/Scope'; import attachScopes from './ast/attachScopes'; diff --git a/src/ast/attachScopes.js b/src/ast/attachScopes.js index 49716a4..ffd47f9 100644 --- a/src/ast/attachScopes.js +++ b/src/ast/attachScopes.js @@ -1,4 +1,4 @@ -import walk from './walk'; +import { walk } from 'estree-walker'; import Scope from './Scope'; const blockDeclarations = { diff --git a/src/ast/walk.js b/src/ast/walk.js deleted file mode 100644 index b91c68b..0000000 --- a/src/ast/walk.js +++ /dev/null @@ -1,59 +0,0 @@ -import { blank } from '../utils/object'; - -let shouldSkip; -let shouldAbort; - -export default function walk ( ast, { enter, leave }) { - shouldAbort = false; - visit( ast, null, enter, leave ); -} - -let context = { - skip: () => shouldSkip = true, - abort: () => shouldAbort = true -}; - -let childKeys = blank(); - -let toString = Object.prototype.toString; - -function isArray ( thing ) { - return toString.call( thing ) === '[object Array]'; -} - -function visit ( node, parent, enter, leave ) { - if ( !node || shouldAbort ) return; - - if ( enter ) { - shouldSkip = false; - enter.call( context, node, parent ); - if ( shouldSkip || shouldAbort ) return; - } - - let keys = childKeys[ node.type ] || ( - childKeys[ node.type ] = Object.keys( node ).filter( key => typeof node[ key ] === 'object' ) - ); - - let key, value, i, j; - - i = keys.length; - while ( i-- ) { - key = keys[i]; - value = node[ key ]; - - if ( isArray( value ) ) { - j = value.length; - while ( j-- ) { - visit( value[j], node, enter, leave ); - } - } - - else if ( value && value.type ) { - visit( value, node, enter, leave ); - } - } - - if ( leave && !shouldAbort ) { - leave( node, parent ); - } -}