@pob/codemods 0.0.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
## [0.1.2](https://github.com/christophehurpeau/pob/compare/@pob/codemods@0.1.1...@pob/codemods@0.1.2) (2024-07-27)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* **deps:** update dependency glob to v10.4.2 ([#2104](https://github.com/christophehurpeau/pob/issues/2104)) ([4c4ae33](https://github.com/christophehurpeau/pob/commit/4c4ae339d047027c0d7ff8c55167b31573b37711))
|
|
11
|
+
* **deps:** update dependency glob to v10.4.3 ([#2130](https://github.com/christophehurpeau/pob/issues/2130)) ([f179294](https://github.com/christophehurpeau/pob/commit/f179294dd405ee64d0376835caaaee503d67de41))
|
|
12
|
+
* **deps:** update dependency glob to v10.4.4 ([#2133](https://github.com/christophehurpeau/pob/issues/2133)) ([0989105](https://github.com/christophehurpeau/pob/commit/098910588a434b4829207d6de0c65066adaabe4b))
|
|
13
|
+
* **deps:** update dependency glob to v10.4.5 ([#2138](https://github.com/christophehurpeau/pob/issues/2138)) ([58b2d12](https://github.com/christophehurpeau/pob/commit/58b2d12c103eb55e5fe1820c2aff6ad65efd36a1))
|
|
14
|
+
|
|
15
|
+
## [0.1.1](https://github.com/christophehurpeau/pob/compare/@pob/codemods@0.0.0...@pob/codemods@0.1.1) (2024-06-07)
|
|
16
|
+
|
|
17
|
+
Note: no notable changes
|
|
18
|
+
|
|
19
|
+
|
package/bin/migrate.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import { readdirSync } from
|
|
4
|
-
import { fileURLToPath } from
|
|
5
|
-
import { glob } from
|
|
6
|
-
import runJscodeshift from
|
|
3
|
+
import { readdirSync } from "node:fs";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { glob } from "glob";
|
|
6
|
+
import runJscodeshift from "../lib/runJscodeshift.cjs";
|
|
7
7
|
|
|
8
8
|
const argv = process.argv.slice(2);
|
|
9
9
|
|
|
@@ -12,12 +12,12 @@ const globPaths = argv[1];
|
|
|
12
12
|
|
|
13
13
|
if (!transformerName || !globPaths) {
|
|
14
14
|
const transformerList = readdirSync(
|
|
15
|
-
new URL(
|
|
15
|
+
new URL("../lib/transforms/", import.meta.url),
|
|
16
16
|
);
|
|
17
17
|
throw new Error(
|
|
18
18
|
`Usage: pob-migrate <transformer-name> <glob-paths>\n${transformerList
|
|
19
19
|
.map((t) => `- "${t.slice(0, -4)}"`)
|
|
20
|
-
.join(
|
|
20
|
+
.join("\n")}`,
|
|
21
21
|
);
|
|
22
22
|
}
|
|
23
23
|
|
|
@@ -34,4 +34,4 @@ const res = await runJscodeshift(transformPath, paths, {});
|
|
|
34
34
|
if (res.errors) {
|
|
35
35
|
process.exitCode = 1;
|
|
36
36
|
}
|
|
37
|
-
console.log(
|
|
37
|
+
console.log("Done!");
|
package/lib/runJscodeshift.cjs
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
/* eslint-disable unicorn/prefer-array-some */
|
|
2
2
|
/* eslint-disable unicorn/no-array-method-this-argument */
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
"use strict";
|
|
5
5
|
|
|
6
|
-
const { prependImport } = require(
|
|
6
|
+
const { prependImport } = require("../utils/prependImport.cjs");
|
|
7
7
|
|
|
8
8
|
const jestToNodeGlobalMethods = {
|
|
9
|
-
test:
|
|
10
|
-
describe:
|
|
11
|
-
it:
|
|
12
|
-
beforeAll:
|
|
13
|
-
afterAll:
|
|
14
|
-
beforeEach:
|
|
15
|
-
afterEach:
|
|
9
|
+
test: "test",
|
|
10
|
+
describe: "describe",
|
|
11
|
+
it: "it",
|
|
12
|
+
beforeAll: "before",
|
|
13
|
+
afterAll: "after",
|
|
14
|
+
beforeEach: "beforeEach",
|
|
15
|
+
afterEach: "afterEach",
|
|
16
16
|
};
|
|
17
17
|
const propertiesMap = {
|
|
18
|
-
toBe: { default:
|
|
19
|
-
toStrictEqual: { default:
|
|
20
|
-
toEqual: { default:
|
|
18
|
+
toBe: { default: "equal", not: "notEqual" },
|
|
19
|
+
toStrictEqual: { default: "equal", not: "notEqual" },
|
|
20
|
+
toEqual: { default: "equal", not: "notEqual" },
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
// const mocks = {
|
|
@@ -29,15 +29,15 @@ const propertiesMap = {
|
|
|
29
29
|
module.exports = (file, api, options) => {
|
|
30
30
|
if (
|
|
31
31
|
!(
|
|
32
|
-
file.path.endsWith(
|
|
33
|
-
file.path.endsWith(
|
|
34
|
-
file.path.endsWith(
|
|
32
|
+
file.path.endsWith(".test.js") ||
|
|
33
|
+
file.path.endsWith(".test.ts") ||
|
|
34
|
+
file.path.endsWith(".test.tsx")
|
|
35
35
|
)
|
|
36
36
|
) {
|
|
37
37
|
throw new Error(`Invalid file: "${file.path}"`);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
const j = api.jscodeshift.withParser(
|
|
40
|
+
const j = api.jscodeshift.withParser("tsx");
|
|
41
41
|
const root = j(file.source);
|
|
42
42
|
|
|
43
43
|
function updateTopLevelMethods() {
|
|
@@ -68,7 +68,7 @@ module.exports = (file, api, options) => {
|
|
|
68
68
|
[...topLevelMethodsUsed].map((method) =>
|
|
69
69
|
j.importSpecifier(j.identifier(method)),
|
|
70
70
|
),
|
|
71
|
-
j.stringLiteral(
|
|
71
|
+
j.stringLiteral("node:test"),
|
|
72
72
|
),
|
|
73
73
|
);
|
|
74
74
|
}
|
|
@@ -80,7 +80,7 @@ module.exports = (file, api, options) => {
|
|
|
80
80
|
root
|
|
81
81
|
.find(j.MemberExpression, {
|
|
82
82
|
object: {
|
|
83
|
-
callee: { name:
|
|
83
|
+
callee: { name: "expect" },
|
|
84
84
|
},
|
|
85
85
|
})
|
|
86
86
|
.forEach((memberExpressionPath) => {
|
|
@@ -88,7 +88,7 @@ module.exports = (file, api, options) => {
|
|
|
88
88
|
let memberExpressionPathToReplace = memberExpressionPath;
|
|
89
89
|
let propertyName = memberExpression.property.name;
|
|
90
90
|
let not = false;
|
|
91
|
-
if (propertyName ===
|
|
91
|
+
if (propertyName === "not") {
|
|
92
92
|
not = true;
|
|
93
93
|
memberExpressionPathToReplace = parentPath;
|
|
94
94
|
propertyName = parentPath.value.property.name;
|
|
@@ -100,34 +100,35 @@ module.exports = (file, api, options) => {
|
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
const assertPropertyName =
|
|
103
|
-
propertiesMap[propertyName][not ?
|
|
103
|
+
propertiesMap[propertyName][not ? "not" : "default"];
|
|
104
104
|
const callExpressionPath = memberExpressionPathToReplace.parentPath;
|
|
105
105
|
|
|
106
|
-
const
|
|
107
|
-
const
|
|
106
|
+
const value = memberExpression.object.arguments[0];
|
|
107
|
+
const expected = callExpressionPath.value.arguments[0];
|
|
108
108
|
|
|
109
109
|
hasAssertions = true;
|
|
110
110
|
callExpressionPath.value.callee = j.memberExpression(
|
|
111
|
-
j.identifier(
|
|
111
|
+
j.identifier("assert"),
|
|
112
112
|
j.identifier(assertPropertyName),
|
|
113
113
|
);
|
|
114
|
-
callExpressionPath.value.arguments = [
|
|
114
|
+
callExpressionPath.value.arguments = [value, expected];
|
|
115
115
|
});
|
|
116
116
|
|
|
117
117
|
if (hasAssertions) {
|
|
118
118
|
root
|
|
119
|
-
.find(j.ImportDeclaration, { source: { value:
|
|
119
|
+
.find(j.ImportDeclaration, { source: { value: "node:assert" } })
|
|
120
120
|
.remove();
|
|
121
121
|
if (
|
|
122
|
+
// eslint-disable-next-line no-constant-condition
|
|
122
123
|
(!root.find(j.ImportDeclaration),
|
|
123
|
-
{ source: { value:
|
|
124
|
+
{ source: { value: "node:assert/strict" } })
|
|
124
125
|
) {
|
|
125
126
|
prependImport(
|
|
126
127
|
j,
|
|
127
128
|
root,
|
|
128
129
|
j.importDeclaration(
|
|
129
|
-
[j.importDefaultSpecifier(j.identifier(
|
|
130
|
-
j.stringLiteral(
|
|
130
|
+
[j.importDefaultSpecifier(j.identifier("assert"))],
|
|
131
|
+
j.stringLiteral("node:assert/strict"),
|
|
131
132
|
),
|
|
132
133
|
);
|
|
133
134
|
}
|
|
@@ -138,7 +139,7 @@ module.exports = (file, api, options) => {
|
|
|
138
139
|
updateAssertions();
|
|
139
140
|
|
|
140
141
|
root
|
|
141
|
-
.find(j.ImportDeclaration, { source: { value:
|
|
142
|
+
.find(j.ImportDeclaration, { source: { value: "@jest/globals" } })
|
|
142
143
|
.remove();
|
|
143
144
|
|
|
144
145
|
return root.toSource(options);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
/* eslint-disable complexity */
|
|
4
4
|
module.exports = (file, api, options) => {
|
|
5
|
-
const j = api.jscodeshift.withParser(
|
|
5
|
+
const j = api.jscodeshift.withParser("tsx");
|
|
6
6
|
const root = j(file.source);
|
|
7
7
|
let hasModifications = false;
|
|
8
8
|
|
|
@@ -10,38 +10,38 @@ module.exports = (file, api, options) => {
|
|
|
10
10
|
const node = path.value;
|
|
11
11
|
const { returnType } = node;
|
|
12
12
|
if (returnType == null) return;
|
|
13
|
-
if (returnType.type !==
|
|
13
|
+
if (returnType.type !== "TSTypeAnnotation") return;
|
|
14
14
|
const { typeAnnotation } = returnType;
|
|
15
15
|
if (typeAnnotation == null) return;
|
|
16
16
|
|
|
17
17
|
if (
|
|
18
|
-
typeAnnotation.type ===
|
|
18
|
+
typeAnnotation.type === "TSTypeReference" &&
|
|
19
19
|
typeAnnotation.typeName &&
|
|
20
|
-
typeAnnotation.typeName.type ===
|
|
20
|
+
typeAnnotation.typeName.type === "Identifier"
|
|
21
21
|
) {
|
|
22
|
-
if (typeAnnotation.typeName.name ===
|
|
23
|
-
typeAnnotation.typeName.name =
|
|
22
|
+
if (typeAnnotation.typeName.name === "ReactElement") {
|
|
23
|
+
typeAnnotation.typeName.name = "ReactNode";
|
|
24
24
|
hasModifications = true;
|
|
25
25
|
}
|
|
26
26
|
} else if (
|
|
27
|
-
typeAnnotation.type ===
|
|
27
|
+
typeAnnotation.type === "TSUnionType" &&
|
|
28
28
|
typeAnnotation.types.every((unionTypeItem) => {
|
|
29
|
-
if (unionTypeItem.type ===
|
|
29
|
+
if (unionTypeItem.type === "TSTypeReference") {
|
|
30
30
|
return (
|
|
31
31
|
unionTypeItem.typeName &&
|
|
32
|
-
unionTypeItem.typeName.type ===
|
|
33
|
-
unionTypeItem.typeName.name ===
|
|
32
|
+
unionTypeItem.typeName.type === "Identifier" &&
|
|
33
|
+
unionTypeItem.typeName.name === "ReactElement"
|
|
34
34
|
);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
if (unionTypeItem.type ===
|
|
37
|
+
if (unionTypeItem.type === "TSNullKeyword") {
|
|
38
38
|
return true;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
return false;
|
|
42
42
|
})
|
|
43
43
|
) {
|
|
44
|
-
returnType.typeAnnotation = j.tsTypeReference(j.identifier(
|
|
44
|
+
returnType.typeAnnotation = j.tsTypeReference(j.identifier("ReactNode"));
|
|
45
45
|
hasModifications = true;
|
|
46
46
|
}
|
|
47
47
|
}
|
|
@@ -51,23 +51,23 @@ module.exports = (file, api, options) => {
|
|
|
51
51
|
if (hasModifications) {
|
|
52
52
|
root.find(j.ImportDeclaration).forEach((importDeclaration) => {
|
|
53
53
|
const { node } = importDeclaration;
|
|
54
|
-
if (node.source.value !==
|
|
55
|
-
if (node.importKind !==
|
|
54
|
+
if (node.source.value !== "react") return;
|
|
55
|
+
if (node.importKind !== "type") return;
|
|
56
56
|
|
|
57
57
|
node.specifiers = node.specifiers.filter((importSpecifier) => {
|
|
58
|
-
if (importSpecifier.local.name !==
|
|
59
|
-
if (importSpecifier.importKind !==
|
|
58
|
+
if (importSpecifier.local.name !== "ReactElement") return true;
|
|
59
|
+
if (importSpecifier.importKind !== "value") return true;
|
|
60
60
|
return (
|
|
61
61
|
j(importDeclaration)
|
|
62
62
|
.closestScope()
|
|
63
63
|
// eslint-disable-next-line unicorn/no-array-method-this-argument -- not an array
|
|
64
|
-
.find(j.Identifier, { name:
|
|
64
|
+
.find(j.Identifier, { name: "ReactElement" })
|
|
65
65
|
.filter((p) => {
|
|
66
66
|
if (p.value.start === importSpecifier.local.start) return false;
|
|
67
|
-
if (p.parentPath.value.type ===
|
|
67
|
+
if (p.parentPath.value.type === "Property" && p.name === "key") {
|
|
68
68
|
return false;
|
|
69
69
|
}
|
|
70
|
-
if (p.name ===
|
|
70
|
+
if (p.name === "property") return false;
|
|
71
71
|
return true;
|
|
72
72
|
})
|
|
73
73
|
.size() > 0
|
|
@@ -76,11 +76,11 @@ module.exports = (file, api, options) => {
|
|
|
76
76
|
if (
|
|
77
77
|
!node.specifiers.some(
|
|
78
78
|
(specifier) =>
|
|
79
|
-
specifier.type ===
|
|
80
|
-
specifier.imported.name ===
|
|
79
|
+
specifier.type === "ImportSpecifier" &&
|
|
80
|
+
specifier.imported.name === "ReactNode",
|
|
81
81
|
)
|
|
82
82
|
) {
|
|
83
|
-
node.specifiers.push(j.importSpecifier(j.identifier(
|
|
83
|
+
node.specifiers.push(j.importSpecifier(j.identifier("ReactNode")));
|
|
84
84
|
}
|
|
85
85
|
});
|
|
86
86
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
exports.prependImport = (j, source, importDeclaration) => {
|
|
4
4
|
const existingImports = source.find(j.ImportDeclaration);
|
|
@@ -14,7 +14,7 @@ exports.prependImport = (j, source, importDeclaration) => {
|
|
|
14
14
|
return;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
const firstNode = source.find(j.Program).get(
|
|
17
|
+
const firstNode = source.find(j.Program).get("body", 0).node;
|
|
18
18
|
const { comments } = firstNode;
|
|
19
19
|
if (comments?.length) {
|
|
20
20
|
const comment = comments[0];
|
|
@@ -23,13 +23,13 @@ exports.prependImport = (j, source, importDeclaration) => {
|
|
|
23
23
|
// line-level and JSDoc-style comments because these probably belong
|
|
24
24
|
// to the first node, rather than the file.
|
|
25
25
|
if (
|
|
26
|
-
(comment.type ===
|
|
27
|
-
!comment.value.startsWith(
|
|
26
|
+
(comment.type === "Block" || comment.type === "CommentBlock") &&
|
|
27
|
+
!comment.value.startsWith("*")
|
|
28
28
|
) {
|
|
29
29
|
importDeclaration.comments = comments;
|
|
30
30
|
firstNode.comments = null;
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
source.get(
|
|
34
|
+
source.get("program", "body").unshift(importDeclaration);
|
|
35
35
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pob/codemods",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "codemods for pob",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"author": "Christophe Hurpeau <302891+christophehurpeau@users.noreply.github.com> (https://christophe.hurpeau.com)",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"pob": {},
|
|
35
35
|
"prettier": "@pob/root/prettier-config",
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"glob": "10.
|
|
38
|
-
"jscodeshift": "0.15.
|
|
37
|
+
"glob": "10.4.5",
|
|
38
|
+
"jscodeshift": "0.15.2"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/jscodeshift": "0.11.11"
|