HelperManager.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. const HELPERS = {
  2. require: `
  3. import {createRequire as CREATE_REQUIRE_NAME} from "module";
  4. const require = CREATE_REQUIRE_NAME(import.meta.url);
  5. `,
  6. interopRequireWildcard: `
  7. function interopRequireWildcard(obj) {
  8. if (obj && obj.__esModule) {
  9. return obj;
  10. } else {
  11. var newObj = {};
  12. if (obj != null) {
  13. for (var key in obj) {
  14. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  15. newObj[key] = obj[key];
  16. }
  17. }
  18. }
  19. newObj.default = obj;
  20. return newObj;
  21. }
  22. }
  23. `,
  24. interopRequireDefault: `
  25. function interopRequireDefault(obj) {
  26. return obj && obj.__esModule ? obj : { default: obj };
  27. }
  28. `,
  29. createNamedExportFrom: `
  30. function createNamedExportFrom(obj, localName, importedName) {
  31. Object.defineProperty(exports, localName, {enumerable: true, configurable: true, get: () => obj[importedName]});
  32. }
  33. `,
  34. // Note that TypeScript and Babel do this differently; TypeScript does a simple existence
  35. // check in the exports object and does a plain assignment, whereas Babel uses
  36. // defineProperty and builds an object of explicitly-exported names so that star exports can
  37. // always take lower precedence. For now, we do the easier TypeScript thing.
  38. createStarExport: `
  39. function createStarExport(obj) {
  40. Object.keys(obj)
  41. .filter((key) => key !== "default" && key !== "__esModule")
  42. .forEach((key) => {
  43. if (exports.hasOwnProperty(key)) {
  44. return;
  45. }
  46. Object.defineProperty(exports, key, {enumerable: true, configurable: true, get: () => obj[key]});
  47. });
  48. }
  49. `,
  50. nullishCoalesce: `
  51. function nullishCoalesce(lhs, rhsFn) {
  52. if (lhs != null) {
  53. return lhs;
  54. } else {
  55. return rhsFn();
  56. }
  57. }
  58. `,
  59. asyncNullishCoalesce: `
  60. async function asyncNullishCoalesce(lhs, rhsFn) {
  61. if (lhs != null) {
  62. return lhs;
  63. } else {
  64. return await rhsFn();
  65. }
  66. }
  67. `,
  68. optionalChain: `
  69. function optionalChain(ops) {
  70. let lastAccessLHS = undefined;
  71. let value = ops[0];
  72. let i = 1;
  73. while (i < ops.length) {
  74. const op = ops[i];
  75. const fn = ops[i + 1];
  76. i += 2;
  77. if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) {
  78. return undefined;
  79. }
  80. if (op === 'access' || op === 'optionalAccess') {
  81. lastAccessLHS = value;
  82. value = fn(value);
  83. } else if (op === 'call' || op === 'optionalCall') {
  84. value = fn((...args) => value.call(lastAccessLHS, ...args));
  85. lastAccessLHS = undefined;
  86. }
  87. }
  88. return value;
  89. }
  90. `,
  91. asyncOptionalChain: `
  92. async function asyncOptionalChain(ops) {
  93. let lastAccessLHS = undefined;
  94. let value = ops[0];
  95. let i = 1;
  96. while (i < ops.length) {
  97. const op = ops[i];
  98. const fn = ops[i + 1];
  99. i += 2;
  100. if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) {
  101. return undefined;
  102. }
  103. if (op === 'access' || op === 'optionalAccess') {
  104. lastAccessLHS = value;
  105. value = await fn(value);
  106. } else if (op === 'call' || op === 'optionalCall') {
  107. value = await fn((...args) => value.call(lastAccessLHS, ...args));
  108. lastAccessLHS = undefined;
  109. }
  110. }
  111. return value;
  112. }
  113. `,
  114. optionalChainDelete: `
  115. function optionalChainDelete(ops) {
  116. const result = OPTIONAL_CHAIN_NAME(ops);
  117. return result == null ? true : result;
  118. }
  119. `,
  120. asyncOptionalChainDelete: `
  121. async function asyncOptionalChainDelete(ops) {
  122. const result = await ASYNC_OPTIONAL_CHAIN_NAME(ops);
  123. return result == null ? true : result;
  124. }
  125. `,
  126. };
  127. export class HelperManager {
  128. __init() {this.helperNames = {}}
  129. __init2() {this.createRequireName = null}
  130. constructor( nameManager) {;this.nameManager = nameManager;HelperManager.prototype.__init.call(this);HelperManager.prototype.__init2.call(this);}
  131. getHelperName(baseName) {
  132. let helperName = this.helperNames[baseName];
  133. if (helperName) {
  134. return helperName;
  135. }
  136. helperName = this.nameManager.claimFreeName(`_${baseName}`);
  137. this.helperNames[baseName] = helperName;
  138. return helperName;
  139. }
  140. emitHelpers() {
  141. let resultCode = "";
  142. if (this.helperNames.optionalChainDelete) {
  143. this.getHelperName("optionalChain");
  144. }
  145. if (this.helperNames.asyncOptionalChainDelete) {
  146. this.getHelperName("asyncOptionalChain");
  147. }
  148. for (const [baseName, helperCodeTemplate] of Object.entries(HELPERS)) {
  149. const helperName = this.helperNames[baseName];
  150. let helperCode = helperCodeTemplate;
  151. if (baseName === "optionalChainDelete") {
  152. helperCode = helperCode.replace("OPTIONAL_CHAIN_NAME", this.helperNames.optionalChain);
  153. } else if (baseName === "asyncOptionalChainDelete") {
  154. helperCode = helperCode.replace(
  155. "ASYNC_OPTIONAL_CHAIN_NAME",
  156. this.helperNames.asyncOptionalChain,
  157. );
  158. } else if (baseName === "require") {
  159. if (this.createRequireName === null) {
  160. this.createRequireName = this.nameManager.claimFreeName("_createRequire");
  161. }
  162. helperCode = helperCode.replace(/CREATE_REQUIRE_NAME/g, this.createRequireName);
  163. }
  164. if (helperName) {
  165. resultCode += " ";
  166. resultCode += helperCode.replace(baseName, helperName).replace(/\s+/g, " ").trim();
  167. }
  168. }
  169. return resultCode;
  170. }
  171. }