Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consider import removal to be dirty #89

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading