HelperManager.js 5.6 KB

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