@suspensive/codemods 3.0.0 → 3.1.1
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/dist/bin/codemods.cjs
CHANGED
|
@@ -29,7 +29,7 @@ var import_extra_typings = require("@commander-js/extra-typings");
|
|
|
29
29
|
// package.json
|
|
30
30
|
var package_default = {
|
|
31
31
|
name: "@suspensive/codemods",
|
|
32
|
-
version: "3.
|
|
32
|
+
version: "3.1.1",
|
|
33
33
|
description: "Codemods for @suspensive.",
|
|
34
34
|
keywords: [
|
|
35
35
|
"suspensive",
|
|
@@ -87,7 +87,15 @@ var import_prompts = __toESM(require("prompts"), 1);
|
|
|
87
87
|
var TRANSFORMER_INQUIRER_CHOICES = [
|
|
88
88
|
{
|
|
89
89
|
title: "tanstack-query-import",
|
|
90
|
-
description: "Migrate imports to @tanstack/react-query in @suspensive/react-query
|
|
90
|
+
description: "Migrate imports to @tanstack/react-query in @suspensive/react-query"
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
title: "migrate-query-client-consumer-props",
|
|
94
|
+
description: "Migrate QueryClientConsumer context prop to queryClient"
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
title: "migrate-with-api",
|
|
98
|
+
description: "Migrate wrap API to with API for Suspensive v3"
|
|
91
99
|
}
|
|
92
100
|
];
|
|
93
101
|
function onCancel() {
|
|
@@ -41,7 +41,15 @@ var import_prompts = __toESM(require("prompts"), 1);
|
|
|
41
41
|
var TRANSFORMER_INQUIRER_CHOICES = [
|
|
42
42
|
{
|
|
43
43
|
title: "tanstack-query-import",
|
|
44
|
-
description: "Migrate imports to @tanstack/react-query in @suspensive/react-query
|
|
44
|
+
description: "Migrate imports to @tanstack/react-query in @suspensive/react-query"
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
title: "migrate-query-client-consumer-props",
|
|
48
|
+
description: "Migrate QueryClientConsumer context prop to queryClient"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
title: "migrate-with-api",
|
|
52
|
+
description: "Migrate wrap API to with API for Suspensive v3"
|
|
45
53
|
}
|
|
46
54
|
];
|
|
47
55
|
function onCancel() {
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/transforms/migrate-with-api.ts
|
|
31
|
+
var migrate_with_api_exports = {};
|
|
32
|
+
__export(migrate_with_api_exports, {
|
|
33
|
+
default: () => transformer
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(migrate_with_api_exports);
|
|
36
|
+
|
|
37
|
+
// src/transforms/utils/createParserFromPath.ts
|
|
38
|
+
var import_jscodeshift = __toESM(require("jscodeshift"), 1);
|
|
39
|
+
var import_babylon = __toESM(require("jscodeshift/parser/babylon"), 1);
|
|
40
|
+
var import_tsOptions = __toESM(require("jscodeshift/parser/tsOptions"), 1);
|
|
41
|
+
function createParserFromPath(filePath) {
|
|
42
|
+
const isDeclarationFile = /\.d\.[mc]?ts$/.test(filePath);
|
|
43
|
+
if (isDeclarationFile) {
|
|
44
|
+
return import_jscodeshift.default.withParser(
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call
|
|
46
|
+
(0, import_babylon.default)({
|
|
47
|
+
...import_tsOptions.default,
|
|
48
|
+
plugins: [
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
50
|
+
...import_tsOptions.default.plugins.filter((plugin) => plugin !== "typescript"),
|
|
51
|
+
["typescript", { dts: true }]
|
|
52
|
+
]
|
|
53
|
+
})
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
const isTsFile = /\.[mc]?ts$/.test(filePath);
|
|
57
|
+
return isTsFile ? import_jscodeshift.default.withParser("ts") : import_jscodeshift.default.withParser("tsx");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/transforms/migrate-with-api.ts
|
|
61
|
+
function transformer(file) {
|
|
62
|
+
const j2 = createParserFromPath(file.path);
|
|
63
|
+
const root = j2(file.source);
|
|
64
|
+
const usedSuspensiveAPIs = /* @__PURE__ */ new Set();
|
|
65
|
+
const wrapImportNodes = root.find(j2.ImportDeclaration, { source: { value: "@suspensive/react" } });
|
|
66
|
+
const wrapImportNames = wrapImportNodes.find(j2.ImportSpecifier, { imported: { name: "wrap" } }).nodes().map((spec) => spec.local?.name ?? "wrap");
|
|
67
|
+
for (const path of wrapImportNodes.nodes()) {
|
|
68
|
+
path.specifiers = path.specifiers?.filter(
|
|
69
|
+
(spec) => !(spec.type === "ImportSpecifier" && spec.imported.name === "wrap")
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
const callPaths = root.find(j2.CallExpression, {
|
|
73
|
+
callee: {
|
|
74
|
+
type: "MemberExpression",
|
|
75
|
+
property: { type: "Identifier", name: "on" }
|
|
76
|
+
}
|
|
77
|
+
}).paths();
|
|
78
|
+
for (const path of callPaths) {
|
|
79
|
+
let current = path.node;
|
|
80
|
+
const stages = [];
|
|
81
|
+
while (current.type === "CallExpression" && current.callee.type === "MemberExpression") {
|
|
82
|
+
const prop = current.callee.property;
|
|
83
|
+
if (prop.type === "Identifier") {
|
|
84
|
+
stages.unshift({ name: prop.name, args: current.arguments });
|
|
85
|
+
}
|
|
86
|
+
current = current.callee.object;
|
|
87
|
+
}
|
|
88
|
+
const isWrapRoot = current.type === "Identifier" && wrapImportNames.includes(current.name) || current.type === "MemberExpression" && current.object.type === "Identifier" && wrapImportNames.includes(current.object.name);
|
|
89
|
+
if (!isWrapRoot) continue;
|
|
90
|
+
const onStage = stages.find((s) => s.name === "on");
|
|
91
|
+
if (!onStage) continue;
|
|
92
|
+
const wrapComponentStages = stages.filter((s) => s.name !== "on");
|
|
93
|
+
for (const stage of wrapComponentStages) {
|
|
94
|
+
usedSuspensiveAPIs.add(stage.name);
|
|
95
|
+
}
|
|
96
|
+
let withNode = onStage.args[0];
|
|
97
|
+
for (let i = wrapComponentStages.length - 1; i >= 0; i--) {
|
|
98
|
+
const { name, args } = wrapComponentStages[i];
|
|
99
|
+
const options = args[0] ?? j2.objectExpression([]);
|
|
100
|
+
withNode = j2.callExpression(j2.memberExpression(j2.identifier(name), j2.identifier("with")), [options, withNode]);
|
|
101
|
+
}
|
|
102
|
+
j2(path).replaceWith(withNode);
|
|
103
|
+
}
|
|
104
|
+
if (usedSuspensiveAPIs.size > 0) {
|
|
105
|
+
const suspensiveReactImport = root.find(j2.ImportDeclaration, { source: { value: "@suspensive/react" } });
|
|
106
|
+
if (suspensiveReactImport.size() > 0) {
|
|
107
|
+
for (const path of suspensiveReactImport.nodes()) {
|
|
108
|
+
const existing = path.specifiers?.map(
|
|
109
|
+
(spec) => spec.type === "ImportSpecifier" ? spec.imported.name : spec.local?.name
|
|
110
|
+
);
|
|
111
|
+
for (const name of Array.from(usedSuspensiveAPIs)) {
|
|
112
|
+
if (!existing?.includes(name)) {
|
|
113
|
+
path.specifiers?.push(j2.importSpecifier(j2.identifier(name)));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
for (const path of root.find(j2.Program).nodes()) {
|
|
119
|
+
path.body.unshift(
|
|
120
|
+
j2.importDeclaration(
|
|
121
|
+
Array.from(usedSuspensiveAPIs).map((name) => j2.importSpecifier(j2.identifier(name))),
|
|
122
|
+
j2.literal("@suspensive/react")
|
|
123
|
+
)
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return root.toSource();
|
|
128
|
+
}
|
|
129
|
+
}
|