-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support ImportEqualsDeclaration (#283)
- Loading branch information
Showing
22 changed files
with
319 additions
and
3 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 | ||
--- | ||
|
||
Add support for ImportEqualsDeclaration |
11 changes: 11 additions & 0 deletions
11
scripts/generateNewClientTests/getGlobalImportEqualsInput.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,11 @@ | ||
import { CLIENTS_TO_TEST } from "./config"; | ||
import { getV2ClientsNewExpressionCode } from "./getV2ClientsNewExpressionCode"; | ||
|
||
export const getGlobalImportEqualsInput = (codegenComment: string) => { | ||
let globalImportEqualsInputContent = `${codegenComment}\n`; | ||
|
||
globalImportEqualsInputContent += `import AWS = require("aws-sdk");\n\n`; | ||
globalImportEqualsInputContent += getV2ClientsNewExpressionCode(CLIENTS_TO_TEST, `AWS.`); | ||
|
||
return globalImportEqualsInputContent; | ||
}; |
29 changes: 29 additions & 0 deletions
29
scripts/generateNewClientTests/getGlobalImportEqualsOutput.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,29 @@ | ||
import { CLIENT_NAMES_MAP, CLIENT_PACKAGE_NAMES_MAP } from "../../src/transforms/v2-to-v3/config"; | ||
import { getV3ClientDefaultLocalName } from "../../src/transforms/v2-to-v3/utils"; | ||
import { CLIENTS_TO_TEST } from "./config"; | ||
import { getClientNamesSortedByPackageName } from "./getClientNamesSortedByPackageName"; | ||
import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; | ||
|
||
export const getGlobalImportEqualsOutput = (codegenComment: string) => { | ||
let globalImportEqualsOutputContent = `${codegenComment};\n`; | ||
|
||
const sortedClientNames = getClientNamesSortedByPackageName(CLIENTS_TO_TEST); | ||
|
||
for (const v2ClientName of sortedClientNames) { | ||
const v3ClientDefaultLocalName = getV3ClientDefaultLocalName(v2ClientName); | ||
const v3ClientPackageName = `@aws-sdk/${CLIENT_PACKAGE_NAMES_MAP[v2ClientName]}`; | ||
globalImportEqualsOutputContent += `import ${v3ClientDefaultLocalName} = require("${v3ClientPackageName}");\n\n`; | ||
|
||
const v3ClientName = CLIENT_NAMES_MAP[v2ClientName]; | ||
const v3ObjectPattern = | ||
v3ClientName === v2ClientName ? v3ClientName : `${v3ClientName}: ${v2ClientName}`; | ||
globalImportEqualsOutputContent += | ||
`const {\n` + | ||
` ${v3ObjectPattern}\n` + | ||
`} = ${getV3ClientDefaultLocalName(v2ClientName)};\n\n`; | ||
} | ||
|
||
globalImportEqualsOutputContent += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST); | ||
|
||
return globalImportEqualsOutputContent; | ||
}; |
13 changes: 13 additions & 0 deletions
13
scripts/generateNewClientTests/getServiceImportEqualsInput.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,13 @@ | ||
import { CLIENTS_TO_TEST } from "./config"; | ||
import { getV2ClientsNewExpressionCode } from "./getV2ClientsNewExpressionCode"; | ||
|
||
export const getServiceImportEqualsInput = (codegenComment: string) => { | ||
let serviceImportEqualsInputContent = `${codegenComment}\n`; | ||
|
||
for (const clientName of CLIENTS_TO_TEST) { | ||
serviceImportEqualsInputContent += `import ${clientName} = require("aws-sdk/clients/${clientName.toLowerCase()}");\n`; | ||
} | ||
serviceImportEqualsInputContent += getV2ClientsNewExpressionCode(CLIENTS_TO_TEST); | ||
|
||
return serviceImportEqualsInputContent; | ||
}; |
26 changes: 26 additions & 0 deletions
26
scripts/generateNewClientTests/getServiceImportEqualsOutput.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,26 @@ | ||
import { CLIENT_NAMES_MAP, CLIENT_PACKAGE_NAMES_MAP } from "../../src/transforms/v2-to-v3/config"; | ||
import { getV3ClientDefaultLocalName } from "../../src/transforms/v2-to-v3/utils"; | ||
import { CLIENTS_TO_TEST } from "./config"; | ||
import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; | ||
|
||
export const getServiceImportEqualsOutput = (codegenComment: string) => { | ||
let serviceImportEqualsOutputContent = `${codegenComment};\n`; | ||
|
||
for (const v2ClientName of CLIENTS_TO_TEST) { | ||
const v3ClientDefaultLocalName = getV3ClientDefaultLocalName(v2ClientName); | ||
const v3ClientPackageName = `@aws-sdk/${CLIENT_PACKAGE_NAMES_MAP[v2ClientName]}`; | ||
serviceImportEqualsOutputContent += `import ${v3ClientDefaultLocalName} = require("${v3ClientPackageName}");\n\n`; | ||
|
||
const v3ClientName = CLIENT_NAMES_MAP[v2ClientName]; | ||
const v3ObjectPattern = | ||
v3ClientName === v2ClientName ? v3ClientName : `${v3ClientName}: ${v2ClientName}`; | ||
serviceImportEqualsOutputContent += | ||
`const {\n` + | ||
` ${v3ObjectPattern}\n` + | ||
`} = ${getV3ClientDefaultLocalName(v2ClientName)};\n\n`; | ||
} | ||
|
||
serviceImportEqualsOutputContent += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST); | ||
|
||
return serviceImportEqualsOutputContent; | ||
}; |
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
8 changes: 8 additions & 0 deletions
8
src/transforms/v2-to-v3/__fixtures__/new-client/global-import-equals.input.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,8 @@ | ||
// 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"; | ||
import AWS = require("aws-sdk"); | ||
|
||
new AWS.ACM(); | ||
new AWS.AccessAnalyzer(); | ||
new AWS.Discovery(); |
24 changes: 24 additions & 0 deletions
24
src/transforms/v2-to-v3/__fixtures__/new-client/global-import-equals.output.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,24 @@ | ||
// 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";; | ||
import AWS_AccessAnalyzer = require("@aws-sdk/client-accessanalyzer"); | ||
|
||
const { | ||
AccessAnalyzer | ||
} = AWS_AccessAnalyzer; | ||
|
||
import AWS_ACM = require("@aws-sdk/client-acm"); | ||
|
||
const { | ||
ACM | ||
} = AWS_ACM; | ||
|
||
import AWS_Discovery = require("@aws-sdk/client-application-discovery-service"); | ||
|
||
const { | ||
ApplicationDiscoveryService: Discovery | ||
} = AWS_Discovery; | ||
|
||
new ACM(); | ||
new AccessAnalyzer(); | ||
new Discovery(); |
9 changes: 9 additions & 0 deletions
9
src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals.input.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,9 @@ | ||
// 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"; | ||
import ACM = require("aws-sdk/clients/acm"); | ||
import AccessAnalyzer = require("aws-sdk/clients/accessanalyzer"); | ||
import Discovery = require("aws-sdk/clients/discovery"); | ||
new ACM(); | ||
new AccessAnalyzer(); | ||
new Discovery(); |
24 changes: 24 additions & 0 deletions
24
src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals.output.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,24 @@ | ||
// 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";; | ||
import AWS_ACM = require("@aws-sdk/client-acm"); | ||
|
||
const { | ||
ACM | ||
} = AWS_ACM; | ||
|
||
import AWS_AccessAnalyzer = require("@aws-sdk/client-accessanalyzer"); | ||
|
||
const { | ||
AccessAnalyzer | ||
} = AWS_AccessAnalyzer; | ||
|
||
import AWS_Discovery = require("@aws-sdk/client-application-discovery-service"); | ||
|
||
const { | ||
ApplicationDiscoveryService: Discovery | ||
} = AWS_Discovery; | ||
|
||
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
80 changes: 80 additions & 0 deletions
80
src/transforms/v2-to-v3/modules/addV3ClientImportEquals.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,80 @@ | ||
import { Collection, JSCodeshift, TSExternalModuleReference } from "jscodeshift"; | ||
|
||
import { PACKAGE_NAME } from "../config"; | ||
import { getV2ServiceModulePath, getV3ClientDefaultLocalName } from "../utils"; | ||
import { getImportEqualsDeclaration } from "./getImportEqualsDeclaration"; | ||
import { V3ClientModulesOptions } from "./types"; | ||
|
||
export const addV3ClientImportEquals = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
{ | ||
v2ClientLocalName, | ||
v2ClientName, | ||
v2GlobalName, | ||
v3ClientName, | ||
v3ClientPackageName, | ||
}: V3ClientModulesOptions | ||
): void => { | ||
const v3ClientDefaultLocalName = getV3ClientDefaultLocalName(v2ClientLocalName); | ||
const existingImportEquals = source.find( | ||
j.TSImportEqualsDeclaration, | ||
getImportEqualsDeclaration(v3ClientPackageName) | ||
); | ||
|
||
if (existingImportEquals.size()) { | ||
if ( | ||
existingImportEquals | ||
.nodes() | ||
.some( | ||
(importEqualsDeclaration) => importEqualsDeclaration.id.name === v3ClientDefaultLocalName | ||
) | ||
) { | ||
return; | ||
} | ||
} | ||
|
||
// Insert after global, or service import equals. | ||
source | ||
.find(j.TSImportEqualsDeclaration, getImportEqualsDeclaration()) | ||
.filter((importEqualsDeclaration) => { | ||
const identifierName = importEqualsDeclaration.value.id.name; | ||
const importEqualsModuleRef = importEqualsDeclaration.value | ||
.moduleReference as TSExternalModuleReference; | ||
const expressionValue = importEqualsModuleRef.expression.value; | ||
|
||
if (expressionValue === PACKAGE_NAME && identifierName === v2GlobalName) { | ||
return true; | ||
} | ||
|
||
if ( | ||
expressionValue === getV2ServiceModulePath(v2ClientName) && | ||
identifierName === v2ClientLocalName | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}) | ||
.at(0) | ||
.insertAfter( | ||
j.variableDeclaration("const", [ | ||
j.variableDeclarator( | ||
j.objectPattern([ | ||
j.objectProperty.from({ | ||
key: j.identifier(v3ClientName), | ||
value: j.identifier(v2ClientLocalName), | ||
shorthand: true, | ||
}), | ||
]), | ||
j.identifier(v3ClientDefaultLocalName) | ||
), | ||
]) | ||
) | ||
.insertAfter( | ||
j.tsImportEqualsDeclaration( | ||
j.identifier(v3ClientDefaultLocalName), | ||
j.tsExternalModuleReference(j.stringLiteral(v3ClientPackageName)) | ||
) | ||
); | ||
}; |
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/modules/getImportEqualsDeclaration.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,10 @@ | ||
import { TSImportEqualsDeclaration } from "jscodeshift"; | ||
|
||
export const getImportEqualsDeclaration = (expressionValue?: string) => | ||
({ | ||
type: "TSImportEqualsDeclaration", | ||
moduleReference: { | ||
type: "TSExternalModuleReference", | ||
expression: { type: "StringLiteral", ...(expressionValue && { value: expressionValue }) }, | ||
}, | ||
} as TSImportEqualsDeclaration); |
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,6 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
import { getImportEqualsDeclaration } from "./getImportEqualsDeclaration"; | ||
|
||
export const hasImportEquals = (j: JSCodeshift, source: Collection<unknown>) => | ||
source.find(j.TSImportEqualsDeclaration, getImportEqualsDeclaration()).size() > 0; |
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
23 changes: 23 additions & 0 deletions
23
src/transforms/v2-to-v3/modules/removeImportEqualsIdentifierName.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,23 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
export interface RemoveImportEqualsIdentifierNameOptions { | ||
localName: string; | ||
sourceValue: string; | ||
} | ||
|
||
export const removeImportEqualsIdentifierName = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
{ localName, sourceValue }: RemoveImportEqualsIdentifierNameOptions | ||
) => { | ||
source | ||
.find(j.TSImportEqualsDeclaration, { | ||
type: "TSImportEqualsDeclaration", | ||
id: { name: localName }, | ||
moduleReference: { | ||
type: "TSExternalModuleReference", | ||
expression: { type: "StringLiteral", value: sourceValue }, | ||
}, | ||
}) | ||
.remove(); | ||
}; |
Oops, something went wrong.