Skip to content

Commit 25559fe

Browse files
authored
Use default imports for types (#276)
1 parent 38920b3 commit 25559fe

22 files changed

+356
-252
lines changed

.changeset/tough-chicken-rush.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": minor
3+
---
4+
5+
Use default imports for types
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { DynamoDB, ListTablesCommandInput, ListTablesCommandOutput } from "@aws-sdk/client-dynamodb";
2-
import { STS, GetCallerIdentityCommandInput, GetCallerIdentityCommandOutput } from "@aws-sdk/client-sts";
1+
import AWS_DynamoDB, { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
import AWS_STS, { STS } from "@aws-sdk/client-sts";
33

44
const ddbClient = new DynamoDB({ region: "us-west-2" });
5-
const listTablesInput: ListTablesCommandInput = { Limit: 10 };
6-
const listTablesOutput: ListTablesCommandOutput = await ddbClient
5+
const listTablesInput: AWS_DynamoDB.ListTablesCommandInput = { Limit: 10 };
6+
const listTablesOutput: AWS_DynamoDB.ListTablesCommandOutput = await ddbClient
77
.listTables(listTablesInput);
88

99
const stsClient = new STS({ region: "us-west-2" });
10-
const getCallerIdentityInput: GetCallerIdentityCommandInput = {};
11-
const getCallerIdentityOutput: GetCallerIdentityCommandOutput = await stsClient
10+
const getCallerIdentityInput: AWS_STS.GetCallerIdentityCommandInput = {};
11+
const getCallerIdentityOutput: AWS_STS.GetCallerIdentityCommandOutput = await stsClient
1212
.getCallerIdentity(getCallerIdentityInput);
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
const {
2-
DynamoDB,
3-
ListTablesCommandInput,
4-
ListTablesCommandOutput
5-
} = require("@aws-sdk/client-dynamodb"),
1+
const AWS_DynamoDB = require("@aws-sdk/client-dynamodb"),
62
{
7-
STS,
8-
GetCallerIdentityCommandInput,
9-
GetCallerIdentityCommandOutput
10-
} = require("@aws-sdk/client-sts");
3+
DynamoDB
4+
} = AWS_DynamoDB,
5+
AWS_STS = require("@aws-sdk/client-sts"),
6+
{
7+
STS
8+
} = AWS_STS;
119

1210
const ddbClient = new DynamoDB({ region: "us-west-2" });
13-
const listTablesInput: ListTablesCommandInput = { Limit: 10 };
14-
const listTablesOutput: ListTablesCommandOutput = await ddbClient
11+
const listTablesInput: AWS_DynamoDB.ListTablesCommandInput = { Limit: 10 };
12+
const listTablesOutput: AWS_DynamoDB.ListTablesCommandOutput = await ddbClient
1513
.listTables(listTablesInput);
1614

1715
const stsClient = new STS({ region: "us-west-2" });
18-
const getCallerIdentityInput: GetCallerIdentityCommandInput = {};
19-
const getCallerIdentityOutput: GetCallerIdentityCommandOutput = await stsClient
16+
const getCallerIdentityInput: AWS_STS.GetCallerIdentityCommandInput = {};
17+
const getCallerIdentityOutput: AWS_STS.GetCallerIdentityCommandOutput = await stsClient
2018
.getCallerIdentity(getCallerIdentityInput);
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { DynamoDB, ListTablesCommandInput, ListTablesCommandOutput } from "@aws-sdk/client-dynamodb";
2-
import { STS, GetCallerIdentityCommandInput, GetCallerIdentityCommandOutput } from "@aws-sdk/client-sts";
1+
import AWS_DynamoDB, { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
import AWS_STS, { STS } from "@aws-sdk/client-sts";
33

44
const ddbClient = new DynamoDB({ region: "us-west-2" });
5-
const listTablesInput: ListTablesCommandInput = { Limit: 10 };
6-
const listTablesOutput: ListTablesCommandOutput = await ddbClient
5+
const listTablesInput: AWS_DynamoDB.ListTablesCommandInput = { Limit: 10 };
6+
const listTablesOutput: AWS_DynamoDB.ListTablesCommandOutput = await ddbClient
77
.listTables(listTablesInput);
88

99
const stsClient = new STS({ region: "us-west-2" });
10-
const getCallerIdentityInput: GetCallerIdentityCommandInput = {};
11-
const getCallerIdentityOutput: GetCallerIdentityCommandOutput = await stsClient
10+
const getCallerIdentityInput: AWS_STS.GetCallerIdentityCommandInput = {};
11+
const getCallerIdentityOutput: AWS_STS.GetCallerIdentityCommandOutput = await stsClient
1212
.getCallerIdentity(getCallerIdentityInput);
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { DynamoDB, ListTablesCommandInput, ListTablesCommandOutput } from "@aws-sdk/client-dynamodb";
2-
import { STS, GetCallerIdentityCommandInput, GetCallerIdentityCommandOutput } from "@aws-sdk/client-sts";
1+
import AWS_DynamoDB, { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
import AWS_STS, { STS } from "@aws-sdk/client-sts";
33

44
const ddbClient = new DynamoDB({ region: "us-west-2" });
5-
const listTablesInput: ListTablesCommandInput = { Limit: 10 };
6-
const listTablesOutput: ListTablesCommandOutput = await ddbClient
5+
const listTablesInput: AWS_DynamoDB.ListTablesCommandInput = { Limit: 10 };
6+
const listTablesOutput: AWS_DynamoDB.ListTablesCommandOutput = await ddbClient
77
.listTables(listTablesInput);
88

99
const stsClient = new STS({ region: "us-west-2" });
10-
const getCallerIdentityInput: GetCallerIdentityCommandInput = {};
11-
const getCallerIdentityOutput: GetCallerIdentityCommandOutput = await stsClient
10+
const getCallerIdentityInput: AWS_STS.GetCallerIdentityCommandInput = {};
11+
const getCallerIdentityOutput: AWS_STS.GetCallerIdentityCommandOutput = await stsClient
1212
.getCallerIdentity(getCallerIdentityInput);
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
const {
2-
DynamoDB,
3-
ListTablesCommandInput,
4-
ListTablesCommandOutput
5-
} = require("@aws-sdk/client-dynamodb");
6-
const {
7-
STS,
8-
GetCallerIdentityCommandInput,
9-
GetCallerIdentityCommandOutput
10-
} = require("@aws-sdk/client-sts");
1+
const AWS_DynamoDB = require("@aws-sdk/client-dynamodb"),
2+
{
3+
DynamoDB
4+
} = AWS_DynamoDB;
5+
const AWS_STS = require("@aws-sdk/client-sts"),
6+
{
7+
STS
8+
} = AWS_STS;
119

1210
const ddbClient = new DynamoDB({ region: "us-west-2" });
13-
const listTablesInput: ListTablesCommandInput = { Limit: 10 };
14-
const listTablesOutput: ListTablesCommandOutput = await ddbClient
11+
const listTablesInput: AWS_DynamoDB.ListTablesCommandInput = { Limit: 10 };
12+
const listTablesOutput: AWS_DynamoDB.ListTablesCommandOutput = await ddbClient
1513
.listTables(listTablesInput);
1614

1715
const stsClient = new STS({ region: "us-west-2" });
18-
const getCallerIdentityInput: GetCallerIdentityCommandInput = {};
19-
const getCallerIdentityOutput: GetCallerIdentityCommandOutput = await stsClient
16+
const getCallerIdentityInput: AWS_STS.GetCallerIdentityCommandInput = {};
17+
const getCallerIdentityOutput: AWS_STS.GetCallerIdentityCommandOutput = await stsClient
2018
.getCallerIdentity(getCallerIdentityInput);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Collection, ImportDefaultSpecifier, JSCodeshift } from "jscodeshift";
2+
3+
import { getV3ClientDefaultLocalName } from "../utils";
4+
import { V3ClientModulesOptions } from "./types";
5+
6+
export const addV3ClientDefaultImport = (
7+
j: JSCodeshift,
8+
source: Collection<unknown>,
9+
{ v2ClientLocalName, v3ClientPackageName }: V3ClientModulesOptions
10+
) => {
11+
const localName = getV3ClientDefaultLocalName(v2ClientLocalName);
12+
const existingImports = source.find(j.ImportDeclaration, {
13+
source: { value: v3ClientPackageName },
14+
});
15+
16+
const existingImportDefaultSpecifiers = existingImports
17+
.nodes()
18+
.map((importDeclaration) => importDeclaration.specifiers)
19+
.flat()
20+
.filter(
21+
(importSpecifier) => importSpecifier?.type === "ImportDefaultSpecifier"
22+
) as ImportDefaultSpecifier[];
23+
24+
if (!existingImportDefaultSpecifiers.find((specifier) => specifier?.local?.name === localName)) {
25+
existingImports.nodes()[0].specifiers?.push(j.importDefaultSpecifier(j.identifier(localName)));
26+
}
27+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
import { getV3ClientDefaultLocalName } from "../utils";
4+
import { getRequireVariableDeclarators } from "./getRequireVariableDeclarators";
5+
import { getV2RequireDeclarator } from "./getV2RequireDeclarator";
6+
import { V3ClientModulesOptions } from "./types";
7+
8+
export const addV3ClientDefaultRequire = (
9+
j: JSCodeshift,
10+
source: Collection<unknown>,
11+
{ v2ClientName, v2ClientLocalName, v3ClientPackageName, v2GlobalName }: V3ClientModulesOptions
12+
) => {
13+
const identifierName = getV3ClientDefaultLocalName(v2ClientLocalName);
14+
const existingRequires = getRequireVariableDeclarators(j, source, v3ClientPackageName);
15+
16+
if (
17+
existingRequires &&
18+
existingRequires.nodes().length > 0 &&
19+
existingRequires
20+
.nodes()
21+
.find(
22+
(variableDeclarator) =>
23+
variableDeclarator.id.type === "Identifier" &&
24+
variableDeclarator.id.name === identifierName
25+
)
26+
) {
27+
return;
28+
}
29+
30+
// prettier-ignore
31+
const v2RequireDeclarator =
32+
getV2RequireDeclarator(j, source, { v2ClientName, v2ClientLocalName, v2GlobalName });
33+
34+
const requireDeclarator = j.variableDeclarator(
35+
j.identifier(identifierName),
36+
j.callExpression(j.identifier("require"), [j.literal(v3ClientPackageName)])
37+
);
38+
39+
if (v2RequireDeclarator && v2RequireDeclarator.nodes().length > 0) {
40+
v2RequireDeclarator.insertAfter(requireDeclarator);
41+
} else {
42+
// Insert at the top of the file.
43+
source.insertBefore(requireDeclarator);
44+
}
45+
};
Lines changed: 7 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,22 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
22

3-
import { PACKAGE_NAME } from "../config";
43
import { getV3ClientTypeNames } from "../ts-type";
5-
import { getV2ServiceModulePath } from "../utils";
6-
import { addV3ClientModuleImport } from "./addV3ClientModuleImport";
7-
import { getV3ClientImportSpecifier } from "./getV3ClientImportSpecifier";
4+
import { addV3ClientDefaultImport } from "./addV3ClientDefaultImport";
5+
import { addV3ClientNamedImport } from "./addV3ClientNamedImport";
86
import { V3ClientModulesOptions } from "./types";
97

108
export const addV3ClientImports = (
119
j: JSCodeshift,
1210
source: Collection<unknown>,
13-
{
14-
v2ClientName,
15-
v2ClientLocalName,
16-
v2GlobalName,
17-
v3ClientName,
18-
v3ClientPackageName,
19-
}: V3ClientModulesOptions
11+
options: V3ClientModulesOptions
2012
): void => {
21-
const existingImports = source.find(j.ImportDeclaration, {
22-
source: { value: v3ClientPackageName },
23-
});
13+
addV3ClientNamedImport(j, source, options);
2414

25-
// Import declaration already exists.
26-
if (existingImports.size()) {
27-
addV3ClientModuleImport(j, existingImports, {
28-
localName: v2ClientLocalName,
29-
importedName: v3ClientName,
30-
});
31-
} else {
32-
// Insert after global import, or service import.
33-
source
34-
.find(j.ImportDeclaration)
35-
.filter((importDeclaration) => {
36-
const sourceValue = importDeclaration.value.source.value as string;
37-
38-
if (
39-
sourceValue === PACKAGE_NAME &&
40-
importDeclaration.value.specifiers?.some(
41-
(specifier) =>
42-
["ImportNamespaceSpecifier", "ImportDefaultSpecifier"].includes(specifier.type) ||
43-
(specifier.type === "ImportSpecifier" && specifier.local?.name === v2ClientLocalName)
44-
)
45-
) {
46-
return true;
47-
}
48-
49-
if (sourceValue === getV2ServiceModulePath(v2ClientName)) {
50-
return true;
51-
}
52-
53-
return false;
54-
})
55-
.at(0)
56-
.insertAfter(
57-
j.importDeclaration(
58-
[
59-
getV3ClientImportSpecifier(j, {
60-
localName: v2ClientLocalName,
61-
importedName: v3ClientName,
62-
}),
63-
],
64-
j.stringLiteral(v3ClientPackageName)
65-
)
66-
);
67-
}
68-
69-
// Add require for input/output types, if needed.
15+
const { v2ClientName, v2GlobalName } = options;
7016
const v3ClientTypeNames = getV3ClientTypeNames(j, source, { v2ClientName, v2GlobalName });
7117

18+
// Add default import for types, if needed.
7219
if (v3ClientTypeNames.length > 0) {
73-
const clientImports = source.find(j.ImportDeclaration, {
74-
source: { value: v3ClientPackageName },
75-
});
76-
for (const v3ClientTypeName of v3ClientTypeNames.sort()) {
77-
addV3ClientModuleImport(j, clientImports, {
78-
localName: v3ClientTypeName,
79-
importedName: v3ClientTypeName,
80-
});
81-
}
20+
addV3ClientDefaultImport(j, source, options);
8221
}
8322
};

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

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)