Skip to content

Commit

Permalink
Preserve comments while deleting variable declarator
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Oct 18, 2023
1 parent f023187 commit b627e9f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { ASTPath, Collection, ImportDeclaration, JSCodeshift } from "jscodeshift";
import {
ASTPath,
Collection,
ImportDeclaration,
JSCodeshift,
VariableDeclaration,
} from "jscodeshift";

/**
* Removes import declaration, but preserves comments if they're top level comments.
* Removes import/variable declaration, but preserves comments if they're top level comments.
*/
export const removeImportDeclaration = (
export const removeDeclaration = (
j: JSCodeshift,
source: Collection<unknown>,
declarationPath: ASTPath<ImportDeclaration>
declarationPath: ASTPath<ImportDeclaration | VariableDeclaration>
) => {
const firstNode = source.find(j.Program).get("body", 0).node;
if (firstNode === declarationPath.node) {
Expand Down
4 changes: 2 additions & 2 deletions src/transforms/v2-to-v3/modules/removeImportDefault.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { removeImportDeclaration } from "./removeImportDeclaration";
import { removeDeclaration } from "./removeDeclaration";

export interface RemoveImportDefaultOptions {
localName: string;
Expand Down Expand Up @@ -27,7 +27,7 @@ export const removeImportDefault = (

// Remove ImportDeclaration if there are no import specifiers.
if (declarationPath.value.specifiers?.length === 0) {
removeImportDeclaration(j, source, declarationPath);
removeDeclaration(j, source, declarationPath);
}
});
};
4 changes: 2 additions & 2 deletions src/transforms/v2-to-v3/modules/removeImportEquals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { removeImportDeclaration } from "./removeImportDeclaration";
import { removeDeclaration } from "./removeDeclaration";

export interface RemoveImportEqualsOptions {
localName: string;
Expand All @@ -20,6 +20,6 @@ export const removeImportEquals = (
},
});
if (importEqualsDeclaration.length) {
removeImportDeclaration(j, source, importEqualsDeclaration.get());
removeDeclaration(j, source, importEqualsDeclaration.get());
}
};
4 changes: 2 additions & 2 deletions src/transforms/v2-to-v3/modules/removeImportNamed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { removeImportDeclaration } from "./removeImportDeclaration";
import { removeDeclaration } from "./removeDeclaration";

export interface RemoveImportNamedOptions {
importedName?: string;
Expand Down Expand Up @@ -37,7 +37,7 @@ export const removeImportNamed = (

// Remove ImportDeclaration if there are no import specifiers.
if (declarationPath.value.specifiers?.length === 0) {
removeImportDeclaration(j, source, declarationPath);
removeDeclaration(j, source, declarationPath);
}
});
};
3 changes: 2 additions & 1 deletion src/transforms/v2-to-v3/modules/removeRequireIdentifier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Collection, Identifier, JSCodeshift, VariableDeclarator } from "jscodeshift";

import { getRequireDeclaratorsWithIdentifier } from "./getRequireDeclaratorsWithIdentifier";
import { removeDeclaration } from "./removeDeclaration";

export interface RemoveRequireIdentifierOptions {
localName: string;
Expand Down Expand Up @@ -50,7 +51,7 @@ export const removeRequireIdentifier = (

// Remove VariableDeclaration if there are no declarations.
if (varDeclaration.value.declarations?.length === 0) {
j(varDeclaration).remove();
removeDeclaration(j, source, varDeclaration);
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {

import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
import { getRequireDeclaratorsWithObjectPattern } from "./getRequireDeclaratorsWithObjectPattern";
import { removeDeclaration } from "./removeDeclaration";

export interface RemoveRequireObjectPropertyOptions {
localName: string;
Expand Down Expand Up @@ -44,7 +45,7 @@ export const removeRequireObjectPattern = (

// Remove VariableDeclaration if there are no declarations.
if (varDeclaration.value.declarations?.length === 0) {
j(varDeclaration).remove();
removeDeclaration(j, source, varDeclaration);
}
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/transforms/v2-to-v3/modules/removeRequireProperty.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Collection, Identifier, JSCodeshift, VariableDeclarator } from "jscodeshift";

import { getRequireDeclaratorsWithProperty } from "./getRequireDeclaratorsWithProperty";
import { removeDeclaration } from "./removeDeclaration";

export interface RemoveRequireObjectPropertyOptions {
localName: string;
Expand Down Expand Up @@ -57,7 +58,7 @@ export const removeRequireProperty = (

// Remove VariableDeclaration if there are no declarations.
if (varDeclaration.value.declarations?.length === 0) {
j(varDeclaration).remove();
removeDeclaration(j, source, varDeclaration);
}
});
};

0 comments on commit b627e9f

Please sign in to comment.