From 5b324297dff2ec90ee9bd0caaf7f3b72f71c39c9 Mon Sep 17 00:00:00 2001 From: 43081j <43081j@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:05:08 +0100 Subject: [PATCH] fix: consider import removal to be dirty When using the `transformArrayMethod` helper, we currently only consider the change to be dirty if call expressions we mutated. This means imports of these modules will not be removed unless there are also associated call expressions. This fix simply considers removal of the import to be a dirty change. --- codemods/shared.js | 18 +++++++++++++++--- .../fixtures/array-every/imports-only/after.js | 1 + .../array-every/imports-only/before.js | 3 +++ .../array-every/imports-only/result.js | 1 + 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/array-every/imports-only/after.js create mode 100644 test/fixtures/array-every/imports-only/before.js create mode 100644 test/fixtures/array-every/imports-only/result.js diff --git a/codemods/shared.js b/codemods/shared.js index d85b48f..ec865f1 100644 --- a/codemods/shared.js +++ b/codemods/shared.js @@ -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'` */ @@ -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 }; } /** @@ -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: { diff --git a/test/fixtures/array-every/imports-only/after.js b/test/fixtures/array-every/imports-only/after.js new file mode 100644 index 0000000..4800b93 --- /dev/null +++ b/test/fixtures/array-every/imports-only/after.js @@ -0,0 +1 @@ +export const n = 303; diff --git a/test/fixtures/array-every/imports-only/before.js b/test/fixtures/array-every/imports-only/before.js new file mode 100644 index 0000000..bbf2aed --- /dev/null +++ b/test/fixtures/array-every/imports-only/before.js @@ -0,0 +1,3 @@ +const every = require("array-every"); + +export const n = 303; diff --git a/test/fixtures/array-every/imports-only/result.js b/test/fixtures/array-every/imports-only/result.js new file mode 100644 index 0000000..4800b93 --- /dev/null +++ b/test/fixtures/array-every/imports-only/result.js @@ -0,0 +1 @@ +export const n = 303;