1 line
No EOL
9.4 KiB
Text
Executable file
1 line
No EOL
9.4 KiB
Text
Executable file
{"version":3,"names":["_helperFunctionName","require","_template","_t","blockStatement","callExpression","functionExpression","isAssignmentPattern","isFunctionDeclaration","isRestElement","returnStatement","isCallExpression","buildAnonymousExpressionWrapper","template","expression","buildNamedExpressionWrapper","buildDeclarationWrapper","statements","classOrObjectMethod","path","callId","node","body","container","async","generator","get","unwrapFunctionEnvironment","plainFunction","inPath","noNewArrows","ignoreFunctionLength","functionId","nodeParams","params","isArrowFunctionExpression","_path$arrowFunctionTo","arrowFunctionToExpression","isDeclaration","built","id","type","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 isCallExpression,\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 inPath: NodePath<Exclude<t.Function, t.Method>>,\n callId: t.Expression,\n noNewArrows: boolean,\n ignoreFunctionLength: boolean,\n) {\n let path: NodePath<\n | t.FunctionDeclaration\n | t.FunctionExpression\n | t.CallExpression\n | t.ArrowFunctionExpression\n > = inPath;\n let node;\n let functionId = null;\n const nodeParams = inPath.node.params;\n\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\n | t.FunctionDeclaration\n | t.FunctionExpression\n | t.CallExpression;\n } else {\n node = path.node as t.FunctionDeclaration | t.FunctionExpression;\n }\n\n const isDeclaration = isFunctionDeclaration(node);\n\n let built = node;\n if (!isCallExpression(node)) {\n functionId = node.id;\n node.id = null;\n node.type = \"FunctionExpression\";\n built = callExpression(callId, [\n node as Exclude<typeof node, t.FunctionDeclaration>,\n ]);\n }\n\n const params: t.Identifier[] = [];\n for (const param of nodeParams) {\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 compliance\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,IAAAA,mBAAA,GAAAC,OAAA;AACA,IAAAC,SAAA,GAAAD,OAAA;AACA,IAAAE,EAAA,GAAAF,OAAA;AASsB;EARpBG,cAAc;EACdC,cAAc;EACdC,kBAAkB;EAClBC,mBAAmB;EACnBC,qBAAqB;EACrBC,aAAa;EACbC,eAAe;EACfC;AAAgB,IAAAR,EAAA;AAqBlB,MAAMS,+BAA+B,GAAGC,iBAAQ,CAACC,UAAU,CAAE;AAC7D;AACA;AACA;AACA;AACA;AACA;AACA,CAAC,CAEA;AAED,MAAMC,2BAA2B,GAAGF,iB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