Skip to content

Commit

Permalink
Merge pull request #89 from es-tooling/fix-dirty-arrays
Browse files Browse the repository at this point in the history
fix: consider import removal to be dirty
  • Loading branch information
thepassle authored Aug 7, 2024
2 parents f5f2b45 + 5b32429 commit 5e85a9a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
18 changes: 15 additions & 3 deletions codemods/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* type definition for return type object
* @typedef {Object} RemoveImport
* @property {string} identifier - the name of the module as it was imported or required. for example, `keys` in `import keys from 'object-keys'`
* @property {boolean} dirtyFlag - whether imports were removed or not
* @typedef {Object} ReplaceDefaultImport
* @property {string} identifier - the name of the module as it was imported or required. for example, `keys` in `import keys from 'object-keys'`
*/
Expand Down Expand Up @@ -78,7 +79,13 @@ export function removeImport(name, root, j) {
requireAssignment.remove();
sideEffectRequireExpression.remove();

return { identifier };
const dirtyFlag =
importDeclaration.length > 0 ||
requireDeclaration.length > 0 ||
requireAssignment.length > 0 ||
sideEffectRequireExpression.length > 0;

return { identifier, dirtyFlag };
}

/**
Expand Down Expand Up @@ -305,9 +312,14 @@ export function replaceDefaultImport(name, newSpecifier, newName, root, j) {
* @returns {boolean} - true if the method was found and transformed, false otherwise
*/
export function transformArrayMethod(method, identifierName, root, j) {
const { identifier } = removeImport(method, root, j);
const { identifier, dirtyFlag: importDirtyFlag } = removeImport(
method,
root,
j,
);

let dirtyFlag = importDirtyFlag;

let dirtyFlag = false;
root
.find(j.CallExpression, {
callee: {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/array-every/imports-only/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const n = 303;
3 changes: 3 additions & 0 deletions test/fixtures/array-every/imports-only/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const every = require("array-every");

export const n = 303;
1 change: 1 addition & 0 deletions test/fixtures/array-every/imports-only/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const n = 303;

0 comments on commit 5e85a9a

Please sign in to comment.