Skip to content

Commit 538670b

Browse files
authored
Remove type assertions for Literal and StringLiteral (#917)
1 parent 0f3f42c commit 538670b

File tree

8 files changed

+39
-38
lines changed

8 files changed

+39
-38
lines changed

.changeset/tricky-tigers-smash.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 Literal and StringLiteral

src/transforms/v2-to-v3/apis/getS3SignedUrlApiNames.ts

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

33
import type { ClientIdentifier } from "../types";
44

@@ -20,7 +20,14 @@ export const getS3SignedUrlApiNames = (
2020
},
2121
})
2222
.forEach((callExpression) => {
23-
apiNames.add((callExpression.value.arguments[0] as Literal).value as string);
23+
const callExpressionArg = callExpression.value.arguments[0];
24+
if (callExpressionArg.type !== "Literal" && callExpressionArg.type !== "StringLiteral") {
25+
return;
26+
}
27+
if (typeof callExpressionArg.value !== "string") {
28+
return;
29+
}
30+
apiNames.add(callExpressionArg.value);
2431
});
2532
}
2633
}

src/transforms/v2-to-v3/apis/replaceS3GetSignedUrlApi.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type {
22
Collection,
33
JSCodeshift,
4-
Literal,
54
NewExpression,
65
ObjectExpression,
76
ObjectProperty,
@@ -26,7 +25,15 @@ export const replaceS3GetSignedUrlApi = (
2625
.replaceWith((callExpression) => {
2726
const args = callExpression.node.arguments;
2827

29-
const apiName = (args[0] as Literal).value as string;
28+
if (args[0].type !== "Literal" && args[0].type !== "StringLiteral") {
29+
return callExpression;
30+
}
31+
32+
if (typeof args[0].value !== "string") {
33+
return callExpression;
34+
}
35+
36+
const apiName = args[0].value;
3037
const params = args[1];
3138

3239
const options = j.objectExpression([]);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ export const FUNCTION_TYPE_LIST = [
1212
"FunctionExpression",
1313
"ArrowFunctionExpression",
1414
];
15-
export const STRING_LITERAL_TYPE_LIST = ["Literal", "StringLiteral"];
1615

1716
export const NOT_SUPPORTED_COMMENT = "not supported in AWS SDK for JavaScript (v3)";

src/transforms/v2-to-v3/modules/importEqualsModule/getImportEqualsDeclarations.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import type {
2-
Collection,
3-
JSCodeshift,
4-
StringLiteral,
5-
TSExternalModuleReference,
6-
} from "jscodeshift";
1+
import type { Collection, JSCodeshift, TSExternalModuleReference } from "jscodeshift";
72
import { PACKAGE_NAME } from "../../config";
83

94
export const getImportEqualsDeclarations = (
@@ -22,7 +17,7 @@ export const getImportEqualsDeclarations = (
2217
.filter((importEqualsDeclaration) => {
2318
const moduleReference = importEqualsDeclaration.value
2419
.moduleReference as TSExternalModuleReference;
25-
const expressionValue = (moduleReference.expression as StringLiteral).value;
20+
const expressionValue = moduleReference.expression.value;
2621
if (path) {
2722
return expressionValue === path;
2823
}

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

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

11-
import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME, STRING_LITERAL_TYPE_LIST } from "../../config";
3+
import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME } from "../../config";
124
import { objectPatternPropertyCompareFn } from "../objectPatternPropertyCompareFn";
135
import { getRequireDeclarators } from "../requireModule";
146
import type { ModulesOptions } from "../types";
@@ -73,11 +65,10 @@ export const addNamedModule = (
7365
})
7466
.filter((callExpression) => {
7567
const arg = callExpression.value.arguments[0];
76-
if (!STRING_LITERAL_TYPE_LIST.includes(arg.type)) {
68+
if (arg.type !== "Literal" && arg.type !== "StringLiteral") {
7769
return false;
7870
}
79-
const argValue = (arg as Literal | StringLiteral).value;
80-
return typeof argValue === "string" && argValue.startsWith(PACKAGE_NAME);
71+
return typeof arg.value === "string" && arg.value.startsWith(PACKAGE_NAME);
8172
});
8273

8374
if (v2RequireCallExpressions.size()) {

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import type {
2-
CallExpression,
3-
Collection,
4-
JSCodeshift,
5-
Literal,
6-
VariableDeclarator,
7-
} from "jscodeshift";
1+
import type { CallExpression, Collection, JSCodeshift, VariableDeclarator } from "jscodeshift";
82
import { PACKAGE_NAME } from "../../config";
93

104
const isValidRequireCallExpression = (callExpression: CallExpression, path?: string) => {
115
if (callExpression.arguments.length !== 1) {
126
return false;
137
}
148

15-
const { value: sourceValue } = callExpression.arguments[0] as Literal;
9+
const callExpressionArg = callExpression.arguments[0];
10+
if (callExpressionArg.type !== "Literal" && callExpressionArg.type !== "StringLiteral") {
11+
return false;
12+
}
13+
14+
const sourceValue = callExpressionArg.value;
1615
if (typeof sourceValue !== "string") {
1716
return false;
1817
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ import type {
33
Collection,
44
Identifier,
55
JSCodeshift,
6-
Literal,
76
ObjectPattern,
87
ObjectProperty,
98
Property,
10-
StringLiteral,
119
VariableDeclarator,
1210
} from "jscodeshift";
13-
import { OBJECT_PROPERTY_TYPE_LIST, STRING_LITERAL_TYPE_LIST } from "../../config";
11+
import { OBJECT_PROPERTY_TYPE_LIST } from "../../config";
1412
import { removeDeclaration } from "../removeDeclaration";
1513
import type { ImportSpecifierType } from "../types";
1614
import { getRequireDeclarators } from "./getRequireDeclarators";
@@ -37,9 +35,9 @@ const isAnotherSpecifier = (j: JSCodeshift, source: Collection<unknown>, localNa
3735
const initArgs = init.arguments;
3836
if (!initArgs || initArgs.length !== 0) return false;
3937

40-
if (STRING_LITERAL_TYPE_LIST.includes(initArgs[0].type)) return false;
38+
if (initArgs[0].type !== "Literal" && initArgs[0].type !== "StringLiteral") return false;
4139

42-
const sourceValue = (initArgs[0] as Literal | StringLiteral).value;
40+
const sourceValue = initArgs[0].value;
4341
if (typeof sourceValue !== "string") return false;
4442
return sourceValue.startsWith("@aws-sdk/");
4543
})

0 commit comments

Comments
 (0)