Skip to content

Commit

Permalink
feat: add is-builtin-module
Browse files Browse the repository at this point in the history
  • Loading branch information
thepassle committed Jul 19, 2024
1 parent a139882 commit a06e73e
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 36 deletions.
75 changes: 39 additions & 36 deletions codemods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,41 +70,44 @@ import arrayPrototypeFlatMap from './array.prototype.flatMap/index.js';

import arrayPrototypeForEach from './array.prototype.forEach/index.js';

import isBuiltinModule from './is-builtin-module/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-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-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
'is-boolean-object': isBooleanObject,
'is-date-object': isDateObject,
'is-number-object': isNumberObject,
'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,
};
47 changes: 47 additions & 0 deletions codemods/is-builtin-module/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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: "is-builtin-module",
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

// Replace import statement
root
.find(j.ImportDeclaration, {
source: { value: "is-builtin-module" },
})
.forEach((path) => {
path.value.source.value = "node:module";
path.value.specifiers = [
j.importSpecifier(j.identifier("isBuiltin")),
];
dirtyFlag = true;
});

// Replace function calls
root
.find(j.CallExpression, {
callee: { name: "isBuiltinModule" },
})
.forEach((path) => {
// @ts-expect-error
path.value.callee.name = "isBuiltin";
dirtyFlag = true;
});

return dirtyFlag ? root.toSource() : file.source;
},
};
}
13 changes: 13 additions & 0 deletions test/fixtures/is-builtin-module/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { isBuiltin } from "node:module";

isBuiltin('fs');
//=> true

isBuiltin('fs/promises');
//=> true

isBuiltin('node:fs/promises');
//=> true

isBuiltin('unicorn');
//=> false
13 changes: 13 additions & 0 deletions test/fixtures/is-builtin-module/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import isBuiltinModule from 'is-builtin-module';

isBuiltinModule('fs');
//=> true

isBuiltinModule('fs/promises');
//=> true

isBuiltinModule('node:fs/promises');
//=> true

isBuiltinModule('unicorn');
//=> false
13 changes: 13 additions & 0 deletions test/fixtures/is-builtin-module/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { isBuiltin } from "node:module";

isBuiltin('fs');
//=> true

isBuiltin('fs/promises');
//=> true

isBuiltin('node:fs/promises');
//=> true

isBuiltin('unicorn');
//=> false

0 comments on commit a06e73e

Please sign in to comment.