-
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.
Remove client import equals if clients are not created (#310)
- Loading branch information
Showing
9 changed files
with
160 additions
and
85 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 | ||
--- | ||
|
||
Remove client import equals if clients are not created |
4 changes: 0 additions & 4 deletions
4
src/transforms/v2-to-v3/__fixtures__/api-basic-type/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 |
---|---|---|
@@ -1,7 +1,3 @@ | ||
import AWS_S3 = require("@aws-sdk/client-s3"); | ||
|
||
const { | ||
S3 | ||
} = AWS_S3; | ||
|
||
const testTags: AWS_S3.Tag[] = [{ Key: "key", Value: "value" }]; |
4 changes: 0 additions & 4 deletions
4
src/transforms/v2-to-v3/__fixtures__/api-basic-type/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 |
---|---|---|
@@ -1,7 +1,3 @@ | ||
import AWS_S3 = require("@aws-sdk/client-s3"); | ||
|
||
const { | ||
S3 | ||
} = AWS_S3; | ||
|
||
const testTags: AWS_S3.Tag[] = [{ Key: "key", Value: "value" }]; |
52 changes: 52 additions & 0 deletions
52
src/transforms/v2-to-v3/modules/addV3ClientDefaultImportEquals.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,52 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
import { getV3ClientDefaultLocalName } from "../utils"; | ||
import { getImportEqualsDeclaration } from "./getImportEqualsDeclaration"; | ||
import { getV2ImportEqualsDeclaration } from "./getV2ImportEqualsDeclaration"; | ||
import { V3ClientModulesOptions } from "./types"; | ||
|
||
export const addV3ClientDefaultImportEquals = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
{ v2ClientLocalName, v2ClientName, v2GlobalName, v3ClientPackageName }: V3ClientModulesOptions | ||
) => { | ||
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. | ||
const v2ImportEqualsDeclaration = getV2ImportEqualsDeclaration(j, source, { | ||
v2ClientName, | ||
v2ClientLocalName, | ||
v2GlobalName, | ||
}).at(0); | ||
|
||
const importDeclaration = j.tsImportEqualsDeclaration( | ||
j.identifier(v3ClientDefaultLocalName), | ||
j.tsExternalModuleReference(j.stringLiteral(v3ClientPackageName)) | ||
); | ||
|
||
if (v2ImportEqualsDeclaration && v2ImportEqualsDeclaration.nodes().length > 0) { | ||
v2ImportEqualsDeclaration.at(0).insertAfter(importDeclaration); | ||
} else { | ||
// Unreachable code, throw error | ||
throw new Error( | ||
"Base Import Equals Declaration not found to insert new Import Declaration.\n" + | ||
"Please report your use case on https://github.com/awslabs/aws-sdk-js-codemod" | ||
); | ||
} | ||
}; |
82 changes: 12 additions & 70 deletions
82
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 |
---|---|---|
@@ -1,80 +1,22 @@ | ||
import { Collection, JSCodeshift, TSExternalModuleReference } from "jscodeshift"; | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
import { PACKAGE_NAME } from "../config"; | ||
import { getV2ServiceModulePath, getV3ClientDefaultLocalName } from "../utils"; | ||
import { getImportEqualsDeclaration } from "./getImportEqualsDeclaration"; | ||
import { addV3ClientDefaultImportEquals } from "./addV3ClientDefaultImportEquals"; | ||
import { addV3ClientNamedImportEquals } from "./addV3ClientNamedImportEquals"; | ||
import { getClientTSTypeRefCount } from "./getClientTSTypeRefCount"; | ||
import { getNewExpressionCount } from "./getNewExpressionCount"; | ||
import { V3ClientModulesOptions } from "./types"; | ||
|
||
export const addV3ClientImportEquals = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
{ | ||
v2ClientLocalName, | ||
v2ClientName, | ||
v2GlobalName, | ||
v3ClientName, | ||
v3ClientPackageName, | ||
}: V3ClientModulesOptions | ||
options: V3ClientModulesOptions | ||
): void => { | ||
const v3ClientDefaultLocalName = getV3ClientDefaultLocalName(v2ClientLocalName); | ||
const existingImportEquals = source.find( | ||
j.TSImportEqualsDeclaration, | ||
getImportEqualsDeclaration(v3ClientPackageName) | ||
); | ||
addV3ClientDefaultImportEquals(j, source, options); | ||
|
||
if (existingImportEquals.size()) { | ||
if ( | ||
existingImportEquals | ||
.nodes() | ||
.some( | ||
(importEqualsDeclaration) => importEqualsDeclaration.id.name === v3ClientDefaultLocalName | ||
) | ||
) { | ||
return; | ||
} | ||
if ( | ||
getNewExpressionCount(j, source, options) > 0 || | ||
getClientTSTypeRefCount(j, source, options) > 0 | ||
) { | ||
addV3ClientNamedImportEquals(j, source, options); | ||
} | ||
|
||
// 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
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
48 changes: 48 additions & 0 deletions
48
src/transforms/v2-to-v3/modules/addV3ClientNamedImportEquals.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,48 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
import { getV3ClientDefaultLocalName } from "../utils"; | ||
import { getImportEqualsDeclaration } from "./getImportEqualsDeclaration"; | ||
import { V3ClientModulesOptions } from "./types"; | ||
|
||
export const addV3ClientNamedImportEquals = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
{ v2ClientLocalName, v3ClientName, v3ClientPackageName }: V3ClientModulesOptions | ||
) => { | ||
const v3ClientDefaultLocalName = getV3ClientDefaultLocalName(v2ClientLocalName); | ||
const existingImportEquals = source.find( | ||
j.TSImportEqualsDeclaration, | ||
getImportEqualsDeclaration(v3ClientPackageName) | ||
); | ||
|
||
const varDeclaration = j.variableDeclaration("const", [ | ||
j.variableDeclarator( | ||
j.objectPattern([ | ||
j.objectProperty.from({ | ||
key: j.identifier(v3ClientName), | ||
value: j.identifier(v2ClientLocalName), | ||
shorthand: true, | ||
}), | ||
]), | ||
j.identifier(v3ClientDefaultLocalName) | ||
), | ||
]); | ||
|
||
if (existingImportEquals.size()) { | ||
const v3ClientImportEquals = existingImportEquals.filter( | ||
(importEqualsDeclaration) => | ||
importEqualsDeclaration.value.id.name === v3ClientDefaultLocalName | ||
); | ||
|
||
if (v3ClientImportEquals.size() > 0) { | ||
v3ClientImportEquals.at(0).insertAfter(varDeclaration); | ||
return; | ||
} | ||
} | ||
|
||
// Unreachable code, throw error | ||
throw new Error( | ||
"The named import equals can't exist on it's own.\n" + | ||
"Please report your use case on https://github.com/awslabs/aws-sdk-js-codemod" | ||
); | ||
}; |
39 changes: 39 additions & 0 deletions
39
src/transforms/v2-to-v3/modules/getV2ImportEqualsDeclaration.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,39 @@ | ||
import { Collection, JSCodeshift, TSExternalModuleReference } from "jscodeshift"; | ||
|
||
import { PACKAGE_NAME } from "../config"; | ||
import { getV2ServiceModulePath } from "../utils"; | ||
import { getImportEqualsDeclaration } from "./getImportEqualsDeclaration"; | ||
|
||
export interface GetV2ImportEqualsDeclarationOptions { | ||
v2ClientName: string; | ||
v2ClientLocalName: string; | ||
v2GlobalName?: string; | ||
} | ||
|
||
export const getV2ImportEqualsDeclaration = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
{ v2ClientName, v2ClientLocalName, v2GlobalName }: GetV2ImportEqualsDeclarationOptions | ||
) => | ||
// Return global or service import declaration. | ||
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; | ||
}); |