Code coverage report for 6to5/lib/6to5/register.js

Statements: 77.32% (75 / 97)      Branches: 54.76% (23 / 42)      Functions: 80% (8 / 10)      Lines: 84.09% (74 / 88)      Ignored: none     

All files » 6to5/lib/6to5/ » register.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 1491   1 1 1 1 1 1   1   676 676           676             1   1 18 18 2   16       18       1 1 1 1 1 1 1 1       1   1 1 1 1 1 1 1 1 1 1 1 1       1 1 1 1 1 1 1 1   1       1 9       9   9             9             9         9   9     1 1       1   1 3 3       1   1   2 2 2   2 2   2   2 2 2           2    
require("./polyfill");
 
var sourceMapSupport = require("source-map-support");
var roadrunner       = require("roadrunner");
var util             = require("./util");
var to5              = require("./index");
var fs               = require("fs");
var _                = require("lodash");
 
sourceMapSupport.install({
  retrieveSourceMap: function (source) {
    var map = maps && maps[source];
    Iif (map) {
      return {
        url: null,
        map: map
      };
    } else {
      return null;
    }
  }
});
 
//
 
var blacklist  = [];
 
var blacklistTest = function (transformer, code) {
  try {
    if (_.isFunction(code)) {
      code();
    } else {
      new Function(code);
    }
    blacklist.push(transformer);
  } catch (err) {
    Iif (err.name !== "SyntaxError") throw err;
  }
};
 
blacklistTest("arrayComprehension", "var foo = [for (foo of bar) foo * foo];");
blacklistTest("generatorComprehension", "var foo = (for (foo of bar) foo * foo)");
blacklistTest("arrowFunctions", "var foo = x => x * x;");
blacklistTest("classes", "class Foo {}");
blacklistTest("computedPropertyNames", "var foo = { [foo]: bar };");
blacklistTest("constants", function () {
  try {
    new Function("const foo = 'foo';\nfoo = 'wow';");
  } catch (err) {
    return; // constants are supported
  }
  throw new SyntaxError;
});
blacklistTest("defaultParamaters", "var foo = function (bar = 0) {};");
blacklistTest("destructuring", "var { x, y } = { x: 0, y: 0 };");
blacklistTest("forOf", "for (var foo of bar) {}");
blacklistTest("generators", "function* foo() {}\nasync function bar() {}"); // generators/async functions delegated to same transformer
blacklistTest("letScoping", "let foo = 0;");
blacklistTest("modules", 'import foo from "from";');
blacklistTest("propertyMethodAssignment", "{ get foo() {} }");
blacklistTest("propertyNameShorthand", "var foo = { x, y };");
blacklistTest("restParameters", "function foo(...bar) {}");
blacklistTest("spread", "foo(...bar);");
blacklistTest("templateLiterals", "var foo = `foo`;");
blacklistTest("unicodeRegex", function () { new RegExp("foo", "u"); });
 
//
 
var transformOpts = {};
var ignoreRegex   = /node_modules/;
var onlyRegex;
var whitelist     = [];
var cache;
var exts          = {};
var maps          = {};
var old           = require.extensions[".js"];
 
var mtime = function (filename) {
  return +fs.statSync(filename).mtime;
};
 
var loader = function (m, filename) {
  Iif ((ignoreRegex && ignoreRegex.test(filename)) || (onlyRegex && !onlyRegex.test(filename))) {
    return old.apply(this, arguments);
  }
 
  var result;
 
  Iif (cache) {
    var cached = cache[filename];
    if (cached && cached.mtime === mtime(filename)) {
      result = cached;
    }
  }
 
  result = result || to5.transformFileSync(filename, _.extend({
    whitelist: whitelist,
    blacklist: blacklist,
    sourceMap: true,
    ast:       false
  }, transformOpts));
 
  Iif (cache) {
    result.mtime = mtime(filename);
    cache[filename] = result;
  }
 
  maps[filename] = result.map;
 
  m._compile(result.code, filename);
};
 
var hookExtensions = function (_exts) {
  _.each(exts, function (old, ext) {
    require.extensions[ext] = old;
  });
 
  exts = {};
 
  _.each(_exts, function (ext) {
    exts[ext] = require.extensions[ext];
    require.extensions[ext] = loader;
  });
};
 
hookExtensions([".es6", ".es", ".js"]);
 
module.exports = function (opts) {
  // normalise options
  opts = opts || {};
  Iif (_.isRegExp(opts)) opts = { ignore: opts };
  Iif (opts.ignoreRegex != null) opts.ignore = opts.ignoreRegex;
 
  Iif (opts.only != null) onlyRegex = util.regexify(opts.only);
  Iif (opts.ignore != null) ignoreRegex = util.regexify(opts.ignore);
 
  Iif (opts.extensions) hookExtensions(util.arrayify(opts.extensions));
 
  Iif (opts.cache) cache = opts.cache;
  Iif (opts.cache === false) cache = null;
  Iif (opts.cache === true) {
    roadrunner.load();
    roadrunner.setup();
    cache = roadrunner.get("6to5");
  }
 
  _.extend(transformOpts, opts);
};