Skip to content

Commit 669ad59

Browse files
authored
Add utility getNewExpressionCount (#308)
1 parent d180fe5 commit 669ad59

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
22

33
import { getV3ClientTypeNames } from "../ts-type";
4-
import { getV2ClientNewExpression } from "../utils";
54
import { addV3ClientDefaultRequire } from "./addV3ClientDefaultRequire";
65
import { addV3ClientNamedRequire } from "./addV3ClientNamedRequire";
6+
import { getNewExpressionCount } from "./getNewExpressionCount";
77
import { V3ClientModulesOptions } from "./types";
88

99
export const addV3ClientRequires = (
1010
j: JSCodeshift,
1111
source: Collection<unknown>,
1212
options: V3ClientModulesOptions
1313
): void => {
14-
const { v2ClientName, v2ClientLocalName, v2GlobalName } = options;
14+
const { v2ClientName, v2GlobalName } = options;
1515
const v3ClientTypeNames = getV3ClientTypeNames(j, source, { v2ClientName, v2GlobalName });
1616

1717
// Add default require for types, if needed.
1818
if (v3ClientTypeNames.length > 0) {
1919
addV3ClientDefaultRequire(j, source, options);
2020
}
2121

22-
const newExpressions = source.find(
23-
j.NewExpression,
24-
getV2ClientNewExpression({ v2ClientName, v2ClientLocalName, v2GlobalName })
25-
);
26-
27-
if (newExpressions.length) {
22+
if (getNewExpressionCount(j, source, options) > 0) {
2823
addV3ClientNamedRequire(j, source, options);
2924
}
3025
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
import { getV2ClientNewExpression } from "../utils";
4+
import { V3ClientModulesOptions } from "./types";
5+
6+
export const getNewExpressionCount = (
7+
j: JSCodeshift,
8+
source: Collection<unknown>,
9+
{ v2ClientName, v2ClientLocalName, v2GlobalName }: V3ClientModulesOptions
10+
): number => {
11+
let newExpressionCount = 0;
12+
13+
if (v2GlobalName) {
14+
const newExpressionsFromGlobal = source.find(
15+
j.NewExpression,
16+
getV2ClientNewExpression({ v2ClientName, v2GlobalName })
17+
);
18+
newExpressionCount += newExpressionsFromGlobal.length;
19+
}
20+
21+
const newExpressionsFromClientLocalName = source.find(
22+
j.NewExpression,
23+
getV2ClientNewExpression({ v2ClientLocalName })
24+
);
25+
newExpressionCount += newExpressionsFromClientLocalName.length;
26+
27+
return newExpressionCount;
28+
};

src/transforms/v2-to-v3/utils/getV2ClientNewExpression.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ export const getV2ClientNewExpression = ({
1717
);
1818
}
1919

20+
if (v2GlobalName && v2ClientLocalName) {
21+
throw new Error(
22+
`Only one of the following options must be provided: v2ClientLocalName, v2GlobalName`
23+
);
24+
}
25+
2026
if (v2GlobalName) {
2127
return {
2228
type: "NewExpression",

0 commit comments

Comments
 (0)