rewrite-live-references.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = rewriteLiveReferences;
  6. function _assert() {
  7. const data = _interopRequireDefault(require("assert"));
  8. _assert = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function t() {
  14. const data = _interopRequireWildcard(require("@babel/types"));
  15. t = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _template() {
  21. const data = _interopRequireDefault(require("@babel/template"));
  22. _template = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _helperSimpleAccess() {
  28. const data = _interopRequireDefault(require("@babel/helper-simple-access"));
  29. _helperSimpleAccess = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  35. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  36. function rewriteLiveReferences(programPath, metadata) {
  37. const imported = new Map();
  38. const exported = new Map();
  39. const requeueInParent = path => {
  40. programPath.requeue(path);
  41. };
  42. for (const [source, data] of metadata.source) {
  43. for (const [localName, importName] of data.imports) {
  44. imported.set(localName, [source, importName, null]);
  45. }
  46. for (const localName of data.importsNamespace) {
  47. imported.set(localName, [source, null, localName]);
  48. }
  49. }
  50. for (const [local, data] of metadata.local) {
  51. let exportMeta = exported.get(local);
  52. if (!exportMeta) {
  53. exportMeta = [];
  54. exported.set(local, exportMeta);
  55. }
  56. exportMeta.push(...data.names);
  57. }
  58. programPath.traverse(rewriteBindingInitVisitor, {
  59. metadata,
  60. requeueInParent,
  61. scope: programPath.scope,
  62. exported
  63. });
  64. (0, _helperSimpleAccess().default)(programPath, new Set([...Array.from(imported.keys()), ...Array.from(exported.keys())]));
  65. programPath.traverse(rewriteReferencesVisitor, {
  66. seen: new WeakSet(),
  67. metadata,
  68. requeueInParent,
  69. scope: programPath.scope,
  70. imported,
  71. exported,
  72. buildImportReference: ([source, importName, localName], identNode) => {
  73. const meta = metadata.source.get(source);
  74. if (localName) {
  75. if (meta.lazy) identNode = t().callExpression(identNode, []);
  76. return identNode;
  77. }
  78. let namespace = t().identifier(meta.name);
  79. if (meta.lazy) namespace = t().callExpression(namespace, []);
  80. return t().memberExpression(namespace, t().identifier(importName));
  81. }
  82. });
  83. }
  84. const rewriteBindingInitVisitor = {
  85. ClassProperty(path) {
  86. path.skip();
  87. },
  88. Function(path) {
  89. path.skip();
  90. },
  91. ClassDeclaration(path) {
  92. const {
  93. requeueInParent,
  94. exported,
  95. metadata
  96. } = this;
  97. const {
  98. id
  99. } = path.node;
  100. if (!id) throw new Error("Expected class to have a name");
  101. const localName = id.name;
  102. const exportNames = exported.get(localName) || [];
  103. if (exportNames.length > 0) {
  104. const statement = t().expressionStatement(buildBindingExportAssignmentExpression(metadata, exportNames, t().identifier(localName)));
  105. statement._blockHoist = path.node._blockHoist;
  106. requeueInParent(path.insertAfter(statement)[0]);
  107. }
  108. },
  109. VariableDeclaration(path) {
  110. const {
  111. requeueInParent,
  112. exported,
  113. metadata
  114. } = this;
  115. Object.keys(path.getOuterBindingIdentifiers()).forEach(localName => {
  116. const exportNames = exported.get(localName) || [];
  117. if (exportNames.length > 0) {
  118. const statement = t().expressionStatement(buildBindingExportAssignmentExpression(metadata, exportNames, t().identifier(localName)));
  119. statement._blockHoist = path.node._blockHoist;
  120. requeueInParent(path.insertAfter(statement)[0]);
  121. }
  122. });
  123. }
  124. };
  125. const buildBindingExportAssignmentExpression = (metadata, exportNames, localExpr) => {
  126. return (exportNames || []).reduce((expr, exportName) => {
  127. return t().assignmentExpression("=", t().memberExpression(t().identifier(metadata.exportName), t().identifier(exportName)), expr);
  128. }, localExpr);
  129. };
  130. const buildImportThrow = localName => {
  131. return _template().default.expression.ast`
  132. (function() {
  133. throw new Error('"' + '${localName}' + '" is read-only.');
  134. })()
  135. `;
  136. };
  137. const rewriteReferencesVisitor = {
  138. ReferencedIdentifier(path) {
  139. const {
  140. seen,
  141. buildImportReference,
  142. scope,
  143. imported,
  144. requeueInParent
  145. } = this;
  146. if (seen.has(path.node)) return;
  147. seen.add(path.node);
  148. const localName = path.node.name;
  149. const localBinding = path.scope.getBinding(localName);
  150. const rootBinding = scope.getBinding(localName);
  151. if (rootBinding !== localBinding) return;
  152. const importData = imported.get(localName);
  153. if (importData) {
  154. const ref = buildImportReference(importData, path.node);
  155. ref.loc = path.node.loc;
  156. if (path.parentPath.isCallExpression({
  157. callee: path.node
  158. }) && t().isMemberExpression(ref)) {
  159. path.replaceWith(t().sequenceExpression([t().numericLiteral(0), ref]));
  160. } else if (path.isJSXIdentifier() && t().isMemberExpression(ref)) {
  161. const {
  162. object,
  163. property
  164. } = ref;
  165. path.replaceWith(t().JSXMemberExpression(t().JSXIdentifier(object.name), t().JSXIdentifier(property.name)));
  166. } else {
  167. path.replaceWith(ref);
  168. }
  169. requeueInParent(path);
  170. path.skip();
  171. }
  172. },
  173. AssignmentExpression: {
  174. exit(path) {
  175. const {
  176. scope,
  177. seen,
  178. imported,
  179. exported,
  180. requeueInParent,
  181. buildImportReference
  182. } = this;
  183. if (seen.has(path.node)) return;
  184. seen.add(path.node);
  185. const left = path.get("left");
  186. if (left.isIdentifier()) {
  187. const localName = left.node.name;
  188. if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
  189. return;
  190. }
  191. const exportedNames = exported.get(localName) || [];
  192. const importData = imported.get(localName);
  193. if (exportedNames.length > 0 || importData) {
  194. (0, _assert().default)(path.node.operator === "=", "Path was not simplified");
  195. const assignment = path.node;
  196. if (importData) {
  197. assignment.left = buildImportReference(importData, assignment.left);
  198. assignment.right = t().sequenceExpression([assignment.right, buildImportThrow(localName)]);
  199. }
  200. path.replaceWith(buildBindingExportAssignmentExpression(this.metadata, exportedNames, assignment));
  201. requeueInParent(path);
  202. }
  203. } else if (left.isMemberExpression()) {} else {
  204. const ids = left.getOuterBindingIdentifiers();
  205. const id = Object.keys(ids).filter(localName => imported.has(localName)).pop();
  206. if (id) {
  207. path.node.right = t().sequenceExpression([path.node.right, buildImportThrow(id)]);
  208. }
  209. const items = [];
  210. Object.keys(ids).forEach(localName => {
  211. if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
  212. return;
  213. }
  214. const exportedNames = exported.get(localName) || [];
  215. if (exportedNames.length > 0) {
  216. items.push(buildBindingExportAssignmentExpression(this.metadata, exportedNames, t().identifier(localName)));
  217. }
  218. });
  219. if (items.length > 0) {
  220. let node = t().sequenceExpression(items);
  221. if (path.parentPath.isExpressionStatement()) {
  222. node = t().expressionStatement(node);
  223. node._blockHoist = path.parentPath.node._blockHoist;
  224. }
  225. const statement = path.insertAfter(node)[0];
  226. requeueInParent(statement);
  227. }
  228. }
  229. }
  230. }
  231. };