Skip to content

Commit d0d27cc

Browse files
committed
chore: throw errors where we expect identifier.name as string
1 parent 0588036 commit d0d27cc

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ export const getImportSpecifiers = (
1010
const importSpecifiers = new Set<ImportSpecifierType>();
1111

1212
getImportEqualsDeclarations(j, source, path).forEach((importEqualsDeclaration) => {
13-
importSpecifiers.add({ localName: importEqualsDeclaration.value.id.name });
13+
const localName = importEqualsDeclaration.value.id.name;
14+
if (typeof localName !== "string") {
15+
throw new Error("Please report your use case on https://github.com/aws/aws-sdk-js-codemod");
16+
}
17+
importSpecifiers.add({ localName });
1418
});
1519

1620
return Array.from(importSpecifiers);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const isAnotherSpecifier = (j: JSCodeshift, source: Collection<unknown>, localNa
88
export const removeImportEquals = (j: JSCodeshift, source: Collection<unknown>) =>
99
getImportEqualsDeclarations(j, source).forEach((importEqualsDeclaration) => {
1010
const localName = importEqualsDeclaration.value.id.name;
11+
if (typeof localName !== "string") {
12+
throw new Error("Please report your use case on https://github.com/aws/aws-sdk-js-codemod");
13+
}
1114
const identifiers = source.find(j.Identifier, { name: localName });
1215

1316
// Either the identifier is the only occurence on the page.

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,28 @@ export const getImportSpecifiers = (
1515
switch (specifier.type) {
1616
case "ImportSpecifier": {
1717
const importedName = specifier.imported.name;
18+
const localName = specifier.local?.name;
19+
if (typeof importedName !== "string" || (localName && typeof localName !== "string")) {
20+
throw new Error(
21+
"Please report your use case on https://github.com/aws/aws-sdk-js-codemod"
22+
);
23+
}
1824
importSpecifiers.add({
1925
importedName,
20-
localName: specifier.local?.name || importedName,
26+
localName: localName || importedName,
2127
});
2228
break;
2329
}
2430
case "ImportNamespaceSpecifier":
2531
case "ImportDefaultSpecifier": {
2632
if (specifier.local) {
27-
importSpecifiers.add({ localName: specifier.local.name });
33+
const localName = specifier.local.name;
34+
if (typeof localName !== "string") {
35+
throw new Error(
36+
"Please report your use case on https://github.com/aws/aws-sdk-js-codemod"
37+
);
38+
}
39+
importSpecifiers.add({ localName });
2840
}
2941
break;
3042
}

src/transforms/v2-to-v3/modules/importModule/removeImport.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export const removeImport = (j: JSCodeshift, source: Collection<unknown>) =>
2222
if (!localName) {
2323
return true;
2424
}
25+
if (typeof localName !== "string") {
26+
throw new Error(
27+
"Please report your use case on https://github.com/aws/aws-sdk-js-codemod"
28+
);
29+
}
2530
const identifiers = source.find(j.Identifier, { name: localName });
2631
const importedName = specifier.type === "ImportSpecifier" && specifier.imported?.name;
2732

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,42 @@ export const importSpecifierCompareFn = (
99
specifier2: ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier
1010
) => {
1111
if (specifier1.type === "ImportSpecifier" && specifier2.type === "ImportSpecifier") {
12-
return specifier1.imported.name.localeCompare(specifier2.imported.name);
12+
const specifier1ImportedName = specifier1.imported.name;
13+
const specifier2ImportedName = specifier2.imported.name;
14+
if (typeof specifier1ImportedName !== "string" || typeof specifier2ImportedName !== "string") {
15+
throw new Error("Please report your use case on https://github.com/aws/aws-sdk-js-codemod");
16+
}
17+
return specifier1ImportedName.localeCompare(specifier2ImportedName);
1318
}
1419

1520
if (
1621
specifier1.type === "ImportDefaultSpecifier" &&
1722
specifier2.type === "ImportDefaultSpecifier"
1823
) {
19-
if (specifier1.local && specifier2.local)
20-
return specifier1.local.name.localeCompare(specifier2.local.name);
21-
return 0;
24+
if (!specifier1.local || !specifier2.local) {
25+
return 0;
26+
}
27+
const specifier1LocalName = specifier1.local.name;
28+
const specifier2LocalName = specifier2.local.name;
29+
if (typeof specifier1LocalName !== "string" || typeof specifier2LocalName !== "string") {
30+
throw new Error("Please report your use case on https://github.com/aws/aws-sdk-js-codemod");
31+
}
32+
return specifier1LocalName.localeCompare(specifier2LocalName);
2233
}
2334

2435
if (
2536
specifier1.type === "ImportNamespaceSpecifier" &&
2637
specifier2.type === "ImportNamespaceSpecifier"
2738
) {
28-
if (specifier1.local && specifier2.local)
29-
return specifier1.local.name.localeCompare(specifier2.local.name);
30-
return 0;
39+
if (!specifier1.local || !specifier2.local) {
40+
return 0;
41+
}
42+
const specifier1LocalName = specifier1.local.name;
43+
const specifier2LocalName = specifier2.local.name;
44+
if (typeof specifier1LocalName !== "string" || typeof specifier2LocalName !== "string") {
45+
throw new Error("Please report your use case on https://github.com/aws/aws-sdk-js-codemod");
46+
}
47+
return specifier1LocalName.localeCompare(specifier2LocalName);
3148
}
3249

3350
return 0;

0 commit comments

Comments
 (0)