1 |
- {"version":3,"names":["getTDZStatus","refPath","bindingPath","executionStatus","_guessExecutionStatusRelativeTo","buildTDZAssert","node","state","t","callExpression","addHelper","stringLiteral","name","isReference","scope","declared","letReferences","get","getBindingIdentifier","visitedMaybeTDZNodes","WeakSet","visitor","ReferencedIdentifier","path","tdzEnabled","parent","parentPath","isFor","left","getBinding","isFunctionDeclaration","status","has","add","assert","_tdzThis","isUpdateExpression","_ignoreBlockScopingTDZ","replaceWith","sequenceExpression","template","ast","AssignmentExpression","exit","nodes","ids","getBindingIdentifiers","Object","keys","id","push","length","replaceWithMultiple","map","n","expressionStatement"],"sources":["../src/tdz.ts"],"sourcesContent":["import { types as t, template, type PluginPass } from \"@babel/core\";\nimport type { NodePath, Scope, Visitor } from \"@babel/traverse\";\n\nfunction getTDZStatus(\n refPath: NodePath<t.Identifier | t.JSXIdentifier>,\n bindingPath: NodePath,\n) {\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\nfunction buildTDZAssert(\n node: t.Identifier | t.JSXIdentifier,\n state: TDZVisitorState,\n) {\n return t.callExpression(state.addHelper(\"temporalRef\"), [\n // @ts-expect-error Fixme: we may need to handle JSXIdentifier\n node,\n t.stringLiteral(node.name),\n ]);\n}\n\nfunction isReference(\n node: t.Identifier | t.JSXIdentifier,\n scope: Scope,\n state: TDZVisitorState,\n) {\n const declared = state.letReferences.get(node.name);\n if (!declared) return false;\n\n // declared node is different in this scope\n return scope.getBindingIdentifier(node.name) === declared;\n}\n\nconst visitedMaybeTDZNodes = new WeakSet();\n\nexport interface TDZVisitorState {\n tdzEnabled: boolean;\n addHelper: PluginPass[\"addHelper\"];\n letReferences: Map<string, t.Identifier>;\n}\n\nexport const visitor: Visitor<TDZVisitorState> = {\n ReferencedIdentifier(path, state) {\n if (!state.tdzEnabled) return;\n\n const { node, parent, scope } = path;\n\n if (path.parentPath.isFor({ left: node })) return;\n if (!isReference(node, scope, state)) return;\n\n const bindingPath = scope.getBinding(node.name).path;\n\n if (bindingPath.isFunctionDeclaration()) return;\n\n const status = getTDZStatus(path, bindingPath);\n if (status === \"outside\") return;\n\n if (status === \"maybe\") {\n if (visitedMaybeTDZNodes.has(node)) {\n return;\n }\n visitedMaybeTDZNodes.add(node);\n const assert = buildTDZAssert(node, state);\n\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 if (path.parentPath.isUpdateExpression()) {\n // @ts-expect-error todo(flow->ts): avoid node mutations\n if (parent._ignoreBlockScopingTDZ) return;\n path.parentPath.replaceWith(\n t.sequenceExpression([assert, parent as t.UpdateExpression]),\n );\n } else {\n path.replaceWith(assert);\n }\n } else if (status === \"inside\") {\n path.replaceWith(\n template.ast`${state.addHelper(\"tdz\")}(\"${node.name}\")` as t.Statement,\n );\n }\n },\n\n AssignmentExpression: {\n exit(path, state) {\n if (!state.tdzEnabled) return;\n\n const { node } = path;\n\n // @ts-expect-error todo(flow->ts): avoid node mutations\n if (node._ignoreBlockScopingTDZ) return;\n\n const nodes = [];\n const ids = path.getBindingIdentifiers();\n\n for (const name of Object.keys(ids)) {\n const id = ids[name];\n\n if (isReference(id, path.scope, state)) {\n nodes.push(id);\n }\n }\n\n if (nodes.length) {\n // @ts-expect-error todo(flow->ts): avoid mutations\n node._ignoreBlockScopingTDZ = true;\n nodes.push(node);\n path.replaceWithMultiple(nodes.map(n => t.expressionStatement(n)));\n }\n },\n },\n};\n"],"mappings":";;;;;;AAAA;AAGA,SAASA,YAAY,CACnBC,OAAiD,EACjDC,WAAqB,EACrB;EACA,MAAMC,eAAe,GAAGD,WAAW,CAACE,+BAA+B,CAACH,OAAO,CAAC;EAE5E,IAAIE,eAAe,KAAK,QAAQ,EAAE;IAChC,OAAO,SAAS;EAClB,CAAC,MAAM,IAAIA,eAAe,KAAK,OAAO,EA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