"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLoopBodyBindings = getLoopBodyBindings; exports.getUsageInBody = getUsageInBody; exports.isVarInForStatementInit = isVarInForStatementInit; exports.isVarInLoopHead = isVarInLoopHead; exports.moveCapturingTestAndUpdateIntoBody = moveCapturingTestAndUpdateIntoBody; exports.wrapLoopBody = wrapLoopBody; var _core = require("@babel/core"); const collectLoopBodyBindingsVisitor = { "Expression|Declaration"(path) { path.skip(); }, Loop(path, state) { path.skip(); if (!path.isForStatement()) return; const init = path.get("init"); if (!init.isVariableDeclaration() || init.node.kind === "var") return; for (const name of Object.keys(init.getBindingIdentifiers())) { const binding = path.scope.getOwnBinding(name); if (!binding) continue; const capturedInHeadClosure = [...binding.referencePaths, ...binding.constantViolations].some(ref => { const { inHead, inClosure } = relativeLoopLocation(ref, path); return inHead && inClosure; }); if (capturedInHeadClosure) state.blockScoped.push(binding); } }, 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; let capturedInHeadClosure = false; const headUsages = []; const constantViolations = []; for (const path of binding.constantViolations) { var _id$getBindingIdentif; const { inBody, inHead, inClosure } = relativeLoopLocation(path, loopPath); const id = path.isUpdateExpression() ? path.get("argument") : path.isAssignmentExpression() ? path.get("left") : null; if (id) seen.add(id.node); const ids = (_id$getBindingIdentif = id == null ? void 0 : id.getBindingIdentifierPaths(true)[binding.identifier.name]) != null ? _id$getBindingIdentif : []; if (inHead) { capturedInHeadClosure || (capturedInHeadClosure = inClosure); headUsages.push(...ids); continue; } if (!inBody) continue; capturedInClosure || (capturedInClosure = inClosure); constantViolations.push(...ids); } const references = filterMap(binding.referencePaths, path => { if (seen.has(path.node)) return null; const { inBody, inHead, inClosure } = relativeLoopLocation(path, loopPath); if (inHead) { capturedInHeadClosure || (capturedInHeadClosure = inClosure); headUsages.push(path); return null; } if (!inBody) return null; capturedInClosure || (capturedInClosure = inClosure); return path; }); return { capturedInClosure, hasConstantViolations: constantViolations.length > 0, usages: references.concat(constantViolations), capturedInHeadClosure, headUsages }; } function moveCapturingTestAndUpdateIntoBody(loopPath) { const init = loopPath.get("init"); if (!init.isVariableDeclaration() || init.node.kind !== "let") return; let testCaptures = false; let updateCaptures = false; for (const name of Object.keys(init.getBindingIdentifiers())) { const binding = loopPath.scope.getOwnBinding(name); if (!(binding != null && binding.constantViolations.length)) continue; for (const ref of [...binding.referencePaths, ...binding.constantViolations]) { const { inTest, inUpdate, inClosure } = relativeLoopLocation(ref, loopPath); testCaptures || (testCaptures = inTest && inClosure); updateCaptures || (updateCaptures = inUpdate && inClosure); } } if (!testCaptures && !updateCaptures) return; makeAnnexBAssignmentsExplicit(loopPath); const { node } = loopPath; const bodyStmts = []; if (updateCaptures && node.update) { const first = loopPath.scope.generateUid("first"); init.node.declarations.push(_core.types.variableDeclarator(_core.types.identifier(first), _core.types.booleanLiteral(true))); bodyStmts.push(_core.types.ifStatement(_core.types.unaryExpression("!", _core.types.identifier(first)), _core.types.expressionStatement(node.update))); node.update = _core.types.assignmentExpression("=", _core.types.identifier(first), _core.types.booleanLiteral(false)); } if (node.test) { bodyStmts.push(_core.types.ifStatement(_core.types.unaryExpression("!", node.test), _core.types.breakStatement())); node.test = null; } if (!_core.types.isEmptyStatement(node.body)) bodyStmts.push(node.body); node.body = _core.types.blockStatement(bodyStmts); loopPath.scope.crawl(); } function makeAnnexBAssignmentsExplicit(loopPath) { const declarations = []; loopPath.get("body").traverse({ Function(path) { if (path.isFunctionDeclaration() && (path.parentPath.isBlockStatement() || path.parentPath.isSwitchCase()) && path.node.id && !path.node.generator && !path.node.async && !path.isInStrictMode() && !hasLexicalConflict(loopPath, path.node.id.name)) { declarations.push(path); } path.skip(); } }); if (declarations.length === 0) return; const names = new Set(); const renamed = new Map(); for (const path of declarations) { const name = path.node.id.name; names.add(name); const declScope = path.scope.parent; if (!renamed.has(declScope)) renamed.set(declScope, new Set()); if (!renamed.get(declScope).has(name)) { renamed.get(declScope).add(name); declScope.rename(name); } path.insertAfter(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(name), _core.types.cloneNode(path.node.id)))); } const body = _core.types.toBlock(loopPath.node.body); body.body.unshift(_core.types.variableDeclaration("var", Array.from(names, name => _core.types.variableDeclarator(_core.types.identifier(name))))); loopPath.node.body = body; } function hasLexicalConflict(loopPath, name) { const binding = loopPath.scope.getBinding(name); return !!binding && binding.kind !== "var" && binding.kind !== "hoisted"; } function relativeLoopLocation(path, loopPath) { const bodyPath = loopPath.get("body"); const headPath = loopPath.isForStatement() ? loopPath.get("init") : null; const testPath = loopPath.isForStatement() ? loopPath.get("test") : null; const updatePath = loopPath.isForStatement() ? loopPath.get("update") : null; let inClosure = false; for (let currPath = path; currPath; currPath = currPath.parentPath) { if (currPath.isFunction() || currPath.isClass() || currPath.isMethod()) { inClosure = true; } const location = { inBody: false, inHead: false, inTest: false, inUpdate: false, inClosure }; if (currPath === bodyPath) { return Object.assign({}, location, { inBody: true }); } else if (headPath && currPath === headPath) { return Object.assign({}, location, { inHead: true }); } else if (testPath != null && testPath.node && currPath === testPath) { return Object.assign({}, location, { inTest: true }); } else if (updatePath != null && updatePath.node && currPath === updatePath) { return Object.assign({}, location, { inUpdate: true }); } else if (currPath === loopPath) { return location; } } 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.blockStatement([])); let call = _core.types.callExpression(_core.types.identifier(id), callArgs); const fnParent = loopPath.findParent(p => p.isFunctionParent()); if (fnParent) { const { async = false, generator = false } = 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; 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)); } else if (_core.types.isForXStatement(varPath.parent, { left: varPath.node })) { assign.push(decl.id); } } if (assign.length > 0) { const replacement = assign.length === 1 ? assign[0] : _core.types.sequenceExpression(assign); varPath.replaceWith(replacement); } else { varPath.remove(); } } if (varNames.length) { varPath.pushContainer("declarations", varNames.map(name => _core.types.variableDeclarator(_core.types.identifier(name)))); } const labelNum = state.breaksContinues.length; const returnNum = state.returns.length; if (labelNum + returnNum === 0) { bodyStmts.push(_core.types.expressionStatement(call)); } else if (labelNum === 1 && returnNum === 0) { 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.addComment(_core.types.returnStatement(_core.types.numericLiteral(1)), "trailing", " " + name, true)); if (updaterNode) path.insertBefore(_core.types.cloneNode(updaterNode)); bodyStmts.push(_core.template.statement.ast` if (${call}) ${node} `); } } else { const completionId = loopPath.scope.generateUid("ret"); if (varPath.isVariableDeclaration()) { varPath.pushContainer("declarations", [_core.types.variableDeclarator(_core.types.identifier(completionId))]); bodyStmts.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(completionId), call))); } else { bodyStmts.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(completionId), call)])); } const injected = []; for (const path of state.breaksContinues) { const { node } = path; const { type, label } = node; let name = type === "BreakStatement" ? "break" : "continue"; if (label) name += " " + label.name; let i = injected.indexOf(name); const hasInjected = i !== -1; if (!hasInjected) { injected.push(name); i = injected.length - 1; } path.replaceWith(_core.types.addComment(_core.types.returnStatement(_core.types.numericLiteral(i)), "trailing", " " + name, true)); if (updaterNode) path.insertBefore(_core.types.cloneNode(updaterNode)); if (hasInjected) continue; bodyStmts.push(_core.template.statement.ast` if (${_core.types.identifier(completionId)} === ${_core.types.numericLiteral(i)}) ${node} `); } if (returnNum) { 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 (${_core.types.identifier(completionId)}) return ${_core.types.identifier(completionId)}.v; `); } } const loopBlockBody = _core.types.toBlock(loopNode.body); loopNode.body = _core.types.blockStatement(bodyStmts); fn.body = loopBlockBody; if (updaterNode) loopBlockBody.body.push(updaterNode); 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 isVarInForStatementInit(path) { return path.parentPath.isForStatement() && path.key === "init"; } 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