Skip to content

Commit 2ea4ce2

Browse files
refator: transformer util function
1 parent 7ba1555 commit 2ea4ce2

5 files changed

Lines changed: 44 additions & 24 deletions

File tree

package/src/transformers/change-props-value/transformer.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { OptionsSchema } from "./optionsSchema";
22
import getConvertedPath from "../../utils/getConvertedPath";
33
import type { API, FileInfo } from "jscodeshift";
4+
import generateLiteralNode from "../../utils/jscodeshift/generateLiteralNode";
5+
46
function transformer(file: FileInfo, api: API, options: OptionsSchema) {
57
const sourceCode = file.source;
68
const jscodeshift = api.jscodeshift;
@@ -20,22 +22,6 @@ function transformer(file: FileInfo, api: API, options: OptionsSchema) {
2022
targetPath: componentSource,
2123
});
2224

23-
const getPropValueNode = () => {
24-
if (typeof propsValue === "string") {
25-
return jscodeshift.stringLiteral(propsValue);
26-
}
27-
28-
if (typeof propsValue === "boolean") {
29-
return jscodeshift.booleanLiteral(propsValue);
30-
}
31-
32-
if (typeof propsValue === "number") {
33-
return jscodeshift.numericLiteral(propsValue);
34-
}
35-
36-
return jscodeshift.literal(propsValue);
37-
};
38-
3925
let convertedComponentName: null | string = null;
4026

4127
jscodeshift(sourceCode)
@@ -107,7 +93,7 @@ function transformer(file: FileInfo, api: API, options: OptionsSchema) {
10793

10894
if (attribute?.type === "JSXAttribute") {
10995
attribute.value = jscodeshift.jsxExpressionContainer(
110-
getPropValueNode()
96+
generateLiteralNode(propsValue)
11197
);
11298
}
11399
})

package/src/transformers/change-source/transformer.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,26 @@ function transformer(file: FileInfo, api: API, options: OptionsSchema) {
77
const sourceCode = file.source;
88
const jscodeshift = api.jscodeshift;
99
const { sourceType = "absolute", fromSource, toSource } = options;
10+
const root = jscodeshift(sourceCode);
1011

1112
const convertedFromPath = getConvertedPath({
1213
type: sourceType,
1314
currentPath: file.path,
1415
targetPath: fromSource,
1516
});
17+
1618
const convertedToPath = getConvertedPath({
1719
type: sourceType,
1820
currentPath: file.path,
1921
targetPath: toSource,
2022
});
2123

22-
const changedImportDeclarationSourceCode = jscodeshift(sourceCode)
24+
root
2325
.find(jscodeshift.ImportDeclaration)
2426
.filter((node) => node.value.source.value === convertedFromPath)
25-
.forEach((node) => (node.value.source.value = convertedToPath))
26-
.toSource();
27+
.forEach((node) => (node.value.source.value = convertedToPath));
2728

28-
return jscodeshift(changedImportDeclarationSourceCode)
29+
root
2930
.find(jscodeshift.CallExpression, (node) => node.callee.type === "Import")
3031
.filter(
3132
(node) =>
@@ -36,8 +37,9 @@ function transformer(file: FileInfo, api: API, options: OptionsSchema) {
3637
(node) =>
3738
node.value.arguments[0].type === "StringLiteral" &&
3839
(node.value.arguments[0].value = options.toSource)
39-
)
40-
.toSource();
40+
);
41+
42+
return root.toSource();
4143
}
4244

4345
export default transformer;

package/src/transformers/remove-import/transformer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ function transformer(file: FileInfo, api: API, options: OptionsSchema) {
77
const sourceCode = file.source;
88
const jscodeshift = api.jscodeshift;
99
const { sourceType = "absolute", source } = options;
10+
const root = jscodeshift(sourceCode);
1011

1112
const convertedToPath = getConvertedPath({
1213
type: sourceType,
1314
currentPath: file.path,
1415
targetPath: source,
1516
});
1617

17-
return jscodeshift(sourceCode)
18+
return root
1819
.find(jscodeshift.ImportDeclaration)
1920
.filter(
2021
(node) =>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import jscodeshift from "jscodeshift";
2+
3+
function generateLiteralNode(
4+
value: string | number | boolean | RegExp | bigint | null
5+
) {
6+
if (typeof value === "string") {
7+
return jscodeshift.stringLiteral(value);
8+
}
9+
10+
if (typeof value === "boolean") {
11+
return jscodeshift.booleanLiteral(value);
12+
}
13+
14+
if (typeof value === "number") {
15+
return jscodeshift.numericLiteral(value);
16+
}
17+
18+
return jscodeshift.literal(value);
19+
}
20+
21+
export default generateLiteralNode;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ASTPath } from "jscodeshift";
2+
import { namedTypes } from "ast-types";
3+
4+
function isImportDeclarationNoneSpecifier(
5+
node: ASTPath<namedTypes.ImportDeclaration>
6+
) {
7+
return !node.value.specifiers?.length;
8+
}
9+
10+
export default isImportDeclarationNoneSpecifier;

0 commit comments

Comments
 (0)