'use strict';

// MODULES //

var walk = require( 'acorn/dist/walk.js' ).full;
var contains = require( '@stdlib/assert/contains' );
var types = require( './node_types.js' );


// MAIN //

/**
* Flattens an AST.
*
* @private
* @param {Node} ast - AST node
* @returns {Array} flattened AST
*/
function flatten( ast ) {
	var out = [];
	walk( ast, visit );
	return out;

	/**
	* Callback invoked upon visiting an AST node.
	*
	* @private
	* @param {Node} node - AST node
	* @param {*} state - state
	* @param {string} type - node type
	*/
	function visit( node, state, type ) {
		if ( node !== ast && contains( types, type ) ) {
			out.push( node );
		}
	} // end FUNCTION visit()
} // end FUNCTION flatten()


// EXPORTS //

module.exports = flatten;

'use strict';

// MODULES //

var walk = require( 'acorn/dist/walk.js' ).fullAncestor;
var contains = require( '@stdlib/assert/contains' );
var types = require( './node_types.js' );


// MAIN //

/**
* Flattens an AST.
*
* @private
* @param {Node} ast - AST node
* @returns {Array} flattened AST
*/
function flatten( ast ) {
	var out = [];
	walk( ast, visit );
	return out;

	/**
	* Callback invoked upon visiting an AST node.
	*
	* @private
	* @param {Node} node - AST node
	* @param {*} state - state
	* @param {Array<Node>} ancestors - ancestor nodes
	*/
	function visit( node ) {
		if ( node !== ast && contains( types, node.type ) ) {
			out.push( node );
		}
	} // end FUNCTION visit()
} // end FUNCTION flatten()


// EXPORTS //

module.exports = flatten;

Accumulate ancestor nodes during walk
Accumulate ancestor nodes during walk
