| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = rewriteLiveReferences;
|
|---|
| 7 | var _core = require("@babel/core");
|
|---|
| 8 | function isInType(path) {
|
|---|
| 9 | do {
|
|---|
| 10 | switch (path.parent.type) {
|
|---|
| 11 | case "TSTypeAnnotation":
|
|---|
| 12 | case "TSTypeAliasDeclaration":
|
|---|
| 13 | case "TSTypeReference":
|
|---|
| 14 | case "TypeAnnotation":
|
|---|
| 15 | case "TypeAlias":
|
|---|
| 16 | return true;
|
|---|
| 17 | case "ExportSpecifier":
|
|---|
| 18 | return path.parentPath.parent.exportKind === "type";
|
|---|
| 19 | default:
|
|---|
| 20 | if (path.parentPath.isStatement() || path.parentPath.isExpression()) {
|
|---|
| 21 | return false;
|
|---|
| 22 | }
|
|---|
| 23 | }
|
|---|
| 24 | } while (path = path.parentPath);
|
|---|
| 25 | }
|
|---|
| 26 | function rewriteLiveReferences(programPath, metadata, wrapReference) {
|
|---|
| 27 | const imported = new Map();
|
|---|
| 28 | const exported = new Map();
|
|---|
| 29 | const requeueInParent = path => {
|
|---|
| 30 | programPath.requeue(path);
|
|---|
| 31 | };
|
|---|
| 32 | for (const [source, data] of metadata.source) {
|
|---|
| 33 | for (const [localName, importName] of data.imports) {
|
|---|
| 34 | imported.set(localName, [source, importName, null]);
|
|---|
| 35 | }
|
|---|
| 36 | for (const localName of data.importsNamespace) {
|
|---|
| 37 | imported.set(localName, [source, null, localName]);
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 | for (const [local, data] of metadata.local) {
|
|---|
| 41 | let exportMeta = exported.get(local);
|
|---|
| 42 | if (!exportMeta) {
|
|---|
| 43 | exportMeta = [];
|
|---|
| 44 | exported.set(local, exportMeta);
|
|---|
| 45 | }
|
|---|
| 46 | exportMeta.push(...data.names);
|
|---|
| 47 | }
|
|---|
| 48 | const rewriteBindingInitVisitorState = {
|
|---|
| 49 | metadata,
|
|---|
| 50 | requeueInParent,
|
|---|
| 51 | scope: programPath.scope,
|
|---|
| 52 | exported
|
|---|
| 53 | };
|
|---|
| 54 | programPath.traverse(rewriteBindingInitVisitor, rewriteBindingInitVisitorState);
|
|---|
| 55 | const rewriteReferencesVisitorState = {
|
|---|
| 56 | seen: new WeakSet(),
|
|---|
| 57 | metadata,
|
|---|
| 58 | requeueInParent,
|
|---|
| 59 | scope: programPath.scope,
|
|---|
| 60 | imported,
|
|---|
| 61 | exported,
|
|---|
| 62 | buildImportReference([source, importName, localName], identNode) {
|
|---|
| 63 | const meta = metadata.source.get(source);
|
|---|
| 64 | meta.referenced = true;
|
|---|
| 65 | if (localName) {
|
|---|
| 66 | if (meta.wrap) {
|
|---|
| 67 | var _wrapReference;
|
|---|
| 68 | identNode = (_wrapReference = wrapReference(identNode, meta.wrap)) != null ? _wrapReference : identNode;
|
|---|
| 69 | }
|
|---|
| 70 | return identNode;
|
|---|
| 71 | }
|
|---|
| 72 | let namespace = _core.types.identifier(meta.name);
|
|---|
| 73 | if (meta.wrap) {
|
|---|
| 74 | var _wrapReference2;
|
|---|
| 75 | namespace = (_wrapReference2 = wrapReference(namespace, meta.wrap)) != null ? _wrapReference2 : namespace;
|
|---|
| 76 | }
|
|---|
| 77 | if (importName === "default" && meta.interop === "node-default") {
|
|---|
| 78 | return namespace;
|
|---|
| 79 | }
|
|---|
| 80 | const computed = metadata.stringSpecifiers.has(importName);
|
|---|
| 81 | return _core.types.memberExpression(namespace, computed ? _core.types.stringLiteral(importName) : _core.types.identifier(importName), computed);
|
|---|
| 82 | }
|
|---|
| 83 | };
|
|---|
| 84 | programPath.traverse(rewriteReferencesVisitor, rewriteReferencesVisitorState);
|
|---|
| 85 | }
|
|---|
| 86 | const rewriteBindingInitVisitor = {
|
|---|
| 87 | Scope(path) {
|
|---|
| 88 | path.skip();
|
|---|
| 89 | },
|
|---|
| 90 | ClassDeclaration(path) {
|
|---|
| 91 | const {
|
|---|
| 92 | requeueInParent,
|
|---|
| 93 | exported,
|
|---|
| 94 | metadata
|
|---|
| 95 | } = this;
|
|---|
| 96 | const {
|
|---|
| 97 | id
|
|---|
| 98 | } = path.node;
|
|---|
| 99 | if (!id) throw new Error("Expected class to have a name");
|
|---|
| 100 | const localName = id.name;
|
|---|
| 101 | const exportNames = exported.get(localName) || [];
|
|---|
| 102 | if (exportNames.length > 0) {
|
|---|
| 103 | const statement = _core.types.expressionStatement(buildBindingExportAssignmentExpression(metadata, exportNames, _core.types.identifier(localName), path.scope));
|
|---|
| 104 | statement._blockHoist = path.node._blockHoist;
|
|---|
| 105 | requeueInParent(path.insertAfter(statement)[0]);
|
|---|
| 106 | }
|
|---|
| 107 | },
|
|---|
| 108 | VariableDeclaration(path) {
|
|---|
| 109 | const {
|
|---|
| 110 | requeueInParent,
|
|---|
| 111 | exported,
|
|---|
| 112 | metadata
|
|---|
| 113 | } = this;
|
|---|
| 114 | const isVar = path.node.kind === "var";
|
|---|
| 115 | for (const decl of path.get("declarations")) {
|
|---|
| 116 | const {
|
|---|
| 117 | id
|
|---|
| 118 | } = decl.node;
|
|---|
| 119 | let {
|
|---|
| 120 | init
|
|---|
| 121 | } = decl.node;
|
|---|
| 122 | if (_core.types.isIdentifier(id) && exported.has(id.name) && !_core.types.isArrowFunctionExpression(init) && (!_core.types.isFunctionExpression(init) || init.id) && (!_core.types.isClassExpression(init) || init.id)) {
|
|---|
| 123 | if (!init) {
|
|---|
| 124 | if (isVar) {
|
|---|
| 125 | continue;
|
|---|
| 126 | } else {
|
|---|
| 127 | init = path.scope.buildUndefinedNode();
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 | decl.node.init = buildBindingExportAssignmentExpression(metadata, exported.get(id.name), init, path.scope);
|
|---|
| 131 | requeueInParent(decl.get("init"));
|
|---|
| 132 | } else {
|
|---|
| 133 | for (const localName of Object.keys(decl.getOuterBindingIdentifiers())) {
|
|---|
| 134 | if (exported.has(localName)) {
|
|---|
| 135 | const statement = _core.types.expressionStatement(buildBindingExportAssignmentExpression(metadata, exported.get(localName), _core.types.identifier(localName), path.scope));
|
|---|
| 136 | statement._blockHoist = path.node._blockHoist;
|
|---|
| 137 | requeueInParent(path.insertAfter(statement)[0]);
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 | };
|
|---|
| 144 | const buildBindingExportAssignmentExpression = (metadata, exportNames, localExpr, scope) => {
|
|---|
| 145 | const exportsObjectName = metadata.exportName;
|
|---|
| 146 | for (let currentScope = scope; currentScope != null; currentScope = currentScope.parent) {
|
|---|
| 147 | if (currentScope.hasOwnBinding(exportsObjectName)) {
|
|---|
| 148 | currentScope.rename(exportsObjectName);
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 | return (exportNames || []).reduce((expr, exportName) => {
|
|---|
| 152 | const {
|
|---|
| 153 | stringSpecifiers
|
|---|
| 154 | } = metadata;
|
|---|
| 155 | const computed = stringSpecifiers.has(exportName);
|
|---|
| 156 | return _core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.identifier(exportsObjectName), computed ? _core.types.stringLiteral(exportName) : _core.types.identifier(exportName), computed), expr);
|
|---|
| 157 | }, localExpr);
|
|---|
| 158 | };
|
|---|
| 159 | const buildImportThrow = localName => {
|
|---|
| 160 | return _core.template.expression.ast`
|
|---|
| 161 | (function() {
|
|---|
| 162 | throw new Error('"' + '${localName}' + '" is read-only.');
|
|---|
| 163 | })()
|
|---|
| 164 | `;
|
|---|
| 165 | };
|
|---|
| 166 | const rewriteReferencesVisitor = {
|
|---|
| 167 | ReferencedIdentifier(path) {
|
|---|
| 168 | const {
|
|---|
| 169 | seen,
|
|---|
| 170 | buildImportReference,
|
|---|
| 171 | scope,
|
|---|
| 172 | imported,
|
|---|
| 173 | requeueInParent
|
|---|
| 174 | } = this;
|
|---|
| 175 | if (seen.has(path.node)) return;
|
|---|
| 176 | seen.add(path.node);
|
|---|
| 177 | const localName = path.node.name;
|
|---|
| 178 | const importData = imported.get(localName);
|
|---|
| 179 | if (importData) {
|
|---|
| 180 | if (isInType(path)) {
|
|---|
| 181 | throw path.buildCodeFrameError(`Cannot transform the imported binding "${localName}" since it's also used in a type annotation. ` + `Please strip type annotations using @babel/preset-typescript or @babel/preset-flow.`);
|
|---|
| 182 | }
|
|---|
| 183 | const localBinding = path.scope.getBinding(localName);
|
|---|
| 184 | const rootBinding = scope.getBinding(localName);
|
|---|
| 185 | if (rootBinding !== localBinding) return;
|
|---|
| 186 | const ref = buildImportReference(importData, path.node);
|
|---|
| 187 | ref.loc = path.node.loc;
|
|---|
| 188 | if ((path.parentPath.isCallExpression({
|
|---|
| 189 | callee: path.node
|
|---|
| 190 | }) || path.parentPath.isOptionalCallExpression({
|
|---|
| 191 | callee: path.node
|
|---|
| 192 | }) || path.parentPath.isTaggedTemplateExpression({
|
|---|
| 193 | tag: path.node
|
|---|
| 194 | })) && _core.types.isMemberExpression(ref)) {
|
|---|
| 195 | path.replaceWith(_core.types.sequenceExpression([_core.types.numericLiteral(0), ref]));
|
|---|
| 196 | } else if (path.isJSXIdentifier() && _core.types.isMemberExpression(ref)) {
|
|---|
| 197 | const {
|
|---|
| 198 | object,
|
|---|
| 199 | property
|
|---|
| 200 | } = ref;
|
|---|
| 201 | path.replaceWith(_core.types.jsxMemberExpression(_core.types.jsxIdentifier(object.name), _core.types.jsxIdentifier(property.name)));
|
|---|
| 202 | } else {
|
|---|
| 203 | path.replaceWith(ref);
|
|---|
| 204 | }
|
|---|
| 205 | requeueInParent(path);
|
|---|
| 206 | path.skip();
|
|---|
| 207 | }
|
|---|
| 208 | },
|
|---|
| 209 | UpdateExpression(path) {
|
|---|
| 210 | const {
|
|---|
| 211 | scope,
|
|---|
| 212 | seen,
|
|---|
| 213 | imported,
|
|---|
| 214 | exported,
|
|---|
| 215 | requeueInParent,
|
|---|
| 216 | buildImportReference
|
|---|
| 217 | } = this;
|
|---|
| 218 | if (seen.has(path.node)) return;
|
|---|
| 219 | seen.add(path.node);
|
|---|
| 220 | const arg = path.get("argument");
|
|---|
| 221 | if (arg.isMemberExpression()) return;
|
|---|
| 222 | const update = path.node;
|
|---|
| 223 | if (arg.isIdentifier()) {
|
|---|
| 224 | const localName = arg.node.name;
|
|---|
| 225 | if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
|
|---|
| 226 | return;
|
|---|
| 227 | }
|
|---|
| 228 | const exportedNames = exported.get(localName);
|
|---|
| 229 | const importData = imported.get(localName);
|
|---|
| 230 | if ((exportedNames == null ? void 0 : exportedNames.length) > 0 || importData) {
|
|---|
| 231 | if (importData) {
|
|---|
| 232 | path.replaceWith(_core.types.assignmentExpression(update.operator[0] + "=", buildImportReference(importData, arg.node), buildImportThrow(localName)));
|
|---|
| 233 | } else if (update.prefix) {
|
|---|
| 234 | path.replaceWith(buildBindingExportAssignmentExpression(this.metadata, exportedNames, _core.types.cloneNode(update), path.scope));
|
|---|
| 235 | } else {
|
|---|
| 236 | const ref = scope.generateDeclaredUidIdentifier(localName);
|
|---|
| 237 | path.replaceWith(_core.types.sequenceExpression([_core.types.assignmentExpression("=", _core.types.cloneNode(ref), _core.types.cloneNode(update)), buildBindingExportAssignmentExpression(this.metadata, exportedNames, _core.types.identifier(localName), path.scope), _core.types.cloneNode(ref)]));
|
|---|
| 238 | }
|
|---|
| 239 | }
|
|---|
| 240 | }
|
|---|
| 241 | requeueInParent(path);
|
|---|
| 242 | path.skip();
|
|---|
| 243 | },
|
|---|
| 244 | AssignmentExpression: {
|
|---|
| 245 | exit(path) {
|
|---|
| 246 | const {
|
|---|
| 247 | scope,
|
|---|
| 248 | seen,
|
|---|
| 249 | imported,
|
|---|
| 250 | exported,
|
|---|
| 251 | requeueInParent,
|
|---|
| 252 | buildImportReference
|
|---|
| 253 | } = this;
|
|---|
| 254 | if (seen.has(path.node)) return;
|
|---|
| 255 | seen.add(path.node);
|
|---|
| 256 | const left = path.get("left");
|
|---|
| 257 | if (left.isMemberExpression()) return;
|
|---|
| 258 | if (left.isIdentifier()) {
|
|---|
| 259 | const localName = left.node.name;
|
|---|
| 260 | if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
|
|---|
| 261 | return;
|
|---|
| 262 | }
|
|---|
| 263 | const exportedNames = exported.get(localName);
|
|---|
| 264 | const importData = imported.get(localName);
|
|---|
| 265 | if ((exportedNames == null ? void 0 : exportedNames.length) > 0 || importData) {
|
|---|
| 266 | const assignment = path.node;
|
|---|
| 267 | if (importData) {
|
|---|
| 268 | assignment.left = buildImportReference(importData, left.node);
|
|---|
| 269 | assignment.right = _core.types.sequenceExpression([assignment.right, buildImportThrow(localName)]);
|
|---|
| 270 | }
|
|---|
| 271 | const {
|
|---|
| 272 | operator
|
|---|
| 273 | } = assignment;
|
|---|
| 274 | let newExpr;
|
|---|
| 275 | if (operator === "=") {
|
|---|
| 276 | newExpr = assignment;
|
|---|
| 277 | } else if (operator === "&&=" || operator === "||=" || operator === "??=") {
|
|---|
| 278 | newExpr = _core.types.assignmentExpression("=", assignment.left, _core.types.logicalExpression(operator.slice(0, -1), _core.types.cloneNode(assignment.left), assignment.right));
|
|---|
| 279 | } else {
|
|---|
| 280 | newExpr = _core.types.assignmentExpression("=", assignment.left, _core.types.binaryExpression(operator.slice(0, -1), _core.types.cloneNode(assignment.left), assignment.right));
|
|---|
| 281 | }
|
|---|
| 282 | path.replaceWith(buildBindingExportAssignmentExpression(this.metadata, exportedNames, newExpr, path.scope));
|
|---|
| 283 | requeueInParent(path);
|
|---|
| 284 | path.skip();
|
|---|
| 285 | }
|
|---|
| 286 | } else {
|
|---|
| 287 | const ids = left.getOuterBindingIdentifiers();
|
|---|
| 288 | const programScopeIds = Object.keys(ids).filter(localName => scope.getBinding(localName) === path.scope.getBinding(localName));
|
|---|
| 289 | const id = programScopeIds.find(localName => imported.has(localName));
|
|---|
| 290 | if (id) {
|
|---|
| 291 | path.node.right = _core.types.sequenceExpression([path.node.right, buildImportThrow(id)]);
|
|---|
| 292 | }
|
|---|
| 293 | const items = [];
|
|---|
| 294 | programScopeIds.forEach(localName => {
|
|---|
| 295 | const exportedNames = exported.get(localName) || [];
|
|---|
| 296 | if (exportedNames.length > 0) {
|
|---|
| 297 | items.push(buildBindingExportAssignmentExpression(this.metadata, exportedNames, _core.types.identifier(localName), path.scope));
|
|---|
| 298 | }
|
|---|
| 299 | });
|
|---|
| 300 | if (items.length > 0) {
|
|---|
| 301 | let node = _core.types.sequenceExpression(items);
|
|---|
| 302 | if (path.parentPath.isExpressionStatement()) {
|
|---|
| 303 | node = _core.types.expressionStatement(node);
|
|---|
| 304 | node._blockHoist = path.parentPath.node._blockHoist;
|
|---|
| 305 | }
|
|---|
| 306 | const statement = path.insertAfter(node)[0];
|
|---|
| 307 | requeueInParent(statement);
|
|---|
| 308 | }
|
|---|
| 309 | }
|
|---|
| 310 | }
|
|---|
| 311 | },
|
|---|
| 312 | ForXStatement(path) {
|
|---|
| 313 | const {
|
|---|
| 314 | scope,
|
|---|
| 315 | node
|
|---|
| 316 | } = path;
|
|---|
| 317 | const {
|
|---|
| 318 | left
|
|---|
| 319 | } = node;
|
|---|
| 320 | const {
|
|---|
| 321 | exported,
|
|---|
| 322 | imported,
|
|---|
| 323 | scope: programScope
|
|---|
| 324 | } = this;
|
|---|
| 325 | if (!_core.types.isVariableDeclaration(left)) {
|
|---|
| 326 | let didTransformExport = false,
|
|---|
| 327 | importConstViolationName;
|
|---|
| 328 | const loopBodyScope = path.get("body").scope;
|
|---|
| 329 | for (const name of Object.keys(_core.types.getOuterBindingIdentifiers(left))) {
|
|---|
| 330 | if (programScope.getBinding(name) === scope.getBinding(name)) {
|
|---|
| 331 | if (exported.has(name)) {
|
|---|
| 332 | didTransformExport = true;
|
|---|
| 333 | if (loopBodyScope.hasOwnBinding(name)) {
|
|---|
| 334 | loopBodyScope.rename(name);
|
|---|
| 335 | }
|
|---|
| 336 | }
|
|---|
| 337 | if (imported.has(name) && !importConstViolationName) {
|
|---|
| 338 | importConstViolationName = name;
|
|---|
| 339 | }
|
|---|
| 340 | }
|
|---|
| 341 | }
|
|---|
| 342 | if (!didTransformExport && !importConstViolationName) {
|
|---|
| 343 | return;
|
|---|
| 344 | }
|
|---|
| 345 | path.ensureBlock();
|
|---|
| 346 | const bodyPath = path.get("body");
|
|---|
| 347 | const newLoopId = scope.generateUidIdentifierBasedOnNode(left);
|
|---|
| 348 | path.get("left").replaceWith(_core.types.variableDeclaration("let", [_core.types.variableDeclarator(_core.types.cloneNode(newLoopId))]));
|
|---|
| 349 | scope.registerDeclaration(path.get("left"));
|
|---|
| 350 | if (didTransformExport) {
|
|---|
| 351 | bodyPath.unshiftContainer("body", _core.types.expressionStatement(_core.types.assignmentExpression("=", left, newLoopId)));
|
|---|
| 352 | }
|
|---|
| 353 | if (importConstViolationName) {
|
|---|
| 354 | bodyPath.unshiftContainer("body", _core.types.expressionStatement(buildImportThrow(importConstViolationName)));
|
|---|
| 355 | }
|
|---|
| 356 | }
|
|---|
| 357 | }
|
|---|
| 358 | };
|
|---|
| 359 |
|
|---|
| 360 | //# sourceMappingURL=rewrite-live-references.js.map
|
|---|