Skip to content

Commit

Permalink
Enable transformation when a mixture of require and imports is present (
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Jan 9, 2023
1 parent e5093f2 commit 39b1518
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/nasty-zoos-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Enable transformation when a mixture of require and imports is present
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const debug = require("debug")("http");
import AWS from "aws-sdk";

const client = new AWS.DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const debug = require("debug")("http");
import { DynamoDB } from "@aws-sdk/client-dynamodb";

const client = new DynamoDB();
14 changes: 13 additions & 1 deletion src/transforms/v2-to-v3/modules/hasImportEquals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,16 @@ import { Collection, JSCodeshift } from "jscodeshift";
import { getImportEqualsDeclaration } from "./getImportEqualsDeclaration";

export const hasImportEquals = (j: JSCodeshift, source: Collection<unknown>) =>
source.find(j.TSImportEqualsDeclaration, getImportEqualsDeclaration()).size() > 0;
source
.find(j.TSImportEqualsDeclaration, getImportEqualsDeclaration())
.filter((importEqualsDeclaration) => {
const { moduleReference } = importEqualsDeclaration.value;
if (moduleReference.type !== "TSExternalModuleReference") return false;
const { expression } = moduleReference;
return (
expression.type === "StringLiteral" &&
typeof expression.value === "string" &&
expression.value.startsWith("aws-sdk")
);
})
.size() > 0;
9 changes: 9 additions & 0 deletions src/transforms/v2-to-v3/modules/hasRequire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@ export const hasRequire = (j: JSCodeshift, source: Collection<unknown>) =>
.find(j.CallExpression, {
callee: { type: "Identifier", name: "require" },
})
.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")
);
})
.size() > 0;

0 comments on commit 39b1518

Please sign in to comment.