All files / src/helpers templates.js

100% Statements 21/21
100% Branches 0/0
100% Functions 1/1
100% Lines 21/21
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      1x   1x   1x       1x           1x           1x           1x       1x                       24x             1x                           1x       1x       1x       1x         1x                                       1x       1x       1x         1x                     1x                 1x          
 
import { template, types as t } from "@babel/core";
 
const exportName = "__exports";
 
export const exportsIdentifier = t.identifier(exportName);
 
export const buildAssign = template(`
  OBJECT.NAME = VALUE;
`);
 
export const buildDefine = template(`
  sap.ui.define(SOURCES, function (PARAMS) {
    BODY;
  });
`);
 
export const buildDefineGlobal = template(`
  sap.ui.define(SOURCES, function (PARAMS) {
    BODY;
  }, true);
`);
 
export const buildDeclareExports = template(`
  const ${exportName} = {
    __esModule: true
  };
`);
 
export const buildTempExport = template(`
  const ${exportName} = VALUE;
`);
 
export const buildReturnExports = template(`
  return ${exportName};
`);
 
// export const buildExportsModuleDeclaration = template(`
//   Object.defineProperty(${exportString}, "__esModule", {
//     value: true
//   });
// `)
 
export function buildNamedExport(obj) {
  // console.log(obj);
  return buildAssign({
    OBJECT: exportsIdentifier,
    NAME: obj.key,
    VALUE: obj.value
  });
}
 
export const buildAllExportHelper = template(`
  function extendExports(exports, obj) {
    Object.keys(obj).forEach(function (key) {
      if (key === "default" || key === "__esModule") return;
      Object.defineProperty(exports, key, {
        enumerable: true,
        get: function get() {
          return obj[key];
        }
      });
    });
  }
`);
 
export const buildAllExport = template(`
  extendExports(${exportName}, LOCAL);
`);
 
export const buildReturn = template(`
  return ID;
`);
 
export const buildDefaultImportInterop = template(`
  function _interopRequireDefault(obj) { return (obj && obj.__esModule && typeof obj.default !== "undefined") ? obj.default : obj; }
`);
 
export const buildDefaultImportDestructor = template(`
  const LOCAL = _interopRequireDefault(MODULE);
`);
 
// TODO: inject __extends instead of Object.assign unless useBuiltIns in set
export const buildDynamicImportHelper = template(`
  function __ui5_require_async(path) {
    return new Promise((resolve, reject) => {
      sap.ui.require([path], (module) => {
        if (!module) { 
          return reject("No module returned from " + path); 
        } else if (module.__esModule) { 
          return resolve(module); 
        } else if (module.default) {
          return reject(new Error(path + " module includes a 'default' property but not __esModule. Cannot use as dynamic import"));
        } else {
          module.default = typeof module === "object" ? Object.assign({}, module) : module;
          module.__esModule = true;
          resolve(module);
        }
      })
    })
  }
`);
 
export const buildConstDeclaration = template(`
  const NAME = VALUE;
`);
 
export const buildNamedImportDestructor = template(`
  const LOCAL = MODULE[IMPORTED];
`);
 
export const buildExtendAssign = template(`
  const NAME = SUPERNAME.extend(FQN, OBJECT);
`);
 
// TODO: get this one to use buildAssign
export const buildThisAssignment = template(`
  this.NAME = VALUE;
`);
 
// export const buildDefaultConstructorFunction = template(`
//   function constructor() {
//     SUPER.prototype.constructor.apply(this, arguments);
//   }
// `)
 
// This is use when there is not already the function, so always propagate arguments.
export const buildInheritingFunction = template(`
  function NAME() {
    if (typeof SUPER.prototype.NAME === 'function') {
      SUPER.prototype.NAME.apply(this, arguments);
    }
  }
`);
 
// This is use when there is not already the function, so always propagate arguments.
export const buildInheritingConstructor = template(`
  function constructor() {
    SUPER.prototype.constructor.apply(this, arguments);
  }
`);