You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
15 KiB
1 line
15 KiB
{"version":3,"names":["_helperPluginUtils","require","_helperSkipTransparentExpressionWrappers","_core","_default","exports","default","declare","api","options","_api$assumption","_options$allowArrayLi","assertVersion","iterableIsArray","assumption","loose","arrayLikeIsIterable","allowArrayLike","getSpreadLiteral","spread","scope","t","isIdentifier","argument","name","node","binding","getBinding","constant","path","isGenericType","isArrayExpression","template","expression","ast","args","helperName","unshift","hub","addHelper","callExpression","hasHole","elements","includes","hasSpread","nodes","i","length","isSpreadElement","push","_props","arrayExpression","build","props","file","prop","spreadLiteral","visitor","ArrayExpression","first","replaceWith","shift","memberExpression","identifier","CallExpression","arguments","calleePath","skipTransparentExprWrappers","get","isSuper","buildCodeFrameError","contextLiteral","buildUndefinedNode","callee","isMemberExpression","temp","maybeGenerateMemoised","object","assignmentExpression","cloneNode","thisExpression","NewExpression"],"sources":["../src/index.ts"],"sourcesContent":["import { declare } from \"@babel/helper-plugin-utils\";\nimport { skipTransparentExprWrappers } from \"@babel/helper-skip-transparent-expression-wrappers\";\nimport { types as t, template } from \"@babel/core\";\nimport type { File, NodePath, Scope } from \"@babel/core\";\n\ntype ListElement = t.SpreadElement | t.Expression;\n\nexport interface Options {\n allowArrayLike?: boolean;\n loose?: boolean;\n}\n\nexport default declare((api, options: Options) => {\n api.assertVersion(REQUIRED_VERSION(7));\n\n const iterableIsArray = api.assumption(\"iterableIsArray\") ?? options.loose;\n const arrayLikeIsIterable =\n options.allowArrayLike ?? api.assumption(\"arrayLikeIsIterable\");\n\n function getSpreadLiteral(\n spread: t.SpreadElement,\n scope: Scope,\n ): t.Expression {\n if (\n iterableIsArray &&\n !t.isIdentifier(spread.argument, { name: \"arguments\" })\n ) {\n return spread.argument;\n } else {\n const node = spread.argument;\n\n if (t.isIdentifier(node)) {\n const binding = scope.getBinding(node.name);\n if (binding?.constant && binding.path.isGenericType(\"Array\")) {\n return node;\n }\n }\n\n if (t.isArrayExpression(node)) {\n return node;\n }\n\n if (t.isIdentifier(node, { name: \"arguments\" })) {\n return template.expression.ast`\n Array.prototype.slice.call(${node})\n `;\n }\n\n const args = [node];\n let helperName = \"toConsumableArray\";\n\n if (arrayLikeIsIterable) {\n args.unshift(scope.path.hub.addHelper(helperName));\n helperName = \"maybeArrayLike\";\n }\n\n return t.callExpression(scope.path.hub.addHelper(helperName), args);\n }\n }\n\n function hasHole(spread: t.ArrayExpression): boolean {\n return spread.elements.includes(null);\n }\n\n function hasSpread(nodes: Array<t.Node>): boolean {\n for (let i = 0; i < nodes.length; i++) {\n if (t.isSpreadElement(nodes[i])) {\n return true;\n }\n }\n return false;\n }\n\n function push(_props: Array<ListElement>, nodes: Array<t.Expression>) {\n if (!_props.length) return _props;\n nodes.push(t.arrayExpression(_props));\n return [];\n }\n\n function build(\n props: Array<ListElement>,\n scope: Scope,\n file: File,\n ): t.Expression[] {\n const nodes: Array<t.Expression> = [];\n let _props: Array<ListElement> = [];\n\n for (const prop of props) {\n if (t.isSpreadElement(prop)) {\n _props = push(_props, nodes);\n let spreadLiteral = getSpreadLiteral(prop, scope);\n\n if (t.isArrayExpression(spreadLiteral) && hasHole(spreadLiteral)) {\n spreadLiteral = t.callExpression(\n file.addHelper(\n process.env.BABEL_8_BREAKING\n ? \"arrayLikeToArray\"\n : \"arrayWithoutHoles\",\n ),\n [spreadLiteral],\n );\n }\n\n nodes.push(spreadLiteral);\n } else {\n _props.push(prop);\n }\n }\n\n push(_props, nodes);\n\n return nodes;\n }\n\n return {\n name: \"transform-spread\",\n\n visitor: {\n ArrayExpression(path): void {\n const { node, scope } = path;\n const elements = node.elements;\n if (!hasSpread(elements)) return;\n\n const nodes = build(elements, scope, this.file);\n let first = nodes[0];\n\n // If there is only one element in the ArrayExpression and\n // the element was transformed (Array.prototype.slice.call or toConsumableArray)\n // we know that the transformed code already takes care of cloning the array.\n // So we can simply return that element.\n if (\n nodes.length === 1 &&\n first !== (elements[0] as t.SpreadElement).argument\n ) {\n path.replaceWith(first);\n return;\n }\n\n // If the first element is a ArrayExpression we can directly call\n // concat on it.\n // `[..].concat(..)`\n // If not then we have to use `[].concat(arr)` and not `arr.concat`\n // because `arr` could be extended/modified (e.g. Immutable) and we do not know exactly\n // what concat would produce.\n if (!t.isArrayExpression(first)) {\n first = t.arrayExpression([]);\n } else {\n nodes.shift();\n }\n\n path.replaceWith(\n t.callExpression(\n t.memberExpression(first, t.identifier(\"concat\")),\n nodes,\n ),\n );\n },\n CallExpression(path): void {\n const { node, scope } = path;\n\n const args = node.arguments as Array<ListElement>;\n if (!hasSpread(args)) return;\n const calleePath = skipTransparentExprWrappers(\n path.get(\"callee\") as NodePath<t.Expression>,\n );\n if (calleePath.isSuper()) {\n // NOTE: spread and classes have almost the same compat data, so this is very unlikely to happen in practice.\n throw path.buildCodeFrameError(\n \"It's not possible to compile spread arguments in `super()` without compiling classes.\\n\" +\n \"Please add '@babel/plugin-transform-classes' to your Babel configuration.\",\n );\n }\n let contextLiteral: t.Expression | t.Super = scope.buildUndefinedNode();\n node.arguments = [];\n\n let nodes: t.Expression[];\n if (\n args.length === 1 &&\n t.isIdentifier((args[0] as t.SpreadElement).argument, {\n name: \"arguments\",\n })\n ) {\n nodes = [(args[0] as t.SpreadElement).argument];\n } else {\n nodes = build(args, scope, this.file);\n }\n\n const first = nodes.shift();\n if (nodes.length) {\n node.arguments.push(\n t.callExpression(\n t.memberExpression(first, t.identifier(\"concat\")),\n nodes,\n ),\n );\n } else {\n node.arguments.push(first);\n }\n\n const callee = calleePath.node as t.MemberExpression;\n\n if (t.isMemberExpression(callee)) {\n const temp = scope.maybeGenerateMemoised(callee.object);\n if (temp) {\n callee.object = t.assignmentExpression(\n \"=\",\n temp,\n // object must not be Super when `temp` is an identifier\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n callee.object as t.Expression,\n );\n contextLiteral = temp;\n } else {\n contextLiteral = t.cloneNode(callee.object);\n }\n }\n\n // We use the original callee here, to preserve any types/parentheses\n node.callee = t.memberExpression(\n node.callee as t.Expression,\n t.identifier(\"apply\"),\n );\n if (t.isSuper(contextLiteral)) {\n contextLiteral = t.thisExpression();\n }\n\n node.arguments.unshift(t.cloneNode(contextLiteral));\n },\n\n NewExpression(path): void {\n const { node, scope } = path;\n if (!hasSpread(node.arguments)) return;\n\n const nodes = build(\n node.arguments as Array<ListElement>,\n scope,\n this.file,\n );\n\n const first = nodes.shift();\n\n let args: t.Expression;\n if (nodes.length) {\n args = t.callExpression(\n t.memberExpression(first, t.identifier(\"concat\")),\n nodes,\n );\n } else {\n args = first;\n }\n\n path.replaceWith(\n t.callExpression(path.hub.addHelper(\"construct\"), [\n node.callee as t.Expression,\n args,\n ]),\n );\n },\n },\n };\n});\n"],"mappings":";;;;;;AAAA,IAAAA,kBAAA,GAAAC,OAAA;AACA,IAAAC,wCAAA,GAAAD,OAAA;AACA,IAAAE,KAAA,GAAAF,OAAA;AAAmD,IAAAG,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAUpC,IAAAC,0BAAO,EAAC,CAACC,GAAG,EAAEC,OAAgB,KAAK;EAAA,IAAAC,eAAA,EAAAC,qBAAA;EAChDH,GAAG,CAACI,aAAa,CAAkB,CAAE,CAAC;EAEtC,MAAMC,eAAe,IAAAH,eAAA,GAAGF,GAAG,CAACM,UAAU,CAAC,iBAAiB,CAAC,YAAAJ,eAAA,GAAID,OAAO,CAACM,KAAK;EAC1E,MAAMC,mBAAmB,IAAAL,qBAAA,GACvBF,OAAO,CAACQ,cAAc,YAAAN,qBAAA,GAAIH,GAAG,CAACM,UAAU,CAAC,qBAAqB,CAAC;EAEjE,SAASI,gBAAgBA,CACvBC,MAAuB,EACvBC,KAAY,EACE;IACd,IACEP,eAAe,IACf,CAACQ,WAAC,CAACC,YAAY,CAACH,MAAM,CAACI,QAAQ,EAAE;MAAEC,IAAI,EAAE;IAAY,CAAC,CAAC,EACvD;MACA,OAAOL,MAAM,CAACI,QAAQ;IACxB,CAAC,MA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