mirror of https://github.com/lukechilds/rollup.git
Rich-Harris
9 years ago
5 changed files with 4 additions and 64 deletions
@ -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 ); |
|
||||
} |
|
||||
} |
|
Loading…
Reference in new issue