8889841cREADME.md000066600000000637150444362230006040 0ustar00# @babel/plugin-transform-block-scoping > Compile ES2015 block scoping (const and let) to ES5 See our website [@babel/plugin-transform-block-scoping](https://babeljs.io/docs/en/babel-plugin-transform-block-scoping) for more information. ## Install Using npm: ```sh npm install --save-dev @babel/plugin-transform-block-scoping ``` or using yarn: ```sh yarn add @babel/plugin-transform-block-scoping --dev ``` lib/index.js000066600000014205150444362230006770 0ustar00"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _helperPluginUtils = require("@babel/helper-plugin-utils"); var _core = require("@babel/core"); var _loop = require("./loop"); var _validation = require("./validation"); var _annexB_3_ = require("./annex-B_3_3"); var _default = (0, _helperPluginUtils.declare)((api, opts) => { api.assertVersion(7); const { throwIfClosureRequired = false, tdz: tdzEnabled = false } = opts; if (typeof throwIfClosureRequired !== "boolean") { throw new Error(`.throwIfClosureRequired must be a boolean, or undefined`); } if (typeof tdzEnabled !== "boolean") { throw new Error(`.tdz must be a boolean, or undefined`); } return { name: "transform-block-scoping", visitor: _core.traverse.visitors.merge([_annexB_3_.annexB33FunctionsVisitor, { Loop(path, state) { const isForStatement = path.isForStatement(); const headPath = isForStatement ? path.get("init") : path.isForXStatement() ? path.get("left") : null; let needsBodyWrap = false; const markNeedsBodyWrap = () => { if (throwIfClosureRequired) { throw path.buildCodeFrameError("Compiling let/const in this block would add a closure " + "(throwIfClosureRequired)."); } needsBodyWrap = true; }; const body = path.get("body"); let bodyScope; if (body.isBlockStatement()) { bodyScope = body.scope; const bindings = (0, _loop.getLoopBodyBindings)(path); for (const binding of bindings) { const { capturedInClosure } = (0, _loop.getUsageInBody)(binding, path); if (capturedInClosure) markNeedsBodyWrap(); } } const captured = []; const updatedBindingsUsages = new Map(); if (headPath && isBlockScoped(headPath.node)) { const names = Object.keys(headPath.getBindingIdentifiers()); const headScope = headPath.scope; for (let name of names) { var _bodyScope; if ((_bodyScope = bodyScope) != null && _bodyScope.hasOwnBinding(name)) continue; let binding = headScope.getOwnBinding(name); if (!binding) { headScope.crawl(); binding = headScope.getOwnBinding(name); } const { usages, capturedInClosure, hasConstantViolations } = (0, _loop.getUsageInBody)(binding, path); if (headScope.parent.hasBinding(name) || headScope.parent.hasGlobal(name)) { const newName = headScope.generateUid(name); headScope.rename(name, newName); name = newName; } if (capturedInClosure) { markNeedsBodyWrap(); captured.push(name); } if (isForStatement && hasConstantViolations) { updatedBindingsUsages.set(name, usages); } } } if (needsBodyWrap) { const varPath = (0, _loop.wrapLoopBody)(path, captured, updatedBindingsUsages); if (headPath != null && headPath.isVariableDeclaration()) { transformBlockScopedVariable(headPath, state, tdzEnabled); } varPath.get("declarations.0.init").unwrapFunctionEnvironment(); } }, VariableDeclaration(path, state) { transformBlockScopedVariable(path, state, tdzEnabled); }, ClassDeclaration(path) { const { id } = path.node; if (!id) return; const { scope } = path.parentPath; if (!(0, _annexB_3_.isVarScope)(scope) && scope.parent.hasBinding(id.name, { noUids: true })) { path.scope.rename(id.name); } } }]) }; }); exports.default = _default; const conflictingFunctionsVisitor = { Scope(path, { names }) { for (const name of names) { const binding = path.scope.getOwnBinding(name); if (binding && binding.kind === "hoisted") { path.scope.rename(name); } } }, "Expression|Declaration"(path) { path.skip(); } }; function transformBlockScopedVariable(path, state, tdzEnabled) { if (!isBlockScoped(path.node)) return; const dynamicTDZNames = (0, _validation.validateUsage)(path, state, tdzEnabled); path.node.kind = "var"; const bindingNames = Object.keys(path.getBindingIdentifiers()); for (const name of bindingNames) { const binding = path.scope.getOwnBinding(name); if (!binding) continue; binding.kind = "var"; } if (isInLoop(path) && !(0, _loop.isVarInLoopHead)(path) || dynamicTDZNames.length > 0) { for (const decl of path.node.declarations) { var _decl$init; (_decl$init = decl.init) != null ? _decl$init : decl.init = path.scope.buildUndefinedNode(); } } const blockScope = path.scope; const varScope = blockScope.getFunctionParent() || blockScope.getProgramParent(); if (varScope !== blockScope) { for (const name of bindingNames) { let newName = name; if (blockScope.parent.hasBinding(name, { noUids: true }) || blockScope.parent.hasGlobal(name)) { newName = blockScope.generateUid(name); blockScope.rename(name, newName); } blockScope.moveBindingTo(newName, varScope); } } blockScope.path.traverse(conflictingFunctionsVisitor, { names: bindingNames }); for (const name of dynamicTDZNames) { path.scope.push({ id: _core.types.identifier(name), init: state.addHelper("temporalUndefined") }); } } function isLetOrConst(kind) { return kind === "let" || kind === "const"; } function isInLoop(path) { if (!path.parentPath) return false; if (path.parentPath.isLoop()) return true; if (path.parentPath.isFunctionParent()) return false; return isInLoop(path.parentPath); } function isBlockScoped(node) { if (!_core.types.isVariableDeclaration(node)) return false; if (node[_core.types.BLOCK_SCOPED_SYMBOL]) { return true; } if (!isLetOrConst(node.kind) && node.kind !== "using") { return false; } return true; } //# sourceMappingURL=index.js.map lib/index.js.map000066600000036766150444362230007564 0ustar00{"version":3,"names":["declare","api","opts","assertVersion","throwIfClosureRequired","tdz","tdzEnabled","Error","name","visitor","traverse","visitors","merge","annexB33FunctionsVisitor","Loop","path","state","isForStatement","headPath","get","isForXStatement","needsBodyWrap","markNeedsBodyWrap","buildCodeFrameError","body","bodyScope","isBlockStatement","scope","bindings","getLoopBodyBindings","binding","capturedInClosure","getUsageInBody","captured","updatedBindingsUsages","Map","isBlockScoped","node","names","Object","keys","getBindingIdentifiers","headScope","hasOwnBinding","getOwnBinding","crawl","usages","hasConstantViolations","parent","hasBinding","hasGlobal","newName","generateUid","rename","push","set","varPath","wrapLoopBody","isVariableDeclaration","transformBlockScopedVariable","unwrapFunctionEnvironment","VariableDeclaration","ClassDeclaration","id","parentPath","isVarScope","noUids","conflictingFunctionsVisitor","Scope","kind","skip","dynamicTDZNames","validateUsage","bindingNames","isInLoop","isVarInLoopHead","length","decl","declarations","init","buildUndefinedNode","blockScope","varScope","getFunctionParent","getProgramParent","moveBindingTo","t","identifier","addHelper","isLetOrConst","isLoop","isFunctionParent","BLOCK_SCOPED_SYMBOL"],"sources":["../src/index.ts"],"sourcesContent":["import { declare } from \"@babel/helper-plugin-utils\";\nimport type { NodePath, Scope, Visitor } from \"@babel/traverse\";\nimport { type PluginPass, types as t, traverse } from \"@babel/core\";\n\nimport {\n getLoopBodyBindings,\n getUsageInBody,\n isVarInLoopHead,\n wrapLoopBody,\n} from \"./loop\";\nimport { validateUsage } from \"./validation\";\nimport { annexB33FunctionsVisitor, isVarScope } from \"./annex-B_3_3\";\n\nexport interface Options {\n tdz?: boolean;\n throwIfClosureRequired?: boolean;\n}\n\nexport default declare((api, opts: Options) => {\n api.assertVersion(7);\n\n const { throwIfClosureRequired = false, tdz: tdzEnabled = false } = opts;\n if (typeof throwIfClosureRequired !== \"boolean\") {\n throw new Error(`.throwIfClosureRequired must be a boolean, or undefined`);\n }\n if (typeof tdzEnabled !== \"boolean\") {\n throw new Error(`.tdz must be a boolean, or undefined`);\n }\n\n return {\n name: \"transform-block-scoping\",\n\n visitor: traverse.visitors.merge([\n // TODO: Consider adding an option to control Annex B behavior.\n annexB33FunctionsVisitor,\n {\n Loop(path: NodePath, state) {\n const isForStatement = path.isForStatement();\n const headPath = isForStatement\n ? path.get(\"init\")\n : path.isForXStatement()\n ? path.get(\"left\")\n : null;\n\n let needsBodyWrap = false;\n const markNeedsBodyWrap = () => {\n if (throwIfClosureRequired) {\n throw path.buildCodeFrameError(\n \"Compiling let/const in this block would add a closure \" +\n \"(throwIfClosureRequired).\",\n );\n }\n needsBodyWrap = true;\n };\n\n const body = path.get(\"body\");\n let bodyScope: Scope | null;\n if (body.isBlockStatement()) {\n bodyScope = body.scope;\n\n const bindings = getLoopBodyBindings(path);\n for (const binding of bindings) {\n const { capturedInClosure } = getUsageInBody(binding, path);\n if (capturedInClosure) markNeedsBodyWrap();\n }\n }\n\n const captured: string[] = [];\n const updatedBindingsUsages: Map[]> =\n new Map();\n\n if (headPath && isBlockScoped(headPath.node)) {\n const names = Object.keys(headPath.getBindingIdentifiers());\n const headScope = headPath.scope;\n\n for (let name of names) {\n if (bodyScope?.hasOwnBinding(name)) continue; // shadowed\n\n let binding = headScope.getOwnBinding(name);\n if (!binding) {\n headScope.crawl();\n binding = headScope.getOwnBinding(name);\n }\n const { usages, capturedInClosure, hasConstantViolations } =\n getUsageInBody(binding, path);\n\n if (\n headScope.parent.hasBinding(name) ||\n headScope.parent.hasGlobal(name)\n ) {\n // If the binding is not captured, there is no need\n // of adding it to the closure param. However, rename\n // it if it shadows an outer binding, because the\n // closure will be moved to an outer level.\n const newName = headScope.generateUid(name);\n headScope.rename(name, newName);\n name = newName;\n }\n\n if (capturedInClosure) {\n markNeedsBodyWrap();\n captured.push(name);\n }\n\n if (isForStatement && hasConstantViolations) {\n updatedBindingsUsages.set(name, usages);\n }\n }\n }\n\n if (needsBodyWrap) {\n const varPath = wrapLoopBody(path, captured, updatedBindingsUsages);\n\n if (headPath?.isVariableDeclaration()) {\n // If we wrap the loop body, we transform the var\n // declaration in the loop head now, to avoid\n // invalid references that break other plugins:\n //\n // for (let head of x) {\n // let i = head;\n // setTimeout(() => i);\n // }\n //\n // would become\n //\n // function _loop() {\n // let i = head;\n // setTimeout(() => i);\n // }\n // for (let head of x) _loop();\n //\n // which references `head` in a scope where it's not visible.\n transformBlockScopedVariable(headPath, state, tdzEnabled);\n }\n\n varPath.get(\"declarations.0.init\").unwrapFunctionEnvironment();\n }\n },\n\n VariableDeclaration(path, state) {\n transformBlockScopedVariable(path, state, tdzEnabled);\n },\n\n // Class declarations are block-scoped: if there is\n // a class declaration in a nested block that conflicts\n // with an outer block-scoped binding, rename it.\n // TODO: Should this be moved to the classes plugin?\n ClassDeclaration(path) {\n const { id } = path.node;\n if (!id) return;\n\n const { scope } = path.parentPath;\n if (\n !isVarScope(scope) &&\n scope.parent.hasBinding(id.name, { noUids: true })\n ) {\n path.scope.rename(id.name);\n }\n },\n },\n ]),\n };\n});\n\nconst conflictingFunctionsVisitor: Visitor<{ names: string[] }> = {\n Scope(path, { names }) {\n for (const name of names) {\n const binding = path.scope.getOwnBinding(name);\n if (binding && binding.kind === \"hoisted\") {\n path.scope.rename(name);\n }\n }\n },\n \"Expression|Declaration\"(path) {\n path.skip();\n },\n};\n\nfunction transformBlockScopedVariable(\n path: NodePath,\n state: PluginPass,\n tdzEnabled: boolean,\n) {\n if (!isBlockScoped(path.node)) return;\n\n const dynamicTDZNames = validateUsage(path, state, tdzEnabled);\n\n path.node.kind = \"var\";\n\n const bindingNames = Object.keys(path.getBindingIdentifiers());\n for (const name of bindingNames) {\n const binding = path.scope.getOwnBinding(name);\n if (!binding) continue;\n binding.kind = \"var\";\n }\n\n if (\n (isInLoop(path) && !isVarInLoopHead(path)) ||\n dynamicTDZNames.length > 0\n ) {\n for (const decl of path.node.declarations) {\n // We explicitly add `void 0` to cases like\n // for (;;) { let a; }\n // to make sure that `a` doesn't keep the value from\n // the previous iteration.\n decl.init ??= path.scope.buildUndefinedNode();\n }\n }\n\n const blockScope = path.scope;\n const varScope =\n blockScope.getFunctionParent() || blockScope.getProgramParent();\n\n if (varScope !== blockScope) {\n for (const name of bindingNames) {\n let newName = name;\n if (\n // We pass `noUids` true because, if `name` was a generated\n // UID, it has been used to declare the current variable in\n // a nested scope and thus we don't need to assume that it\n // may be declared (but not registered yet) in an upper one.\n blockScope.parent.hasBinding(name, { noUids: true }) ||\n blockScope.parent.hasGlobal(name)\n ) {\n newName = blockScope.generateUid(name);\n blockScope.rename(name, newName);\n }\n\n blockScope.moveBindingTo(newName, varScope);\n }\n }\n\n blockScope.path.traverse(conflictingFunctionsVisitor, {\n names: bindingNames,\n });\n\n for (const name of dynamicTDZNames) {\n path.scope.push({\n id: t.identifier(name),\n init: state.addHelper(\"temporalUndefined\"),\n });\n }\n}\n\nfunction isLetOrConst(kind: string): kind is \"let\" | \"const\" {\n return kind === \"let\" || kind === \"const\";\n}\n\nfunction isInLoop(path: NodePath): boolean {\n if (!path.parentPath) return false;\n if (path.parentPath.isLoop()) return true;\n if (path.parentPath.isFunctionParent()) return false;\n return isInLoop(path.parentPath);\n}\n\nfunction isBlockScoped(node: t.Node): node is t.VariableDeclaration {\n if (!t.isVariableDeclaration(node)) return false;\n if (\n // @ts-expect-error Fixme: document symbol properties\n node[t.BLOCK_SCOPED_SYMBOL]\n ) {\n return true;\n }\n\n if (!isLetOrConst(node.kind) && node.kind !== \"using\") {\n return false;\n }\n\n return true;\n}\n"],"mappings":";;;;;;AAAA;AAEA;AAEA;AAMA;AACA;AAAqE,eAOtD,IAAAA,0BAAO,EAAC,CAACC,GAAG,EAAEC,IAAa,KAAK;EAC7CD,GAAG,CAACE,aAAa,CAAC,CAAC,CAAC;EAEpB,MAAM;IAAEC,sBAAsB,GAAG,KAAK;IAAEC,GAAG,EAAEC,UAAU,GAAG;EAAM,CAAC,GAAGJ,IAAI;EACxE,IAAI,OAAOE,sBAAsB,KAAK,SAAS,EAAE;IAC/C,MAAM,IAAIG,KAAK,CAAE,yDAAwD,CAAC;EAC5E;EACA,IAAI,OAAOD,UAAU,KAAK,SAAS,EAAE;IACnC,MAAM,IAAIC,KAAK,CAAE,sCAAqC,CAAC;EACzD;EAEA,OAAO;IACLC,IAAI,EAAE,yBAAyB;IAE/BC,OAAO,EAAEC,cAAQ,CAACC,QAAQ,CAACC,KAAK,CAAa,CAE3CC,mCAAwB,EACxB;MACEC,IAAI,CAACC,IAAsB,EAAEC,KAAK,EAAE;QAClC,MAAMC,cAAc,GAAGF,IAAI,CAACE,cAAc,EAAE;QAC5C,MAAMC,QAAQ,GAAGD,cAAc,GAC3BF,IAAI,CAACI,GAAG,CAAC,MAAM,CAAC,GAChBJ,IAAI,CAACK,eAAe,EAAE,GACtBL,IAAI,CAACI,GAAG,CAAC,MAAM,CAAC,GAChB,IAAI;QAER,IAAIE,aAAa,GAAG,KAAK;QACzB,MAAMC,iBAAiB,GAAG,MAAM;UAC9B,IAAIlB,sBAAsB,EAAE;YAC1B,MAAMW,IAAI,CAACQ,mBAAmB,CAC5B,wDAAwD,GACtD,2BAA2B,CAC9B;UACH;UACAF,aAAa,GAAG,IAAI;QACtB,CAAC;QAED,MAAMG,IAAI,GAAGT,IAAI,CAACI,GAAG,C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AOA,IAAI,KAAK,KAAK,IAAIA,IAAI,KAAK,OAAO;AAC3C;AAEA,SAASK,QAAQ,CAAC3D,IAAsB,EAAW;EACjD,IAAI,CAACA,IAAI,CAACiD,UAAU,EAAE,OAAO,KAAK;EAClC,IAAIjD,IAAI,CAACiD,UAAU,CAAC0B,MAAM,EAAE,EAAE,OAAO,IAAI;EACzC,IAAI3E,IAAI,CAACiD,UAAU,CAAC2B,gBAAgB,EAAE,EAAE,OAAO,KAAK;EACpD,OAAOjB,QAAQ,CAAC3D,IAAI,CAACiD,UAAU,CAAC;AAClC;AAEA,SAAS5B,aAAa,CAACC,IAAY,EAAiC;EAClE,IAAI,CAACiD,WAAC,CAAC5B,qBAAqB,CAACrB,IAAI,CAAC,EAAE,OAAO,KAAK;EAChD,IAEEA,IAAI,CAACiD,WAAC,CAACM,mBAAmB,CAAC,EAC3B;IACA,OAAO,IAAI;EACb;EAEA,IAAI,CAACH,YAAY,CAACpD,IAAI,CAACgC,IAAI,CAAC,IAAIhC,IAAI,CAACgC,IAAI,KAAK,OAAO,EAAE;IACrD,OAAO,KAAK;EACd;EAEA,OAAO,IAAI;AACb"}lib/loop.js000066600000021123150444362230006627 0ustar00"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLoopBodyBindings = getLoopBodyBindings; exports.getUsageInBody = getUsageInBody; exports.isVarInLoopHead = isVarInLoopHead; exports.wrapLoopBody = wrapLoopBody; var _core = require("@babel/core"); const collectLoopBodyBindingsVisitor = { "Expression|Declaration|Loop"(path) { path.skip(); }, Scope(path, state) { if (path.isFunctionParent()) path.skip(); const { bindings } = path.scope; for (const name of Object.keys(bindings)) { const binding = bindings[name]; if (binding.kind === "let" || binding.kind === "const" || binding.kind === "hoisted") { state.blockScoped.push(binding); } } } }; function getLoopBodyBindings(loopPath) { const state = { blockScoped: [] }; loopPath.traverse(collectLoopBodyBindingsVisitor, state); return state.blockScoped; } function getUsageInBody(binding, loopPath) { const seen = new WeakSet(); let capturedInClosure = false; const constantViolations = filterMap(binding.constantViolations, path => { const { inBody, inClosure } = relativeLoopLocation(path, loopPath); if (!inBody) return null; capturedInClosure || (capturedInClosure = inClosure); const id = path.isUpdateExpression() ? path.get("argument") : path.isAssignmentExpression() ? path.get("left") : null; if (id) seen.add(id.node); return id; }); const references = filterMap(binding.referencePaths, path => { if (seen.has(path.node)) return null; const { inBody, inClosure } = relativeLoopLocation(path, loopPath); if (!inBody) return null; capturedInClosure || (capturedInClosure = inClosure); return path; }); return { capturedInClosure, hasConstantViolations: constantViolations.length > 0, usages: references.concat(constantViolations) }; } function relativeLoopLocation(path, loopPath) { const bodyPath = loopPath.get("body"); let inClosure = false; for (let currPath = path; currPath; currPath = currPath.parentPath) { if (currPath.isFunction() || currPath.isClass()) inClosure = true; if (currPath === bodyPath) { return { inBody: true, inClosure }; } else if (currPath === loopPath) { return { inBody: false, inClosure }; } } throw new Error("Internal Babel error: path is not in loop. Please report this as a bug."); } const collectCompletionsAndVarsVisitor = { Function(path) { path.skip(); }, LabeledStatement: { enter({ node }, state) { state.labelsStack.push(node.label.name); }, exit({ node }, state) { const popped = state.labelsStack.pop(); if (popped !== node.label.name) { throw new Error("Assertion failure. Please report this bug to Babel."); } } }, Loop: { enter(_, state) { state.labellessContinueTargets++; state.labellessBreakTargets++; }, exit(_, state) { state.labellessContinueTargets--; state.labellessBreakTargets--; } }, SwitchStatement: { enter(_, state) { state.labellessBreakTargets++; }, exit(_, state) { state.labellessBreakTargets--; } }, "BreakStatement|ContinueStatement"(path, state) { const { label } = path.node; if (label) { if (state.labelsStack.includes(label.name)) return; } else if (path.isBreakStatement() ? state.labellessBreakTargets > 0 : state.labellessContinueTargets > 0) { return; } state.breaksContinues.push(path); }, ReturnStatement(path, state) { state.returns.push(path); }, VariableDeclaration(path, state) { if (path.parent === state.loopNode && isVarInLoopHead(path)) return; if (path.node.kind === "var") state.vars.push(path); } }; function wrapLoopBody(loopPath, captured, updatedBindingsUsages) { const loopNode = loopPath.node; const state = { breaksContinues: [], returns: [], labelsStack: [], labellessBreakTargets: 0, labellessContinueTargets: 0, vars: [], loopNode }; loopPath.traverse(collectCompletionsAndVarsVisitor, state); const callArgs = []; const closureParams = []; const updater = []; for (const [name, updatedUsage] of updatedBindingsUsages) { callArgs.push(_core.types.identifier(name)); const innerName = loopPath.scope.generateUid(name); closureParams.push(_core.types.identifier(innerName)); updater.push(_core.types.assignmentExpression("=", _core.types.identifier(name), _core.types.identifier(innerName))); for (const path of updatedUsage) path.replaceWith(_core.types.identifier(innerName)); } for (const name of captured) { if (updatedBindingsUsages.has(name)) continue; callArgs.push(_core.types.identifier(name)); closureParams.push(_core.types.identifier(name)); } const id = loopPath.scope.generateUid("loop"); const fn = _core.types.functionExpression(null, closureParams, _core.types.toBlock(loopNode.body)); let call = _core.types.callExpression(_core.types.identifier(id), callArgs); const fnParent = loopPath.findParent(p => p.isFunction()); if (fnParent) { const { async, generator } = fnParent.node; fn.async = async; fn.generator = generator; if (generator) call = _core.types.yieldExpression(call, true);else if (async) call = _core.types.awaitExpression(call); } const updaterNode = updater.length > 0 ? _core.types.expressionStatement(_core.types.sequenceExpression(updater)) : null; if (updaterNode) fn.body.body.push(updaterNode); const [varPath] = loopPath.insertBefore(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(id), fn)])); const bodyStmts = []; const varNames = []; for (const varPath of state.vars) { const assign = []; for (const decl of varPath.node.declarations) { varNames.push(...Object.keys(_core.types.getBindingIdentifiers(decl.id))); if (decl.init) { assign.push(_core.types.assignmentExpression("=", decl.id, decl.init)); } } if (assign.length > 0) { let replacement = assign.length === 1 ? assign[0] : _core.types.sequenceExpression(assign); if (!_core.types.isForStatement(varPath.parent, { init: varPath.node }) && !_core.types.isForXStatement(varPath.parent, { left: varPath.node })) { replacement = _core.types.expressionStatement(replacement); } varPath.replaceWith(replacement); } else { varPath.remove(); } } if (varNames.length) { bodyStmts.push(_core.types.variableDeclaration("var", varNames.map(name => _core.types.variableDeclarator(_core.types.identifier(name))))); } if (state.breaksContinues.length === 0 && state.returns.length === 0) { bodyStmts.push(_core.types.expressionStatement(call)); } else { const completionId = loopPath.scope.generateUid("ret"); bodyStmts.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(completionId), call)])); const injected = new Set(); for (const path of state.breaksContinues) { const { node } = path; const { type, label } = node; let name = type === "BreakStatement" ? "break" : "continue"; if (label) name += "|" + label.name; path.replaceWith(_core.types.returnStatement(_core.types.stringLiteral(name))); if (updaterNode) path.insertBefore(_core.types.cloneNode(updaterNode)); if (injected.has(name)) continue; injected.add(name); bodyStmts.push(_core.template.statement.ast` if ( ${_core.types.identifier(completionId)} === ${_core.types.stringLiteral(name)} ) ${node} `); } if (state.returns.length) { for (const path of state.returns) { const arg = path.node.argument || path.scope.buildUndefinedNode(); path.replaceWith(_core.template.statement.ast` return { v: ${arg} }; `); } bodyStmts.push(_core.template.statement.ast` if (typeof ${_core.types.identifier(completionId)} === "object") return ${_core.types.identifier(completionId)}.v; `); } } loopNode.body = _core.types.blockStatement(bodyStmts); return varPath; } function isVarInLoopHead(path) { if (_core.types.isForStatement(path.parent)) return path.key === "init"; if (_core.types.isForXStatement(path.parent)) return path.key === "left"; return false; } function filterMap(list, fn) { const result = []; for (const item of list) { const mapped = fn(item); if (mapped) result.push(mapped); } return result; } //# sourceMappingURL=loop.js.map lib/validation.js000066600000011651150444362230010015 0ustar00"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateUsage = validateUsage; var _core = require("@babel/core"); function validateUsage(path, state, tdzEnabled) { const dynamicTDZNames = []; for (const name of Object.keys(path.getBindingIdentifiers())) { const binding = path.scope.getBinding(name); if (!binding) continue; if (tdzEnabled) { if (injectTDZChecks(binding, state)) dynamicTDZNames.push(name); } if (path.node.kind === "const") { disallowConstantViolations(name, binding, state); } } return dynamicTDZNames; } function disallowConstantViolations(name, binding, state) { for (const violation of binding.constantViolations) { const readOnlyError = state.addHelper("readOnlyError"); const throwNode = _core.types.callExpression(readOnlyError, [_core.types.stringLiteral(name)]); if (violation.isAssignmentExpression()) { const { operator, left, right } = violation.node; if (operator === "=") { const exprs = [right]; exprs.push(throwNode); violation.replaceWith(_core.types.sequenceExpression(exprs)); } else if (["&&=", "||=", "??="].includes(operator)) { violation.replaceWith(_core.types.logicalExpression(operator.slice(0, -1), left, _core.types.sequenceExpression([right, throwNode]))); } else { violation.replaceWith(_core.types.sequenceExpression([_core.types.binaryExpression(operator.slice(0, -1), left, right), throwNode])); } } else if (violation.isUpdateExpression()) { violation.replaceWith(_core.types.sequenceExpression([_core.types.unaryExpression("+", violation.get("argument").node), throwNode])); } else if (violation.isForXStatement()) { violation.ensureBlock(); violation.get("left").replaceWith(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(violation.scope.generateUidIdentifier(name))])); violation.node.body.body.unshift(_core.types.expressionStatement(throwNode)); } } } function getTDZStatus(refPath, bindingPath) { const executionStatus = bindingPath._guessExecutionStatusRelativeTo(refPath); if (executionStatus === "before") { return "outside"; } else if (executionStatus === "after") { return "inside"; } else { return "maybe"; } } const skipTDZChecks = new WeakSet(); function buildTDZAssert(status, node, state) { if (status === "maybe") { const clone = _core.types.cloneNode(node); skipTDZChecks.add(clone); return _core.types.callExpression(state.addHelper("temporalRef"), [clone, _core.types.stringLiteral(node.name)]); } else { return _core.types.callExpression(state.addHelper("tdz"), [_core.types.stringLiteral(node.name)]); } } function getTDZReplacement(path, state, id = path.node) { var _path$scope$getBindin; if (skipTDZChecks.has(id)) return; skipTDZChecks.add(id); const bindingPath = (_path$scope$getBindin = path.scope.getBinding(id.name)) == null ? void 0 : _path$scope$getBindin.path; if (!bindingPath || bindingPath.isFunctionDeclaration()) return; const status = getTDZStatus(path, bindingPath); if (status === "outside") return; if (status === "maybe") { bindingPath.parent._tdzThis = true; } return { status, node: buildTDZAssert(status, id, state) }; } function injectTDZChecks(binding, state) { const allUsages = new Set(binding.referencePaths); binding.constantViolations.forEach(allUsages.add, allUsages); let dynamicTdz = false; for (const path of binding.constantViolations) { const { node } = path; if (skipTDZChecks.has(node)) continue; skipTDZChecks.add(node); if (path.isUpdateExpression()) { const arg = path.get("argument"); const replacement = getTDZReplacement(path, state, arg.node); if (!replacement) continue; if (replacement.status === "maybe") { dynamicTdz = true; path.insertBefore(replacement.node); } else { path.replaceWith(replacement.node); } } else if (path.isAssignmentExpression()) { const nodes = []; const ids = path.getBindingIdentifiers(); for (const name of Object.keys(ids)) { const replacement = getTDZReplacement(path, state, ids[name]); if (replacement) { nodes.push(_core.types.expressionStatement(replacement.node)); if (replacement.status === "inside") break; if (replacement.status === "maybe") dynamicTdz = true; } } if (nodes.length > 0) path.insertBefore(nodes); } } for (const path of binding.referencePaths) { if (path.parentPath.isUpdateExpression()) continue; if (path.parentPath.isFor({ left: path.node })) continue; const replacement = getTDZReplacement(path, state); if (!replacement) continue; if (replacement.status === "maybe") dynamicTdz = true; path.replaceWith(replacement.node); } return dynamicTdz; } //# sourceMappingURL=validation.js.map lib/loop.js.map000066600000052313150444362230007410 0ustar00{"version":3,"names":["collectLoopBodyBindingsVisitor","path","skip","Scope","state","isFunctionParent","bindings","scope","name","Object","keys","binding","kind","blockScoped","push","getLoopBodyBindings","loopPath","traverse","getUsageInBody","seen","WeakSet","capturedInClosure","constantViolations","filterMap","inBody","inClosure","relativeLoopLocation","id","isUpdateExpression","get","isAssignmentExpression","add","node","references","referencePaths","has","hasConstantViolations","length","usages","concat","bodyPath","currPath","parentPath","isFunction","isClass","Error","collectCompletionsAndVarsVisitor","Function","LabeledStatement","enter","labelsStack","label","exit","popped","pop","Loop","_","labellessContinueTargets","labellessBreakTargets","SwitchStatement","includes","isBreakStatement","breaksContinues","ReturnStatement","returns","VariableDeclaration","parent","loopNode","isVarInLoopHead","vars","wrapLoopBody","captured","updatedBindingsUsages","callArgs","closureParams","updater","updatedUsage","t","identifier","innerName","generateUid","assignmentExpression","replaceWith","fn","functionExpression","toBlock","body","call","callExpression","fnParent","findParent","p","async","generator","yieldExpression","awaitExpression","updaterNode","expressionStatement","sequenceExpression","varPath","insertBefore","variableDeclaration","variableDeclarator","bodyStmts","varNames","assign","decl","declarations","getBindingIdentifiers","init","replacement","isForStatement","isForXStatement","left","remove","map","completionId","injected","Set","type","returnStatement","stringLiteral","cloneNode","template","statement","ast","arg","argument","buildUndefinedNode","blockStatement","key","list","result","item","mapped"],"sources":["../src/loop.ts"],"sourcesContent":["import { template, types as t } from \"@babel/core\";\nimport type { NodePath, Visitor, Binding } from \"@babel/traverse\";\n\ninterface LoopBodyBindingsState {\n blockScoped: Binding[];\n}\n\nconst collectLoopBodyBindingsVisitor: Visitor = {\n \"Expression|Declaration|Loop\"(path) {\n path.skip();\n },\n Scope(path, state) {\n if (path.isFunctionParent()) path.skip();\n\n const { bindings } = path.scope;\n for (const name of Object.keys(bindings)) {\n const binding = bindings[name];\n if (\n binding.kind === \"let\" ||\n binding.kind === \"const\" ||\n binding.kind === \"hoisted\"\n ) {\n state.blockScoped.push(binding);\n }\n }\n },\n};\n\nexport function getLoopBodyBindings(loopPath: NodePath) {\n const state: LoopBodyBindingsState = { blockScoped: [] };\n loopPath.traverse(collectLoopBodyBindingsVisitor, state);\n return state.blockScoped;\n}\n\nexport function getUsageInBody(binding: Binding, loopPath: NodePath) {\n // UpdateExpressions are counted both as a reference and a mutation,\n // so we need to de-duplicate them.\n const seen = new WeakSet();\n\n let capturedInClosure = false;\n\n const constantViolations = filterMap(binding.constantViolations, path => {\n const { inBody, inClosure } = relativeLoopLocation(path, loopPath);\n if (!inBody) return null;\n capturedInClosure ||= inClosure;\n\n const id = path.isUpdateExpression()\n ? path.get(\"argument\")\n : path.isAssignmentExpression()\n ? path.get(\"left\")\n : null;\n if (id) seen.add(id.node);\n return id as NodePath | null;\n });\n\n const references = filterMap(binding.referencePaths, path => {\n if (seen.has(path.node)) return null;\n\n const { inBody, inClosure } = relativeLoopLocation(path, loopPath);\n if (!inBody) return null;\n capturedInClosure ||= inClosure;\n\n return path as NodePath;\n });\n\n return {\n capturedInClosure,\n hasConstantViolations: constantViolations.length > 0,\n usages: references.concat(constantViolations),\n };\n}\n\nfunction relativeLoopLocation(path: NodePath, loopPath: NodePath) {\n const bodyPath = loopPath.get(\"body\");\n let inClosure = false;\n\n for (let currPath = path; currPath; currPath = currPath.parentPath) {\n if (currPath.isFunction() || currPath.isClass()) inClosure = true;\n if (currPath === bodyPath) {\n return { inBody: true, inClosure };\n } else if (currPath === loopPath) {\n return { inBody: false, inClosure };\n }\n }\n\n throw new Error(\n \"Internal Babel error: path is not in loop. Please report this as a bug.\",\n );\n}\n\ninterface CompletionsAndVarsState {\n breaksContinues: NodePath[];\n returns: NodePath[];\n labelsStack: string[];\n labellessContinueTargets: number;\n labellessBreakTargets: number;\n\n vars: NodePath[];\n loopNode: t.Loop;\n}\n\nconst collectCompletionsAndVarsVisitor: Visitor = {\n Function(path) {\n path.skip();\n },\n LabeledStatement: {\n enter({ node }, state) {\n state.labelsStack.push(node.label.name);\n },\n exit({ node }, state) {\n const popped = state.labelsStack.pop();\n if (popped !== node.label.name) {\n throw new Error(\"Assertion failure. Please report this bug to Babel.\");\n }\n },\n },\n Loop: {\n enter(_, state) {\n state.labellessContinueTargets++;\n state.labellessBreakTargets++;\n },\n exit(_, state) {\n state.labellessContinueTargets--;\n state.labellessBreakTargets--;\n },\n },\n SwitchStatement: {\n enter(_, state) {\n state.labellessBreakTargets++;\n },\n exit(_, state) {\n state.labellessBreakTargets--;\n },\n },\n \"BreakStatement|ContinueStatement\"(\n path: NodePath,\n state,\n ) {\n const { label } = path.node;\n if (label) {\n if (state.labelsStack.includes(label.name)) return;\n } else if (\n path.isBreakStatement()\n ? state.labellessBreakTargets > 0\n : state.labellessContinueTargets > 0\n ) {\n return;\n }\n state.breaksContinues.push(path);\n },\n ReturnStatement(path, state) {\n state.returns.push(path);\n },\n VariableDeclaration(path, state) {\n if (path.parent === state.loopNode && isVarInLoopHead(path)) return;\n if (path.node.kind === \"var\") state.vars.push(path);\n },\n};\n\nexport function wrapLoopBody(\n loopPath: NodePath,\n captured: string[],\n updatedBindingsUsages: Map[]>,\n) {\n const loopNode = loopPath.node;\n const state: CompletionsAndVarsState = {\n breaksContinues: [],\n returns: [],\n labelsStack: [],\n labellessBreakTargets: 0,\n labellessContinueTargets: 0,\n vars: [],\n loopNode,\n };\n loopPath.traverse(collectCompletionsAndVarsVisitor, state);\n\n const callArgs = [];\n const closureParams = [];\n const updater = [];\n for (const [name, updatedUsage] of updatedBindingsUsages) {\n callArgs.push(t.identifier(name));\n\n const innerName = loopPath.scope.generateUid(name);\n closureParams.push(t.identifier(innerName));\n updater.push(\n t.assignmentExpression(\"=\", t.identifier(name), t.identifier(innerName)),\n );\n for (const path of updatedUsage) path.replaceWith(t.identifier(innerName));\n }\n for (const name of captured) {\n if (updatedBindingsUsages.has(name)) continue; // already injected\n callArgs.push(t.identifier(name));\n closureParams.push(t.identifier(name));\n }\n\n const id = loopPath.scope.generateUid(\"loop\");\n const fn = t.functionExpression(\n null,\n closureParams,\n t.toBlock(loopNode.body),\n );\n let call: t.Expression = t.callExpression(t.identifier(id), callArgs);\n\n const fnParent = loopPath.findParent(p => p.isFunction());\n if (fnParent) {\n const { async, generator } = fnParent.node as t.Function;\n fn.async = async;\n fn.generator = generator;\n if (generator) call = t.yieldExpression(call, true);\n else if (async) call = t.awaitExpression(call);\n }\n\n const updaterNode =\n updater.length > 0\n ? t.expressionStatement(t.sequenceExpression(updater))\n : null;\n if (updaterNode) fn.body.body.push(updaterNode);\n\n // NOTE: Calling .insertBefore on the loop path might cause the\n // loop to be moved in the AST. For example, in\n // if (true) for (let x of y) ...\n // .insertBefore will replace the loop with a block:\n // if (true) { var _loop = ...; for (let x of y) ... }\n // All subsequent operations in this function on the loop node\n // must not assume that loopPath still represents the loop.\n // TODO: Consider using a function declaration\n const [varPath] = loopPath.insertBefore(\n t.variableDeclaration(\"var\", [t.variableDeclarator(t.identifier(id), fn)]),\n ) as [NodePath];\n\n const bodyStmts: t.Statement[] = [];\n\n const varNames: string[] = [];\n for (const varPath of state.vars) {\n const assign = [];\n for (const decl of varPath.node.declarations) {\n varNames.push(...Object.keys(t.getBindingIdentifiers(decl.id)));\n if (decl.init) {\n assign.push(t.assignmentExpression(\"=\", decl.id, decl.init));\n }\n }\n if (assign.length > 0) {\n let replacement: t.Node =\n assign.length === 1 ? assign[0] : t.sequenceExpression(assign);\n if (\n !t.isForStatement(varPath.parent, { init: varPath.node }) &&\n !t.isForXStatement(varPath.parent, { left: varPath.node })\n ) {\n replacement = t.expressionStatement(replacement);\n }\n varPath.replaceWith(replacement);\n } else {\n varPath.remove();\n }\n }\n if (varNames.length) {\n bodyStmts.push(\n t.variableDeclaration(\n \"var\",\n varNames.map(name => t.variableDeclarator(t.identifier(name))),\n ),\n );\n }\n\n if (state.breaksContinues.length === 0 && state.returns.length === 0) {\n bodyStmts.push(t.expressionStatement(call));\n } else {\n const completionId = loopPath.scope.generateUid(\"ret\");\n bodyStmts.push(\n t.variableDeclaration(\"var\", [\n t.variableDeclarator(t.identifier(completionId), call),\n ]),\n );\n\n const injected = new Set();\n for (const path of state.breaksContinues) {\n const { node } = path;\n const { type, label } = node;\n let name = type === \"BreakStatement\" ? \"break\" : \"continue\";\n if (label) name += \"|\" + label.name;\n path.replaceWith(t.returnStatement(t.stringLiteral(name)));\n if (updaterNode) path.insertBefore(t.cloneNode(updaterNode));\n\n if (injected.has(name)) continue;\n injected.add(name);\n\n bodyStmts.push(\n template.statement.ast`\n if (\n ${t.identifier(completionId)} === ${t.stringLiteral(name)}\n ) ${node}\n `,\n );\n }\n if (state.returns.length) {\n for (const path of state.returns) {\n const arg = path.node.argument || path.scope.buildUndefinedNode();\n path.replaceWith(\n template.statement.ast`\n return { v: ${arg} };\n `,\n );\n }\n\n bodyStmts.push(\n template.statement.ast`\n if (typeof ${t.identifier(completionId)} === \"object\")\n return ${t.identifier(completionId)}.v;\n `,\n );\n }\n }\n\n loopNode.body = t.blockStatement(bodyStmts);\n\n return varPath;\n}\n\nexport function isVarInLoopHead(path: NodePath) {\n if (t.isForStatement(path.parent)) return path.key === \"init\";\n if (t.isForXStatement(path.parent)) return path.key === \"left\";\n return false;\n}\n\nfunction filterMap(list: T[], fn: (item: T) => U | null) {\n const result: U[] = [];\n for (const item of list) {\n const mapped = fn(item);\n if (mapped) result.push(mapped);\n }\n return result;\n}\n"],"mappings":";;;;;;;;;AAAA;AAOA,MAAMA,8BAA8D,GAAG;EACrE,6BAA6B,CAACC,IAAI,EAAE;IAClCA,IAAI,CAACC,IAAI,EAAE;EACb,CAAC;EACDC,KAAK,CAACF,IAAI,EAAEG,KAAK,EAAE;IACjB,IAAIH,IAAI,CAACI,gBAAgB,EAAE,EAAEJ,IAAI,CAACC,IAAI,EAAE;IAExC,MAAM;MAAEI;IAAS,CAAC,GAAGL,IAAI,CAACM,KAAK;IAC/B,KAAK,MAAMC,IAAI,IAAIC,MAAM,CAACC,IAAI,CAACJ,QAAQ,CAAC,EAAE;MACxC,MAAMK,OAAO,GAAGL,QAAQ,CAACE,IAAI,CAAC;MAC9B,IACEG,OAAO,CAACC,IAAI,KAAK,KAAK,IACtBD,OAAO,CAACC,IAAI,KAAK,OAAO,IACxBD,OAAO,CAACC,IAAI,KAAK,SAAS,EAC1B;QACAR,KAAK,CAACS,WAAW,CAACC,IAAI,CAACH,OAAO,CAAC;MACjC;IACF;EACF;AACF,CAAC;AAEM,SAASI,mBAAmB,CAACC,QAA0B,EAAE;EAC9D,MAAMZ,KAA4B,GAAG;IAAES,WAAW,EAAE;EAAG,CAAC;EACxDG,QAAQ,CAACC,QAAQ,CAACjB,8BAA8B,EAAEI,KAAK,CAAC;EACxD,OAAOA,KAAK,CAACS,WAAW;AAC1B;AAEO,SAASK,cAAc,CAACP,OAAgB,EAAEK,QAA0B,EAAE;EAG3E,MAAMG,IAAI,GAAG,IAAIC,OAAO,EAAU;EAElC,IAAIC,iBAAiB,GAAG,KAAK;EAE7B,MAAMC,kBAAkB,GAAGC,SAAS,CAACZ,OAAO,CAACW,kBAAkB,EAAErB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CAACiE,MAAM,CAAC,EAAE,OAAOjE,IAAI,CAACiI,GAAG,KAAK,MAAM;EAC9D,OAAO,KAAK;AACd;AAEA,SAAS3G,SAAS,CAAsB4G,IAAS,EAAEhD,EAAyB,EAAE;EAC5E,MAAMiD,MAAW,GAAG,EAAE;EACtB,KAAK,MAAMC,IAAI,IAAIF,IAAI,EAAE;IACvB,MAAMG,MAAM,GAAGnD,EAAE,CAACkD,IAAI,CAAC;IACvB,IAAIC,MAAM,EAAEF,MAAM,CAACtH,IAAI,CAACwH,MAAM,CAAC;EACjC;EACA,OAAOF,MAAM;AACf"}lib/annex-B_3_3.js.map000066600000017015150444362230010373 0ustar00{"version":3,"names":["annexB33FunctionsVisitor","VariableDeclaration","path","isStrict","node","kind","varScope","scope","getFunctionParent","getProgramParent","traverse","functionsToVarVisitor","names","Object","keys","getBindingIdentifiers","BlockStatement","t","isFunction","parent","body","transformStatementList","get","SwitchCase","paths","outer","isFunctionDeclaration","async","generator","parentPath","isVarScope","name","id","currScope","hasOwnBinding","maybeTransformBlockScopedFunction","removeOwnBinding","varNode","variableDeclaration","variableDeclarator","toExpression","_blockHoist","varPath","replaceWith","registerDeclaration","Scope","binding","getOwnBinding","skip","isFunctionParent","isProgram","find","sourceType","isClass","isBlockStatement","directives","some","directive","value"],"sources":["../src/annex-B_3_3.ts"],"sourcesContent":["import { types as t } from \"@babel/core\";\nimport type { NodePath, Visitor, Scope } from \"@babel/traverse\";\n\n// Whenever a function declaration in a nested block scope\n// doesn't conflict with a block-scoped binding from an outer\n// scope, we transform it to a variable declaration.\n//\n// This implements the Annex B.3.3 behavior.\n//\n// TODO(Babel 8): Figure out how this should interact with\n// the transform-block-scoped functions plugin (it feels\n// wrong to handle this transform here), and what we want\n// to do with Annex B behavior in general.\n\n// To avoid confusing block-scoped variables transformed to\n// var with original vars, this transformation happens in two\n// different places:\n// 1. For functions that \"conflict\" with var-variables, in\n// the VariableDeclaration visitor.\n// 2. For functions that don't conflict with any variable,\n// in the FunctionDeclaration visitor.\n\nexport const annexB33FunctionsVisitor: Visitor = {\n VariableDeclaration(path) {\n if (isStrict(path)) return;\n if (path.node.kind !== \"var\") return;\n\n const varScope =\n path.scope.getFunctionParent() || path.scope.getProgramParent();\n // eslint-disable-next-line @typescript-eslint/no-use-before-define\n varScope.path.traverse(functionsToVarVisitor, {\n names: Object.keys(path.getBindingIdentifiers()),\n });\n },\n\n // NOTE: These two visitors target the same nodes as the\n // block-scoped-functions plugin\n\n BlockStatement(path) {\n if (isStrict(path)) return;\n if (t.isFunction(path.parent, { body: path.node })) return;\n transformStatementList(path.get(\"body\"));\n },\n\n SwitchCase(path) {\n if (isStrict(path)) return;\n transformStatementList(path.get(\"consequent\"));\n },\n};\n\nfunction transformStatementList(paths: NodePath[]) {\n outer: for (const path of paths) {\n if (!path.isFunctionDeclaration()) continue;\n // Annex B.3.3 only applies to plain functions.\n if (path.node.async || path.node.generator) return;\n\n const { scope } = path.parentPath;\n if (isVarScope(scope)) return;\n\n const { name } = path.node.id;\n let currScope = scope;\n do {\n if (currScope.parent.hasOwnBinding(name)) continue outer;\n currScope = currScope.parent;\n } while (!isVarScope(currScope));\n\n maybeTransformBlockScopedFunction(path);\n }\n}\n\nfunction maybeTransformBlockScopedFunction(\n path: NodePath,\n) {\n const {\n node,\n parentPath: { scope },\n } = path;\n\n const { id } = node;\n scope.removeOwnBinding(id.name);\n node.id = null;\n\n const varNode = t.variableDeclaration(\"var\", [\n t.variableDeclarator(id, t.toExpression(node)),\n ]);\n // @ts-expect-error undocumented property\n varNode._blockHoist = 2;\n\n const [varPath] = path.replaceWith(varNode);\n scope.registerDeclaration(varPath);\n}\n\nconst functionsToVarVisitor: Visitor<{ names: string[] }> = {\n Scope(path, { names }) {\n for (const name of names) {\n const binding = path.scope.getOwnBinding(name);\n if (binding && binding.kind === \"hoisted\") {\n maybeTransformBlockScopedFunction(\n binding.path as NodePath,\n );\n }\n }\n },\n \"Expression|Declaration\"(path) {\n path.skip();\n },\n};\n\nexport function isVarScope(scope: Scope) {\n return scope.path.isFunctionParent() || scope.path.isProgram();\n}\n\nfunction isStrict(path: NodePath) {\n return !!path.find(({ node }) => {\n if (t.isProgram(node)) {\n if (node.sourceType === \"module\") return true;\n } else if (t.isClass(node)) {\n return true;\n } else if (!t.isBlockStatement(node)) {\n return false;\n }\n\n return node.directives?.some(\n directive => directive.value.value === \"use strict\",\n );\n });\n}\n"],"mappings":";;;;;;;AAAA;AAsBO,MAAMA,wBAAiC,GAAG;EAC/CC,mBAAmB,CAACC,IAAI,EAAE;IACxB,IAAIC,QAAQ,CAACD,IAAI,CAAC,EAAE;IACpB,IAAIA,IAAI,CAACE,IAAI,CAACC,IAAI,KAAK,KAAK,EAAE;IAE9B,MAAMC,QAAQ,GACZJ,IAAI,CAACK,KAAK,CAACC,iBAAiB,EAAE,IAAIN,IAAI,CAACK,KAAK,CAACE,gBAAgB,EAAE;IAEjEH,QAAQ,CAACJ,IAAI,CAACQ,QAAQ,CAACC,qBAAqB,EAAE;MAC5CC,KAAK,EAAEC,MAAM,CAACC,IAAI,CAACZ,IAAI,CAACa,qBAAqB,EAAE;IACjD,CAAC,CAAC;EACJ,CAAC;EAKDC,cAAc,CAACd,IAAI,EAAE;IACnB,IAAIC,QAAQ,CAACD,IAAI,CAAC,EAAE;IACpB,IAAIe,WAAC,CAACC,UAAU,CAAChB,IAAI,CAACiB,MAAM,EAAE;MAAEC,IAAI,EAAElB,IAAI,CAACE;IAAK,CAAC,CAAC,EAAE;IACpDiB,sBAAsB,CAACnB,IAAI,CAACoB,GAAG,CAAC,MAAM,CAAC,CAAC;EAC1C,CAAC;EAEDC,UAAU,CAACrB,IAAI,EAAE;IACf,IAAIC,QAAQ,CAACD,IAAI,CAAC,EAAE;IACpBmB,sBAAsB,CAACnB,IAAI,CAACoB,GAAG,CAAC,YAAY,CAAC,CAAC;EAChD;AACF,CAAC;AAAC;AAEF,SAASD,sBAAsB,CAACG,KAA8B,EAAE;EAC9DC,KAAK,EAAE,KAAK,M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CAAC;AACpC;AAEA,MAAM/B,qBAAmD,GAAG;EAC1DkC,KAAK,CAAC3C,IAAI,EAAE;IAAEU;EAAM,CAAC,EAAE;IACrB,KAAK,MAAMmB,IAAI,IAAInB,KAAK,EAAE;MACxB,MAAMkC,OAAO,GAAG5C,IAAI,CAACK,KAAK,CAACwC,aAAa,CAAChB,IAAI,CAAC;MAC9C,IAAIe,OAAO,IAAIA,OAAO,CAACzC,IAAI,KAAK,SAAS,EAAE;QACzC8B,iCAAiC,CAC/BW,OAAO,CAAC5C,IAAI,CACb;MACH;IACF;EACF,CAAC;EACD,wBAAwB,CAACA,IAAI,EAAE;IAC7BA,IAAI,CAAC8C,IAAI,EAAE;EACb;AACF,CAAC;AAEM,SAASlB,UAAU,CAACvB,KAAY,EAAE;EACvC,OAAOA,KAAK,CAACL,IAAI,CAAC+C,gBAAgB,EAAE,IAAI1C,KAAK,CAACL,IAAI,CAACgD,SAAS,EAAE;AAChE;AAEA,SAAS/C,QAAQ,CAACD,IAAc,EAAE;EAChC,OAAO,CAAC,CAACA,IAAI,CAACiD,IAAI,CAAC,CAAC;IAAE/C;EAAK,CAAC,KAAK;IAAA;IAC/B,IAAIa,WAAC,CAACiC,SAAS,CAAC9C,IAAI,CAAC,EAAE;MACrB,IAAIA,IAAI,CAACgD,UAAU,KAAK,QAAQ,EAAE,OAAO,IAAI;IAC/C,CAAC,MAAM,IAAInC,WAAC,CAACoC,OAAO,CAACjD,IAAI,CAAC,EAAE;MAC1B,OAAO,IAAI;IACb,CAAC,MAAM,IAAI,CAACa,WAAC,CAACqC,gBAAgB,CAAClD,IAAI,CAAC,EAAE;MACpC,OAAO,KAAK;IACd;IAEA,2BAAOA,IAAI,CAACmD,UAAU,qBAAf,iBAAiBC,IAAI,CAC1BC,SAAS,IAAIA,SAAS,CAACC,KAAK,CAACA,KAAK,KAAK,YAAY,CACpD;EACH,CAAC,CAAC;AACJ"}lib/validation.js.map000066600000031267150444362230010576 0ustar00{"version":3,"names":["validateUsage","path","state","tdzEnabled","dynamicTDZNames","name","Object","keys","getBindingIdentifiers","binding","scope","getBinding","injectTDZChecks","push","node","kind","disallowConstantViolations","violation","constantViolations","readOnlyError","addHelper","throwNode","t","callExpression","stringLiteral","isAssignmentExpression","operator","left","right","exprs","replaceWith","sequenceExpression","includes","logicalExpression","slice","binaryExpression","isUpdateExpression","unaryExpression","get","isForXStatement","ensureBlock","variableDeclaration","variableDeclarator","generateUidIdentifier","body","unshift","expressionStatement","getTDZStatus","refPath","bindingPath","executionStatus","_guessExecutionStatusRelativeTo","skipTDZChecks","WeakSet","buildTDZAssert","status","clone","cloneNode","add","getTDZReplacement","id","has","isFunctionDeclaration","parent","_tdzThis","allUsages","Set","referencePaths","forEach","dynamicTdz","arg","replacement","insertBefore","nodes","ids","length","parentPath","isFor"],"sources":["../src/validation.ts"],"sourcesContent":["import { types as t, type PluginPass } from \"@babel/core\";\nimport type { Binding, NodePath } from \"@babel/traverse\";\n\nexport function validateUsage(\n path: NodePath,\n state: PluginPass,\n tdzEnabled: boolean,\n) {\n const dynamicTDZNames = [];\n\n for (const name of Object.keys(path.getBindingIdentifiers())) {\n const binding = path.scope.getBinding(name);\n // binding may be null. ref: https://github.com/babel/babel/issues/15300\n if (!binding) continue;\n if (tdzEnabled) {\n if (injectTDZChecks(binding, state)) dynamicTDZNames.push(name);\n }\n if (path.node.kind === \"const\") {\n disallowConstantViolations(name, binding, state);\n }\n }\n\n return dynamicTDZNames;\n}\n\nfunction disallowConstantViolations(\n name: string,\n binding: Binding,\n state: PluginPass,\n) {\n for (const violation of binding.constantViolations) {\n const readOnlyError = state.addHelper(\"readOnlyError\");\n const throwNode = t.callExpression(readOnlyError, [t.stringLiteral(name)]);\n\n if (violation.isAssignmentExpression()) {\n const { operator, left, right } = violation.node;\n if (operator === \"=\") {\n const exprs = [right];\n exprs.push(throwNode);\n violation.replaceWith(t.sequenceExpression(exprs));\n } else if ([\"&&=\", \"||=\", \"??=\"].includes(operator)) {\n violation.replaceWith(\n t.logicalExpression(\n // @ts-expect-error todo: give a better type to operator\n operator.slice(0, -1),\n left,\n t.sequenceExpression([right, throwNode]),\n ),\n );\n } else {\n violation.replaceWith(\n t.sequenceExpression([\n t.binaryExpression(\n // @ts-expect-error todo: give a better type to operator\n operator.slice(0, -1),\n left,\n right,\n ),\n throwNode,\n ]),\n );\n }\n } else if (violation.isUpdateExpression()) {\n violation.replaceWith(\n t.sequenceExpression([\n t.unaryExpression(\"+\", violation.get(\"argument\").node),\n throwNode,\n ]),\n );\n } else if (violation.isForXStatement()) {\n violation.ensureBlock();\n violation\n .get(\"left\")\n .replaceWith(\n t.variableDeclaration(\"var\", [\n t.variableDeclarator(violation.scope.generateUidIdentifier(name)),\n ]),\n );\n violation.node.body.body.unshift(t.expressionStatement(throwNode));\n }\n }\n}\n\nfunction getTDZStatus(refPath: NodePath, bindingPath: NodePath) {\n const executionStatus = bindingPath._guessExecutionStatusRelativeTo(refPath);\n\n if (executionStatus === \"before\") {\n return \"outside\";\n } else if (executionStatus === \"after\") {\n return \"inside\";\n } else {\n return \"maybe\";\n }\n}\n\nconst skipTDZChecks = new WeakSet();\n\nfunction buildTDZAssert(\n status: \"maybe\" | \"inside\",\n node: t.Identifier | t.JSXIdentifier,\n state: PluginPass,\n) {\n if (status === \"maybe\") {\n const clone = t.cloneNode(node);\n skipTDZChecks.add(clone);\n return t.callExpression(state.addHelper(\"temporalRef\"), [\n // @ts-expect-error Fixme: we may need to handle JSXIdentifier\n clone,\n t.stringLiteral(node.name),\n ]);\n } else {\n return t.callExpression(state.addHelper(\"tdz\"), [\n t.stringLiteral(node.name),\n ]);\n }\n}\n\ntype TDZReplacement = { status: \"maybe\" | \"inside\"; node: t.Expression };\nfunction getTDZReplacement(\n path: NodePath,\n state: PluginPass,\n): TDZReplacement | undefined;\nfunction getTDZReplacement(\n path: NodePath,\n state: PluginPass,\n id: t.Identifier | t.JSXIdentifier,\n): TDZReplacement | undefined;\nfunction getTDZReplacement(\n path: NodePath,\n state: PluginPass,\n id: t.Identifier | t.JSXIdentifier = path.node as any,\n): TDZReplacement | undefined {\n if (skipTDZChecks.has(id)) return;\n skipTDZChecks.add(id);\n\n const bindingPath = path.scope.getBinding(id.name)?.path;\n\n if (!bindingPath || bindingPath.isFunctionDeclaration()) return;\n\n const status = getTDZStatus(path, bindingPath);\n if (status === \"outside\") return;\n\n if (status === \"maybe\") {\n // add tdzThis to parent variable declarator so it's exploded\n // @ts-expect-error todo(flow->ts): avoid mutations\n bindingPath.parent._tdzThis = true;\n }\n\n return { status, node: buildTDZAssert(status, id, state) };\n}\n\nfunction injectTDZChecks(binding: Binding, state: PluginPass) {\n const allUsages = new Set(binding.referencePaths);\n binding.constantViolations.forEach(allUsages.add, allUsages);\n\n let dynamicTdz = false;\n\n for (const path of binding.constantViolations) {\n const { node } = path;\n if (skipTDZChecks.has(node)) continue;\n skipTDZChecks.add(node);\n\n if (path.isUpdateExpression()) {\n // arg is an identifier referencing the current binding\n const arg = path.get(\"argument\") as NodePath;\n\n const replacement = getTDZReplacement(path, state, arg.node);\n if (!replacement) continue;\n\n if (replacement.status === \"maybe\") {\n dynamicTdz = true;\n path.insertBefore(replacement.node);\n } else {\n path.replaceWith(replacement.node);\n }\n } else if (path.isAssignmentExpression()) {\n const nodes = [];\n const ids = path.getBindingIdentifiers();\n\n for (const name of Object.keys(ids)) {\n const replacement = getTDZReplacement(path, state, ids[name]);\n if (replacement) {\n nodes.push(t.expressionStatement(replacement.node));\n if (replacement.status === \"inside\") break;\n if (replacement.status === \"maybe\") dynamicTdz = true;\n }\n }\n\n if (nodes.length > 0) path.insertBefore(nodes);\n }\n }\n\n for (const path of binding.referencePaths as NodePath[]) {\n if (path.parentPath.isUpdateExpression()) continue;\n // It will be handled after transforming the loop\n if (path.parentPath.isFor({ left: path.node })) continue;\n\n const replacement = getTDZReplacement(path, state);\n if (!replacement) continue;\n if (replacement.status === \"maybe\") dynamicTdz = true;\n\n path.replaceWith(replacement.node);\n }\n\n return dynamicTdz;\n}\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IAAI,CAAC;EACpC;EAEA,OAAOuD,UAAU;AACnB"}lib/annex-B_3_3.js000066600000005426150444362230007622 0ustar00"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.annexB33FunctionsVisitor = void 0; exports.isVarScope = isVarScope; var _core = require("@babel/core"); const annexB33FunctionsVisitor = { VariableDeclaration(path) { if (isStrict(path)) return; if (path.node.kind !== "var") return; const varScope = path.scope.getFunctionParent() || path.scope.getProgramParent(); varScope.path.traverse(functionsToVarVisitor, { names: Object.keys(path.getBindingIdentifiers()) }); }, BlockStatement(path) { if (isStrict(path)) return; if (_core.types.isFunction(path.parent, { body: path.node })) return; transformStatementList(path.get("body")); }, SwitchCase(path) { if (isStrict(path)) return; transformStatementList(path.get("consequent")); } }; exports.annexB33FunctionsVisitor = annexB33FunctionsVisitor; function transformStatementList(paths) { outer: for (const path of paths) { if (!path.isFunctionDeclaration()) continue; if (path.node.async || path.node.generator) return; const { scope } = path.parentPath; if (isVarScope(scope)) return; const { name } = path.node.id; let currScope = scope; do { if (currScope.parent.hasOwnBinding(name)) continue outer; currScope = currScope.parent; } while (!isVarScope(currScope)); maybeTransformBlockScopedFunction(path); } } function maybeTransformBlockScopedFunction(path) { const { node, parentPath: { scope } } = path; const { id } = node; scope.removeOwnBinding(id.name); node.id = null; const varNode = _core.types.variableDeclaration("var", [_core.types.variableDeclarator(id, _core.types.toExpression(node))]); varNode._blockHoist = 2; const [varPath] = path.replaceWith(varNode); scope.registerDeclaration(varPath); } const functionsToVarVisitor = { Scope(path, { names }) { for (const name of names) { const binding = path.scope.getOwnBinding(name); if (binding && binding.kind === "hoisted") { maybeTransformBlockScopedFunction(binding.path); } } }, "Expression|Declaration"(path) { path.skip(); } }; function isVarScope(scope) { return scope.path.isFunctionParent() || scope.path.isProgram(); } function isStrict(path) { return !!path.find(({ node }) => { var _node$directives; if (_core.types.isProgram(node)) { if (node.sourceType === "module") return true; } else if (_core.types.isClass(node)) { return true; } else if (!_core.types.isBlockStatement(node)) { return false; } return (_node$directives = node.directives) == null ? void 0 : _node$directives.some(directive => directive.value.value === "use strict"); }); } //# sourceMappingURL=annex-B_3_3.js.map LICENSE000066600000002122150444362230005555 0ustar00MIT License Copyright (c) 2014-present Sebastian McKenzie and other contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package.json000066600000001623150444362230007043 0ustar00{ "name": "@babel/plugin-transform-block-scoping", "version": "7.21.0", "description": "Compile ES2015 block scoping (const and let) to ES5", "repository": { "type": "git", "url": "https://github.com/babel/babel.git", "directory": "packages/babel-plugin-transform-block-scoping" }, "homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-block-scoping", "license": "MIT", "publishConfig": { "access": "public" }, "main": "./lib/index.js", "dependencies": { "@babel/helper-plugin-utils": "^7.20.2" }, "keywords": [ "babel-plugin" ], "peerDependencies": { "@babel/core": "^7.0.0-0" }, "devDependencies": { "@babel/core": "^7.21.0", "@babel/helper-plugin-test-runner": "^7.18.6", "@babel/traverse": "^7.21.0" }, "engines": { "node": ">=6.9.0" }, "author": "The Babel Team (https://babel.dev/team)", "type": "commonjs" }