Skip to content

Commit 39b1518

Browse files
authored
Enable transformation when a mixture of require and imports is present (#336)
1 parent e5093f2 commit 39b1518

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

.changeset/nasty-zoos-hammer.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+
Enable transformation when a mixture of require and imports is present
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const debug = require("debug")("http");
2+
import AWS from "aws-sdk";
3+
4+
const client = new AWS.DynamoDB();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const debug = require("debug")("http");
2+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
3+
4+
const client = new DynamoDB();

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,16 @@ import { Collection, JSCodeshift } from "jscodeshift";
33
import { getImportEqualsDeclaration } from "./getImportEqualsDeclaration";
44

55
export const hasImportEquals = (j: JSCodeshift, source: Collection<unknown>) =>
6-
source.find(j.TSImportEqualsDeclaration, getImportEqualsDeclaration()).size() > 0;
6+
source
7+
.find(j.TSImportEqualsDeclaration, getImportEqualsDeclaration())
8+
.filter((importEqualsDeclaration) => {
9+
const { moduleReference } = importEqualsDeclaration.value;
10+
if (moduleReference.type !== "TSExternalModuleReference") return false;
11+
const { expression } = moduleReference;
12+
return (
13+
expression.type === "StringLiteral" &&
14+
typeof expression.value === "string" &&
15+
expression.value.startsWith("aws-sdk")
16+
);
17+
})
18+
.size() > 0;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,13 @@ export const hasRequire = (j: JSCodeshift, source: Collection<unknown>) =>
55
.find(j.CallExpression, {
66
callee: { type: "Identifier", name: "require" },
77
})
8+
.filter((callExpression) => {
9+
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+
);
16+
})
817
.size() > 0;

0 commit comments

Comments
 (0)