index.js.map
8.65 KB
{"version":3,"names":["blockStatement","callExpression","functionExpression","isAssignmentPattern","isFunctionDeclaration","isRestElement","returnStatement","buildAnonymousExpressionWrapper","template","expression","buildNamedExpressionWrapper","buildDeclarationWrapper","statements","classOrObjectMethod","path","callId","node","body","container","async","generator","get","unwrapFunctionEnvironment","plainFunction","noNewArrows","ignoreFunctionLength","functionId","isArrowFunctionExpression","arrowFunctionToExpression","isDeclaration","id","type","built","params","param","push","scope","generateUidIdentifier","wrapperArgs","NAME","REF","name","FUNCTION","PARAMS","replaceWith","insertAfter","returnFn","callee","argument","nameFunction","parent","length","wrapFunction","isMethod"],"sources":["../src/index.ts"],"sourcesContent":["import type { NodePath } from \"@babel/traverse\";\nimport nameFunction from \"@babel/helper-function-name\";\nimport template from \"@babel/template\";\nimport {\n blockStatement,\n callExpression,\n functionExpression,\n isAssignmentPattern,\n isFunctionDeclaration,\n isRestElement,\n returnStatement,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\n\ntype ExpressionWrapperBuilder<ExtraBody extends t.Node[]> = (\n replacements?: Parameters<ReturnType<typeof template.expression>>[0],\n) => t.CallExpression & {\n callee: t.FunctionExpression & {\n body: {\n body: [\n t.VariableDeclaration & {\n declarations: [\n { init: t.FunctionExpression | t.ArrowFunctionExpression },\n ];\n },\n ...ExtraBody,\n ];\n };\n };\n};\n\nconst buildAnonymousExpressionWrapper = template.expression(`\n (function () {\n var REF = FUNCTION;\n return function NAME(PARAMS) {\n return REF.apply(this, arguments);\n };\n })()\n`) as ExpressionWrapperBuilder<\n [t.ReturnStatement & { argument: t.FunctionExpression }]\n>;\n\nconst buildNamedExpressionWrapper = template.expression(`\n (function () {\n var REF = FUNCTION;\n function NAME(PARAMS) {\n return REF.apply(this, arguments);\n }\n return NAME;\n })()\n`) as ExpressionWrapperBuilder<\n [t.FunctionDeclaration, t.ReturnStatement & { argument: t.Identifier }]\n>;\n\nconst buildDeclarationWrapper = template.statements(`\n function NAME(PARAMS) { return REF.apply(this, arguments); }\n function REF() {\n REF = FUNCTION;\n return REF.apply(this, arguments);\n }\n`);\n\nfunction classOrObjectMethod(\n path: NodePath<t.ClassMethod | t.ClassPrivateMethod | t.ObjectMethod>,\n callId: t.Expression,\n) {\n const node = path.node;\n const body = node.body;\n\n const container = functionExpression(\n null,\n [],\n blockStatement(body.body),\n true,\n );\n body.body = [\n returnStatement(callExpression(callExpression(callId, [container]), [])),\n ];\n\n // Regardless of whether or not the wrapped function is a an async method\n // or generator the outer function should not be\n node.async = false;\n node.generator = false;\n\n // Unwrap the wrapper IIFE's environment so super and this and such still work.\n (\n path.get(\"body.body.0.argument.callee.arguments.0\") as NodePath\n ).unwrapFunctionEnvironment();\n}\n\nfunction plainFunction(\n path: NodePath<Exclude<t.Function, t.Method>>,\n callId: t.Expression,\n noNewArrows: boolean,\n ignoreFunctionLength: boolean,\n) {\n let functionId = null;\n let node;\n if (path.isArrowFunctionExpression()) {\n if (process.env.BABEL_8_BREAKING) {\n path = path.arrowFunctionToExpression({ noNewArrows });\n } else {\n // arrowFunctionToExpression returns undefined in @babel/traverse < 7.18.10\n path = path.arrowFunctionToExpression({ noNewArrows }) ?? path;\n }\n node = path.node as t.FunctionDeclaration | t.FunctionExpression;\n } else {\n node = path.node as t.FunctionDeclaration | t.FunctionExpression;\n }\n\n const isDeclaration = isFunctionDeclaration(node);\n\n functionId = node.id;\n node.id = null;\n node.type = \"FunctionExpression\";\n\n const built = callExpression(callId, [\n node as Exclude<typeof node, t.FunctionDeclaration>,\n ]);\n\n const params: t.Identifier[] = [];\n for (const param of node.params) {\n if (isAssignmentPattern(param) || isRestElement(param)) {\n break;\n }\n params.push(path.scope.generateUidIdentifier(\"x\"));\n }\n\n const wrapperArgs = {\n NAME: functionId || null,\n REF: path.scope.generateUidIdentifier(functionId ? functionId.name : \"ref\"),\n FUNCTION: built,\n PARAMS: params,\n };\n\n if (isDeclaration) {\n const container = buildDeclarationWrapper(wrapperArgs);\n path.replaceWith(container[0]);\n path.insertAfter(container[1]);\n } else {\n let container;\n\n if (functionId) {\n container = buildNamedExpressionWrapper(wrapperArgs);\n } else {\n container = buildAnonymousExpressionWrapper(wrapperArgs);\n\n const returnFn = container.callee.body.body[1].argument;\n nameFunction({\n node: returnFn,\n parent: (path as NodePath<t.FunctionExpression>).parent,\n scope: path.scope,\n });\n functionId = returnFn.id;\n }\n\n if (functionId || (!ignoreFunctionLength && params.length)) {\n path.replaceWith(container);\n } else {\n // we can omit this wrapper as the conditions it protects for do not apply\n path.replaceWith(built);\n }\n }\n}\n\nexport default function wrapFunction(\n path: NodePath<t.Function>,\n callId: t.Expression,\n // TODO(Babel 8): Consider defaulting to false for spec compliancy\n noNewArrows: boolean = true,\n ignoreFunctionLength: boolean = false,\n) {\n if (path.isMethod()) {\n classOrObjectMethod(path, callId);\n } else {\n plainFunction(\n path as NodePath<Exclude<t.Function, t.Method>>,\n callId,\n noNewArrows,\n ignoreFunctionLength,\n );\n }\n}\n"],"mappings":";;;;;;;AACA;;AACA;;AACA;;;EACEA,c;EACAC,c;EACAC,kB;EACAC,mB;EACAC,qB;EACAC,a;EACAC;;;AAqBF,MAAMC,+BAA+B,GAAGC,iBAAA,CAASC,UAAT,CAAqB;AAC7D;AACA;AACA;AACA;AACA;AACA;AACA,CAPwC,CAAxC;;AAWA,MAAMC,2BAA2B,GAAGF,iBAAA,CAASC,UAAT,CAAqB;AACzD;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CARoC,CAApC;;AAYA,MAAME,uBAAuB,GAAGH,iBAAA,CAASI,UAAT,CAAqB;AACrD;AACA;AACA;AACA;AACA;AACA,CANgC,CAAhC;;AAQA,SAASC,mBAAT,CACEC,IADF,EAEEC,MAFF,EAGE;EACA,MAAMC,IAAI,GAAGF,IAAI,CAACE,IAAlB;EACA,MAAMC,IAAI,GAAGD,IAAI,CAACC,IAAlB;EAEA,MAAMC,SAAS,GAAGhB,kBAAkB,CAClC,IADkC,EAElC,EAFkC,EAGlCF,cAAc,CAACiB,IAAI,CA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