Skip to content

Commit ef36957

Browse files
authored
Add utility removeImportDefault & removeImportNamed (#296)
1 parent 8b48b59 commit ef36957

File tree

4 files changed

+62
-43
lines changed

4 files changed

+62
-43
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
export interface RemoveImportDefaultOptions {
4+
localName: string;
5+
sourceValue: string;
6+
}
7+
8+
export const removeImportDefault = (
9+
j: JSCodeshift,
10+
source: Collection<unknown>,
11+
{ localName, sourceValue }: RemoveImportDefaultOptions
12+
) => {
13+
source
14+
.find(j.ImportDeclaration, {
15+
specifiers: [{ local: { name: localName } }],
16+
source: { value: sourceValue },
17+
})
18+
.forEach((declarationPath) => {
19+
// Remove default/namespace import from ImportDeclaration if there is a match
20+
declarationPath.value.specifiers = declarationPath.value.specifiers?.filter((specifier) => {
21+
if (!["ImportDefaultSpecifier", "ImportNamespaceSpecifier"].includes(specifier.type)) {
22+
return true;
23+
}
24+
return specifier.local?.name !== localName;
25+
});
26+
27+
// Remove ImportDeclaration if there are no import specifiers.
28+
if (declarationPath.value.specifiers?.length === 0) {
29+
j(declarationPath).remove();
30+
}
31+
});
32+
};

src/transforms/v2-to-v3/modules/removeImportIdentifierName.ts renamed to src/transforms/v2-to-v3/modules/removeImportNamed.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
22

3-
export interface RemoveImportIdentifierNameOptions {
3+
export interface RemoveImportNamedOptions {
44
importedName?: string;
55
localName: string;
66
sourceValue: string;
77
}
88

9-
export const removeImportIdentifierName = (
9+
export const removeImportNamed = (
1010
j: JSCodeshift,
1111
source: Collection<unknown>,
12-
{ importedName, localName, sourceValue }: RemoveImportIdentifierNameOptions
12+
{ importedName, localName, sourceValue }: RemoveImportNamedOptions
1313
) => {
1414
source
1515
.find(j.ImportDeclaration, {
1616
specifiers: [{ local: { name: localName } }],
1717
source: { value: sourceValue },
1818
})
1919
.forEach((declarationPath) => {
20-
// Remove import from ImportDeclaration.
20+
// Remove named import from ImportDeclaration if there is a match.
2121
declarationPath.value.specifiers = declarationPath.value.specifiers?.filter((specifier) => {
22-
if (specifier.local?.name === localName) {
23-
if (specifier.type === "ImportSpecifier" && importedName) {
24-
return specifier.imported?.name === importedName;
25-
}
26-
return false;
22+
if (specifier.type !== "ImportSpecifier") {
23+
return true;
2724
}
25+
return (
26+
specifier.local?.name !== localName ||
27+
(importedName && specifier.imported?.name !== importedName)
28+
);
2829
});
29-
// Remove ImportDeclaration if there are no other imports.
30+
31+
// Remove ImportDeclaration if there are no import specifiers.
3032
if (declarationPath.value.specifiers?.length === 0) {
3133
j(declarationPath).remove();
3234
}

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

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { getV2ClientTypeNames } from "../ts-type";
55
import { getV2ServiceModulePath } from "../utils";
66
import { hasImportEquals } from "./hasImportEquals";
77
import { hasRequire } from "./hasRequire";
8+
import { removeImportDefault } from "./removeImportDefault";
89
import { removeImportEqualsIdentifierName } from "./removeImportEqualsIdentifierName";
9-
import { removeImportIdentifierName } from "./removeImportIdentifierName";
10+
import { removeImportNamed } from "./removeImportNamed";
1011
import { removeRequireIdentifier } from "./removeRequireIdentifier";
1112
import { removeRequireObjectProperty } from "./removeRequireObjectProperty";
1213

@@ -23,37 +24,24 @@ export const removeV2ClientModule = (
2324
) => {
2425
const { v2ClientName, v2ClientLocalName } = options;
2526
const serviceModulePath = getV2ServiceModulePath(v2ClientName);
26-
const sourceValues = [PACKAGE_NAME, serviceModulePath];
27+
28+
const defaultOptions = { localName: v2ClientLocalName, sourceValue: serviceModulePath };
29+
const namedOptions = { localName: v2ClientLocalName, sourceValue: PACKAGE_NAME };
2730

2831
if (hasRequire(j, source)) {
29-
removeRequireIdentifier(j, source, {
30-
localName: v2ClientLocalName,
31-
sourceValue: serviceModulePath,
32-
});
33-
removeRequireObjectProperty(j, source, {
34-
localName: v2ClientLocalName,
35-
sourceValue: PACKAGE_NAME,
36-
});
32+
removeRequireIdentifier(j, source, defaultOptions);
33+
removeRequireObjectProperty(j, source, namedOptions);
3734
} else if (hasImportEquals(j, source)) {
38-
removeImportEqualsIdentifierName(j, source, {
39-
localName: v2ClientLocalName,
40-
sourceValue: serviceModulePath,
41-
});
35+
removeImportEqualsIdentifierName(j, source, defaultOptions);
4236
} else {
43-
sourceValues.forEach((sourceValue) => {
44-
removeImportIdentifierName(j, source, {
45-
localName: v2ClientLocalName,
46-
sourceValue,
47-
});
48-
});
37+
removeImportDefault(j, source, defaultOptions);
38+
removeImportNamed(j, source, namedOptions);
4939

5040
const v2ClientTypeNames = getV2ClientTypeNames(j, source, options);
5141
for (const v2ClientTypeName of v2ClientTypeNames) {
52-
sourceValues.forEach((sourceValue) => {
53-
removeImportIdentifierName(j, source, {
54-
localName: v2ClientTypeName,
55-
sourceValue,
56-
});
42+
removeImportNamed(j, source, {
43+
localName: v2ClientTypeName,
44+
sourceValue: serviceModulePath,
5745
});
5846
}
5947
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { Collection, JSCodeshift } from "jscodeshift";
33
import { PACKAGE_NAME } from "../config";
44
import { hasImportEquals } from "./hasImportEquals";
55
import { hasRequire } from "./hasRequire";
6+
import { removeImportDefault } from "./removeImportDefault";
67
import { removeImportEqualsIdentifierName } from "./removeImportEqualsIdentifierName";
7-
import { removeImportIdentifierName } from "./removeImportIdentifierName";
88
import { removeRequireIdentifier } from "./removeRequireIdentifier";
99

1010
export const removeV2GlobalModule = (
@@ -16,16 +16,13 @@ export const removeV2GlobalModule = (
1616

1717
// Only usage is import/require.
1818
if (identifierUsages.size() === 1) {
19-
const removeIdentifierNameOptions = {
20-
localName: v2GlobalName,
21-
sourceValue: PACKAGE_NAME,
22-
};
19+
const defaultOptions = { localName: v2GlobalName, sourceValue: PACKAGE_NAME };
2320
if (hasRequire(j, source)) {
24-
removeRequireIdentifier(j, source, removeIdentifierNameOptions);
21+
removeRequireIdentifier(j, source, defaultOptions);
2522
} else if (hasImportEquals(j, source)) {
26-
removeImportEqualsIdentifierName(j, source, removeIdentifierNameOptions);
23+
removeImportEqualsIdentifierName(j, source, defaultOptions);
2724
} else {
28-
removeImportIdentifierName(j, source, removeIdentifierNameOptions);
25+
removeImportDefault(j, source, defaultOptions);
2926
}
3027
}
3128
};

0 commit comments

Comments
 (0)