Skip to content

Commit c72eac2

Browse files
authored
Check for StringLiteral type to handle requires in TypeScript code (#653)
1 parent d0a44bc commit c72eac2

File tree

8 files changed

+33
-16
lines changed

8 files changed

+33
-16
lines changed

.changeset/breezy-rice-stare.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+
Check for StringLiteral type to handle requires in TypeScript code
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const AWS = require("aws-sdk");
2+
3+
const client = new AWS.DynamoDB();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const {
2+
DynamoDB
3+
} = require("@aws-sdk/client-dynamodb");
4+
5+
const client = new DynamoDB();

src/transforms/v2-to-v3/config/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export const FUNCTION_TYPE_LIST = [
1212
"FunctionExpression",
1313
"ArrowFunctionExpression",
1414
];
15+
export const STRING_LITERAL_TYPE_LIST = ["Literal", "StringLiteral"];
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Collection, JSCodeshift } from "jscodeshift";
1+
import { Collection, JSCodeshift, Literal } from "jscodeshift";
2+
import { PACKAGE_NAME, STRING_LITERAL_TYPE_LIST } from "../config";
23

34
export const hasRequire = (j: JSCodeshift, source: Collection<unknown>) =>
45
source
@@ -7,11 +8,11 @@ export const hasRequire = (j: JSCodeshift, source: Collection<unknown>) =>
78
})
89
.filter((callExpression) => {
910
const { arguments: args } = callExpression.value;
10-
return (
11-
args.length > 0 &&
12-
(args[0].type === "Literal" || args[0].type === "StringLiteral") &&
13-
typeof args[0].value === "string" &&
14-
args[0].value.startsWith("aws-sdk")
15-
);
11+
12+
if (args.length === 0) return false;
13+
if (!STRING_LITERAL_TYPE_LIST.includes(args[0].type)) return false;
14+
15+
const value = (args[0] as Literal).value;
16+
return typeof value === "string" && value.startsWith(PACKAGE_NAME);
1617
})
1718
.size() > 0;

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

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

3+
import { STRING_LITERAL_TYPE_LIST } from "../config";
34
import { getRequireDeclaratorsWithIdentifier } from "./getRequireDeclaratorsWithIdentifier";
45
import { removeDeclaration } from "./removeDeclaration";
56

@@ -42,8 +43,8 @@ export const removeRequireIdentifier = (
4243
const args = init.arguments;
4344
if (!args) return true;
4445
if (args.length !== 1) return true;
45-
if (args[0].type !== "Literal") return true;
46-
if (args[0].value !== sourceValue) return true;
46+
if (!STRING_LITERAL_TYPE_LIST.includes(args[0].type)) return true;
47+
if ((args[0] as Literal).value !== sourceValue) return true;
4748

4849
return false;
4950
}

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

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

3+
import { STRING_LITERAL_TYPE_LIST } from "../config";
34
import { getRequireDeclaratorsWithProperty } from "./getRequireDeclaratorsWithProperty";
45
import { removeDeclaration } from "./removeDeclaration";
56

@@ -45,8 +46,8 @@ export const removeRequireProperty = (
4546

4647
const args = object.arguments;
4748
if (args.length !== 1) return true;
48-
if (args[0].type !== "Literal") return true;
49-
if (args[0].value !== sourceValue) return true;
49+
if (!STRING_LITERAL_TYPE_LIST.includes(args[0].type)) return true;
50+
if ((args[0] as Literal).value !== sourceValue) return true;
5051

5152
const property = init.property;
5253
if (property.type !== "Identifier") return true;

src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Collection, JSCodeshift, ObjectPattern, ObjectProperty, Property } from "jscodeshift";
22

3-
import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME } from "../../config";
3+
import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME, STRING_LITERAL_TYPE_LIST } from "../../config";
44
import { getRequireDeclarators } from "../getRequireDeclarators";
55
import { getRequireProperty } from "../getRequireProperty";
66
import { objectPatternPropertyCompareFn } from "../objectPatternPropertyCompareFn";
@@ -75,7 +75,7 @@ export const addNamedModule = (
7575
const args = init.arguments;
7676
if (!args) return false;
7777
if (args.length !== 1) return false;
78-
if (args[0].type !== "Literal") return false;
78+
if (!STRING_LITERAL_TYPE_LIST.includes(args[0].type)) return true;
7979
if (typeof args[0].value !== "string") return false;
8080
if (!args[0].value.startsWith(PACKAGE_NAME)) return false;
8181

@@ -93,7 +93,7 @@ export const addNamedModule = (
9393

9494
const args = object.arguments;
9595
if (args.length !== 1) return false;
96-
if (args[0].type !== "Literal") return false;
96+
if (!STRING_LITERAL_TYPE_LIST.includes(args[0].type)) return true;
9797
if (args[0].value !== PACKAGE_NAME) return false;
9898

9999
return true;

0 commit comments

Comments
 (0)