1 line
13 KiB
Plaintext
1 line
13 KiB
Plaintext
|
{"version":3,"names":["_helperPluginUtils","require","_helperSkipTransparentExpressionWrappers","_core","_default","declare","api","options","_api$assumption","_options$allowArrayLi","assertVersion","iterableIsArray","assumption","loose","arrayLikeIsIterable","allowArrayLike","getSpreadLiteral","spread","scope","t","isIdentifier","argument","name","toArray","hasHole","elements","some","el","hasSpread","nodes","i","length","isSpreadElement","push","_props","arrayExpression","build","props","file","prop","spreadLiteral","isArrayExpression","callExpression","addHelper","visitor","ArrayExpression","path","node","first","replaceWith","shift","memberExpression","identifier","CallExpression","args","arguments","calleePath","skipTransparentExprWrappers","get","isSuper","buildCodeFrameError","contextLiteral","buildUndefinedNode","callee","isMemberExpression","temp","maybeGenerateMemoised","object","assignmentExpression","cloneNode","thisExpression","unshift","NewExpression","hub","exports","default"],"sources":["../src/index.ts"],"sourcesContent":["import { declare } from \"@babel/helper-plugin-utils\";\nimport { skipTransparentExprWrappers } from \"@babel/helper-skip-transparent-expression-wrappers\";\nimport type { File } from \"@babel/core\";\nimport { types as t } from \"@babel/core\";\nimport type { NodePath, Scope } from \"@babel/traverse\";\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(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 return scope.toArray(spread.argument, true, arrayLikeIsIterable);\n }\n }\n\n function hasHole(spread: t.ArrayExpression): boolean {\n return spread.elements.some(el => el === 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
|