({ template, types: t })
| 12 | * @returns {babel.PluginObj} |
| 13 | */ |
| 14 | export default function fastRestTransform({ template, types: t }) { |
| 15 | const slice = template`var IDENT = Array.prototype.slice;`; |
| 16 | |
| 17 | const VISITOR = { |
| 18 | RestElement(path, state) { |
| 19 | if (path.parentKey !== 'params') return; |
| 20 | |
| 21 | // Create a global _slice alias |
| 22 | let slice = state.get('slice'); |
| 23 | if (!slice) { |
| 24 | slice = path.scope.generateUidIdentifier('slice'); |
| 25 | state.set('slice', slice); |
| 26 | } |
| 27 | |
| 28 | // _slice.call(arguments) or _slice.call(arguments, 1) |
| 29 | const args = [t.identifier('arguments')]; |
| 30 | if (path.key) args.push(t.numericLiteral(path.key)); |
| 31 | const sliced = t.callExpression( |
| 32 | t.memberExpression(t.clone(slice), t.identifier('call')), |
| 33 | args, |
| 34 | ); |
| 35 | |
| 36 | const ident = path.node.argument; |
| 37 | const binding = path.scope.getBinding(ident.name); |
| 38 | |
| 39 | if (binding.referencePaths.length !== 0) { |
| 40 | // arguments access requires a non-Arrow function: |
| 41 | const func = path.parentPath; |
| 42 | if (t.isArrowFunctionExpression(func)) { |
| 43 | func.arrowFunctionToExpression(); |
| 44 | } |
| 45 | |
| 46 | if ( |
| 47 | binding.constant && |
| 48 | binding.referencePaths.length === 1 && |
| 49 | sameArgumentsObject(binding.referencePaths[0], func, t) |
| 50 | ) { |
| 51 | // one usage, never assigned - replace usage inline |
| 52 | binding.referencePaths[0].replaceWith(sliced); |
| 53 | } else { |
| 54 | // unknown usage, create a binding |
| 55 | const decl = t.variableDeclaration('var', [ |
| 56 | t.variableDeclarator(t.clone(ident), sliced), |
| 57 | ]); |
| 58 | func.get('body').unshiftContainer('body', decl); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | path.remove(); |
| 63 | }, |
| 64 | }; |
| 65 | |
| 66 | return { |
| 67 | name: 'transform-fast-rest', |
| 68 | visitor: { |
| 69 | Program(path, state) { |
| 70 | const childState = new Map(); |
| 71 | const useHelper = state.opts.helper === true; // defaults to false |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…