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

Add utility importEqualsModule getImportSpecifiers #790

Merged
merged 13 commits into from
Feb 29, 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/gorgeous-bees-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Add utility importEqualsModule getImportSpecifiers
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { CLIENT_NAMES, PACKAGE_NAME } from "../config";
import { getImportEqualsDeclarationType } from "../modules";
import { getImportSpecifiers as getImportEqualsSpecifiers } from "../modules/importEqualsModule";
import { getImportSpecifiers } from "../modules/importModule";
import { getClientDeepImportPath } from "../utils";

Expand Down Expand Up @@ -33,12 +33,9 @@ export const getClientNamesRecordFromImport = (
clientNamesRecord[clientName] = specifiersFromDeepImport[0].localName;
}

const identifiersFromImportEquals = source.find(
j.TSImportEqualsDeclaration,
getImportEqualsDeclarationType(deepImportPath)
);
const identifiersFromImportEquals = getImportEqualsSpecifiers(j, source, deepImportPath);
if (identifiersFromImportEquals.length > 0) {
clientNamesRecord[clientName] = identifiersFromImportEquals.nodes()[0]?.id.name;
clientNamesRecord[clientName] = identifiersFromImportEquals[0].localName;
}
}

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

import { PACKAGE_NAME } from "../config";
import { getImportEqualsDeclarationType } from "./getImportEqualsDeclarationType";
import { getImportSpecifiers as getImportEqualsSpecifiers } from "../modules/importEqualsModule";
import { getImportSpecifiers } from "./importModule";
import { getRequireDeclarators } from "./requireModule";

Expand All @@ -25,13 +25,10 @@ export const getGlobalNameFromModule = (
return importDefaultSpecifiers[0].localName;
}

const importEqualsDeclarations = source.find(
j.TSImportEqualsDeclaration,
getImportEqualsDeclarationType(PACKAGE_NAME)
);
const importEqualsDeclarations = getImportEqualsSpecifiers(j, source, PACKAGE_NAME);

if (importEqualsDeclarations.length > 0) {
return importEqualsDeclarations.nodes()[0]?.id?.name;
return importEqualsDeclarations[0].localName;
}

return undefined;
Expand Down

This file was deleted.

18 changes: 2 additions & 16 deletions src/transforms/v2-to-v3/modules/hasImportEquals.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
import { Collection, JSCodeshift } from "jscodeshift";

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

export const hasImportEquals = (j: JSCodeshift, source: Collection<unknown>) =>
source
.find(j.TSImportEqualsDeclaration, getImportEqualsDeclarationType())
.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(PACKAGE_NAME)
);
})
.size() > 0;
getImportEqualsDeclarations(j, source).size() > 0;
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { PACKAGE_NAME } from "../../config";
import { getImportEqualsDeclarationType } from "../getImportEqualsDeclarationType";
import { getImportEqualsDeclarations, getImportSpecifiers } from "../importEqualsModule";
import { getDefaultName } from "./getDefaultName";

export const addDefaultModule = (
Expand All @@ -10,16 +9,11 @@ export const addDefaultModule = (
packageName: string
) => {
const defaultLocalName = getDefaultName(packageName);
const existingImportEquals = source.find(
j.TSImportEqualsDeclaration,
getImportEqualsDeclarationType(packageName)
);
const existingImportEquals = getImportSpecifiers(j, source, packageName);

if (existingImportEquals.size()) {
if (existingImportEquals.length > 0) {
if (
existingImportEquals
.nodes()
.some((importEqualsDeclaration) => importEqualsDeclaration.id.name === defaultLocalName)
existingImportEquals.some((importSpecifier) => importSpecifier.localName === defaultLocalName)
) {
return;
}
Expand All @@ -31,16 +25,7 @@ export const addDefaultModule = (
j.tsExternalModuleReference(j.stringLiteral(packageName))
);

const v2ImportEquals = source
.find(j.TSImportEqualsDeclaration, getImportEqualsDeclarationType())
.filter((importEqualsDeclaration) => {
const { moduleReference } = importEqualsDeclaration.value;
if (moduleReference.type !== "TSExternalModuleReference") return false;
const { expression } = moduleReference;
if (expression.type !== "StringLiteral") return false;
const { value } = expression;
return value.startsWith(PACKAGE_NAME);
});
const v2ImportEquals = getImportEqualsDeclarations(j, source);

if (v2ImportEquals.size()) {
// Insert it after the first import equals declaration.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { getImportEqualsDeclarationType } from "../getImportEqualsDeclarationType";
import { ModulesOptions } from "../types";
import { addDefaultModule } from "./addDefaultModule";
import { getDefaultName } from "./getDefaultName";
import { getImportEqualsDeclarations } from "./getImportEqualsDeclarations";
import { getImportSpecifiers } from "./getImportSpecifiers";

export const addNamedModule = (
j: JSCodeshift,
Expand All @@ -16,29 +17,19 @@ export const addNamedModule = (

const existingDeclaration = source.find(j.TSImportEqualsDeclaration, {
type: "TSImportEqualsDeclaration",
id: {
type: "Identifier",
name: localName,
},
id: { type: "Identifier", name: localName },
moduleReference: {
type: "TSQualifiedName",
left: {
type: "Identifier",
name: defaultLocalName,
},
right: {
type: "Identifier",
name: importedName,
},
left: { type: "Identifier", name: defaultLocalName },
right: { type: "Identifier", name: importedName },
},
});

if (existingDeclaration.size()) {
return;
}

const defaultDeclaration = getImportEqualsDeclarationType(packageName);
if (source.find(j.TSImportEqualsDeclaration, defaultDeclaration).size() === 0) {
if (getImportSpecifiers(j, source, packageName).length === 0) {
addDefaultModule(j, source, packageName);
}

Expand All @@ -47,11 +38,9 @@ export const addNamedModule = (
j.tsQualifiedName(j.identifier(defaultLocalName), j.identifier(importedName))
);

const v3ClientImportEquals = source
.find(j.TSImportEqualsDeclaration, defaultDeclaration)
.filter(
(importEqualsDeclaration) => importEqualsDeclaration.value.id.name === defaultLocalName
);
const v3ClientImportEquals = getImportEqualsDeclarations(j, source, packageName).filter(
(importEqualsDeclaration) => importEqualsDeclaration.value.id.name === defaultLocalName
);

// Insert import equals after the package import equals.
if (v3ClientImportEquals.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Collection, JSCodeshift, StringLiteral, TSExternalModuleReference } from "jscodeshift";
import { PACKAGE_NAME } from "../../config";

export const getImportEqualsDeclarations = (
j: JSCodeshift,
source: Collection<unknown>,
path?: string
) =>
source
.find(j.TSImportEqualsDeclaration, {
type: "TSImportEqualsDeclaration",
moduleReference: {
type: "TSExternalModuleReference",
expression: { type: "StringLiteral" },
},
})
.filter((importEqualsDeclaration) => {
const moduleReference = importEqualsDeclaration.value
.moduleReference as TSExternalModuleReference;
const expressionValue = (moduleReference.expression as StringLiteral).value;
if (path) {
return expressionValue === path;
}
return expressionValue.startsWith(PACKAGE_NAME);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { ImportSpecifierType } from "../types";
import { getImportEqualsDeclarations } from "./getImportEqualsDeclarations";

export const getImportSpecifiers = (
j: JSCodeshift,
source: Collection<unknown>,
path?: string
): ImportSpecifierType[] => {
const importSpecifiers = new Set<ImportSpecifierType>();

getImportEqualsDeclarations(j, source, path).forEach((importEqualsDeclaration) => {
importSpecifiers.add({ localName: importEqualsDeclaration.value.id.name });
});

return Array.from(importSpecifiers);
};
2 changes: 2 additions & 0 deletions src/transforms/v2-to-v3/modules/importEqualsModule/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./addNamedModule";
export * from "./getImportSpecifiers";
export * from "./getImportEqualsDeclarations";
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ export const addNamedModule = (
// Return if the import specifier already exists.
if (
importSpecifiers.find(
(specifier) =>
typeof specifier === "object" &&
specifier.importedName === importedName &&
specifier.localName === localName
(specifier) => specifier.importedName === importedName && specifier.localName === localName
)
) {
return;
Expand Down
1 change: 0 additions & 1 deletion src/transforms/v2-to-v3/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from "./addClientModules";
export * from "./addNamedModule";
export * from "./getGlobalNameFromModule";
export * from "./getImportEqualsDeclarationType";
export * from "./getImportType";
export * from "./getRequireDeclaratorsWithProperty";
export * from "./removeClientModule";
Expand Down
12 changes: 4 additions & 8 deletions src/transforms/v2-to-v3/modules/removeImportEquals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { getImportEqualsDeclarations } from "./importEqualsModule";
import { removeDeclaration } from "./removeDeclaration";

export interface RemoveImportEqualsOptions {
Expand All @@ -11,14 +12,9 @@ export const removeImportEquals = (
source: Collection<unknown>,
{ localName, sourceValue }: RemoveImportEqualsOptions
) => {
const importEqualsDeclaration = source.find(j.TSImportEqualsDeclaration, {
type: "TSImportEqualsDeclaration",
id: { name: localName },
moduleReference: {
type: "TSExternalModuleReference",
expression: { type: "StringLiteral", value: sourceValue },
},
});
const importEqualsDeclaration = getImportEqualsDeclarations(j, source, sourceValue).filter(
(importEqualsDeclaration) => importEqualsDeclaration.value.id.name === localName
);
if (importEqualsDeclaration.length) {
removeDeclaration(j, source, importEqualsDeclaration.get());
}
Expand Down