Skip to content

Commit 240e8c6

Browse files
authored
Add constant OBJECT_PROPERTY_TYPE_LIST (#387)
1 parent ad0f45a commit 240e8c6

File tree

5 files changed

+41
-33
lines changed

5 files changed

+41
-33
lines changed

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import { Collection, Identifier, JSCodeshift, ObjectPattern, Property } from "jscodeshift";
1+
import {
2+
Collection,
3+
Identifier,
4+
JSCodeshift,
5+
ObjectPattern,
6+
ObjectProperty,
7+
Property,
8+
} from "jscodeshift";
29

3-
import { CLIENT_NAMES, PACKAGE_NAME } from "../config";
10+
import { CLIENT_NAMES, OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME } from "../config";
411
import { getRequireDeclaratorsWithProperty } from "../modules";
512
import { getV2ServiceModulePath } from "../utils";
613
import { getRequireIds } from "./getRequireIds";
@@ -15,18 +22,15 @@ export const getV2ClientNamesRecordFromRequire = (
1522
const idPropertiesFromObjectPattern = getRequireIds(j, source, PACKAGE_NAME)
1623
.filter((id) => id.type === "ObjectPattern")
1724
.map((objectPattern) => (objectPattern as ObjectPattern).properties)
18-
.flat() as Property[];
25+
.flat();
1926

2027
for (const idProperty of idPropertiesFromObjectPattern) {
21-
if (!["Property", "ObjectProperty"].includes(idProperty.type)) {
28+
if (!OBJECT_PROPERTY_TYPE_LIST.includes(idProperty.type)) {
2229
continue;
2330
}
24-
const key = idProperty.key as Identifier;
25-
if (key.type !== "Identifier") {
26-
continue;
27-
}
28-
const value = idProperty.value as Identifier;
29-
if (value.type !== "Identifier") {
31+
const key = (idProperty as Property | ObjectProperty).key;
32+
const value = (idProperty as Property | ObjectProperty).value;
33+
if (key.type !== "Identifier" || value.type !== "Identifier") {
3034
continue;
3135
}
3236
if (CLIENT_NAMES.includes(key.name)) {

src/transforms/v2-to-v3/config/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ export const PACKAGE_NAME = "aws-sdk";
22

33
export const V2_CLIENT_INPUT_SUFFIX_LIST = ["Input", "Request"];
44
export const V2_CLIENT_OUTPUT_SUFFIX_LIST = ["Output", "Response"];
5+
6+
export const OBJECT_PROPERTY_TYPE_LIST = ["Property", "ObjectProperty"];

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Collection, JSCodeshift, ObjectPattern } from "jscodeshift";
1+
import { Collection, JSCodeshift, ObjectPattern, ObjectProperty, Property } from "jscodeshift";
22

3+
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
34
import { getV3ClientDefaultLocalName } from "../utils";
45
import { getRequireDeclarators } from "./getRequireDeclarators";
56
import { getRequireDeclaratorsWithIdentifier } from "./getRequireDeclaratorsWithIdentifier";
@@ -33,14 +34,15 @@ export const addV3ClientNamedRequire = (
3334
existingRequireProperties.find(
3435
(variableDeclarator) =>
3536
variableDeclarator.id.type === "ObjectPattern" &&
36-
variableDeclarator.id.properties.find(
37-
(property) =>
38-
property.type === "Property" &&
39-
property.key.type === "Identifier" &&
40-
property.value.type === "Identifier" &&
41-
property.key.name === keyName &&
42-
property.value.name === valueName
43-
)
37+
variableDeclarator.id.properties.find((property) => {
38+
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) return false;
39+
const key = (property as Property | ObjectProperty).key;
40+
const value = (property as Property | ObjectProperty).value;
41+
if (key.type !== "Identifier" || value.type !== "Identifier") {
42+
return false;
43+
}
44+
return key.name === keyName && value.name === valueName;
45+
})
4446
)
4547
) {
4648
return;

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Collection, JSCodeshift } from "jscodeshift";
1+
import { Collection, JSCodeshift, ObjectProperty, Property } from "jscodeshift";
22

3+
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
34
import { getRequireDeclarators } from "./getRequireDeclarators";
45

56
export interface GetRequireDeclaratorsWithObjectPattern {
@@ -17,10 +18,9 @@ export const getRequireDeclaratorsWithObjectPattern = (
1718
return false;
1819
}
1920
const { properties } = declarator.value.id;
20-
return properties.some(
21-
(property) =>
22-
(property.type === "Property" || property.type === "ObjectProperty") &&
23-
property.value.type === "Identifier" &&
24-
property.value.name === identifierName
25-
);
21+
return properties.some((property) => {
22+
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) return false;
23+
const propertyValue = (property as Property | ObjectProperty).value;
24+
return propertyValue.type === "Identifier" && propertyValue.name === identifierName;
25+
});
2626
});

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Collection, JSCodeshift, ObjectPattern } from "jscodeshift";
1+
import { Collection, JSCodeshift, ObjectPattern, ObjectProperty, Property } from "jscodeshift";
22

3+
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
34
import { getRequireDeclaratorsWithObjectPattern } from "./getRequireDeclaratorsWithObjectPattern";
45

56
export interface RemoveRequireObjectPropertyOptions {
@@ -22,12 +23,11 @@ export const removeRequireObjectProperty = (
2223

2324
// Remove ObjectProperty from Variable Declarator.
2425
const varDeclaratorId = varDeclarator.value.id as ObjectPattern;
25-
varDeclaratorId.properties = varDeclaratorId.properties.filter(
26-
(property) =>
27-
(property.type !== "Property" && property.type !== "ObjectProperty") ||
28-
property.value.type !== "Identifier" ||
29-
property.value.name !== localName
30-
);
26+
varDeclaratorId.properties = varDeclaratorId.properties.filter((property) => {
27+
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) return true;
28+
const propertyValue = (property as Property | ObjectProperty).value;
29+
return propertyValue.type !== "Identifier" || propertyValue.name !== localName;
30+
});
3131

3232
// Remove VariableDeclarator if there are no properties.
3333
if (varDeclaratorId.properties.length === 0) {

0 commit comments

Comments
 (0)