Skip to content

Commit

Permalink
Replace md5 package
Browse files Browse the repository at this point in the history
  • Loading branch information
filipre committed Jul 20, 2024
1 parent d2a6c2f commit e89f0c0
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 57 deletions.
117 changes: 60 additions & 57 deletions codemods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,62 +112,65 @@ import arrayPrototypeFindlast from './array.prototype.findlast/index.js';

import arrayPrototypeTosorted from './array.prototype.tosorted/index.js';

import md5 from './md5/index.js';

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
'is-boolean-object': isBooleanObject,
'is-date-object': isDateObject,
'is-number-object': isNumberObject,
'is-odd': isOdd,
'is-string': isString,
'is-regexp': isRegexp,
'array.prototype.map': arrayPrototypeMap,
'array.prototype.filter': arrayPrototypeFilter,
'array-includes': arrayIncludes,
'object-keys': objectKeys,
'array.prototype.unshift': arrayPrototypeUnshift,
'array.prototype.splice': arrayPrototypeSplice,
'array.prototype.some': arrayPrototypeSome,
'is-negative-zero': isNegativeZero,
'array.prototype.slice': arrayPrototypeSlice,
'array.prototype.reduceright': arrayPrototypeReduceright,
'array.prototype.every': arrayPrototypeEvery,
'function.prototype.name': functionPrototypeName,
'functions-have-names': functionsHaveNames,
gopd: gopd,
'has-proto': hasProto,
'has-symbols': hasSymbols,
'has-tostringtag': hasTostringtag,
'promise.allsettled': promiseAllsettled,
'promise.any': promiseAny,
'promise.prototype.finally': promisePrototypeFinally,
'for-each': forEach,
'array.prototype.at': arrayPrototypeAt,
'array.prototype.concat': arrayPrototypeConcat,
'array.prototype.entries': arrayPrototypeEntries,
'array.prototype.find': arrayPrototypeFind,
'array.prototype.findIndex': arrayPrototypeFindIndex,
'array.prototype.flat': arrayPrototypeFlat,
'array.prototype.flatMap': arrayPrototypeFlatMap,
'array.prototype.forEach': arrayPrototypeForEach,
'is-builtin-module': isBuiltinModule,
'array.from': arrayFrom,
'is-even': isEven,
'is-windows': isWindows,
'is-travis': isTravis,
'is-number': isNumber,
has: has,
hasown: hasown,
'has-own-prop': hasOwnProp,
'object-assign': objectAssign,
'object.entries': objectEntries,
'object-is': objectIs,
'is-nan': isNan,
'number.isnan': numberIsnan,
'object.fromentries': objectFromentries,
'object.values': objectValues,
'string.prototype.matchall': stringPrototypeMatchall,
'string.prototype.repeat': stringPrototypeRepeat,
'array.prototype.findlast': arrayPrototypeFindlast,
'array.prototype.tosorted': arrayPrototypeTosorted,
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
'is-boolean-object': isBooleanObject,
'is-date-object': isDateObject,
'is-number-object': isNumberObject,
'is-odd': isOdd,
'is-string': isString,
'is-regexp': isRegexp,
'array.prototype.map': arrayPrototypeMap,
'array.prototype.filter': arrayPrototypeFilter,
'array-includes': arrayIncludes,
'object-keys': objectKeys,
'array.prototype.unshift': arrayPrototypeUnshift,
'array.prototype.splice': arrayPrototypeSplice,
'array.prototype.some': arrayPrototypeSome,
'is-negative-zero': isNegativeZero,
'array.prototype.slice': arrayPrototypeSlice,
'array.prototype.reduceright': arrayPrototypeReduceright,
'array.prototype.every': arrayPrototypeEvery,
'function.prototype.name': functionPrototypeName,
'functions-have-names': functionsHaveNames,
gopd: gopd,
'has-proto': hasProto,
'has-symbols': hasSymbols,
'has-tostringtag': hasTostringtag,
'promise.allsettled': promiseAllsettled,
'promise.any': promiseAny,
'promise.prototype.finally': promisePrototypeFinally,
'for-each': forEach,
'array.prototype.at': arrayPrototypeAt,
'array.prototype.concat': arrayPrototypeConcat,
'array.prototype.entries': arrayPrototypeEntries,
'array.prototype.find': arrayPrototypeFind,
'array.prototype.findIndex': arrayPrototypeFindIndex,
'array.prototype.flat': arrayPrototypeFlat,
'array.prototype.flatMap': arrayPrototypeFlatMap,
'array.prototype.forEach': arrayPrototypeForEach,
'is-builtin-module': isBuiltinModule,
'array.from': arrayFrom,
'is-even': isEven,
'is-windows': isWindows,
'is-travis': isTravis,
'is-number': isNumber,
has: has,
hasown: hasown,
'has-own-prop': hasOwnProp,
'object-assign': objectAssign,
'object.entries': objectEntries,
'object-is': objectIs,
'is-nan': isNan,
'number.isnan': numberIsnan,
'object.fromentries': objectFromentries,
'object.values': objectValues,
'string.prototype.matchall': stringPrototypeMatchall,
'string.prototype.repeat': stringPrototypeRepeat,
'array.prototype.findlast': arrayPrototypeFindlast,
'array.prototype.tosorted': arrayPrototypeTosorted,
'md5': md5,
};
62 changes: 62 additions & 0 deletions codemods/md5/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import jscodeshift from 'jscodeshift';

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

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

// Replace import statement
root
.find(j.ImportDeclaration, { source: { value: 'md5' } })
.forEach((path) => {
path.value.source.value = 'crypto';
path.value.specifiers = [
j.importDefaultSpecifier(j.identifier('crypto')),
];
dirtyFlag = true;
});

// Replace function calls
root
.find(j.CallExpression, {
callee: { name: 'md5' },
})
.forEach((path) => {
const newExpression = j.callExpression(
j.memberExpression(
j.callExpression(
j.memberExpression(
j.callExpression(
j.memberExpression(
j.identifier('crypto'),
j.identifier('createHash')
),
[j.literal('md5')]
),
j.identifier('update')
),
path.node.arguments
),
j.identifier('digest')
),
[j.literal('hex')]
);
j(path).replaceWith(newExpression);
});

return dirtyFlag ? root.toSource({ quote: 'single' }) : file.source;
},
}
};
2 changes: 2 additions & 0 deletions test/fixtures/md5/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import crypto from 'crypto'
console.log(crypto.createHash('md5').update('message').digest('hex'))
2 changes: 2 additions & 0 deletions test/fixtures/md5/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import md5 from 'md5'
console.log(md5('message'))
2 changes: 2 additions & 0 deletions test/fixtures/md5/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import crypto from "crypto"
console.log(crypto.createHash("md5").update('message').digest("hex"))
6 changes: 6 additions & 0 deletions test/fixtures/md5/case-2/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import fs from 'fs'
import crypto from 'crypto'

fs.readFile('example.txt', function (err, buf) {
console.log(crypto.createHash('md5').update(buf).digest('hex'));
});
6 changes: 6 additions & 0 deletions test/fixtures/md5/case-2/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import fs from 'fs'
import md5 from 'md5'

fs.readFile('example.txt', function (err, buf) {
console.log(md5(buf));
});
6 changes: 6 additions & 0 deletions test/fixtures/md5/case-2/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import fs from 'fs'
import crypto from "crypto"

fs.readFile('example.txt', function (err, buf) {
console.log(crypto.createHash("md5").update(buf).digest("hex"));
});

0 comments on commit e89f0c0

Please sign in to comment.