1 line
12 KiB
Text
1 line
12 KiB
Text
|
{"version":3,"names":["_core","require","_shadowUtils","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.ts\";\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 transform-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
|