node-ejs-renderer/node_modules/@babel/plugin-transform-block-scoping/lib/annex-B_3_3.js.map

1 line
7.7 KiB
Plaintext
Raw Normal View History

2024-06-09 13:55:01 -04:00
{"version":3,"names":["_core","require","annexB33FunctionsVisitor","exports","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","Expression|Declaration","skip","isFunctionParent","isProgram","find","_node$directives","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<t.Statement>[]) {\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<t.FunctionDeclaration>,\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