Innovenergy_trunk/frontend/node_modules/@babel/plugin-transform-block-scoping/lib/validation.js.map

1 line
13 KiB
Plaintext

{"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<t.VariableDeclaration>,\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<t.Identifier | t.JSXIdentifier>,\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<t.Identifier>;\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<t.Identifier>[]) {\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}\n"],"mappings":";;;;;;AAAA;AAGO,SAASA,aAAa,CAC3BC,IAAqC,EACrCC,KAAiB,EACjBC,UAAmB,EACnB;EACA,MAAMC,eAAe,GAAG,EAAE;EAE1B,KAAK,MAAMC,IAAI,IAAIC,MAAM,CAACC,IAAI,CAACN,IAAI,CAACO,qBAAqB,EAAE,CAAC,EAAE;IAC5D,MAAMC,OAAO,GAAGR,IAAI,CAACS,KAAK,CAACC,UAAU,CAACN,IAAI,CAAC;IAE3C,IAAI,CAACI,OAAO,EAAE;IACd,IAAIN,UAAU,EAAE;MACd,IAAIS,eAAe,CAACH,OAAO,EAAEP,KAAK,CAAC,EAAEE,eAAe,CAACS,IAAI,CAACR,IAAI,CAAC;IACjE;IACA,IAAIJ,IAAI,CAACa,IAAI,CAACC,IAAI,KAAK,OAAO,EAAE;MAC9BC,0BAA0B,CAACX,IAAI,EAAEI,OAAO,EAAEP,KAAK,CAAC;IAClD;EACF;EAEA,OAAOE,eAAe;AACxB;AAEA,SAASY,0BAA0B,CACjCX,IAAY,EACZI,OAAgB,EAChBP,KAAiB,EACjB;EACA,KAAK,MAAMe,SAAS,IAAIR,OAAO,CAACS,kBAAkB,EAAE;IAClD,MAAMC,aAAa,GAAGjB,KAAK,CAACkB,SAAS,CAAC,eAAe,CAAC;IACtD,MAAMC,SAAS,GAAGC,WAAC,CAACC,cAAc,CAACJ,aAAa,EAAE,CAACG,WAAC,CAACE,aAAa,CAACnB,IAAI,CAAC,CAAC,CAAC;IAE1E,IAAIY,SAAS,CAACQ,sBAAsB,EAAE,EAAE;MACtC,MAAM;QAAEC,QAAQ;QAAEC,IAAI;QAAEC;MAAM,CAAC,GAAGX,SAAS,CAACH,IAAI;MAChD,IAAIY,QAAQ,KAAK,GAAG,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