Skip to content

Commit

Permalink
reimplement using walker
Browse files Browse the repository at this point in the history
  • Loading branch information
iamstarkov committed Mar 29, 2016
1 parent deb2953 commit f382430
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parse } from 'acorn';
import { parse as _parse } from 'acorn';
import { simple } from 'acorn/dist/walk';
import R from 'ramda';

// contract :: String -> Constructor -> a
Expand All @@ -7,15 +8,20 @@ const contract = R.curry((name, ctor, param) => R.unless(
() => { throw new TypeError(`\`${name}\` should be \`${R.type(ctor())}\`, but got \`${R.type(param)}\``); }
)(param));

const log = console.log.bind(console);
const parse = input => _parse(input, { sourceType: 'module' });
const importValue = R.path(['source', 'value']);

const importList = R.unary(R.pipe(
contract('input', String),
string => parse(string, { sourceType: 'module' }),
R.prop('body'),
R.filter(R.propEq('type', 'ImportDeclaration')),
R.map(R.path(['source', 'value'])),
R.identity
parse,
ast => {
let list = [];
const addToList = item => { list = list.concat(item); };
simple(ast, {
ImportDeclaration: R.pipe(importValue, addToList)
});
return list;
}
));

export default importList;

0 comments on commit f382430

Please sign in to comment.