Skip to content

Commit 3cbcf56

Browse files
authored
Remove type assertions for Identifier (#920)
1 parent de55f71 commit 3cbcf56

File tree

8 files changed

+35
-35
lines changed

8 files changed

+35
-35
lines changed

.changeset/itchy-mugs-rest.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+
Remove type assertions for Identifier

src/transforms/v2-to-v3/client-instances/getObjectWithUpdatedAwsConfigKeys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const getObjectWithUpdatedAwsConfigKeys = (
4242
continue;
4343
}
4444

45-
const propertyKeyName = (propertyKey as Identifier).name;
45+
const propertyKeyName = propertyKey.name;
4646
if (
4747
!propertiesToUpdate
4848
.filter((propertyToUpdate) => OBJECT_PROPERTY_TYPE_LIST.includes(propertyToUpdate.type))

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const getNamesFromTSQualifiedName = (
88
source
99
.find(j.TSQualifiedName, {
1010
left: { name: v2GlobalName },
11+
right: { type: "Identifier" },
1112
})
1213
.nodes()
1314
.map((tsTypeReference) => (tsTypeReference.right as Identifier).name);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Collection, Identifier, JSCodeshift } from "jscodeshift";
1+
import type { Collection, JSCodeshift } from "jscodeshift";
22

33
import { PACKAGE_NAME } from "../config";
44
import { getImportSpecifiers as getImportEqualsSpecifiers } from "../modules/importEqualsModule";
@@ -13,8 +13,8 @@ export const getGlobalNameFromModule = (
1313
.filter((declarator) => declarator.value.id.type === "Identifier")
1414
.nodes();
1515

16-
if (requireIdentifiers.length > 0) {
17-
return (requireIdentifiers[0]?.id as Identifier).name;
16+
if (requireIdentifiers.length > 0 && requireIdentifiers[0].id.type === "Identifier") {
17+
return requireIdentifiers[0].id.name;
1818
}
1919

2020
const importDefaultSpecifiers = getImportSpecifiers(j, source, PACKAGE_NAME).filter(

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Collection, Identifier, JSCodeshift } from "jscodeshift";
1+
import type { Collection, JSCodeshift } from "jscodeshift";
22
import { getRequireDeclarators } from "./requireModule";
33

44
export interface GetRequireDeclaratorsWithPropertyOptions {
@@ -18,8 +18,11 @@ export const getRequireDeclaratorsWithProperty = (
1818

1919
if (declaratorId.type === "Identifier") {
2020
const declaratorIdName = declaratorId.name;
21-
if (declaratorInit?.type === "MemberExpression") {
22-
const importedName = (declaratorInit.property as Identifier).name;
21+
if (
22+
declaratorInit?.type === "MemberExpression" &&
23+
declaratorInit.property.type === "Identifier"
24+
) {
25+
const importedName = declaratorInit.property.name;
2326
if (localName === declaratorIdName && identifierName === importedName) {
2427
return true;
2528
}

src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Collection, Identifier, JSCodeshift, ObjectProperty, Property } from "jscodeshift";
1+
import type { Collection, JSCodeshift, ObjectProperty, Property } from "jscodeshift";
22
import { OBJECT_PROPERTY_TYPE_LIST } from "../../config";
33
import type { ImportSpecifierType } from "../types";
44
import { getRequireDeclarators } from "./getRequireDeclarators";
@@ -37,9 +37,12 @@ export const getImportSpecifiers = (
3737

3838
if (declaratorId.type === "Identifier") {
3939
const declaratorIdName = declaratorId.name;
40-
if (declaratorInit?.type === "MemberExpression") {
40+
if (
41+
declaratorInit?.type === "MemberExpression" &&
42+
declaratorInit.property.type === "Identifier"
43+
) {
4144
importSpecifiers.add({
42-
importedName: (declaratorInit.property as Identifier).name,
45+
importedName: declaratorInit.property.name,
4346
localName: declaratorIdName,
4447
});
4548
} else {

src/transforms/v2-to-v3/ts-type/getClientTypeNames.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import type {
2-
Collection,
3-
Identifier,
4-
JSCodeshift,
5-
TSQualifiedName,
6-
TSTypeReference,
7-
} from "jscodeshift";
1+
import type { Collection, JSCodeshift, TSQualifiedName, TSTypeReference } from "jscodeshift";
82

93
import type { ImportSpecifierType } from "../modules";
104
import { getImportSpecifiers } from "../modules/importModule";
@@ -30,7 +24,7 @@ const getRightIdentifierName = (
3024
.nodes()
3125
.map((node) => (node.typeName as TSQualifiedName).right)
3226
.filter((node) => node.type === "Identifier")
33-
.map((node) => (node as Identifier).name);
27+
.map((node) => node.name);
3428

3529
export const getClientTypeNames = (
3630
j: JSCodeshift,

src/transforms/v2-to-v3/ts-type/replaceTSTypeReference.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import type {
2-
Collection,
3-
Identifier,
4-
JSCodeshift,
5-
TSQualifiedName,
6-
TSTypeReference,
7-
} from "jscodeshift";
1+
import type { Collection, Identifier, JSCodeshift, TSQualifiedName } from "jscodeshift";
82

93
import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT, DYNAMODB_DOCUMENT_CLIENT } from "../config";
104
import { getClientTypeNames } from "./getClientTypeNames";
@@ -18,12 +12,6 @@ export interface ReplaceTSTypeReferenceOptions {
1812
v3ClientName: string;
1913
}
2014

21-
const isRightSectionIdentifier = (node: TSTypeReference) =>
22-
(node.typeName as TSQualifiedName).right.type === "Identifier";
23-
24-
const getRightIdentifierName = (node: TSTypeReference) =>
25-
((node.typeName as TSQualifiedName).right as Identifier).name;
26-
2715
// Replace v2 client type reference with v3 client type reference.
2816
export const replaceTSTypeReference = (
2917
j: JSCodeshift,
@@ -66,9 +54,12 @@ export const replaceTSTypeReference = (
6654
left: getTSQualifiedNameFromClientName(v2ClientName, v2GlobalName),
6755
},
6856
})
69-
.filter((v2ClientType) => isRightSectionIdentifier(v2ClientType.node))
7057
.replaceWith((v2ClientType) => {
71-
const v2ClientTypeName = getRightIdentifierName(v2ClientType.node);
58+
const tSQualifiedName = v2ClientType.node.typeName as TSQualifiedName;
59+
if (tSQualifiedName.right.type !== "Identifier") {
60+
return v2ClientType;
61+
}
62+
const v2ClientTypeName = tSQualifiedName.right.name;
7263
return getV3ClientType(j, { ...clientTypeOptions, v2ClientTypeName });
7364
});
7465
}
@@ -81,9 +72,12 @@ export const replaceTSTypeReference = (
8172
.find(j.TSTypeReference, {
8273
typeName: { left: getTSQualifiedNameFromClientName(v2ClientLocalName) },
8374
})
84-
.filter((v2ClientType) => isRightSectionIdentifier(v2ClientType.node))
8575
.replaceWith((v2ClientType) => {
86-
const v2ClientTypeName = getRightIdentifierName(v2ClientType.node);
76+
const tSQualifiedName = v2ClientType.node.typeName as TSQualifiedName;
77+
if (tSQualifiedName.right.type !== "Identifier") {
78+
return v2ClientType;
79+
}
80+
const v2ClientTypeName = tSQualifiedName.right.name;
8781
return getV3ClientType(j, { ...clientTypeOptions, v2ClientTypeName });
8882
});
8983

0 commit comments

Comments
 (0)