Skip to content

Commit 858acad

Browse files
authored
Skip transformation if "aws-sdk" import is not present (#779)
1 parent 36c992b commit 858acad

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

.changeset/silly-coats-worry.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+
Skip transformation if "aws-sdk" import is not present
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
22
import { hasImport } from "./hasImport";
33
import { hasImportEquals } from "./hasImportEquals";
4+
import { hasRequire } from "./hasRequire";
45
import { ImportType } from "./types";
56

67
export const getImportType = (j: JSCodeshift, source: Collection<unknown>) =>
78
hasImport(j, source)
89
? ImportType.IMPORT
910
: hasImportEquals(j, source)
1011
? ImportType.IMPORT_EQUALS
11-
: ImportType.REQUIRE;
12+
: hasRequire(j, source)
13+
? ImportType.REQUIRE
14+
: null;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
22

3+
import { PACKAGE_NAME } from "../config";
34
import { getImportEqualsDeclarationType } from "./getImportEqualsDeclarationType";
45

56
export const hasImportEquals = (j: JSCodeshift, source: Collection<unknown>) =>
@@ -12,7 +13,7 @@ export const hasImportEquals = (j: JSCodeshift, source: Collection<unknown>) =>
1213
return (
1314
expression.type === "StringLiteral" &&
1415
typeof expression.value === "string" &&
15-
expression.value.startsWith("aws-sdk")
16+
expression.value.startsWith(PACKAGE_NAME)
1617
);
1718
})
1819
.size() > 0;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Collection, JSCodeshift, Literal } from "jscodeshift";
2+
import { PACKAGE_NAME } from "../config";
3+
4+
export const hasRequire = (j: JSCodeshift, source: Collection<unknown>) =>
5+
source
6+
.find(j.CallExpression, {
7+
callee: { type: "Identifier", name: "require" },
8+
})
9+
.filter((callExpression) => {
10+
const { value: sourceValue } = callExpression.value.arguments[0] as Literal;
11+
return typeof sourceValue === "string" && sourceValue.startsWith(PACKAGE_NAME);
12+
})
13+
.size() > 0;

src/transforms/v2-to-v3/transformer.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ const transformer = async (file: FileInfo, api: API) => {
4646
const source = j(file.source);
4747
const importType = getImportType(j, source);
4848

49+
if (importType === null) {
50+
// Skip transformation, since no import/require statements found for "aws-sdk" package.
51+
return file.source;
52+
}
53+
4954
addNotSupportedComments(j, source, importType);
5055

5156
const v2GlobalName = getGlobalNameFromModule(j, source);
5257
const v2ClientNamesRecord = getClientNamesRecord(j, source, importType);
5358

54-
if (!v2GlobalName && Object.keys(v2ClientNamesRecord).length === 0) {
55-
return file.source;
56-
}
57-
5859
if (v2GlobalName) {
5960
for (const v2ClientNameFromGlobal of getClientNamesFromGlobal(j, source, v2GlobalName)) {
6061
if (!(v2ClientNameFromGlobal in v2ClientNamesRecord)) {

0 commit comments

Comments
 (0)