Skip to content

Commit

Permalink
Remove type assertions for Identifier (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Jul 28, 2024
1 parent de55f71 commit 3cbcf56
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-mugs-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Remove type assertions for Identifier
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const getObjectWithUpdatedAwsConfigKeys = (
continue;
}

const propertyKeyName = (propertyKey as Identifier).name;
const propertyKeyName = propertyKey.name;
if (
!propertiesToUpdate
.filter((propertyToUpdate) => OBJECT_PROPERTY_TYPE_LIST.includes(propertyToUpdate.type))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const getNamesFromTSQualifiedName = (
source
.find(j.TSQualifiedName, {
left: { name: v2GlobalName },
right: { type: "Identifier" },
})
.nodes()
.map((tsTypeReference) => (tsTypeReference.right as Identifier).name);
6 changes: 3 additions & 3 deletions src/transforms/v2-to-v3/modules/getGlobalNameFromModule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Collection, Identifier, JSCodeshift } from "jscodeshift";
import type { Collection, JSCodeshift } from "jscodeshift";

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

if (requireIdentifiers.length > 0) {
return (requireIdentifiers[0]?.id as Identifier).name;
if (requireIdentifiers.length > 0 && requireIdentifiers[0].id.type === "Identifier") {
return requireIdentifiers[0].id.name;
}

const importDefaultSpecifiers = getImportSpecifiers(j, source, PACKAGE_NAME).filter(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Collection, Identifier, JSCodeshift } from "jscodeshift";
import type { Collection, JSCodeshift } from "jscodeshift";
import { getRequireDeclarators } from "./requireModule";

export interface GetRequireDeclaratorsWithPropertyOptions {
Expand All @@ -18,8 +18,11 @@ export const getRequireDeclaratorsWithProperty = (

if (declaratorId.type === "Identifier") {
const declaratorIdName = declaratorId.name;
if (declaratorInit?.type === "MemberExpression") {
const importedName = (declaratorInit.property as Identifier).name;
if (
declaratorInit?.type === "MemberExpression" &&
declaratorInit.property.type === "Identifier"
) {
const importedName = declaratorInit.property.name;
if (localName === declaratorIdName && identifierName === importedName) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Collection, Identifier, JSCodeshift, ObjectProperty, Property } from "jscodeshift";
import type { Collection, JSCodeshift, ObjectProperty, Property } from "jscodeshift";
import { OBJECT_PROPERTY_TYPE_LIST } from "../../config";
import type { ImportSpecifierType } from "../types";
import { getRequireDeclarators } from "./getRequireDeclarators";
Expand Down Expand Up @@ -37,9 +37,12 @@ export const getImportSpecifiers = (

if (declaratorId.type === "Identifier") {
const declaratorIdName = declaratorId.name;
if (declaratorInit?.type === "MemberExpression") {
if (
declaratorInit?.type === "MemberExpression" &&
declaratorInit.property.type === "Identifier"
) {
importSpecifiers.add({
importedName: (declaratorInit.property as Identifier).name,
importedName: declaratorInit.property.name,
localName: declaratorIdName,
});
} else {
Expand Down
10 changes: 2 additions & 8 deletions src/transforms/v2-to-v3/ts-type/getClientTypeNames.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import type {
Collection,
Identifier,
JSCodeshift,
TSQualifiedName,
TSTypeReference,
} from "jscodeshift";
import type { Collection, JSCodeshift, TSQualifiedName, TSTypeReference } from "jscodeshift";

import type { ImportSpecifierType } from "../modules";
import { getImportSpecifiers } from "../modules/importModule";
Expand All @@ -30,7 +24,7 @@ const getRightIdentifierName = (
.nodes()
.map((node) => (node.typeName as TSQualifiedName).right)
.filter((node) => node.type === "Identifier")
.map((node) => (node as Identifier).name);
.map((node) => node.name);

export const getClientTypeNames = (
j: JSCodeshift,
Expand Down
28 changes: 11 additions & 17 deletions src/transforms/v2-to-v3/ts-type/replaceTSTypeReference.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import type {
Collection,
Identifier,
JSCodeshift,
TSQualifiedName,
TSTypeReference,
} from "jscodeshift";
import type { Collection, Identifier, JSCodeshift, TSQualifiedName } from "jscodeshift";

import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT, DYNAMODB_DOCUMENT_CLIENT } from "../config";
import { getClientTypeNames } from "./getClientTypeNames";
Expand All @@ -18,12 +12,6 @@ export interface ReplaceTSTypeReferenceOptions {
v3ClientName: string;
}

const isRightSectionIdentifier = (node: TSTypeReference) =>
(node.typeName as TSQualifiedName).right.type === "Identifier";

const getRightIdentifierName = (node: TSTypeReference) =>
((node.typeName as TSQualifiedName).right as Identifier).name;

// Replace v2 client type reference with v3 client type reference.
export const replaceTSTypeReference = (
j: JSCodeshift,
Expand Down Expand Up @@ -66,9 +54,12 @@ export const replaceTSTypeReference = (
left: getTSQualifiedNameFromClientName(v2ClientName, v2GlobalName),
},
})
.filter((v2ClientType) => isRightSectionIdentifier(v2ClientType.node))
.replaceWith((v2ClientType) => {
const v2ClientTypeName = getRightIdentifierName(v2ClientType.node);
const tSQualifiedName = v2ClientType.node.typeName as TSQualifiedName;
if (tSQualifiedName.right.type !== "Identifier") {
return v2ClientType;
}
const v2ClientTypeName = tSQualifiedName.right.name;
return getV3ClientType(j, { ...clientTypeOptions, v2ClientTypeName });
});
}
Expand All @@ -81,9 +72,12 @@ export const replaceTSTypeReference = (
.find(j.TSTypeReference, {
typeName: { left: getTSQualifiedNameFromClientName(v2ClientLocalName) },
})
.filter((v2ClientType) => isRightSectionIdentifier(v2ClientType.node))
.replaceWith((v2ClientType) => {
const v2ClientTypeName = getRightIdentifierName(v2ClientType.node);
const tSQualifiedName = v2ClientType.node.typeName as TSQualifiedName;
if (tSQualifiedName.right.type !== "Identifier") {
return v2ClientType;
}
const v2ClientTypeName = tSQualifiedName.right.name;
return getV3ClientType(j, { ...clientTypeOptions, v2ClientTypeName });
});

Expand Down

0 comments on commit 3cbcf56

Please sign in to comment.