All files / source/CaffeineMc Metacompiler.js

83.04% Statements 93/112
64.38% Branches 47/73
89.47% Functions 17/19
81.73% Lines 85/104

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260  7x   469x 7x   7x   7x   7x   7x   7x   7x   7x   7x   7x 7x   7x 13x 10x   13x     7x   45x       7x           7x                                           18x 18x 9x 9x 9x                               22x 22x 22x 22x 22x     7x   40x 17x         23x           23x       23x 23x                                                               7x 30x 30x 30x     30x 7x   23x       7x 27x     7x   27x     27x     7x   27x 1x 1x 1x       1x             27x     7x   7x 7x           7x 7x             7x 3x   4x       7x   46x 46x 19x 19x   27x       7x             7x   9x 4x   9x     9x 4x   5x 5x 5x 5x 5x           5x     7x          
// Generated by CoffeeScript 1.12.7
(function() {
  var BaseClass, CaffeineMc, CaffeineMcParser, CompileCache, Compilers, Metacompiler, checkWorkingCacheExpiration, dashCase, formattedInspect, isArray, isFunction, isObject, isString, log, lowerCamelCase, merge, objectWithout, present, realRequire, ref, upperCamelCase,
    extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
    hasProp = {}.hasOwnProperty;
 
  Compilers = require('./Compilers');
 
  CaffeineMcParser = require('./CaffeineMcParser');
 
  CaffeineMc = require('./namespace');
 
  CompileCache = require('./CompileCache');
 
  realRequire = eval('require');
 
  ref = require('art-standard-lib'), dashCase = ref.dashCase, formattedInspect = ref.formattedInspect, present = ref.present, isFunction = ref.isFunction, log = ref.log, isString = ref.isString, lowerCamelCase = ref.lowerCamelCase, upperCamelCase = ref.upperCamelCase, merge = ref.merge, objectWithout = ref.objectWithout, isArray = ref.isArray, isObject = ref.isObject;
 
  BaseClass = require('art-class-system').BaseClass;
 
  checkWorkingCacheExpiration = require('./WorkingCache').checkWorkingCacheExpiration;
 
  module.exports = Metacompiler = (function(superClass) {
    extend(Metacompiler, superClass);
 
    Metacompiler.compile = function(code, options) {
      if (options == null) {
        options = {};
      }
      return new Metacompiler().compile(code, options);
    };
 
    Metacompiler.classGetter({
      CaffeineScript: function() {
        return require('caffeine-script');
      }
    });
 
    Metacompiler.getter("compiler lastMetacompilerResult", {
      current: function() {
        return this.compiler;
      }
    });
 
    Metacompiler.setter({
 
      /*
        IN:
          string: configure to use one of the CaffeineCompiler classes
          function: compileFunction
          object:
            compiler: custom compiler instance. Must implement:
              compile: compileFunction
      
        compileFunction: (sourceCode, options) ->
          IN:
            sourceCode: string
            options: {}
          ERROR: throw errors
          OUT:
            evalable-js-string
            OR
            object with at least:
              compiled: js: evalable-js-string
       */
      compiler: function(arg, options) {
        return this._compiler = (function() {
          if (isString(arg)) {
            return this.getCompiler(arg, options);
          } else Eif (isFunction(arg.compile)) {
            return arg;
          } else if (isFunction(arg)) {
            return {
              compile: arg
            };
          } else {
            log.error({
              InavlidCompiler: arg
            });
            throw new Error("CaffeineMc: @compiler must be a function or be an object with a .compile method.");
          }
        }).call(this);
      }
    });
 
    function Metacompiler() {
      Metacompiler.__super__.constructor.apply(this, arguments);
      this._metaParser = new CaffeineMcParser;
      this._metaCompiler = this;
      this._compiler = this["class"].CaffeineScript;
      this.compilers = {};
    }
 
    Metacompiler.prototype.normalizeCompilerResult = function(result) {
      var ref1;
      if (isString(result)) {
        return {
          compiled: {
            js: result
          }
        };
      } else Iif (isString(result != null ? result.code : void 0)) {
        return {
          compiled: {
            js: result.code
          }
        };
      } else Iif (isString(result != null ? result.js : void 0)) {
        return {
          compiled: result
        };
      } else Eif (isString(result != null ? (ref1 = result.compiled) != null ? ref1.js : void 0 : void 0)) {
        return result;
      } else {
        log.error({
          normalizeCompilerResult: {
            result: result,
            compiler: this.compiler
          }
        });
        throw new Error("CaffeineMc: expected @compiler result to be: (string), {js: string}, or {compiled: {js: string}}. Was: " + (formattedInspect(result)));
      }
    };
 
 
    /*
      IN:
        code: string
        options:
          sourceMap: t/f
          inlineMap: t/f
          sourceFile:
          sourceDir:
    
      OUT: (an object)
        compiled: extension => output map
          extension: string, ex: "js"
          output: string, ex: "alert();"
    
          If writing to files, we might do:
          for extension, output of compiled
            write originalFileNameWith(extension), output
     */
 
    Metacompiler.prototype.compile = function(code, options) {
      checkWorkingCacheExpiration();
      options = merge(Neptune.CaffeineMc.globalCompilerOptions, options);
      Iif (options.prettier && (options.inlineMap || options.sourceMap)) {
        throw new Error("prettier does not support sourcemaps");
      }
      if (options.cache && options.sourceFile) {
        return this._compileWithCaching(code, options);
      } else {
        return this._postprocess(options, this._compileWithMetacompiler(code, options));
      }
    };
 
    Metacompiler.prototype._postprocess = function(options, out) {
      return this._postprocesPrettier(options, this._postprocessWithTranspiler(options, out));
    };
 
    Metacompiler.prototype._postprocessWithTranspiler = function(options, out) {
      var transpileOptions;
      Iif (transpileOptions = options.transpile) {
        throw new Error("DEPRICATED: transpile option");
      }
      return out;
    };
 
    Metacompiler.prototype._postprocesPrettier = function(options, out) {
      var e;
      if (options.prettier) {
        try {
          Eif (out.compiled.js != null) {
            out.compiled.js = require("prettier").format(out.compiled.js, {
              parser: "babel"
            });
          }
          out.prettier = true;
        } catch (error) {
          e = error;
          log(e.message);
          throw e;
        }
      }
      return out;
    };
 
    Metacompiler.prototype._compileWithCaching = function(code, options) {
      var cacheInfo, cachedCompile, inlineMap, prettier, ref1, ref2, transpile;
      options = objectWithout(options, "cache");
      cacheInfo = {
        compiler: this.compiler,
        source: code,
        verbose: options.verbose,
        sourceFile: options.sourceFile
      };
      prettier = options.prettier, inlineMap = options.inlineMap, transpile = options.transpile;
      Iif ((ref1 = (ref2 = prettier != null) != null ? ref2 : inlineMap != null) != null ? ref1 : transpile != null) {
        cacheInfo.compilerOptions = {
          prettier: prettier,
          inlineMap: inlineMap,
          transpile: transpile
        };
      }
      if (cachedCompile = CompileCache.fetch(cacheInfo)) {
        return cachedCompile;
      } else {
        return CompileCache.cache(merge(cacheInfo, this._postprocess(options, this._compileWithMetacompiler(code, options))));
      }
    };
 
    Metacompiler.prototype._compileWithMetacompiler = function(rawCode, options) {
      var code, compilerName, metaCode, ref1, result;
      ref1 = this._metaParser.parse(rawCode.toString()), compilerName = ref1.compilerName, metaCode = ref1.metaCode, code = ref1.code;
      if (metaCode || compilerName) {
        this._lastMetacompilerResult = metaCode ? (result = this.normalizeCompilerResult(this.compiler.compile(metaCode)), CaffeineMc.evalInContext(result.compiled.js, this)) : this.setCompiler(compilerName, options);
        return this._compileWithMetacompiler(code, options);
      } else {
        return this.normalizeCompilerResult(this.compiler.compile(code, options));
      }
    };
 
    Metacompiler.getter({
      compilerName: function() {
        var base, base1;
        return (typeof (base = this.compiler).getClassName === "function" ? base.getClassName() : void 0) || (typeof (base1 = this.compiler).getName === "function" ? base1.getName() : void 0) || this._compilerName || 'unknown-compiler';
      }
    });
 
    Metacompiler.prototype.getCompiler = function(compilerName, options) {
      var absolutePath, base, compiler, out, ucCompilerName;
      if (compilerName.toLocaleLowerCase() === "javascript") {
        compilerName = "JavaScript";
      }
      Iif (!present(compilerName)) {
        return this.compiler;
      }
      if (compiler = Compilers[ucCompilerName = upperCamelCase(compilerName)]) {
        return compiler;
      }
      this._compilerName = compilerName;
      absolutePath = CaffeineMc.findModuleSync(compilerName, options).absolutePath;
      try {
        out = (base = this.compilers)[absolutePath] || (base[absolutePath] = realRequire(absolutePath));
        Iif (!isFunction(out.compile)) {
          throw new Error;
        }
      } catch (error) {
        throw new Error("CaffeineMc: compiler not found for: " + compilerName + " (normalized: " + ucCompilerName + ", require: " + absolutePath + ")");
      }
      return out;
    };
 
    return Metacompiler;
 
  })(BaseClass);
 
}).call(this);