Skip to content

Commit

Permalink
Add utility importModule getImportSpecifiers (#786)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Feb 29, 2024
1 parent 10ba465 commit d8249e8
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 67 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-stingrays-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

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

import { CLIENT_NAMES, PACKAGE_NAME } from "../config";
import { getImportEqualsDeclarationType, getImportSpecifiers } from "../modules";
import {
ImportSpecifierDefault,
ImportSpecifierPattern,
getImportEqualsDeclarationType,
} from "../modules";
import { getImportSpecifiers } from "../modules/importModule";
import { getClientDeepImportPath } from "../utils";

export const getClientNamesRecordFromImport = (
Expand All @@ -12,12 +17,10 @@ export const getClientNamesRecordFromImport = (
const clientNamesRecord: Record<string, string> = {};

const specifiersFromNamedImport = getImportSpecifiers(j, source, PACKAGE_NAME).filter(
(specifier) => specifier?.type === "ImportSpecifier"
) as ImportSpecifier[];
(importSpecifier) => typeof importSpecifier === "object"
) as ImportSpecifierPattern[];

for (const specifier of specifiersFromNamedImport) {
const importedName = specifier.imported.name;
const localName = (specifier.local as Identifier).name;
for (const { importedName, localName } of specifiersFromNamedImport) {
if (CLIENT_NAMES.includes(importedName)) {
clientNamesRecord[importedName] = localName ?? importedName;
}
Expand All @@ -27,11 +30,10 @@ export const getClientNamesRecordFromImport = (
const deepImportPath = getClientDeepImportPath(clientName);

const specifiersFromDeepImport = getImportSpecifiers(j, source, deepImportPath).filter(
(specifier) =>
["ImportDefaultSpecifier", "ImportNamespaceSpecifier"].includes(specifier?.type as string)
);
(importSpecifier) => typeof importSpecifier === "string"
) as ImportSpecifierDefault[];
if (specifiersFromDeepImport.length > 0) {
clientNamesRecord[clientName] = (specifiersFromDeepImport[0]?.local as Identifier).name;
clientNamesRecord[clientName] = specifiersFromDeepImport[0];
}

const identifiersFromImportEquals = source.find(
Expand Down
11 changes: 6 additions & 5 deletions src/transforms/v2-to-v3/modules/getGlobalNameFromModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Collection, Identifier, JSCodeshift } from "jscodeshift";

import { PACKAGE_NAME } from "../config";
import { getImportEqualsDeclarationType } from "./getImportEqualsDeclarationType";
import { getImportSpecifiers } from "./getImportSpecifiers";
import { getImportSpecifiers } from "./importModule";
import { getRequireDeclarators } from "./requireModule";
import { ImportSpecifierDefault } from ".";

export const getGlobalNameFromModule = (
j: JSCodeshift,
Expand All @@ -17,12 +18,12 @@ export const getGlobalNameFromModule = (
return (requireIdentifiers[0]?.id as Identifier).name;
}

const importDefaultSpecifiers = getImportSpecifiers(j, source, PACKAGE_NAME).filter((specifier) =>
["ImportDefaultSpecifier", "ImportNamespaceSpecifier"].includes(specifier?.type as string)
);
const importDefaultSpecifiers = getImportSpecifiers(j, source, PACKAGE_NAME).filter(
(importSpecifier) => typeof importSpecifier === "string"
) as ImportSpecifierDefault[];

if (importDefaultSpecifiers.length > 0) {
return (importDefaultSpecifiers[0]?.local as Identifier).name;
return importDefaultSpecifiers[0];
}

const importEqualsDeclarations = source.find(
Expand Down
21 changes: 0 additions & 21 deletions src/transforms/v2-to-v3/modules/getImportSpecifiers.ts

This file was deleted.

34 changes: 13 additions & 21 deletions src/transforms/v2-to-v3/modules/importModule/addNamedModule.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Collection, ImportSpecifier, JSCodeshift } from "jscodeshift";
import { Collection, JSCodeshift } from "jscodeshift";

import { PACKAGE_NAME } from "../../config";
import { getImportSpecifiers } from "../getImportSpecifiers";
import { getImportDeclarations, getImportSpecifiers } from "../importModule";
import { importSpecifierCompareFn } from "../importSpecifierCompareFn";
import { ModulesOptions } from "../types";

Expand All @@ -12,28 +11,24 @@ export const addNamedModule = (
) => {
const { importedName, localName = importedName, packageName } = options;

const importDeclarations = source.find(j.ImportDeclaration, {
source: { value: packageName },
});

if (importDeclarations.size()) {
const importSpecifiers = getImportSpecifiers(j, source, packageName);
const importSpecifiers = getImportSpecifiers(j, source, packageName);

if (importSpecifiers.length > 0) {
// Return if the import specifier already exists.
if (
importSpecifiers
.filter((importSpecifier) => importSpecifier?.type === "ImportSpecifier")
.find(
(specifier) =>
(specifier as ImportSpecifier)?.imported?.name === importedName &&
specifier?.local?.name === localName
)
importSpecifiers.find(
(specifier) =>
typeof specifier === "object" &&
specifier.importedName === importedName &&
specifier.localName === localName
)
) {
return;
}

// Add named import to the first import declaration.
const firstImportDeclSpecifiers = importDeclarations.nodes()[0].specifiers;
const firstImportDeclSpecifiers = getImportDeclarations(j, source, packageName).nodes()[0]
.specifiers;
if (firstImportDeclSpecifiers) {
firstImportDeclSpecifiers.push(
j.importSpecifier(j.identifier(importedName), j.identifier(localName))
Expand All @@ -49,10 +44,7 @@ export const addNamedModule = (
j.stringLiteral(packageName)
);

const v2importDeclarations = source.find(j.ImportDeclaration).filter((path) => {
const { value } = path.node.source;
return typeof value === "string" && value.startsWith(PACKAGE_NAME);
});
const v2importDeclarations = getImportDeclarations(j, source);

if (v2importDeclarations.size()) {
// Insert it after the last import declaration.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { PACKAGE_NAME } from "../../config";

export const getImportDeclarations = (j: JSCodeshift, source: Collection<unknown>, path?: string) =>
source.find(j.ImportDeclaration).filter((importDeclaration) => {
const sourceValue = importDeclaration.value.source.value;
if (typeof sourceValue !== "string") {
return false;
}
if (path) {
return sourceValue === path;
}
return sourceValue.startsWith(PACKAGE_NAME);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { ImportSpecifierType } from "../types";
import { getImportDeclarations } from "./getImportDeclarations";

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

getImportDeclarations(j, source, path).forEach((importDeclaration) => {
const specifiers = importDeclaration.value.specifiers || [];
for (const specifier of specifiers) {
switch (specifier.type) {
case "ImportSpecifier": {
const importedName = specifier.imported.name;
importSpecifiers.add({
importedName,
localName: specifier.local!.name || importedName,
});
break;
}
case "ImportNamespaceSpecifier":
case "ImportDefaultSpecifier": {
if (specifier.local) {
importSpecifiers.add(specifier.local.name);
}
break;
}
}
}
});

return Array.from(importSpecifiers);
};
2 changes: 2 additions & 0 deletions src/transforms/v2-to-v3/modules/importModule/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./addNamedModule";
export * from "./getImportSpecifiers";
export * from "./getImportDeclarations";
1 change: 0 additions & 1 deletion src/transforms/v2-to-v3/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export * from "./addClientModules";
export * from "./addNamedModule";
export * from "./getGlobalNameFromModule";
export * from "./getImportEqualsDeclarationType";
export * from "./getImportSpecifiers";
export * from "./getImportType";
export * from "./getRequireDeclaratorsWithProperty";
export * from "./removeClientModule";
Expand Down
12 changes: 4 additions & 8 deletions src/transforms/v2-to-v3/ts-type/getClientTypeNames.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Collection, Identifier, JSCodeshift, TSQualifiedName, TSTypeReference } from "jscodeshift";

import { getImportSpecifiers } from "../modules";
import { ImportSpecifierPattern } from "../modules";
import { getImportSpecifiers } from "../modules/importModule";
import { getClientDeepImportPath } from "../utils";

export interface GetClientTypeNamesOptions {
Expand Down Expand Up @@ -76,13 +77,8 @@ export const getClientTypeNames = (

clientTypeNames.push(
...getImportSpecifiers(j, source, getClientDeepImportPath(v2ClientName))
.filter(
(importSpecifier) =>
importSpecifier.type === "ImportSpecifier" &&
importSpecifier.local &&
importSpecifier.local.type === "Identifier"
)
.map((importSpecifier) => (importSpecifier.local as Identifier).name)
.filter((importSpecifier) => typeof importSpecifier === "object")
.map((importSpecifier) => (importSpecifier as ImportSpecifierPattern).localName!)
);

return [...new Set(clientTypeNames)];
Expand Down

0 comments on commit d8249e8

Please sign in to comment.