Skip to content

Commit

Permalink
feat(codemod): Add buffer-equal codemod
Browse files Browse the repository at this point in the history
  • Loading branch information
stramel committed Oct 15, 2024
1 parent e6f23a3 commit ae49777
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 0 deletions.
58 changes: 58 additions & 0 deletions codemods/buffer-equal/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import jscodeshift from 'jscodeshift';
import { DEFAULT_IMPORT, getImportIdentifierMap, 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: 'buffer-equal',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let transformCount = 0;
let dirtyFlag = false;

const map = getImportIdentifierMap('buffer-equal', root, j);

const identifier = map[DEFAULT_IMPORT];

const callExpressions = root.find(j.CallExpression, {
callee: {
name: identifier,
},
});

if (!callExpressions.length) {
removeImport('buffer-equal', root, j);
return root.toSource(options);
}

callExpressions
.forEach((p) => {
const args = p.node.arguments;
if (args.length === 2 && args[0].type !== 'SpreadElement') {
const[firstArg, secondArg] = args;
j(p).replaceWith(j.callExpression(
j.memberExpression(firstArg, j.identifier('equals')),
[secondArg],
));
dirtyFlag = true;
transformCount++;
}
});

if (transformCount === callExpressions.length) {
removeImport('buffer-equal', root, j);
}

return dirtyFlag ? root.toSource(options) : file.source;
},
};
}
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import arrayPrototypeUnshift from './codemods/array.prototype.unshift/index.js';
import arrayPrototypeValues from './codemods/array.prototype.values/index.js';
import arrayPrototypeWith from './codemods/array.prototype.with/index.js';
import arraybufferPrototypeSlice from './codemods/arraybuffer.prototype.slice/index.js';
import bufferEqual from './codemods/buffer-equal/index.js';
import chalk from './codemods/chalk/index.js';
import cloneRegexp from './codemods/clone-regexp/index.js';
import concatMap from './codemods/concat-map/index.js';
Expand Down Expand Up @@ -203,6 +204,7 @@ export const codemods = {
"array.prototype.values": arrayPrototypeValues,
"array.prototype.with": arrayPrototypeWith,
"arraybuffer.prototype.slice": arraybufferPrototypeSlice,
"buffer-equal": bufferEqual,
"chalk": chalk,
"clone-regexp": cloneRegexp,
"concat-map": concatMap,
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/buffer-equal/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { Buffer } = require('node:buffer');

const buf1 = Buffer.from('abc');

const arr = Buffer.from([253, 254, 255]).equals(Buffer.from([253, 254, 255]));
const str = buf1.equals(Buffer.from('def'));
7 changes: 7 additions & 0 deletions test/fixtures/buffer-equal/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { Buffer } = require('node:buffer');
const bufferEqual = require('buffer-equal');

const buf1 = Buffer.from('abc');

const arr = bufferEqual(Buffer.from([253, 254, 255]), Buffer.from([253, 254, 255]));
const str = bufferEqual(buf1, Buffer.from('def'));
6 changes: 6 additions & 0 deletions test/fixtures/buffer-equal/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { Buffer } = require('node:buffer');

const buf1 = Buffer.from('abc');

const arr = Buffer.from([253, 254, 255]).equals(Buffer.from([253, 254, 255]));
const str = buf1.equals(Buffer.from('def'));
8 changes: 8 additions & 0 deletions test/fixtures/buffer-equal/case-2/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { Buffer } = require('node:buffer');
const bufferEqual = require('buffer-equal');

const buf1 = Buffer.from('abc');
const buffs = [Buffer.from([253, 254, 255]), Buffer.from([253, 254, 255])]

const arr = bufferEqual(...buffs);
const str = buf1.equals(Buffer.from('def'));
8 changes: 8 additions & 0 deletions test/fixtures/buffer-equal/case-2/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { Buffer } = require('node:buffer');
const bufferEqual = require('buffer-equal');

const buf1 = Buffer.from('abc');
const buffs = [Buffer.from([253, 254, 255]), Buffer.from([253, 254, 255])]

const arr = bufferEqual(...buffs);
const str = bufferEqual(buf1, Buffer.from('def'));
8 changes: 8 additions & 0 deletions test/fixtures/buffer-equal/case-2/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { Buffer } = require('node:buffer');
const bufferEqual = require('buffer-equal');

const buf1 = Buffer.from('abc');
const buffs = [Buffer.from([253, 254, 255]), Buffer.from([253, 254, 255])]

const arr = bufferEqual(...buffs);
const str = buf1.equals(Buffer.from('def'));
5 changes: 5 additions & 0 deletions test/fixtures/buffer-equal/case-3/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { Buffer } = require('node:buffer');

const buf1 = Buffer.from('abc');

const str = buf1.equals(Buffer.from('def'));
6 changes: 6 additions & 0 deletions test/fixtures/buffer-equal/case-3/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { Buffer } = require('node:buffer');
const bufferEqual = require('buffer-equal');

const buf1 = Buffer.from('abc');

const str = buf1.equals(Buffer.from('def'));
5 changes: 5 additions & 0 deletions test/fixtures/buffer-equal/case-3/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { Buffer } = require('node:buffer');

const buf1 = Buffer.from('abc');

const str = buf1.equals(Buffer.from('def'));

0 comments on commit ae49777

Please sign in to comment.