Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip transformation if "aws-sdk" import is not present #779

Merged
merged 6 commits into from
Feb 26, 2024
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/silly-coats-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Skip transformation if "aws-sdk" import is not present
5 changes: 4 additions & 1 deletion src/transforms/v2-to-v3/modules/getImportType.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { hasImport } from "./hasImport";
import { hasImportEquals } from "./hasImportEquals";
import { hasRequire } from "./hasRequire";
import { ImportType } from "./types";

export const getImportType = (j: JSCodeshift, source: Collection<unknown>) =>
hasImport(j, source)
? ImportType.IMPORT
: hasImportEquals(j, source)
? ImportType.IMPORT_EQUALS
: ImportType.REQUIRE;
: hasRequire(j, source)
? ImportType.REQUIRE
: null;
3 changes: 2 additions & 1 deletion src/transforms/v2-to-v3/modules/hasImportEquals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { PACKAGE_NAME } from "../config";
import { getImportEqualsDeclarationType } from "./getImportEqualsDeclarationType";

export const hasImportEquals = (j: JSCodeshift, source: Collection<unknown>) =>
Expand All @@ -12,7 +13,7 @@ export const hasImportEquals = (j: JSCodeshift, source: Collection<unknown>) =>
return (
expression.type === "StringLiteral" &&
typeof expression.value === "string" &&
expression.value.startsWith("aws-sdk")
expression.value.startsWith(PACKAGE_NAME)
);
})
.size() > 0;
13 changes: 13 additions & 0 deletions src/transforms/v2-to-v3/modules/hasRequire.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Collection, JSCodeshift, Literal } from "jscodeshift";
import { PACKAGE_NAME } from "../config";

export const hasRequire = (j: JSCodeshift, source: Collection<unknown>) =>
source
.find(j.CallExpression, {
callee: { type: "Identifier", name: "require" },
})
.filter((callExpression) => {
const { value: sourceValue } = callExpression.value.arguments[0] as Literal;
return typeof sourceValue === "string" && sourceValue.startsWith(PACKAGE_NAME);
})
.size() > 0;
9 changes: 5 additions & 4 deletions src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ const transformer = async (file: FileInfo, api: API) => {
const source = j(file.source);
const importType = getImportType(j, source);

if (importType === null) {
// Skip transformation, since no import/require statements found for "aws-sdk" package.
return file.source;
}

addNotSupportedComments(j, source, importType);

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

if (!v2GlobalName && Object.keys(v2ClientNamesRecord).length === 0) {
return file.source;
}

if (v2GlobalName) {
for (const v2ClientNameFromGlobal of getClientNamesFromGlobal(j, source, v2GlobalName)) {
if (!(v2ClientNameFromGlobal in v2ClientNamesRecord)) {
Expand Down