Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/breezy-rice-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Check for StringLiteral type to handle requires in TypeScript code
3 changes: 3 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/misc/require.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const AWS = require("aws-sdk");

const client = new AWS.DynamoDB();
5 changes: 5 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/misc/require.output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const {
DynamoDB
} = require("@aws-sdk/client-dynamodb");

const client = new DynamoDB();
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export const FUNCTION_TYPE_LIST = [
"FunctionExpression",
"ArrowFunctionExpression",
];
export const STRING_LITERAL_TYPE_LIST = ["Literal", "StringLiteral"];
15 changes: 8 additions & 7 deletions src/transforms/v2-to-v3/modules/hasRequire.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { Collection, JSCodeshift, Literal } from "jscodeshift";
import { PACKAGE_NAME, STRING_LITERAL_TYPE_LIST } from "../config";

export const hasRequire = (j: JSCodeshift, source: Collection<unknown>) =>
source
Expand All @@ -7,11 +8,11 @@ export const hasRequire = (j: JSCodeshift, source: Collection<unknown>) =>
})
.filter((callExpression) => {
const { arguments: args } = callExpression.value;
return (
args.length > 0 &&
(args[0].type === "Literal" || args[0].type === "StringLiteral") &&
typeof args[0].value === "string" &&
args[0].value.startsWith("aws-sdk")
);

if (args.length === 0) return false;
if (!STRING_LITERAL_TYPE_LIST.includes(args[0].type)) return false;

const value = (args[0] as Literal).value;
return typeof value === "string" && value.startsWith(PACKAGE_NAME);
})
.size() > 0;
7 changes: 4 additions & 3 deletions src/transforms/v2-to-v3/modules/removeRequireIdentifier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Collection, Identifier, JSCodeshift, VariableDeclarator } from "jscodeshift";
import { Collection, Identifier, JSCodeshift, Literal, VariableDeclarator } from "jscodeshift";

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

Expand Down Expand Up @@ -42,8 +43,8 @@ export const removeRequireIdentifier = (
const args = init.arguments;
if (!args) return true;
if (args.length !== 1) return true;
if (args[0].type !== "Literal") return true;
if (args[0].value !== sourceValue) return true;
if (!STRING_LITERAL_TYPE_LIST.includes(args[0].type)) return true;
if ((args[0] as Literal).value !== sourceValue) return true;

return false;
}
Expand Down
7 changes: 4 additions & 3 deletions src/transforms/v2-to-v3/modules/removeRequireProperty.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Collection, Identifier, JSCodeshift, VariableDeclarator } from "jscodeshift";
import { Collection, Identifier, JSCodeshift, Literal, VariableDeclarator } from "jscodeshift";

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

Expand Down Expand Up @@ -45,8 +46,8 @@ export const removeRequireProperty = (

const args = object.arguments;
if (args.length !== 1) return true;
if (args[0].type !== "Literal") return true;
if (args[0].value !== sourceValue) return true;
if (!STRING_LITERAL_TYPE_LIST.includes(args[0].type)) return true;
if ((args[0] as Literal).value !== sourceValue) return true;

const property = init.property;
if (property.type !== "Identifier") return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Collection, JSCodeshift, ObjectPattern, ObjectProperty, Property } from "jscodeshift";

import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME } from "../../config";
import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME, STRING_LITERAL_TYPE_LIST } from "../../config";
import { getRequireDeclarators } from "../getRequireDeclarators";
import { getRequireProperty } from "../getRequireProperty";
import { objectPatternPropertyCompareFn } from "../objectPatternPropertyCompareFn";
Expand Down Expand Up @@ -75,7 +75,7 @@ export const addNamedModule = (
const args = init.arguments;
if (!args) return false;
if (args.length !== 1) return false;
if (args[0].type !== "Literal") return false;
if (!STRING_LITERAL_TYPE_LIST.includes(args[0].type)) return true;
if (typeof args[0].value !== "string") return false;
if (!args[0].value.startsWith(PACKAGE_NAME)) return false;

Expand All @@ -93,7 +93,7 @@ export const addNamedModule = (

const args = object.arguments;
if (args.length !== 1) return false;
if (args[0].type !== "Literal") return false;
if (!STRING_LITERAL_TYPE_LIST.includes(args[0].type)) return true;
if (args[0].value !== PACKAGE_NAME) return false;

return true;
Expand Down