|
| 1 | +import jscodeshift from 'jscodeshift'; |
| 2 | +import { removeImport } from '../shared.js'; |
| 3 | + |
| 4 | +/** |
| 5 | + * @typedef {import('../../types.js').Codemod} Codemod |
| 6 | + * @typedef {import('../../types.js').CodemodOptions} CodemodOptions |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @param {CodemodOptions} [options] |
| 11 | + * @returns {Codemod} |
| 12 | + */ |
| 13 | +export default function (options) { |
| 14 | + return { |
| 15 | + name: 'iterate-iterator', |
| 16 | + transform: ({ file }) => { |
| 17 | + const j = jscodeshift; |
| 18 | + const root = j(file.source); |
| 19 | + let isDirty = false; |
| 20 | + |
| 21 | + const { identifier } = removeImport('iterate-iterator', root, j); |
| 22 | + |
| 23 | + if (identifier) { |
| 24 | + const callExpressions = root.find(j.CallExpression, { |
| 25 | + callee: { type: 'Identifier', name: identifier }, |
| 26 | + }); |
| 27 | + |
| 28 | + for (const path of callExpressions.paths()) { |
| 29 | + const args = path.node.arguments; |
| 30 | + if (args.length === 1) { |
| 31 | + // Case: Converting an iterator to an array |
| 32 | + const [iterable] = args; |
| 33 | + const iterableArg = |
| 34 | + iterable.type === 'SpreadElement' ? iterable.argument : iterable; |
| 35 | + |
| 36 | + const wrappedIterable = j.objectExpression([ |
| 37 | + j.property( |
| 38 | + 'init', |
| 39 | + j.memberExpression( |
| 40 | + j.identifier('Symbol'), |
| 41 | + j.identifier('iterator'), |
| 42 | + ), |
| 43 | + j.arrowFunctionExpression([], iterableArg), |
| 44 | + ), |
| 45 | + ]); |
| 46 | + |
| 47 | + if ( |
| 48 | + wrappedIterable.properties[0].type !== 'SpreadProperty' && |
| 49 | + wrappedIterable.properties[0].type !== 'SpreadElement' |
| 50 | + ) { |
| 51 | + wrappedIterable.properties[0].computed = true; |
| 52 | + } |
| 53 | + |
| 54 | + const arrayFromExpression = j.callExpression( |
| 55 | + j.memberExpression(j.identifier('Array'), j.identifier('from')), |
| 56 | + [wrappedIterable], |
| 57 | + ); |
| 58 | + j(path).replaceWith(arrayFromExpression); |
| 59 | + isDirty = true; |
| 60 | + } else if (args.length === 2) { |
| 61 | + // Case: Using a callback function |
| 62 | + const [iterable, callback] = args; |
| 63 | + const iterableArg = |
| 64 | + iterable.type === 'SpreadElement' ? iterable.argument : iterable; |
| 65 | + |
| 66 | + if ( |
| 67 | + callback.type !== 'Identifier' && |
| 68 | + callback.type !== 'FunctionExpression' && |
| 69 | + callback.type !== 'ArrowFunctionExpression' |
| 70 | + ) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + const wrappedIterable = j.objectExpression([ |
| 75 | + j.property( |
| 76 | + 'init', |
| 77 | + j.memberExpression( |
| 78 | + j.identifier('Symbol'), |
| 79 | + j.identifier('iterator'), |
| 80 | + ), |
| 81 | + j.arrowFunctionExpression([], iterableArg), |
| 82 | + ), |
| 83 | + ]); |
| 84 | + |
| 85 | + if ( |
| 86 | + wrappedIterable.properties[0].type !== 'SpreadProperty' && |
| 87 | + wrappedIterable.properties[0].type !== 'SpreadElement' |
| 88 | + ) { |
| 89 | + wrappedIterable.properties[0].computed = true; |
| 90 | + } |
| 91 | + |
| 92 | + const forOfStatement = j.forOfStatement( |
| 93 | + j.variableDeclaration('const', [ |
| 94 | + j.variableDeclarator(j.identifier('i')), |
| 95 | + ]), |
| 96 | + wrappedIterable, |
| 97 | + j.blockStatement([ |
| 98 | + j.expressionStatement( |
| 99 | + j.callExpression( |
| 100 | + callback.type === 'Identifier' |
| 101 | + ? callback |
| 102 | + : j.parenthesizedExpression(callback), |
| 103 | + [j.identifier('i')], |
| 104 | + ), |
| 105 | + ), |
| 106 | + ]), |
| 107 | + ); |
| 108 | + j(path).replaceWith(forOfStatement); |
| 109 | + isDirty = true; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return isDirty ? root.toSource(options) : file.source; |
| 115 | + }, |
| 116 | + }; |
| 117 | +} |
0 commit comments