Skip to content

Commit d57547b

Browse files
authored
Remove references to client while adding named import/requires (#378)
1 parent cf016a0 commit d57547b

File tree

6 files changed

+42
-28
lines changed

6 files changed

+42
-28
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export const addV3ClientImportEquals = (
2222

2323
if (newExpressionCount > 0 || clientTSTypeRefCount > 0) {
2424
addV3ClientDefaultImportEquals(j, source, options);
25-
addV3ClientNamedImportEquals(j, source, options);
25+
addV3ClientNamedImportEquals(j, source, {
26+
...options,
27+
keyName: options.v3ClientName,
28+
valueName: options.v2ClientLocalName,
29+
});
2630
}
2731
};

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export const addV3ClientImports = (
2222
}
2323

2424
if (newExpressionCount > 0 || clientTSTypeRefCount > 0) {
25-
addV3ClientNamedImport(j, source, options);
25+
addV3ClientNamedImport(j, source, {
26+
...options,
27+
importedName: options.v3ClientName,
28+
localName: options.v2ClientLocalName,
29+
});
2630
}
2731
};

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ import { Collection, ImportSpecifier, JSCodeshift } from "jscodeshift";
33
import { getImportSpecifiers } from "./getImportSpecifiers";
44
import { getV2ImportDeclaration } from "./getV2ImportDeclaration";
55
import { getV3ClientImportSpecifier } from "./getV3ClientImportSpecifier";
6-
import { V3ClientModulesOptions } from "./types";
6+
import { V3ClientImportSpecifierOptions, V3ClientModulesOptions } from "./types";
77

88
export const addV3ClientNamedImport = (
99
j: JSCodeshift,
1010
source: Collection<unknown>,
11-
{ v2ClientName, v2ClientLocalName, v3ClientPackageName, v3ClientName }: V3ClientModulesOptions
11+
{
12+
localName,
13+
importedName,
14+
v2ClientName,
15+
v2ClientLocalName,
16+
v3ClientPackageName,
17+
}: V3ClientModulesOptions & V3ClientImportSpecifierOptions
1218
) => {
1319
const importDeclarations = source.find(j.ImportDeclaration, {
1420
source: { value: v3ClientPackageName },
@@ -22,17 +28,15 @@ export const addV3ClientNamedImport = (
2228
if (
2329
importSpecifiers.find(
2430
(specifier) =>
25-
specifier?.imported?.name === v3ClientName && specifier?.local?.name === v2ClientLocalName
31+
specifier?.imported?.name === importedName && specifier?.local?.name === localName
2632
)
2733
) {
2834
return;
2935
}
3036

3137
importDeclarations
3238
.nodes()[0]
33-
.specifiers?.push(
34-
getV3ClientImportSpecifier(j, { localName: v2ClientLocalName, importedName: v3ClientName })
35-
);
39+
.specifiers?.push(getV3ClientImportSpecifier(j, { importedName, localName }));
3640
return;
3741
}
3842

@@ -43,12 +47,7 @@ export const addV3ClientNamedImport = (
4347
});
4448

4549
const importDeclaration = j.importDeclaration(
46-
[
47-
getV3ClientImportSpecifier(j, {
48-
localName: v2ClientLocalName,
49-
importedName: v3ClientName,
50-
}),
51-
],
50+
[getV3ClientImportSpecifier(j, { importedName, localName })],
5251
j.stringLiteral(v3ClientPackageName)
5352
);
5453

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

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

33
import { getV3ClientDefaultLocalName } from "../utils";
44
import { getImportEqualsDeclaration } from "./getImportEqualsDeclaration";
5-
import { V3ClientModulesOptions } from "./types";
5+
import { V3ClientModulesOptions, V3ClientRequirePropertyOptions } from "./types";
66

77
export const addV3ClientNamedImportEquals = (
88
j: JSCodeshift,
99
source: Collection<unknown>,
10-
{ v2ClientLocalName, v3ClientName, v3ClientPackageName }: V3ClientModulesOptions
10+
{
11+
keyName,
12+
valueName,
13+
v2ClientLocalName,
14+
v3ClientPackageName,
15+
}: V3ClientModulesOptions & V3ClientRequirePropertyOptions
1116
) => {
1217
const v3ClientDefaultLocalName = getV3ClientDefaultLocalName(v2ClientLocalName);
1318
const existingImportEquals = source.find(
@@ -19,8 +24,8 @@ export const addV3ClientNamedImportEquals = (
1924
j.variableDeclarator(
2025
j.objectPattern([
2126
j.objectProperty.from({
22-
key: j.identifier(v3ClientName),
23-
value: j.identifier(v2ClientLocalName),
27+
key: j.identifier(keyName),
28+
value: j.identifier(valueName),
2429
shorthand: true,
2530
}),
2631
]),

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,22 @@ import { getRequireDeclarators } from "./getRequireDeclarators";
55
import { getRequireDeclaratorsWithIdentifier } from "./getRequireDeclaratorsWithIdentifier";
66
import { getV2RequireDeclarator } from "./getV2RequireDeclarator";
77
import { getV3ClientRequireProperty } from "./getV3ClientRequireProperty";
8-
import { V3ClientModulesOptions } from "./types";
8+
import { V3ClientModulesOptions, V3ClientRequirePropertyOptions } from "./types";
99

1010
export const addV3ClientNamedRequire = (
1111
j: JSCodeshift,
1212
source: Collection<unknown>,
1313
{
14+
keyName,
15+
valueName,
1416
v2ClientName,
1517
v2ClientLocalName,
1618
v2GlobalName,
17-
v3ClientName,
1819
v3ClientPackageName,
19-
}: V3ClientModulesOptions
20+
}: V3ClientModulesOptions & V3ClientRequirePropertyOptions
2021
) => {
2122
const v3ClientDefaultLocalName = getV3ClientDefaultLocalName(v2ClientLocalName);
22-
const v3ClientObjectProperty = getV3ClientRequireProperty(j, {
23-
keyName: v3ClientName,
24-
valueName: v2ClientLocalName,
25-
});
23+
const v3ClientObjectProperty = getV3ClientRequireProperty(j, { keyName, valueName });
2624
const existingRequires = getRequireDeclarators(j, source, v3ClientPackageName);
2725

2826
if (existingRequires && existingRequires.nodes().length > 0) {
@@ -40,8 +38,8 @@ export const addV3ClientNamedRequire = (
4038
property.type === "Property" &&
4139
property.key.type === "Identifier" &&
4240
property.value.type === "Identifier" &&
43-
property.key.name === v3ClientName &&
44-
property.value.name === v2ClientLocalName
41+
property.key.name === keyName &&
42+
property.value.name === valueName
4543
)
4644
)
4745
) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export const addV3ClientRequires = (
2121
}
2222

2323
if (newExpressionCount > 0 || clientTSTypeRefCount > 0) {
24-
addV3ClientNamedRequire(j, source, options);
24+
addV3ClientNamedRequire(j, source, {
25+
...options,
26+
keyName: options.v3ClientName,
27+
valueName: options.v2ClientLocalName,
28+
});
2529
}
2630
};

0 commit comments

Comments
 (0)