@suspensive/codemods 4.0.0-next.0 → 4.0.0-next.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.
@@ -1,12 +1,10 @@
1
1
  #!/usr/bin/env node
2
- const require_transformRunner = require('./transformRunner-DY4gLJm8.cjs');
3
- let __commander_js_extra_typings = require("@commander-js/extra-typings");
4
-
2
+ const require_transformRunner = require("./transformRunner-Bm8Ge6Vu.cjs");
3
+ let _commander_js_extra_typings = require("@commander-js/extra-typings");
5
4
  //#region package.json
6
5
  var name = "@suspensive/codemods";
7
- var version = "4.0.0-next.0";
6
+ var version = "4.0.0-next.1";
8
7
  var description = "Codemods for @suspensive.";
9
-
10
8
  //#endregion
11
9
  //#region src/bin/codemods.ts
12
10
  /**
@@ -16,8 +14,7 @@ var description = "Codemods for @suspensive.";
16
14
  * LICENSE file in the root directory of this source tree.
17
15
  *
18
16
  */
19
- const program = new __commander_js_extra_typings.Command(name);
17
+ const program = new _commander_js_extra_typings.Command(name);
20
18
  program.description(description).version(version, "-v, --version", "Output the current version of @suspensive/codemods.").argument("[codemod]", "Codemod slug to run. See \"https://suspensive.org/docs/codemods/motivation\"").argument("[path]", "Path to source directory.").usage("[codemod] [path]").helpOption("-h, --help", "Display this help message.").option("-d, --dry", "Dry run (no changes are made to files)").option("-p, --print", "Print transformed files to stdout, useful for development").action(require_transformRunner.transformRunner);
21
19
  program.parse(process.argv);
22
-
23
- //#endregion
20
+ //#endregion
@@ -0,0 +1 @@
1
+ export { };
@@ -1,4 +1,4 @@
1
- //#region rolldown:runtime
1
+ //#region \0rolldown/runtime.js
2
2
  var __create = Object.create;
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -6,16 +6,12 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
8
  var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
- key = keys[i];
12
- if (!__hasOwnProp.call(to, key) && key !== except) {
13
- __defProp(to, key, {
14
- get: ((k) => from[k]).bind(null, key),
15
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
- });
17
- }
18
- }
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
19
15
  }
20
16
  return to;
21
17
  };
@@ -23,16 +19,18 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
19
  value: mod,
24
20
  enumerable: true
25
21
  }) : target, mod));
26
-
27
22
  //#endregion
28
23
  let node_path = require("node:path");
29
24
  let execa = require("execa");
30
- execa = __toESM(execa);
25
+ execa = __toESM(execa, 1);
31
26
  let prompts = require("prompts");
32
- prompts = __toESM(prompts);
33
-
27
+ prompts = __toESM(prompts, 1);
34
28
  //#region src/bin/transformRunner.ts
35
29
  const TRANSFORMER_INQUIRER_CHOICES = [
30
+ {
31
+ title: "migrate-to-v4",
32
+ description: "Migrates Suspensive v3 APIs to v4"
33
+ },
36
34
  {
37
35
  title: "tanstack-query-import",
38
36
  description: "Migrate imports to @tanstack/react-query in @suspensive/react-query"
@@ -60,7 +58,7 @@ async function transformRunner(transform, path, options) {
60
58
  let directory = path ?? "";
61
59
  if (transform && !TRANSFORMER_INQUIRER_CHOICES.find((x) => x.title === transform)) {
62
60
  console.error("Invalid transform choice, pick one of:");
63
- console.error(TRANSFORMER_INQUIRER_CHOICES.map((x) => `- ${+x.title}`).join("\n"));
61
+ console.error(TRANSFORMER_INQUIRER_CHOICES.map((x) => `- ${x.title}`).join("\n"));
64
62
  process.exit(1);
65
63
  }
66
64
  if (!transform) transformer = (await (0, prompts.default)({
@@ -111,29 +109,28 @@ async function transformRunner(transform, path, options) {
111
109
  process.exit(1);
112
110
  }
113
111
  }
114
-
115
112
  //#endregion
116
- Object.defineProperty(exports, '__toESM', {
117
- enumerable: true,
118
- get: function () {
119
- return __toESM;
120
- }
113
+ Object.defineProperty(exports, "__toESM", {
114
+ enumerable: true,
115
+ get: function() {
116
+ return __toESM;
117
+ }
121
118
  });
122
- Object.defineProperty(exports, 'jscodeshiftExecutable', {
123
- enumerable: true,
124
- get: function () {
125
- return jscodeshiftExecutable;
126
- }
119
+ Object.defineProperty(exports, "jscodeshiftExecutable", {
120
+ enumerable: true,
121
+ get: function() {
122
+ return jscodeshiftExecutable;
123
+ }
124
+ });
125
+ Object.defineProperty(exports, "transformRunner", {
126
+ enumerable: true,
127
+ get: function() {
128
+ return transformRunner;
129
+ }
127
130
  });
128
- Object.defineProperty(exports, 'transformRunner', {
129
- enumerable: true,
130
- get: function () {
131
- return transformRunner;
132
- }
131
+ Object.defineProperty(exports, "transformerDirectory", {
132
+ enumerable: true,
133
+ get: function() {
134
+ return transformerDirectory;
135
+ }
133
136
  });
134
- Object.defineProperty(exports, 'transformerDirectory', {
135
- enumerable: true,
136
- get: function () {
137
- return transformerDirectory;
138
- }
139
- });
@@ -1,5 +1,5 @@
1
- const require_transformRunner = require('./transformRunner-DY4gLJm8.cjs');
2
-
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_transformRunner = require("./transformRunner-Bm8Ge6Vu.cjs");
3
3
  exports.jscodeshiftExecutable = require_transformRunner.jscodeshiftExecutable;
4
4
  exports.transformRunner = require_transformRunner.transformRunner;
5
- exports.transformerDirectory = require_transformRunner.transformerDirectory;
5
+ exports.transformerDirectory = require_transformRunner.transformerDirectory;
@@ -0,0 +1,10 @@
1
+ //#region src/bin/transformRunner.d.ts
2
+ declare const jscodeshiftExecutable: string;
3
+ declare const transformerDirectory: string;
4
+ declare function transformRunner(transform?: string, path?: string, options?: {
5
+ dry?: boolean;
6
+ print?: boolean;
7
+ }): Promise<void>;
8
+ //#endregion
9
+ export { jscodeshiftExecutable, transformRunner, transformerDirectory };
10
+ //# sourceMappingURL=transformRunner.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transformRunner.d.cts","names":[],"sources":["../../src/bin/transformRunner.ts"],"mappings":";cA+Ba,qBAAA;AAAA,cACA,oBAAA;AAAA,iBAES,eAAA,CAAgB,SAAA,WAAoB,IAAA,WAAe,OAAA;EAAY,GAAA;EAAe,KAAA;AAAA,IAAiB,OAAA"}
@@ -0,0 +1,49 @@
1
+ //#region \0rolldown/runtime.js
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 __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+ //#endregion
23
+ let jscodeshift = require("jscodeshift");
24
+ jscodeshift = __toESM(jscodeshift, 1);
25
+ let jscodeshift_parser_babylon_js = require("jscodeshift/parser/babylon.js");
26
+ jscodeshift_parser_babylon_js = __toESM(jscodeshift_parser_babylon_js, 1);
27
+ let jscodeshift_parser_tsOptions_js = require("jscodeshift/parser/tsOptions.js");
28
+ jscodeshift_parser_tsOptions_js = __toESM(jscodeshift_parser_tsOptions_js, 1);
29
+ //#region src/transforms/utils/createParserFromPath.ts
30
+ function createParserFromPath(filePath) {
31
+ if (/\.d\.[mc]?ts$/.test(filePath)) return jscodeshift.default.withParser((0, jscodeshift_parser_babylon_js.default)({
32
+ ...jscodeshift_parser_tsOptions_js.default,
33
+ plugins: [...jscodeshift_parser_tsOptions_js.default.plugins.filter((plugin) => plugin !== "typescript"), ["typescript", { dts: true }]]
34
+ }));
35
+ return /\.[mc]?ts$/.test(filePath) ? jscodeshift.default.withParser("ts") : jscodeshift.default.withParser("tsx");
36
+ }
37
+ //#endregion
38
+ Object.defineProperty(exports, "__toESM", {
39
+ enumerable: true,
40
+ get: function() {
41
+ return __toESM;
42
+ }
43
+ });
44
+ Object.defineProperty(exports, "createParserFromPath", {
45
+ enumerable: true,
46
+ get: function() {
47
+ return createParserFromPath;
48
+ }
49
+ });
@@ -1,5 +1,4 @@
1
- const require_createParserFromPath = require('./createParserFromPath-1O_N-zyu.cjs');
2
-
1
+ const require_createParserFromPath = require("./createParserFromPath-7qRmKnCS.cjs");
3
2
  //#region src/transforms/migrate-query-client-consumer-props.ts
4
3
  function transform(file) {
5
4
  const j = require_createParserFromPath.createParserFromPath(file.path);
@@ -15,6 +14,5 @@ function transform(file) {
15
14
  });
16
15
  return root.toSource();
17
16
  }
18
-
19
17
  //#endregion
20
- module.exports = transform;
18
+ module.exports = transform;
@@ -0,0 +1,6 @@
1
+ import { FileInfo } from "jscodeshift";
2
+
3
+ //#region src/transforms/migrate-query-client-consumer-props.d.ts
4
+ declare function transform(file: FileInfo): string;
5
+ export = transform;
6
+ //# sourceMappingURL=migrate-query-client-consumer-props.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate-query-client-consumer-props.d.cts","names":[],"sources":["../../src/transforms/migrate-query-client-consumer-props.ts"],"mappings":";;;iBAGwB,SAAA,CAAU,IAAc,EAAR,QAAQ;AAAA"}
@@ -0,0 +1,156 @@
1
+ const require_createParserFromPath = require("./createParserFromPath-7qRmKnCS.cjs");
2
+ let node_fs = require("node:fs");
3
+ let node_path = require("node:path");
4
+ //#region src/transforms/utils/detectTanStackVersion.ts
5
+ const DEFAULT_VERSION = 5;
6
+ let cachedVersion;
7
+ function detectTanStackVersion(startDir = process.cwd()) {
8
+ const override = Number(process.env.SUSPENSIVE_RQ_TARGET);
9
+ if (!isNaN(override)) return override;
10
+ if (cachedVersion !== void 0) return cachedVersion;
11
+ const range = findTanStackRange(startDir);
12
+ if (range === void 0) {
13
+ console.warn(`Could not detect @tanstack/react-query version from package.json. Defaulting to v${DEFAULT_VERSION}.`);
14
+ cachedVersion = DEFAULT_VERSION;
15
+ return cachedVersion;
16
+ }
17
+ const major = parseMajor(range);
18
+ if (major === void 0) {
19
+ console.warn(`Could not parse major version from @tanstack/react-query range "${range}". Defaulting to v${DEFAULT_VERSION}.`);
20
+ cachedVersion = DEFAULT_VERSION;
21
+ return cachedVersion;
22
+ }
23
+ cachedVersion = major;
24
+ return cachedVersion;
25
+ }
26
+ function findTanStackRange(startDir) {
27
+ let current = startDir;
28
+ let parent = (0, node_path.dirname)(current);
29
+ while (parent !== current) {
30
+ const pkgPath = (0, node_path.join)(current, "package.json");
31
+ if ((0, node_fs.existsSync)(pkgPath)) try {
32
+ const raw = (0, node_fs.readFileSync)(pkgPath, "utf8");
33
+ const pkg = JSON.parse(raw);
34
+ const range = pkg.dependencies?.["@tanstack/react-query"] ?? pkg.devDependencies?.["@tanstack/react-query"] ?? pkg.peerDependencies?.["@tanstack/react-query"];
35
+ if (range) return range;
36
+ } catch {}
37
+ current = parent;
38
+ parent = (0, node_path.dirname)(current);
39
+ }
40
+ }
41
+ function parseMajor(range) {
42
+ const match = /(\d+)/.exec(range);
43
+ if (!match) return;
44
+ const n = Number(match[1]);
45
+ if (Number.isInteger(n)) return n;
46
+ }
47
+ //#endregion
48
+ //#region src/transforms/migrate-to-v4.ts
49
+ const UNVERSIONED_SOURCE = "@suspensive/react-query";
50
+ const SOURCE_PATTERN = /^@suspensive\/react-query(-\d+)?$/;
51
+ const TANSTACK_TARGET = "@tanstack/react-query";
52
+ const TANSTACK_APIS = [
53
+ "useSuspenseQuery",
54
+ "UseSuspenseQueryResult",
55
+ "UseSuspenseQueryOptions",
56
+ "useSuspenseQueries",
57
+ "SuspenseQueriesResults",
58
+ "SuspenseQueriesOptions",
59
+ "useSuspenseInfiniteQuery",
60
+ "UseSuspenseInfiniteQueryResult",
61
+ "UseSuspenseInfiniteQueryOptions",
62
+ "usePrefetchQuery",
63
+ "usePrefetchInfiniteQuery",
64
+ "queryOptions",
65
+ "infiniteQueryOptions",
66
+ "mutationOptions"
67
+ ];
68
+ const DEPRECATED_REMOVED = [
69
+ "SelectedInfiniteOptions",
70
+ "UnSelectedInfiniteOptions",
71
+ "SelectedQueryOptions",
72
+ "UnSelectedQueryOptions"
73
+ ];
74
+ function transform(file) {
75
+ const j = require_createParserFromPath.createParserFromPath(file.path);
76
+ const root = j(file.source);
77
+ const versionedTarget = `@suspensive/react-query-${detectTanStackVersion()}`;
78
+ const resolveSuspensiveTarget = (currentSource) => currentSource === UNVERSIONED_SOURCE ? versionedTarget : currentSource;
79
+ root.find(j.ImportDeclaration, { source: { value: (v) => SOURCE_PATTERN.test(v) } }).forEach((path) => {
80
+ const specifiers = path.node.specifiers ?? [];
81
+ const importKind = path.node.importKind;
82
+ const currentSource = path.node.source.value;
83
+ const suspensiveTarget = resolveSuspensiveTarget(currentSource);
84
+ if (specifiers.length === 0) {
85
+ if (currentSource !== suspensiveTarget) path.node.source = j.stringLiteral(suspensiveTarget);
86
+ return;
87
+ }
88
+ if (specifiers.some((s) => s.type === "ImportNamespaceSpecifier" || s.type === "ImportDefaultSpecifier")) {
89
+ if (currentSource !== suspensiveTarget) path.node.source = j.stringLiteral(suspensiveTarget);
90
+ return;
91
+ }
92
+ const tanstackSpecifiers = [];
93
+ const suspensiveSpecifiers = [];
94
+ for (const spec of specifiers) {
95
+ if (spec.type !== "ImportSpecifier") continue;
96
+ const name = spec.imported.name;
97
+ if (DEPRECATED_REMOVED.includes(name)) continue;
98
+ if (TANSTACK_APIS.includes(name)) tanstackSpecifiers.push(spec);
99
+ else suspensiveSpecifiers.push(spec);
100
+ }
101
+ if (tanstackSpecifiers.length === 0 && suspensiveSpecifiers.length === specifiers.length) {
102
+ if (currentSource !== suspensiveTarget) path.node.source = j.stringLiteral(suspensiveTarget);
103
+ return;
104
+ }
105
+ if (tanstackSpecifiers.length > 0) {
106
+ const tanstackDecl = j.importDeclaration(tanstackSpecifiers, j.stringLiteral(TANSTACK_TARGET));
107
+ if (importKind === "type") tanstackDecl.importKind = "type";
108
+ path.insertBefore(tanstackDecl);
109
+ }
110
+ if (suspensiveSpecifiers.length > 0) {
111
+ const suspensiveDecl = j.importDeclaration(suspensiveSpecifiers, j.stringLiteral(suspensiveTarget));
112
+ if (importKind === "type") suspensiveDecl.importKind = "type";
113
+ path.insertBefore(suspensiveDecl);
114
+ }
115
+ j(path).remove();
116
+ });
117
+ root.find(j.ExportNamedDeclaration, { source: { value: (v) => SOURCE_PATTERN.test(v) } }).forEach((path) => {
118
+ const specifiers = path.node.specifiers;
119
+ const currentSource = path.node.source?.value;
120
+ if (!currentSource) return;
121
+ const suspensiveTarget = resolveSuspensiveTarget(currentSource);
122
+ if (!specifiers || specifiers.length === 0) {
123
+ if (currentSource !== suspensiveTarget) path.node.source = j.stringLiteral(suspensiveTarget);
124
+ return;
125
+ }
126
+ const tanstackSpecifiers = [];
127
+ const suspensiveSpecifiers = [];
128
+ for (const spec of specifiers) {
129
+ const localName = spec.local?.name ?? "";
130
+ if (DEPRECATED_REMOVED.includes(localName)) continue;
131
+ if (TANSTACK_APIS.includes(localName)) tanstackSpecifiers.push(spec);
132
+ else suspensiveSpecifiers.push(spec);
133
+ }
134
+ if (tanstackSpecifiers.length === 0 && suspensiveSpecifiers.length === specifiers.length) {
135
+ if (currentSource !== suspensiveTarget) path.node.source = j.stringLiteral(suspensiveTarget);
136
+ return;
137
+ }
138
+ if (tanstackSpecifiers.length > 0) {
139
+ const tanstackDecl = j.exportNamedDeclaration(null, tanstackSpecifiers, j.stringLiteral(TANSTACK_TARGET));
140
+ path.insertBefore(tanstackDecl);
141
+ }
142
+ if (suspensiveSpecifiers.length > 0) {
143
+ const suspensiveDecl = j.exportNamedDeclaration(null, suspensiveSpecifiers, j.stringLiteral(suspensiveTarget));
144
+ path.insertBefore(suspensiveDecl);
145
+ }
146
+ j(path).remove();
147
+ });
148
+ root.find(j.ExportAllDeclaration, { source: { value: (v) => SOURCE_PATTERN.test(v) } }).forEach((path) => {
149
+ const currentSource = path.node.source.value;
150
+ const suspensiveTarget = resolveSuspensiveTarget(currentSource);
151
+ if (currentSource !== suspensiveTarget) path.node.source = j.stringLiteral(suspensiveTarget);
152
+ });
153
+ return root.toSource();
154
+ }
155
+ //#endregion
156
+ module.exports = transform;
@@ -0,0 +1,6 @@
1
+ import { FileInfo } from "jscodeshift";
2
+
3
+ //#region src/transforms/migrate-to-v4.d.ts
4
+ declare function transform(file: FileInfo): string;
5
+ export = transform;
6
+ //# sourceMappingURL=migrate-to-v4.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate-to-v4.d.cts","names":[],"sources":["../../src/transforms/migrate-to-v4.ts"],"mappings":";;;iBAgCwB,SAAA,CAAU,IAAc,EAAR,QAAQ;AAAA"}
@@ -1,5 +1,4 @@
1
- const require_createParserFromPath = require('./createParserFromPath-1O_N-zyu.cjs');
2
-
1
+ const require_createParserFromPath = require("./createParserFromPath-7qRmKnCS.cjs");
3
2
  //#region src/transforms/migrate-with-api.ts
4
3
  function transformer(file) {
5
4
  const j = require_createParserFromPath.createParserFromPath(file.path);
@@ -49,6 +48,5 @@ function transformer(file) {
49
48
  return root.toSource();
50
49
  }
51
50
  }
52
-
53
51
  //#endregion
54
- module.exports = transformer;
52
+ module.exports = transformer;
@@ -0,0 +1,6 @@
1
+ import { FileInfo } from "jscodeshift";
2
+
3
+ //#region src/transforms/migrate-with-api.d.ts
4
+ declare function transformer(file: FileInfo): string | undefined;
5
+ export = transformer;
6
+ //# sourceMappingURL=migrate-with-api.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate-with-api.d.cts","names":[],"sources":["../../src/transforms/migrate-with-api.ts"],"mappings":";;;iBAGwB,WAAA,CAAY,IAAc,EAAR,QAAQ;AAAA"}
@@ -1,5 +1,4 @@
1
- const require_createParserFromPath = require('./createParserFromPath-1O_N-zyu.cjs');
2
-
1
+ const require_createParserFromPath = require("./createParserFromPath-7qRmKnCS.cjs");
3
2
  //#region src/transforms/remove-networkmode.ts
4
3
  const TARGET_FUNCTIONS = new Set([
5
4
  "queryOptions",
@@ -31,6 +30,5 @@ function transform(file) {
31
30
  });
32
31
  return root.toSource();
33
32
  }
34
-
35
33
  //#endregion
36
- module.exports = transform;
34
+ module.exports = transform;
@@ -0,0 +1,6 @@
1
+ import { FileInfo } from "jscodeshift";
2
+
3
+ //#region src/transforms/remove-networkmode.d.ts
4
+ declare function transform(file: FileInfo): string;
5
+ export = transform;
6
+ //# sourceMappingURL=remove-networkmode.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remove-networkmode.d.cts","names":[],"sources":["../../src/transforms/remove-networkmode.ts"],"mappings":";;;iBAYwB,SAAA,CAAU,IAAc,EAAR,QAAQ;AAAA"}
@@ -1,7 +1,6 @@
1
- const require_createParserFromPath = require('./createParserFromPath-1O_N-zyu.cjs');
2
-
1
+ const require_createParserFromPath = require("./createParserFromPath-7qRmKnCS.cjs");
3
2
  //#region src/transforms/tanstack-query-import.ts
4
- const IMPORT_TO_CHANGE = [
3
+ const TANSTACK_APIS = [
5
4
  "useSuspenseQuery",
6
5
  "UseSuspenseQueryResult",
7
6
  "UseSuspenseQueryOptions",
@@ -12,8 +11,10 @@ const IMPORT_TO_CHANGE = [
12
11
  "UseSuspenseInfiniteQueryResult",
13
12
  "UseSuspenseInfiniteQueryOptions",
14
13
  "usePrefetchQuery",
14
+ "usePrefetchInfiniteQuery",
15
15
  "queryOptions",
16
- "infiniteQueryOption"
16
+ "infiniteQueryOptions",
17
+ "mutationOptions"
17
18
  ];
18
19
  const REMOVE_DEPRECATED_IMPORTS = [
19
20
  "SelectedInfiniteOptions",
@@ -21,39 +22,55 @@ const REMOVE_DEPRECATED_IMPORTS = [
21
22
  "SelectedQueryOptions",
22
23
  "UnSelectedQueryOptions"
23
24
  ];
25
+ const SUSPENSIVE_SOURCE_PATTERN = /^@suspensive\/react-query(-\d+)?$/;
26
+ function getClassificationName(specifier) {
27
+ return specifier.imported.name;
28
+ }
24
29
  function transform(file) {
25
30
  const j = require_createParserFromPath.createParserFromPath(file.path);
26
31
  const root = j(file.source);
27
- root.find(j.ImportDeclaration, { source: { value: (v) => /^@suspensive\/react-query(-\d+)?$/.test(v) } }).forEach((path) => {
32
+ root.find(j.ImportDeclaration, { source: { value: (v) => SUSPENSIVE_SOURCE_PATTERN.test(v) } }).forEach((path) => {
28
33
  const importSpecifiers = path.node.specifiers || [];
29
- const importNamesToChange = importSpecifiers.filter((specifier) => IMPORT_TO_CHANGE.includes(specifier.local?.name ?? ""));
30
- const importsNamesRemained = importSpecifiers.filter((specifier) => !IMPORT_TO_CHANGE.includes(specifier.local?.name ?? "") && !REMOVE_DEPRECATED_IMPORTS.includes(specifier.local?.name ?? ""));
34
+ const importKind = path.node.importKind;
35
+ const importNamesToChange = [];
36
+ const importsNamesRemained = [];
37
+ for (const specifier of importSpecifiers) {
38
+ if (specifier.type !== "ImportSpecifier") {
39
+ importsNamesRemained.push(specifier);
40
+ continue;
41
+ }
42
+ const name = getClassificationName(specifier);
43
+ if (TANSTACK_APIS.includes(name)) importNamesToChange.push(specifier);
44
+ else if (!REMOVE_DEPRECATED_IMPORTS.includes(name)) importsNamesRemained.push(specifier);
45
+ }
31
46
  if (importNamesToChange.length > 0) {
32
47
  const newImportStatement = j.importDeclaration(importNamesToChange, j.stringLiteral("@tanstack/react-query"));
48
+ if (importKind === "type") newImportStatement.importKind = "type";
33
49
  path.insertBefore(newImportStatement);
34
50
  }
35
51
  if (importsNamesRemained.length > 0) {
36
- const remainingSpecifiers = importSpecifiers.filter((specifier) => !IMPORT_TO_CHANGE.includes(specifier.local?.name ?? ""));
37
- const suspensiveRemainImportsStatement = j.importDeclaration(remainingSpecifiers, j.stringLiteral(path.node.source.value || "@suspensive/react-query"));
52
+ const suspensiveRemainImportsStatement = j.importDeclaration(importsNamesRemained, j.stringLiteral(path.node.source.value || "@suspensive/react-query"));
53
+ if (importKind === "type") suspensiveRemainImportsStatement.importKind = "type";
38
54
  path.insertBefore(suspensiveRemainImportsStatement);
39
55
  }
40
56
  j(path).remove();
41
57
  });
42
58
  root.find(j.ExportNamedDeclaration).forEach((path) => {
43
- if (path.node.specifiers) {
44
- const exportSpecifiers = path.node.specifiers;
45
- const exportNamesToChange = exportSpecifiers.filter((specifier) => IMPORT_TO_CHANGE.includes(specifier.local?.name ?? ""));
46
- const exportNamesRemained = exportSpecifiers.filter((specifier) => !IMPORT_TO_CHANGE.includes(specifier.local?.name ?? "") && !REMOVE_DEPRECATED_IMPORTS.includes(specifier.local?.name ?? ""));
47
- if (exportNamesToChange.length > 0) {
48
- const newExportStatement = j.exportNamedDeclaration(null, exportNamesToChange);
49
- path.insertBefore(newExportStatement);
50
- }
51
- if (exportNamesRemained.length > 0) {
52
- const remainingExportStatement = j.exportNamedDeclaration(null, exportNamesRemained);
53
- path.insertBefore(remainingExportStatement);
54
- }
55
- j(path).remove();
59
+ if (!path.node.specifiers) return;
60
+ const exportSpecifiers = path.node.specifiers;
61
+ const originalSource = path.node.source;
62
+ const exportNamesToChange = exportSpecifiers.filter((specifier) => TANSTACK_APIS.includes(specifier.local?.name ?? ""));
63
+ const exportNamesRemained = exportSpecifiers.filter((specifier) => !TANSTACK_APIS.includes(specifier.local?.name ?? "") && !REMOVE_DEPRECATED_IMPORTS.includes(specifier.local?.name ?? ""));
64
+ if (exportNamesToChange.length > 0) {
65
+ const targetSource = originalSource && SUSPENSIVE_SOURCE_PATTERN.test(originalSource.value) ? j.stringLiteral("@tanstack/react-query") : originalSource;
66
+ const newExportStatement = j.exportNamedDeclaration(null, exportNamesToChange, targetSource);
67
+ path.insertBefore(newExportStatement);
68
+ }
69
+ if (exportNamesRemained.length > 0) {
70
+ const remainingExportStatement = j.exportNamedDeclaration(null, exportNamesRemained, originalSource);
71
+ path.insertBefore(remainingExportStatement);
56
72
  }
73
+ j(path).remove();
57
74
  });
58
75
  REMOVE_DEPRECATED_IMPORTS.forEach((deprecatedType) => {
59
76
  root.find(j.TSTypeReference, { typeName: { name: deprecatedType } }).remove();
@@ -61,6 +78,5 @@ function transform(file) {
61
78
  });
62
79
  return root.toSource();
63
80
  }
64
-
65
81
  //#endregion
66
- module.exports = transform;
82
+ module.exports = transform;
@@ -0,0 +1,6 @@
1
+ import { FileInfo } from "jscodeshift";
2
+
3
+ //#region src/transforms/tanstack-query-import.d.ts
4
+ declare function transform(file: FileInfo): string;
5
+ export = transform;
6
+ //# sourceMappingURL=tanstack-query-import.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tanstack-query-import.d.cts","names":[],"sources":["../../src/transforms/tanstack-query-import.ts"],"mappings":";;;iBAiCwB,SAAA,CAAU,IAAc,EAAR,QAAQ;AAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@suspensive/codemods",
3
- "version": "4.0.0-next.0",
3
+ "version": "4.0.0-next.1",
4
4
  "description": "Codemods for @suspensive.",
5
5
  "keywords": [
6
6
  "suspensive",
@@ -1,50 +0,0 @@
1
- //#region rolldown:runtime
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 __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
- key = keys[i];
12
- if (!__hasOwnProp.call(to, key) && key !== except) {
13
- __defProp(to, key, {
14
- get: ((k) => from[k]).bind(null, key),
15
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
- });
17
- }
18
- }
19
- }
20
- return to;
21
- };
22
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
- value: mod,
24
- enumerable: true
25
- }) : target, mod));
26
-
27
- //#endregion
28
- let jscodeshift = require("jscodeshift");
29
- jscodeshift = __toESM(jscodeshift);
30
- let jscodeshift_parser_babylon = require("jscodeshift/parser/babylon");
31
- jscodeshift_parser_babylon = __toESM(jscodeshift_parser_babylon);
32
- let jscodeshift_parser_tsOptions = require("jscodeshift/parser/tsOptions");
33
- jscodeshift_parser_tsOptions = __toESM(jscodeshift_parser_tsOptions);
34
-
35
- //#region src/transforms/utils/createParserFromPath.ts
36
- function createParserFromPath(filePath) {
37
- if (/\.d\.[mc]?ts$/.test(filePath)) return jscodeshift.default.withParser((0, jscodeshift_parser_babylon.default)({
38
- ...jscodeshift_parser_tsOptions.default,
39
- plugins: [...jscodeshift_parser_tsOptions.default.plugins.filter((plugin) => plugin !== "typescript"), ["typescript", { dts: true }]]
40
- }));
41
- return /\.[mc]?ts$/.test(filePath) ? jscodeshift.default.withParser("ts") : jscodeshift.default.withParser("tsx");
42
- }
43
-
44
- //#endregion
45
- Object.defineProperty(exports, 'createParserFromPath', {
46
- enumerable: true,
47
- get: function () {
48
- return createParserFromPath;
49
- }
50
- });