| [9af201e] | 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = void 0;
|
|---|
| 7 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
|---|
| 8 | var _core = require("@babel/core");
|
|---|
| 9 | var _loop = require("./loop.js");
|
|---|
| 10 | var _validation = require("./validation.js");
|
|---|
| 11 | var _annexB_3_ = require("./annex-B_3_3.js");
|
|---|
| 12 | var _default = exports.default = (0, _helperPluginUtils.declare)((api, opts) => {
|
|---|
| 13 | api.assertVersion(7);
|
|---|
| 14 | const {
|
|---|
| 15 | throwIfClosureRequired = false,
|
|---|
| 16 | tdz: tdzEnabled = false
|
|---|
| 17 | } = opts;
|
|---|
| 18 | if (typeof throwIfClosureRequired !== "boolean") {
|
|---|
| 19 | throw new Error(`.throwIfClosureRequired must be a boolean, or undefined`);
|
|---|
| 20 | }
|
|---|
| 21 | if (typeof tdzEnabled !== "boolean") {
|
|---|
| 22 | throw new Error(`.tdz must be a boolean, or undefined`);
|
|---|
| 23 | }
|
|---|
| 24 | return {
|
|---|
| 25 | name: "transform-block-scoping",
|
|---|
| 26 | visitor: _core.traverse.visitors.merge([_annexB_3_.annexB33FunctionsVisitor, {
|
|---|
| 27 | Loop(path, state) {
|
|---|
| 28 | const isForStatement = path.isForStatement();
|
|---|
| 29 | const headPath = isForStatement ? path.get("init") : path.isForXStatement() ? path.get("left") : null;
|
|---|
| 30 | let needsBodyWrap = false;
|
|---|
| 31 | const markNeedsBodyWrap = () => {
|
|---|
| 32 | if (throwIfClosureRequired) {
|
|---|
| 33 | throw path.buildCodeFrameError("Compiling let/const in this block would add a closure " + "(throwIfClosureRequired).");
|
|---|
| 34 | }
|
|---|
| 35 | needsBodyWrap = true;
|
|---|
| 36 | };
|
|---|
| 37 | const body = path.get("body");
|
|---|
| 38 | let bodyScope;
|
|---|
| 39 | if (body.isBlockStatement()) {
|
|---|
| 40 | bodyScope = body.scope;
|
|---|
| 41 | }
|
|---|
| 42 | const bindings = (0, _loop.getLoopBodyBindings)(path);
|
|---|
| 43 | for (const binding of bindings) {
|
|---|
| 44 | const {
|
|---|
| 45 | capturedInClosure
|
|---|
| 46 | } = (0, _loop.getUsageInBody)(binding, path);
|
|---|
| 47 | if (capturedInClosure) markNeedsBodyWrap();
|
|---|
| 48 | }
|
|---|
| 49 | const captured = [];
|
|---|
| 50 | const updatedBindingsUsages = new Map();
|
|---|
| 51 | if (headPath && isBlockScoped(headPath)) {
|
|---|
| 52 | const names = Object.keys(headPath.getBindingIdentifiers());
|
|---|
| 53 | const headScope = headPath.scope;
|
|---|
| 54 | for (let name of names) {
|
|---|
| 55 | var _bodyScope;
|
|---|
| 56 | if ((_bodyScope = bodyScope) != null && _bodyScope.hasOwnBinding(name)) continue;
|
|---|
| 57 | let binding = headScope.getOwnBinding(name);
|
|---|
| 58 | if (!binding) {
|
|---|
| 59 | headScope.crawl();
|
|---|
| 60 | binding = headScope.getOwnBinding(name);
|
|---|
| 61 | }
|
|---|
| 62 | const {
|
|---|
| 63 | usages,
|
|---|
| 64 | capturedInClosure,
|
|---|
| 65 | hasConstantViolations
|
|---|
| 66 | } = (0, _loop.getUsageInBody)(binding, path);
|
|---|
| 67 | if (headScope.parent.hasBinding(name) || headScope.parent.hasGlobal(name)) {
|
|---|
| 68 | const newName = headScope.generateUid(name);
|
|---|
| 69 | headScope.rename(name, newName);
|
|---|
| 70 | name = newName;
|
|---|
| 71 | }
|
|---|
| 72 | if (capturedInClosure) {
|
|---|
| 73 | markNeedsBodyWrap();
|
|---|
| 74 | captured.push(name);
|
|---|
| 75 | }
|
|---|
| 76 | if (isForStatement && hasConstantViolations) {
|
|---|
| 77 | updatedBindingsUsages.set(name, usages);
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | if (needsBodyWrap) {
|
|---|
| 82 | const varPath = (0, _loop.wrapLoopBody)(path, captured, updatedBindingsUsages);
|
|---|
| 83 | if (headPath != null && headPath.isVariableDeclaration()) {
|
|---|
| 84 | transformBlockScopedVariable(headPath, state, tdzEnabled);
|
|---|
| 85 | }
|
|---|
| 86 | varPath.get("declarations.0.init").unwrapFunctionEnvironment();
|
|---|
| 87 | }
|
|---|
| 88 | },
|
|---|
| 89 | VariableDeclaration(path, state) {
|
|---|
| 90 | transformBlockScopedVariable(path, state, tdzEnabled);
|
|---|
| 91 | },
|
|---|
| 92 | ClassDeclaration(path) {
|
|---|
| 93 | const {
|
|---|
| 94 | id
|
|---|
| 95 | } = path.node;
|
|---|
| 96 | if (!id) return;
|
|---|
| 97 | const {
|
|---|
| 98 | scope
|
|---|
| 99 | } = path.parentPath;
|
|---|
| 100 | if (!(0, _annexB_3_.isVarScope)(scope) && scope.parent.hasBinding(id.name, {
|
|---|
| 101 | noUids: true
|
|---|
| 102 | })) {
|
|---|
| 103 | path.scope.rename(id.name);
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 | }])
|
|---|
| 107 | };
|
|---|
| 108 | });
|
|---|
| 109 | const conflictingFunctionsVisitor = {
|
|---|
| 110 | Scope(path, {
|
|---|
| 111 | names
|
|---|
| 112 | }) {
|
|---|
| 113 | for (const name of names) {
|
|---|
| 114 | const binding = path.scope.getOwnBinding(name);
|
|---|
| 115 | if ((binding == null ? void 0 : binding.kind) === "hoisted") {
|
|---|
| 116 | path.scope.rename(name);
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 | },
|
|---|
| 120 | "Expression|Declaration"(path) {
|
|---|
| 121 | path.skip();
|
|---|
| 122 | }
|
|---|
| 123 | };
|
|---|
| 124 | function transformBlockScopedVariable(path, state, tdzEnabled) {
|
|---|
| 125 | if (!isBlockScoped(path)) return;
|
|---|
| 126 | const dynamicTDZNames = (0, _validation.validateUsage)(path, state, tdzEnabled);
|
|---|
| 127 | path.node.kind = "var";
|
|---|
| 128 | const bindingNames = Object.keys(path.getBindingIdentifiers());
|
|---|
| 129 | for (const name of bindingNames) {
|
|---|
| 130 | const binding = path.scope.getOwnBinding(name);
|
|---|
| 131 | if (!binding) continue;
|
|---|
| 132 | binding.kind = "var";
|
|---|
| 133 | }
|
|---|
| 134 | if (isInLoop(path) && !(0, _loop.isVarInLoopHead)(path) || dynamicTDZNames.length > 0) {
|
|---|
| 135 | for (const decl of path.node.declarations) {
|
|---|
| 136 | var _decl$init;
|
|---|
| 137 | (_decl$init = decl.init) != null ? _decl$init : decl.init = path.scope.buildUndefinedNode();
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | const blockScope = path.scope;
|
|---|
| 141 | const varScope = blockScope.getFunctionParent() || blockScope.getProgramParent();
|
|---|
| 142 | if (varScope !== blockScope) {
|
|---|
| 143 | for (const name of bindingNames) {
|
|---|
| 144 | let newName = name;
|
|---|
| 145 | if (blockScope.parent.hasBinding(name, {
|
|---|
| 146 | noUids: true
|
|---|
| 147 | }) || blockScope.parent.hasGlobal(name)) {
|
|---|
| 148 | newName = blockScope.generateUid(name);
|
|---|
| 149 | blockScope.rename(name, newName);
|
|---|
| 150 | }
|
|---|
| 151 | blockScope.moveBindingTo(newName, varScope);
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 | blockScope.path.traverse(conflictingFunctionsVisitor, {
|
|---|
| 155 | names: bindingNames
|
|---|
| 156 | });
|
|---|
| 157 | for (const name of dynamicTDZNames) {
|
|---|
| 158 | path.scope.push({
|
|---|
| 159 | id: _core.types.identifier(name),
|
|---|
| 160 | init: state.addHelper("temporalUndefined")
|
|---|
| 161 | });
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 | function isLetOrConst(kind) {
|
|---|
| 165 | return kind === "let" || kind === "const";
|
|---|
| 166 | }
|
|---|
| 167 | function isInLoop(path) {
|
|---|
| 168 | if (!path.parentPath) return false;
|
|---|
| 169 | if (path.parentPath.isLoop()) return true;
|
|---|
| 170 | if (path.parentPath.isFunctionParent()) return false;
|
|---|
| 171 | return isInLoop(path.parentPath);
|
|---|
| 172 | }
|
|---|
| 173 | function isBlockScoped(path) {
|
|---|
| 174 | const {
|
|---|
| 175 | node
|
|---|
| 176 | } = path;
|
|---|
| 177 | if (!_core.types.isVariableDeclaration(node)) return false;
|
|---|
| 178 | const {
|
|---|
| 179 | kind
|
|---|
| 180 | } = node;
|
|---|
| 181 | if (kind === "using" || kind === "await using") {
|
|---|
| 182 | throw path.buildCodeFrameError(`The ${kind} declaration should be first transformed by \`@babel/plugin-transform-explicit-resource-management\`.`);
|
|---|
| 183 | } else if (!isLetOrConst(kind)) {
|
|---|
| 184 | return false;
|
|---|
| 185 | }
|
|---|
| 186 | return true;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | //# sourceMappingURL=index.js.map
|
|---|