index.js 797B

12345678910111213141516171819202122232425262728293031323334
  1. var through = require('through2');
  2. var recast = require('recast');
  3. var transformer = require('./unreachableBranchTransformer');
  4. module.exports = function (file, opts) {
  5. if (typeof file === 'string') {
  6. opts = opts || [];
  7. var ignore = ['.json'].concat(opts.ignore || []).some(function(ext) {
  8. return file.indexOf(ext, file.length - ext.length) !== -1;
  9. });
  10. if (ignore) {
  11. return through();
  12. }
  13. }
  14. var buffers = [];
  15. return through(function(chunk, enc, cb) {
  16. buffers.push(chunk);
  17. cb();
  18. }, function(cb) {
  19. var source = Buffer.concat(buffers).toString();
  20. this.push(transform(source));
  21. cb();
  22. });
  23. };
  24. function transform(code/*, opts*/) {
  25. return recast.print( transformer( recast.parse(code) ) ).code;
  26. }
  27. module.exports.transform = transform;