Skip to content

Commit

Permalink
feat: add es-get-iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
merodiro committed Jul 21, 2024
1 parent 13d65e5 commit 5a6d86e
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
50 changes: 50 additions & 0 deletions codemods/es-get-iterator/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
*/

/**
* @param {CodemodOptions} [options]
* @returns {Codemod}
*/
export default function (options) {
return {
name: 'es-get-iterator',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('es-get-iterator', root, j);

root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
})
.forEach((path) => {
const args = path.value.arguments;
const newExpression = j.optionalCallExpression(
j.memberExpression(
// @ts-expect-error
args[0],
j.memberExpression(
j.identifier('Symbol'),
j.identifier('iterator'),
),
),
[],
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
});

return dirtyFlag ? root.toSource(options) : file.source;
},
};
}
25 changes: 25 additions & 0 deletions test/fixtures/es-get-iterator/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const assert = require('assert');

const iterator1 = 'a 💩'[Symbol.iterator]?.();
assert.deepEqual(
[iterator1.next(), iterator1.next(), iterator1.next(), iterator1.next()],
[{ done: false, value: 'a' }, { done: false, value: ' ' }, { done: false, value: '💩' }, { done: true, value: undefined }]
);

const iterator2 = [1, 2][Symbol.iterator]?.();
assert.deepEqual(
[iterator2.next(), iterator2.next(), iterator2.next()],
[{ done: false, value: 1 }, { done: false, value: 2 }, { done: true, value: undefined }]
);

const iterator3 = new Set([1, 2])[Symbol.iterator]?.();
assert.deepEqual(
[iterator3.next(), iterator3.next(), iterator3.next()],
[{ done: false, value: 1 }, { done: false, value: 2 }, { done: true, value: undefined }]
);

const iterator4 = new Map([[1, 2], [3, 4]])[Symbol.iterator]?.();
assert.deepEqual(
[iterator4.next(), iterator4.next(), iterator4.next()],
[{ done: false, value: [1, 2] }, { done: false, value: [3, 4] }, { done: true, value: undefined }]
);
26 changes: 26 additions & 0 deletions test/fixtures/es-get-iterator/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const getIterator = require('es-get-iterator');
const assert = require('assert');

const iterator1 = getIterator('a 💩');
assert.deepEqual(
[iterator1.next(), iterator1.next(), iterator1.next(), iterator1.next()],
[{ done: false, value: 'a' }, { done: false, value: ' ' }, { done: false, value: '💩' }, { done: true, value: undefined }]
);

const iterator2 = getIterator([1, 2]);
assert.deepEqual(
[iterator2.next(), iterator2.next(), iterator2.next()],
[{ done: false, value: 1 }, { done: false, value: 2 }, { done: true, value: undefined }]
);

const iterator3 = getIterator(new Set([1, 2]));
assert.deepEqual(
[iterator3.next(), iterator3.next(), iterator3.next()],
[{ done: false, value: 1 }, { done: false, value: 2 }, { done: true, value: undefined }]
);

const iterator4 = getIterator(new Map([[1, 2], [3, 4]]));
assert.deepEqual(
[iterator4.next(), iterator4.next(), iterator4.next()],
[{ done: false, value: [1, 2] }, { done: false, value: [3, 4] }, { done: true, value: undefined }]
);
25 changes: 25 additions & 0 deletions test/fixtures/es-get-iterator/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const assert = require('assert');

const iterator1 = 'a 💩'[Symbol.iterator]?.();
assert.deepEqual(
[iterator1.next(), iterator1.next(), iterator1.next(), iterator1.next()],
[{ done: false, value: 'a' }, { done: false, value: ' ' }, { done: false, value: '💩' }, { done: true, value: undefined }]
);

const iterator2 = [1, 2][Symbol.iterator]?.();
assert.deepEqual(
[iterator2.next(), iterator2.next(), iterator2.next()],
[{ done: false, value: 1 }, { done: false, value: 2 }, { done: true, value: undefined }]
);

const iterator3 = new Set([1, 2])[Symbol.iterator]?.();
assert.deepEqual(
[iterator3.next(), iterator3.next(), iterator3.next()],
[{ done: false, value: 1 }, { done: false, value: 2 }, { done: true, value: undefined }]
);

const iterator4 = new Map([[1, 2], [3, 4]])[Symbol.iterator]?.();
assert.deepEqual(
[iterator4.next(), iterator4.next(), iterator4.next()],
[{ done: false, value: [1, 2] }, { done: false, value: [3, 4] }, { done: true, value: undefined }]
);

0 comments on commit 5a6d86e

Please sign in to comment.