-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support requires with MemberExpression property (#340)
- Loading branch information
Showing
12 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"aws-sdk-js-codemod": patch | ||
--- | ||
|
||
Support requires with MemberExpression property |
14 changes: 14 additions & 0 deletions
14
scripts/generateNewClientTests/getGlobalRequirePropertyInput.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { CLIENTS_TO_TEST } from "./config"; | ||
import { getV2ClientsNewExpressionCode } from "./getV2ClientsNewExpressionCode"; | ||
|
||
export const getGlobalRequirePropertyInput = (codegenComment: string) => { | ||
let globalRequireInputContent = `${codegenComment}\n`; | ||
|
||
for (const clientName of CLIENTS_TO_TEST) { | ||
globalRequireInputContent += `const ${clientName} = require("aws-sdk").${clientName};\n`; | ||
} | ||
globalRequireInputContent += `\n`; | ||
globalRequireInputContent += getV2ClientsNewExpressionCode(CLIENTS_TO_TEST); | ||
|
||
return globalRequireInputContent; | ||
}; |
3 changes: 3 additions & 0 deletions
3
scripts/generateNewClientTests/getGlobalRequirePropertyOutput.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { getServiceRequireDeepOutput } from "./getServiceRequireDeepOutput"; | ||
|
||
export const getGlobalRequirePropertyOutput = getServiceRequireDeepOutput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// This file is generated by scripts/generateNewClientTests/index.ts | ||
// Do not edit this file directly. Instead, edit the script and run it to regenerate this file. | ||
"use strict"; | ||
const ACM = require("aws-sdk").ACM; | ||
const AccessAnalyzer = require("aws-sdk").AccessAnalyzer; | ||
const Discovery = require("aws-sdk").Discovery; | ||
|
||
new ACM(); | ||
new AccessAnalyzer(); | ||
new Discovery(); |
16 changes: 16 additions & 0 deletions
16
src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// This file is generated by scripts/generateNewClientTests/index.ts | ||
// Do not edit this file directly. Instead, edit the script and run it to regenerate this file. | ||
"use strict"; | ||
const { | ||
ACM | ||
} = require("@aws-sdk/client-acm"); | ||
const { | ||
AccessAnalyzer | ||
} = require("@aws-sdk/client-accessanalyzer"); | ||
const { | ||
ApplicationDiscoveryService: Discovery | ||
} = require("@aws-sdk/client-application-discovery-service"); | ||
|
||
new ACM(); | ||
new AccessAnalyzer(); | ||
new Discovery(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/transforms/v2-to-v3/modules/getRequireDeclaratorsWithProperty.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
export interface GetRequireDeclaratorsWithPropertyOptions { | ||
localName?: string; | ||
identifierName?: string; | ||
sourceValue: string; | ||
} | ||
|
||
export const getRequireDeclaratorsWithProperty = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
{ localName, identifierName, sourceValue }: GetRequireDeclaratorsWithPropertyOptions | ||
) => | ||
source.find(j.VariableDeclarator, { | ||
id: { type: "Identifier", ...(localName && { name: localName }) }, | ||
init: { | ||
type: "MemberExpression", | ||
object: { | ||
type: "CallExpression", | ||
callee: { type: "Identifier", name: "require" }, | ||
arguments: [{ value: sourceValue }], | ||
}, | ||
property: { type: "Identifier", ...(identifierName && { name: identifierName }) }, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
import { getRequireDeclaratorsWithProperty } from "./getRequireDeclaratorsWithProperty"; | ||
|
||
export interface RemoveRequireObjectPropertyOptions { | ||
localName: string; | ||
propertyName: string; | ||
sourceValue: string; | ||
} | ||
|
||
export const removeRequireProperty = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
{ localName, propertyName, sourceValue }: RemoveRequireObjectPropertyOptions | ||
) => { | ||
const requireDeclarators = getRequireDeclaratorsWithProperty(j, source, { | ||
identifierName: propertyName, | ||
localName, | ||
sourceValue, | ||
}); | ||
|
||
requireDeclarators.forEach((varDeclarator) => { | ||
const varDeclarationCollection = j(varDeclarator).closest(j.VariableDeclaration); | ||
|
||
// Remove VariableDeclarator as it contains the only identifier. | ||
j(varDeclarator).remove(); | ||
|
||
// Remove VariableDeclaration if there are no declarations. | ||
const varDeclaration = varDeclarationCollection.nodes()[0]; | ||
if (varDeclaration && varDeclaration.declarations?.length === 0) { | ||
varDeclarationCollection.remove(); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters