A fork of acorn-jsx-walk that uses the flow parser to support ecmascript features like decorators and spread operators. Parse and walk through a Javascript JSX code source.
npm install --save flow-jsx-walk
import walk, { base } from 'flow-jsx-walk';
// base contains all the possible node walkers, see walk.js
// Program, BlockStatement, ExpressionStatement, SwitchStatement etc.
const source = `
const a = 2;
const f = (u) => {
const m = <div>Hey</div>;
return m;
}`;
walk(source, {
VariableDeclaration: (node) => { console.log(node.declarations.map(n => n.id.name)) },
});
/*
[ 'a' ]
[ 'm' ]
[ 'f' ]
*/
It's using the walk
method of acorn
but extends it with the JSX addons.
It is shamelessly copied from
acorn code
source because it is not possible to import it in another project properly.