Skip to content

Commit 6ebca1a

Browse files
authored
Remove require declarator from declaration (#626)
1 parent 6c49246 commit 6ebca1a

File tree

7 files changed

+102
-24
lines changed

7 files changed

+102
-24
lines changed

.changeset/wild-games-clap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Remove require declarator from declaration
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const DynDB = require("aws-sdk").DynamoDB;
2+
const { DynamoDB } = require("@aws-sdk/client-dynamodb");
3+
4+
const client1 = new DynDB();
5+
const client2 = new DynamoDB();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const {
2+
DynamoDB,
3+
DynamoDB: DynDB
4+
} = require("@aws-sdk/client-dynamodb");
5+
6+
const client1 = new DynDB();
7+
const client2 = new DynamoDB();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { removeImportDefault } from "./removeImportDefault";
77
import { removeImportEquals } from "./removeImportEquals";
88
import { removeImportNamed } from "./removeImportNamed";
99
import { removeRequireIdentifier } from "./removeRequireIdentifier";
10-
import { removeRequireObjectProperty } from "./removeRequireObjectProperty";
10+
import { removeRequireObjectPattern } from "./removeRequireObjectPattern";
1111
import { removeRequireProperty } from "./removeRequireProperty";
1212
import { ImportType } from "./types";
1313

@@ -31,7 +31,7 @@ export const removeClientModule = (
3131

3232
if (importType === ImportType.REQUIRE) {
3333
removeRequireIdentifier(j, source, defaultOptions);
34-
removeRequireObjectProperty(j, source, namedOptions);
34+
removeRequireObjectPattern(j, source, namedOptions);
3535
removeRequireProperty(j, source, { ...namedOptions, propertyName: v2ClientName });
3636
} else if (importType === ImportType.IMPORT_EQUALS) {
3737
removeImportEquals(j, source, defaultOptions);

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Collection, JSCodeshift } from "jscodeshift";
1+
import { Collection, Identifier, JSCodeshift, VariableDeclarator } from "jscodeshift";
22

33
import { getRequireDeclaratorsWithIdentifier } from "./getRequireDeclaratorsWithIdentifier";
44

@@ -18,15 +18,39 @@ export const removeRequireIdentifier = (
1818
});
1919

2020
requireDeclarators.forEach((varDeclarator) => {
21-
const varDeclarationCollection = j(varDeclarator).closest(j.VariableDeclaration);
21+
const varDeclaration = varDeclarator.parentPath.parentPath;
2222

23-
// Remove VariableDeclarator as it contains the only identifier.
24-
j(varDeclarator).remove();
23+
// Removes variable declarator from the declarations.
24+
varDeclaration.value.declarations = varDeclaration.value.declarations.filter(
25+
(declaration: VariableDeclarator | Identifier) => {
26+
if (declaration.type === "Identifier") return true;
27+
28+
const id = declaration.id;
29+
if (id.type !== "Identifier") return true;
30+
if (id.name !== localName) return true;
31+
32+
const init = declaration.init;
33+
if (!init) return true;
34+
if (init.type !== "CallExpression") return true;
35+
36+
const callee = init.callee;
37+
if (!callee) return true;
38+
if (callee.type !== "Identifier") return true;
39+
if (callee.name !== "require") return true;
40+
41+
const args = init.arguments;
42+
if (!args) return true;
43+
if (args.length !== 1) return true;
44+
if (args[0].type !== "Literal") return true;
45+
if (args[0].value !== sourceValue) return true;
46+
47+
return false;
48+
}
49+
);
2550

2651
// Remove VariableDeclaration if there are no declarations.
27-
const varDeclaration = varDeclarationCollection.nodes()[0];
28-
if (varDeclaration && varDeclaration.declarations?.length === 0) {
29-
varDeclarationCollection.remove();
52+
if (varDeclaration.value.declarations?.length === 0) {
53+
j(varDeclaration).remove();
3054
}
3155
});
3256
};

src/transforms/v2-to-v3/modules/removeRequireObjectProperty.ts renamed to src/transforms/v2-to-v3/modules/removeRequireObjectPattern.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { Collection, JSCodeshift, ObjectPattern, ObjectProperty, Property } from "jscodeshift";
1+
import {
2+
Collection,
3+
Identifier,
4+
JSCodeshift,
5+
ObjectPattern,
6+
ObjectProperty,
7+
Property,
8+
VariableDeclarator,
9+
} from "jscodeshift";
210

311
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
412
import { getRequireDeclaratorsWithObjectPattern } from "./getRequireDeclaratorsWithObjectPattern";
@@ -8,7 +16,7 @@ export interface RemoveRequireObjectPropertyOptions {
816
sourceValue: string;
917
}
1018

11-
export const removeRequireObjectProperty = (
19+
export const removeRequireObjectPattern = (
1220
j: JSCodeshift,
1321
source: Collection<unknown>,
1422
{ localName, sourceValue }: RemoveRequireObjectPropertyOptions
@@ -19,8 +27,6 @@ export const removeRequireObjectProperty = (
1927
});
2028

2129
requireDeclarators.forEach((varDeclarator) => {
22-
const varDeclarationCollection = j(varDeclarator).closest(j.VariableDeclaration);
23-
2430
// Remove ObjectProperty from Variable Declarator.
2531
const varDeclaratorId = varDeclarator.value.id as ObjectPattern;
2632
varDeclaratorId.properties = varDeclaratorId.properties.filter((property) => {
@@ -31,12 +37,14 @@ export const removeRequireObjectProperty = (
3137

3238
// Remove VariableDeclarator if there are no properties.
3339
if (varDeclaratorId.properties.length === 0) {
34-
j(varDeclarator).remove();
40+
const varDeclaration = varDeclarator.parentPath.parentPath;
41+
varDeclaration.value.declarations = varDeclaration.value.declarations.filter(
42+
(declaration: VariableDeclarator | Identifier) => declaration !== varDeclarator.value
43+
);
3544

3645
// Remove VariableDeclaration if there are no declarations.
37-
const varDeclaration = varDeclarationCollection.nodes()[0];
38-
if (varDeclaration && varDeclaration.declarations?.length === 0) {
39-
varDeclarationCollection.remove();
46+
if (varDeclaration.value.declarations?.length === 0) {
47+
j(varDeclaration).remove();
4048
}
4149
}
4250
});

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

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Collection, JSCodeshift } from "jscodeshift";
1+
import { Collection, Identifier, JSCodeshift, VariableDeclarator } from "jscodeshift";
22

33
import { getRequireDeclaratorsWithProperty } from "./getRequireDeclaratorsWithProperty";
44

@@ -20,15 +20,44 @@ export const removeRequireProperty = (
2020
});
2121

2222
requireDeclarators.forEach((varDeclarator) => {
23-
const varDeclarationCollection = j(varDeclarator).closest(j.VariableDeclaration);
23+
const varDeclaration = varDeclarator.parentPath.parentPath;
2424

25-
// Remove VariableDeclarator as it contains the only identifier.
26-
j(varDeclarator).remove();
25+
// Removes variable declarator from the declarations.
26+
varDeclaration.value.declarations = varDeclaration.value.declarations.filter(
27+
(declaration: VariableDeclarator | Identifier) => {
28+
if (declaration.type === "Identifier") return true;
29+
30+
const id = declaration.id;
31+
if (id.type !== "Identifier") return true;
32+
if (id.name !== localName) return true;
33+
34+
const init = declaration.init;
35+
if (!init) return true;
36+
if (init.type !== "MemberExpression") return true;
37+
38+
const object = init.object;
39+
if (object.type !== "CallExpression") return true;
40+
41+
const callee = object.callee;
42+
if (callee.type !== "Identifier") return true;
43+
if (callee.name !== "require") return true;
44+
45+
const args = object.arguments;
46+
if (args.length !== 1) return true;
47+
if (args[0].type !== "Literal") return true;
48+
if (args[0].value !== sourceValue) return true;
49+
50+
const property = init.property;
51+
if (property.type !== "Identifier") return true;
52+
if (property.name !== propertyName) return true;
53+
54+
return false;
55+
}
56+
);
2757

2858
// Remove VariableDeclaration if there are no declarations.
29-
const varDeclaration = varDeclarationCollection.nodes()[0];
30-
if (varDeclaration && varDeclaration.declarations?.length === 0) {
31-
varDeclarationCollection.remove();
59+
if (varDeclaration.value.declarations?.length === 0) {
60+
j(varDeclaration).remove();
3261
}
3362
});
3463
};

0 commit comments

Comments
 (0)