Skip to content

Commit

Permalink
refactor: add removeImport function
Browse files Browse the repository at this point in the history
  • Loading branch information
thepassle committed Jul 17, 2024
1 parent 7eccc2e commit 1f2b54d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 101 deletions.
42 changes: 2 additions & 40 deletions codemods/is-array-buffer/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -17,46 +18,7 @@ export default function (options) {
const root = j(file.source);
let dirtyFlag = false;

// Remove the require statement for 'is-array-buffer'
root.find(j.VariableDeclaration).forEach((path) => {
const declarations = path.value.declarations;
if (
declarations.length === 1 &&
j.VariableDeclarator.check(declarations[0])
) {
const declarator = declarations[0];
if (
j.CallExpression.check(declarator.init) &&
j.Identifier.check(declarator.id)
) {
const callExpr = declarator.init;
if (
j.Identifier.check(callExpr.callee) &&
callExpr.callee.name === 'require'
) {
const args = callExpr.arguments;
if (
args.length === 1 &&
j.Literal.check(args[0]) &&
args[0].value === 'is-array-buffer'
) {
j(path).remove();
dirtyFlag = true;
}
}
}
}
});

// Remove ESM import statement for 'is-array-buffer'
root
.find(j.ImportDeclaration, {
source: { value: 'is-array-buffer' },
})
.forEach((path) => {
j(path).remove();
dirtyFlag = true;
});
removeImport('is-array-buffer', root, j);

// Replace isArrayBuffer calls with (foo instanceof ArrayBuffer)
root
Expand Down
38 changes: 4 additions & 34 deletions codemods/is-boolean-object/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';

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

// Find all require statements for 'is-boolean-object'
root
.find(j.VariableDeclaration)
.filter((path) =>
path.value.declarations.some(
(decl) =>
j.VariableDeclarator.check(decl) &&
decl.init &&
j.CallExpression.check(decl.init) &&
j.Identifier.check(decl.init.callee) &&
decl.init.callee.name === 'require' &&
decl.init.arguments.length > 0 &&
j.Literal.check(decl.init.arguments[0]) &&
decl.init.arguments[0].value === 'is-boolean-object',
),
)
.forEach((path) => {
// Remove the require statement for 'is-boolean-object'
path.prune();
dirtyFlag = true;
});

// Remove ESM import statement for 'is-boolean-object'
root
.find(j.ImportDeclaration, {
source: { value: 'is-boolean-object' },
})
.forEach((path) => {
j(path).remove();
dirtyFlag = true;
});

removeImport('is-boolean-object', root, j);

// Replace all calls to isBoolean with Object.prototype.toString.call
root
.find(j.CallExpression, {
Expand Down Expand Up @@ -91,7 +61,7 @@ export default function (options) {
}
});

return dirtyFlag ? root.toSource({ quote: 'single' }) : file.source;
return root.toSource({ quote: 'single' });
},
};
}
29 changes: 2 additions & 27 deletions codemods/is-whitespace/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -22,30 +23,8 @@ export default function (options) {
const j = jscodeshift;
const root = j(file.source);

// Find the import or require statement for 'is-whitespace'
const importDeclaration = root.find(j.ImportDeclaration, {
source: {
value: 'is-whitespace',
},
});
removeImport('is-whitespace', root, j);

const requireDeclaration = root.find(j.VariableDeclarator, {
init: {
callee: {
name: 'require',
},
arguments: [
{
value: 'is-whitespace',
},
],
},
});

// If 'is-whitespace' is not found in the imported packages, return the original source code
if (importDeclaration.size() === 0 && requireDeclaration.size() === 0) {
return root.toSource();
}

// Find the 'isWhitespace' function calls
root
Expand All @@ -68,10 +47,6 @@ export default function (options) {
);
});

// Remove the import or require statement for 'is-whitespace'
importDeclaration.remove();
requireDeclaration.remove();

return root.toSource({ quote: 'single' });
},
};
Expand Down
29 changes: 29 additions & 0 deletions codemods/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @param {string} name - package name to remove import/require calls for
* @param {any} root - package name to remove import/require calls for
* @param {any} j - jscodeshift instance
*/
export function removeImport(name, root, j) {
// Find the import or require statement for 'is-boolean-object'
const importDeclaration = root.find(j.ImportDeclaration, {
source: {
value: name,
},
});

const requireDeclaration = root.find(j.VariableDeclarator, {
init: {
callee: {
name: "require",
},
arguments: [
{
value: name,
},
],
},
});

importDeclaration.remove();
requireDeclaration.remove();
}

0 comments on commit 1f2b54d

Please sign in to comment.