mirror of https://github.com/jkjoy/sunpeiwen.git
1 line
21 KiB
Plaintext
1 line
21 KiB
Plaintext
|
{"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<LoopBodyBindingsState> = {\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<t.Loop>) {\n const state: LoopBodyBindingsState = { blockScoped: [] };\n loopPath.traverse(collectLoopBodyBindingsVisitor, state);\n return state.blockScoped;\n}\n\nexport function getUsageInBody(binding: Binding, loopPath: NodePath<t.Loop>) {\n // UpdateExpressions are counted both as a reference and a mutation,\n // so we need to de-duplicate them.\n const seen = new WeakSet<t.Node>();\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<t.Identifier> | 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<t.Identifier>;\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<t.Loop>) {\n const bodyPath = loopPath.get(\"body\");\n let inClosure
|