Skip to content

Commit

Permalink
refactor: abstract array methods
Browse files Browse the repository at this point in the history
  • Loading branch information
thepassle committed Jul 19, 2024
1 parent 385aa6f commit b10e7de
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 284 deletions.
51 changes: 16 additions & 35 deletions codemods/array.prototype.at/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jscodeshift from "jscodeshift";
import { removeImport } from "../shared.js";
import jscodeshift from 'jscodeshift';
import { transformArrayMethod } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -18,39 +18,20 @@ import { removeImport } from "../shared.js";
* @returns {Codemod}
*/
export default function (options) {
return {
name: "array.prototype.at",
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;
return {
name: 'array.prototype.at',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

const { identifier } = removeImport("array.prototype.at", root, j);
const dirty = transformArrayMethod(
'array.prototype.at',
'at',
root,
j,
);

root
.find(j.CallExpression, {
callee: {
type: "Identifier",
name: identifier,
},
})
.forEach((path) => {
const [arrayArg, indexArg] = path.node.arguments;
if (
j.Identifier.check(arrayArg) ||
j.ArrayExpression.check(arrayArg)
) {
path.replace(
j.callExpression(
j.memberExpression(arrayArg, j.identifier("at")),
[indexArg]
)
);
dirtyFlag = true;
}
});

return dirtyFlag ? root.toSource(options) : file.source;
},
};
return dirty ? root.toSource(options) : file.source;
},
};
}
34 changes: 8 additions & 26 deletions codemods/array.prototype.concat/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';
import { transformArrayMethod } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -16,33 +16,15 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('array.prototype.concat', root, j);
const dirty = transformArrayMethod(
'array.prototype.concat',
'concat',
root,
j,
);

root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
})
.forEach((path) => {
const args = path.value.arguments;
if (args.length === 2) {
const [array, callback] = args;

const newExpression = j.callExpression(
//@ts-ignore
j.memberExpression(array, j.identifier('concat')),
[callback],
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
}
});

return dirtyFlag ? root.toSource(options) : file.source;
return dirty ? root.toSource(options) : file.source;
},
};
}
34 changes: 8 additions & 26 deletions codemods/array.prototype.every/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';
import { transformArrayMethod } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -16,33 +16,15 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('array.prototype.every', root, j);
const dirty = transformArrayMethod(
'array.prototype.every',
'every',
root,
j,
);

root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
})
.forEach((path) => {
const args = path.value.arguments;
if (args.length === 2) {
const [array, callback] = args;

const newExpression = j.callExpression(
//@ts-ignore
j.memberExpression(array, j.identifier('every')),
[callback],
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
}
});

return dirtyFlag ? root.toSource(options) : file.source;
return dirty ? root.toSource(options) : file.source;
},
};
}
34 changes: 8 additions & 26 deletions codemods/array.prototype.filter/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';
import { transformArrayMethod } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -16,33 +16,15 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('array.prototype.filter', root, j);
const dirty = transformArrayMethod(
'array.prototype.filter',
'filter',
root,
j,
);

root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
})
.forEach((path) => {
const args = path.value.arguments;
if (args.length === 2) {
const [array, callback] = args;

const newExpression = j.callExpression(
//@ts-ignore
j.memberExpression(array, j.identifier('filter')),
[callback],
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
}
});

return dirtyFlag ? root.toSource(options) : file.source;
return dirty ? root.toSource(options) : file.source;
},
};
}
34 changes: 8 additions & 26 deletions codemods/array.prototype.find/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';
import { transformArrayMethod } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -16,33 +16,15 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('array.prototype.find', root, j);
const dirty = transformArrayMethod(
'array.prototype.find',
'find',
root,
j,
);

root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
})
.forEach((path) => {
const args = path.value.arguments;
if (args.length === 2) {
const [array, callback] = args;

const newExpression = j.callExpression(
//@ts-ignore
j.memberExpression(array, j.identifier('find')),
[callback],
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
}
});

return dirtyFlag ? root.toSource(options) : file.source;
return dirty ? root.toSource(options) : file.source;
},
};
}
34 changes: 8 additions & 26 deletions codemods/array.prototype.findIndex/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';
import { transformArrayMethod } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -16,33 +16,15 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('array.prototype.findIndex', root, j);
const dirty = transformArrayMethod(
'array.prototype.findIndex',
'findIndex',
root,
j,
);

root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
})
.forEach((path) => {
const args = path.value.arguments;
if (args.length === 2) {
const [array, callback] = args;

const newExpression = j.callExpression(
//@ts-ignore
j.memberExpression(array, j.identifier('findIndex')),
[callback],
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
}
});

return dirtyFlag ? root.toSource(options) : file.source;
return dirty ? root.toSource(options) : file.source;
},
};
}
34 changes: 8 additions & 26 deletions codemods/array.prototype.flat/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';
import { transformArrayMethod } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -16,33 +16,15 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('array.prototype.flat', root, j);
const dirty = transformArrayMethod(
'array.prototype.flat',
'flat',
root,
j,
);

root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
})
.forEach((path) => {
const args = path.value.arguments;
if (args.length === 2) {
const [array, callback] = args;

const newExpression = j.callExpression(
//@ts-ignore
j.memberExpression(array, j.identifier('flat')),
[callback],
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
}
});

return dirtyFlag ? root.toSource(options) : file.source;
return dirty ? root.toSource(options) : file.source;
},
};
}
34 changes: 8 additions & 26 deletions codemods/array.prototype.flatMap/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';
import { transformArrayMethod } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -16,33 +16,15 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('array.prototype.flatMap', root, j);
const dirty = transformArrayMethod(
'array.prototype.flatMap',
'flatMap',
root,
j,
);

root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
})
.forEach((path) => {
const args = path.value.arguments;
if (args.length === 2) {
const [array, callback] = args;

const newExpression = j.callExpression(
//@ts-ignore
j.memberExpression(array, j.identifier('flatMap')),
[callback],
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
}
});

return dirtyFlag ? root.toSource(options) : file.source;
return dirty ? root.toSource(options) : file.source;
},
};
}
Loading

0 comments on commit b10e7de

Please sign in to comment.