params.js.map
11.6 KB
{"version":3,"names":["buildDefaultParam","template","statement","buildLooseDefaultParam","buildLooseDestructuredDefaultParam","buildSafeArgumentsAccess","convertFunctionParams","path","ignoreFunctionLength","shouldTransformParam","replaceRestElement","params","get","isSimpleParameterList","every","param","isIdentifier","node","scope","body","shadowedParams","Set","collectShadowedParamsNames","state","needsOuterBinding","size","traverse","iifeVisitor","firstOptionalIndex","i","length","transformedRestNodes","paramIsAssignmentPattern","isAssignmentPattern","t","isMethod","kind","left","right","undefinedNode","buildUndefinedNode","push","ASSIGNMENT_IDENTIFIER","cloneNode","DEFAULT_VALUE","UNDEFINED","replaceWith","isObjectPattern","isArrayPattern","paramName","generateUidIdentifier","PARAMETER_NAME","defNode","VARIABLE_NAME","ARGUMENT_KEY","numericLiteral","uid","typeAnnotation","variableDeclaration","variableDeclarator","transformedNode","slice","ensureBlock","async","generator","buildScopeIIFE","set","blockStatement","bodyPath","arrowPath","arrowFunctionToExpression","ast","unshiftContainer"],"sources":["../src/params.ts"],"sourcesContent":["import { template, types as t } from \"@babel/core\";\nimport type { NodePath } from \"@babel/traverse\";\n\nimport {\n iifeVisitor,\n collectShadowedParamsNames,\n buildScopeIIFE,\n} from \"./shadow-utils\";\n\nconst buildDefaultParam = template.statement(`\n let VARIABLE_NAME =\n arguments.length > ARGUMENT_KEY && arguments[ARGUMENT_KEY] !== undefined ?\n arguments[ARGUMENT_KEY]\n :\n DEFAULT_VALUE;\n`);\n\nconst buildLooseDefaultParam = template.statement(`\n if (ASSIGNMENT_IDENTIFIER === UNDEFINED) {\n ASSIGNMENT_IDENTIFIER = DEFAULT_VALUE;\n }\n`);\n\nconst buildLooseDestructuredDefaultParam = template.statement(`\n let ASSIGNMENT_IDENTIFIER = PARAMETER_NAME === UNDEFINED ? DEFAULT_VALUE : PARAMETER_NAME ;\n`);\n\nconst buildSafeArgumentsAccess = template.statement(`\n let $0 = arguments.length > $1 ? arguments[$1] : undefined;\n`);\n\n// last 2 parameters are optional -- they are used by proposal-object-rest-spread/src/index.js\nexport default function convertFunctionParams(\n path: NodePath<t.Function>,\n ignoreFunctionLength: boolean | void,\n shouldTransformParam?: (index: number) => boolean,\n replaceRestElement?: (\n path: NodePath<t.Function>,\n paramPath: NodePath<t.Function[\"params\"][number]>,\n transformedRestNodes: t.Statement[],\n ) => void,\n) {\n const params = path.get(\"params\");\n\n const isSimpleParameterList = params.every(param => param.isIdentifier());\n if (isSimpleParameterList) return false;\n\n const { node, scope } = path;\n\n const body = [];\n const shadowedParams = new Set<string>();\n\n for (const param of params) {\n collectShadowedParamsNames(param, scope, shadowedParams);\n }\n\n const state = {\n needsOuterBinding: false,\n scope,\n };\n if (shadowedParams.size === 0) {\n for (const param of params) {\n if (!param.isIdentifier()) param.traverse(iifeVisitor, state);\n if (state.needsOuterBinding) break;\n }\n }\n\n let firstOptionalIndex = null;\n\n for (let i = 0; i < params.length; i++) {\n const param = params[i];\n\n if (shouldTransformParam && !shouldTransformParam(i)) {\n continue;\n }\n const transformedRestNodes: t.Statement[] = [];\n if (replaceRestElement) {\n replaceRestElement(path, param, transformedRestNodes);\n }\n\n const paramIsAssignmentPattern = param.isAssignmentPattern();\n if (\n paramIsAssignmentPattern &&\n (ignoreFunctionLength || t.isMethod(node, { kind: \"set\" }))\n ) {\n const left = param.get(\"left\");\n const right = param.get(\"right\");\n\n const undefinedNode = scope.buildUndefinedNode();\n\n if (left.isIdentifier()) {\n body.push(\n buildLooseDefaultParam({\n ASSIGNMENT_IDENTIFIER: t.cloneNode(left.node),\n DEFAULT_VALUE: right.node,\n UNDEFINED: undefinedNode,\n }),\n );\n param.replaceWith(left.node);\n } else if (left.isObjectPattern() || left.isArrayPattern()) {\n const paramName = scope.generateUidIdentifier();\n body.push(\n buildLooseDestructuredDefaultParam({\n ASSIGNMENT_IDENTIFIER: left.node,\n DEFAULT_VALUE: right.node,\n PARAMETER_NAME: t.cloneNode(paramName),\n UNDEFINED: undefinedNode,\n }),\n );\n param.replaceWith(paramName);\n }\n } else if (paramIsAssignmentPattern) {\n if (firstOptionalIndex === null) firstOptionalIndex = i;\n\n const left = param.get(\"left\");\n const right = param.get(\"right\");\n\n const defNode = buildDefaultParam({\n VARIABLE_NAME: left.node,\n DEFAULT_VALUE: right.node,\n ARGUMENT_KEY: t.numericLiteral(i),\n });\n body.push(defNode);\n } else if (firstOptionalIndex !== null) {\n const defNode = buildSafeArgumentsAccess([\n param.node,\n t.numericLiteral(i),\n ]);\n body.push(defNode);\n } else if (param.isObjectPattern() || param.isArrayPattern()) {\n const uid = path.scope.generateUidIdentifier(\"ref\");\n uid.typeAnnotation = param.node.typeAnnotation;\n\n const defNode = t.variableDeclaration(\"let\", [\n t.variableDeclarator(param.node, uid),\n ]);\n body.push(defNode);\n\n param.replaceWith(t.cloneNode(uid));\n }\n\n if (transformedRestNodes) {\n for (const transformedNode of transformedRestNodes) {\n body.push(transformedNode);\n }\n }\n }\n\n // we need to cut off all trailing parameters\n if (firstOptionalIndex !== null) {\n node.params = node.params.slice(0, firstOptionalIndex);\n }\n\n // ensure it's a block, useful for arrow functions\n path.ensureBlock();\n\n const { async, generator } = node;\n if (generator || state.needsOuterBinding || shadowedParams.size > 0) {\n body.push(buildScopeIIFE(shadowedParams, path.node.body));\n\n path.set(\"body\", t.blockStatement(body as t.Statement[]));\n\n // We inject an arrow and then transform it to a normal function, to be\n // sure that we correctly handle this and arguments.\n const bodyPath = path.get(\"body.body\") as NodePath<t.Statement>[];\n const arrowPath = bodyPath[bodyPath.length - 1].get(\n \"argument.callee\",\n ) as NodePath<t.ArrowFunctionExpression>;\n\n // This is an IIFE, so we don't need to worry about the noNewArrows assumption\n arrowPath.arrowFunctionToExpression();\n\n arrowPath.node.generator = generator;\n arrowPath.node.async = async;\n\n node.generator = false;\n node.async = false;\n if (async) {\n // If the default value of a parameter throws, it must reject asynchronously.\n path.node.body = template.statement.ast`{\n try {\n ${path.node.body.body}\n } catch (e) {\n return Promise.reject(e);\n }\n }` as t.BlockStatement;\n }\n } else {\n path.get(\"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