Skip to content

Commit 8b09bb6

Browse files
authored
Support requires with MemberExpression property (#340)
1 parent 9982796 commit 8b09bb6

12 files changed

+144
-0
lines changed

.changeset/twelve-clouds-mix.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+
Support requires with MemberExpression property
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { CLIENTS_TO_TEST } from "./config";
2+
import { getV2ClientsNewExpressionCode } from "./getV2ClientsNewExpressionCode";
3+
4+
export const getGlobalRequirePropertyInput = (codegenComment: string) => {
5+
let globalRequireInputContent = `${codegenComment}\n`;
6+
7+
for (const clientName of CLIENTS_TO_TEST) {
8+
globalRequireInputContent += `const ${clientName} = require("aws-sdk").${clientName};\n`;
9+
}
10+
globalRequireInputContent += `\n`;
11+
globalRequireInputContent += getV2ClientsNewExpressionCode(CLIENTS_TO_TEST);
12+
13+
return globalRequireInputContent;
14+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { getServiceRequireDeepOutput } from "./getServiceRequireDeepOutput";
2+
3+
export const getGlobalRequirePropertyOutput = getServiceRequireDeepOutput;

scripts/generateNewClientTests/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { getGlobalImportInput } from "./getGlobalImportInput";
99
import { getGlobalImportOutput } from "./getGlobalImportOutput";
1010
import { getGlobalRequireInput } from "./getGlobalRequireInput";
1111
import { getGlobalRequireOutput } from "./getGlobalRequireOutput";
12+
import { getGlobalRequirePropertyInput } from "./getGlobalRequirePropertyInput";
13+
import { getGlobalRequirePropertyOutput } from "./getGlobalRequirePropertyOutput";
1214
import { getServiceImportDeepInput } from "./getServiceImportDeepInput";
1315
import { getServiceImportDeepOutput } from "./getServiceImportDeepOutput";
1416
import { getServiceImportEqualsInput } from "./getServiceImportEqualsInput";
@@ -42,6 +44,8 @@ const newClientTestsPath = join(__dirname, "..", "..", newClientsTestsFolder);
4244
["global-import-equals.output.ts", getGlobalImportEqualsOutput],
4345
["global-require.input.js", getGlobalRequireInput],
4446
["global-require.output.js", getGlobalRequireOutput],
47+
["global-require-property.input.js", getGlobalRequirePropertyInput],
48+
["global-require-property.output.js", getGlobalRequirePropertyOutput],
4549
["service-import.input.js", getServiceImportInput],
4650
["service-import.output.js", getServiceImportOutput],
4751
["service-import-equals.input.ts", getServiceImportEqualsInput],
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This file is generated by scripts/generateNewClientTests/index.ts
2+
// Do not edit this file directly. Instead, edit the script and run it to regenerate this file.
3+
"use strict";
4+
const ACM = require("aws-sdk").ACM;
5+
const AccessAnalyzer = require("aws-sdk").AccessAnalyzer;
6+
const Discovery = require("aws-sdk").Discovery;
7+
8+
new ACM();
9+
new AccessAnalyzer();
10+
new Discovery();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This file is generated by scripts/generateNewClientTests/index.ts
2+
// Do not edit this file directly. Instead, edit the script and run it to regenerate this file.
3+
"use strict";
4+
const {
5+
ACM
6+
} = require("@aws-sdk/client-acm");
7+
const {
8+
AccessAnalyzer
9+
} = require("@aws-sdk/client-accessanalyzer");
10+
const {
11+
ApplicationDiscoveryService: Discovery
12+
} = require("@aws-sdk/client-application-discovery-service");
13+
14+
new ACM();
15+
new AccessAnalyzer();
16+
new Discovery();

src/transforms/v2-to-v3/client-names/getV2ClientNamesRecordFromRequire.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Collection, Identifier, JSCodeshift, ObjectPattern, Property } from "jscodeshift";
22

33
import { CLIENT_NAMES, PACKAGE_NAME } from "../config";
4+
import { getRequireDeclaratorsWithProperty } from "../modules";
45
import { getV2ServiceModulePath } from "../utils";
56
import { getRequireIds } from "./getRequireIds";
67

@@ -33,6 +34,25 @@ export const getV2ClientNamesRecordFromRequire = (
3334
}
3435
}
3536

37+
const declaratorsWithProperty = getRequireDeclaratorsWithProperty(j, source, {
38+
sourceValue: PACKAGE_NAME,
39+
}).nodes();
40+
41+
for (const declaratorWithProperty of declaratorsWithProperty) {
42+
const { id, init } = declaratorWithProperty;
43+
if (
44+
id.type === "Identifier" &&
45+
init != undefined &&
46+
init.type === "MemberExpression" &&
47+
init.property.type === "Identifier"
48+
) {
49+
const v2ClientName = (init.property as Identifier).name;
50+
if (CLIENT_NAMES.includes(v2ClientName)) {
51+
v2ClientNamesRecord[v2ClientName] = (id as Identifier).name;
52+
}
53+
}
54+
}
55+
3656
for (const clientName of v2ClientNamesWithServiceModule) {
3757
const deepRequirePath = getV2ServiceModulePath(clientName);
3858
const idsFromDefaultImport = getRequireIds(j, source, deepRequirePath).filter(
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
export interface GetRequireDeclaratorsWithPropertyOptions {
4+
localName?: string;
5+
identifierName?: string;
6+
sourceValue: string;
7+
}
8+
9+
export const getRequireDeclaratorsWithProperty = (
10+
j: JSCodeshift,
11+
source: Collection<unknown>,
12+
{ localName, identifierName, sourceValue }: GetRequireDeclaratorsWithPropertyOptions
13+
) =>
14+
source.find(j.VariableDeclarator, {
15+
id: { type: "Identifier", ...(localName && { name: localName }) },
16+
init: {
17+
type: "MemberExpression",
18+
object: {
19+
type: "CallExpression",
20+
callee: { type: "Identifier", name: "require" },
21+
arguments: [{ value: sourceValue }],
22+
},
23+
property: { type: "Identifier", ...(identifierName && { name: identifierName }) },
24+
},
25+
});

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { PACKAGE_NAME } from "../config";
44
import { getV2ServiceModulePath } from "../utils";
55
import { getRequireDeclaratorsWithIdentifier } from "./getRequireDeclaratorsWithIdentifier";
66
import { getRequireDeclaratorsWithObjectPattern } from "./getRequireDeclaratorsWithObjectPattern";
7+
import { getRequireDeclaratorsWithProperty } from "./getRequireDeclaratorsWithProperty";
78

89
export interface GetV2BaseDeclaratorOptions {
910
v2ClientName: string;
@@ -36,6 +37,15 @@ export const getV2RequireDeclarator = (
3637
return requireDeclaratorsWithObjectPattern;
3738
}
3839

40+
const requireDeclaratorsWithProperty = getRequireDeclaratorsWithProperty(j, source, {
41+
identifierName: v2ClientName,
42+
sourceValue: PACKAGE_NAME,
43+
});
44+
45+
if (requireDeclaratorsWithProperty.size() > 0) {
46+
return requireDeclaratorsWithProperty;
47+
}
48+
3949
const v2ServiceModulePath = getV2ServiceModulePath(v2ClientName);
4050
const requireDeclaratorsWithIdentifier = getRequireDeclaratorsWithIdentifier(j, source, {
4151
identifierName: v2ClientLocalName,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from "./addV3ClientModules";
22
export * from "./getImportEqualsDeclaration";
33
export * from "./getImportSpecifiers";
4+
export * from "./getRequireDeclaratorsWithProperty";
45
export * from "./getV2GlobalNameFromModule";
56
export * from "./hasRequire";
67
export * from "./removeV2ClientModule";

0 commit comments

Comments
 (0)