Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
at main 1.3 kB view raw
1const pipeExpression = (t, pipeline) => { 2 let x = pipeline[0]; 3 for (let i = 1; i < pipeline.length; i++) 4 x = t.callExpression(pipeline[i], [x]); 5 return x; 6}; 7 8const pipePlugin = ({ types: t }) => ({ 9 visitor: { 10 ImportDeclaration(path, state) { 11 if (path.node.source.value === 'wonka') { 12 const { specifiers } = path.node; 13 const pipeSpecifierIndex = specifiers.findIndex(spec => { 14 return spec.imported.name === 'pipe'; 15 }); 16 17 if (pipeSpecifierIndex > -1) { 18 const pipeSpecifier = specifiers[pipeSpecifierIndex]; 19 state.pipeName = pipeSpecifier.local.name; 20 if (specifiers.length > 1) { 21 path.node.specifiers.splice(pipeSpecifierIndex, 1); 22 } else { 23 path.remove(); 24 } 25 } 26 } 27 }, 28 CallExpression(path, state) { 29 if (state.pipeName) { 30 const callee = path.node.callee; 31 const args = path.node.arguments; 32 if (callee.name !== state.pipeName) { 33 return; 34 } else if (args.length === 0) { 35 path.replaceWith(t.identifier('undefined')); 36 } else { 37 path.replaceWith(pipeExpression(t, args)); 38 } 39 } 40 }, 41 }, 42}); 43 44export default pipePlugin;