Code coverage report for 6to5/file.js

Statements: 95.52% (64 / 67)      Branches: 83.33% (15 / 18)      Functions: 100% (11 / 11)      Lines: 95.38% (62 / 65)      Ignored: none     

All files » 6to5/ » file.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 1311   1   1 1 1 1 1   1 154 154 154 153     1 154   154               154         154 154   153     1 153 153             153     1 20 20   9 9       9     1 153   153   153 152       1 152   152   152 2916     144   144 9         144     1 144 144   144 144 4     144 144   144         144 1     144             1 124 124   124 124 124 124    
module.exports = File;
 
var SHEBANG_REGEX = /^\#\!.*/;
 
var transform = require("./transform");
var recast    = require("recast");
var util      = require("./util");
var b         = require("recast").types.builders;
var _         = require("lodash");
 
function File(opts) {
  this.declarations = {};
  this.uids         = {};
  this.opts         = File.normaliseOptions(opts);
  this.ast          = {};
}
 
File.normaliseOptions = function (opts) {
  opts = opts || {};
 
  _.defaults(opts, {
    blacklist: [],
    whitelist: [],
    sourceMap: false,
    filename:  "unknown",
    format:    {}
  });
 
  _.defaults(opts, {
    sourceFileName: opts.filename,
    sourceMapName:  opts.filename
  });
 
  transform._ensureTransformerNames("blacklist", opts.blacklist);
  transform._ensureTransformerNames("whitelist", opts.whitelist);
 
  return opts;
};
 
File.prototype.parseShebang = function (code) {
  var shebangMatch = code.match(SHEBANG_REGEX);
  Iif (shebangMatch) {
    this.shebang = shebangMatch[0];
 
    // remove shebang
    code = code.replace(SHEBANG_REGEX, "");
  }
 
  return code;
};
 
File.prototype.addDeclaration = function (name) {
  var declar = this.declarations[name];
  if (declar) return declar.uid;
 
  var uid = b.identifier(this.generateUid(name));
  this.declarations[name] = {
    uid: uid,
    node: util.template(name)
  };
  return uid;
};
 
File.prototype.parse = function (code) {
  var self = this;
 
  code = this.parseShebang(code);
 
  return util.parse(this.opts, code, function (tree) {
    return self.transform(tree);
  });
};
 
File.prototype.transform = function (ast) {
  this.ast = ast;
 
  var self = this;
 
  _.each(transform.transformers, function (transformer) {
    transformer.transform(self);
  });
 
  var body = ast.program.body;
 
  _.each(this.declarations, function (declar) {
    body.unshift(b.variableDeclaration("var", [
      b.variableDeclarator(declar.uid, declar.node)
    ]));
  });
 
  return this.generate();
};
 
File.prototype.generate = function () {
  var opts = this.opts;
  var ast  = this.ast;
 
  var printOpts = {};
  if (opts.sourceMap) {
    printOpts.sourceMapName = opts.sourceMapName;
  }
 
  var result = recast.print(ast, printOpts);
  var code   = result.code;
 
  Iif (this.shebang) {
    // add back shebang
    code = this.shebang + code;
  }
 
  if (opts.sourceMap === "inline") {
    code += "\n" + util.sourceMapToComment(result.map);
  }
 
  return {
    code: code,
    map:  result.map || null,
    ast:  ast
  };
};
 
File.prototype.generateUid = function (name) {
  var uids = this.uids;
  var i = uids[name] || 1;
 
  var id = name;
  if (i > 1) id += i;
  uids[name] = i + 1;
  return "_" + id;
};