Skip to content

Commit 8b48b59

Browse files
authored
Add utility removeRequireObjectProperty (#295)
1 parent 7cbde5a commit 8b48b59

File tree

5 files changed

+85
-54
lines changed

5 files changed

+85
-54
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Collection, Identifier, JSCodeshift } from "jscodeshift";
2+
3+
import { getRequireVariableDeclarators } from "./getRequireVariableDeclarators";
4+
5+
export interface RemoveRequireIdentifierOptions {
6+
localName: string;
7+
sourceValue: string;
8+
}
9+
10+
export const removeRequireIdentifier = (
11+
j: JSCodeshift,
12+
source: Collection<unknown>,
13+
{ localName, sourceValue }: RemoveRequireIdentifierOptions
14+
) => {
15+
const id = { type: "Identifier", name: localName } as Identifier;
16+
const requireDeclarators = getRequireVariableDeclarators(j, source, sourceValue, id);
17+
18+
requireDeclarators.forEach((varDeclarator) => {
19+
const varDeclarationCollection = j(varDeclarator).closest(j.VariableDeclaration);
20+
21+
// Remove VariableDeclarator as it contains the only identifier.
22+
j(varDeclarator).remove();
23+
24+
// Remove VariableDeclaration if there are no declarations.
25+
const varDeclaration = varDeclarationCollection.nodes()[0];
26+
if (varDeclaration && varDeclaration.declarations?.length === 0) {
27+
varDeclarationCollection.remove();
28+
}
29+
});
30+
};

src/transforms/v2-to-v3/modules/removeRequireIdentifierName.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Collection, JSCodeshift, ObjectPattern } from "jscodeshift";
2+
3+
import { getRequireVariableDeclarators } from "./getRequireVariableDeclarators";
4+
5+
export interface RemoveRequireObjectPropertyOptions {
6+
localName: string;
7+
sourceValue: string;
8+
}
9+
10+
export const removeRequireObjectProperty = (
11+
j: JSCodeshift,
12+
source: Collection<unknown>,
13+
{ localName, sourceValue }: RemoveRequireObjectPropertyOptions
14+
) => {
15+
const id = {
16+
type: "ObjectPattern",
17+
properties: [{ type: "Property", value: { type: "Identifier", name: localName } }],
18+
} as ObjectPattern;
19+
const requireDeclarators = getRequireVariableDeclarators(j, source, sourceValue, id);
20+
21+
requireDeclarators.forEach((varDeclarator) => {
22+
const varDeclarationCollection = j(varDeclarator).closest(j.VariableDeclaration);
23+
24+
// Remove ObjectProperty from Variable Declarator.
25+
const varDeclaratorId = varDeclarator.value.id as ObjectPattern;
26+
varDeclaratorId.properties = varDeclaratorId.properties.filter(
27+
(property) =>
28+
property.type !== "Property" ||
29+
property.value.type !== "Identifier" ||
30+
property.value.name !== localName
31+
);
32+
33+
// Remove VariableDeclarator if there are no properties.
34+
if (varDeclaratorId.properties.length === 0) {
35+
j(varDeclarator).remove();
36+
37+
// Remove VariableDeclaration if there are no declarations.
38+
const varDeclaration = varDeclarationCollection.nodes()[0];
39+
if (varDeclaration && varDeclaration.declarations?.length === 0) {
40+
varDeclarationCollection.remove();
41+
}
42+
}
43+
});
44+
};

src/transforms/v2-to-v3/modules/removeV2ClientModule.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { hasImportEquals } from "./hasImportEquals";
77
import { hasRequire } from "./hasRequire";
88
import { removeImportEqualsIdentifierName } from "./removeImportEqualsIdentifierName";
99
import { removeImportIdentifierName } from "./removeImportIdentifierName";
10-
import { removeRequireIdentifierName } from "./removeRequireIdentifierName";
10+
import { removeRequireIdentifier } from "./removeRequireIdentifier";
11+
import { removeRequireObjectProperty } from "./removeRequireObjectProperty";
1112

1213
export interface RemoveV2ClientModuleOptions {
1314
v2ClientName: string;
@@ -25,11 +26,13 @@ export const removeV2ClientModule = (
2526
const sourceValues = [PACKAGE_NAME, serviceModulePath];
2627

2728
if (hasRequire(j, source)) {
28-
sourceValues.forEach((sourceValue) => {
29-
removeRequireIdentifierName(j, source, {
30-
localName: v2ClientLocalName,
31-
sourceValue,
32-
});
29+
removeRequireIdentifier(j, source, {
30+
localName: v2ClientLocalName,
31+
sourceValue: serviceModulePath,
32+
});
33+
removeRequireObjectProperty(j, source, {
34+
localName: v2ClientLocalName,
35+
sourceValue: PACKAGE_NAME,
3336
});
3437
} else if (hasImportEquals(j, source)) {
3538
removeImportEqualsIdentifierName(j, source, {

src/transforms/v2-to-v3/modules/removeV2GlobalModule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { hasImportEquals } from "./hasImportEquals";
55
import { hasRequire } from "./hasRequire";
66
import { removeImportEqualsIdentifierName } from "./removeImportEqualsIdentifierName";
77
import { removeImportIdentifierName } from "./removeImportIdentifierName";
8-
import { removeRequireIdentifierName } from "./removeRequireIdentifierName";
8+
import { removeRequireIdentifier } from "./removeRequireIdentifier";
99

1010
export const removeV2GlobalModule = (
1111
j: JSCodeshift,
@@ -21,7 +21,7 @@ export const removeV2GlobalModule = (
2121
sourceValue: PACKAGE_NAME,
2222
};
2323
if (hasRequire(j, source)) {
24-
removeRequireIdentifierName(j, source, removeIdentifierNameOptions);
24+
removeRequireIdentifier(j, source, removeIdentifierNameOptions);
2525
} else if (hasImportEquals(j, source)) {
2626
removeImportEqualsIdentifierName(j, source, removeIdentifierNameOptions);
2727
} else {

0 commit comments

Comments
 (0)