@fast-check/codemod 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +205 -2
- package/lib/allowAwait-GB5WG8dx.js +48 -0
- package/lib/cli.d.ts +1 -0
- package/lib/cli.js +49 -0
- package/lib/fastCheck-BCuGakPT.js +51 -0
- package/lib/pluginMigration-TF34gzVd.js +215 -0
- package/lib/reportUnsupported-DY7wlHET.js +7 -0
- package/lib/sampling-eUHO0ySk.js +26 -0
- package/lib/transforms/v5/checkToAssert.d.ts +2 -0
- package/lib/transforms/v5/checkToAssert.js +72 -0
- package/lib/transforms/v5/defaultImport.d.ts +2 -0
- package/lib/transforms/v5/defaultImport.js +30 -0
- package/lib/transforms/v5/lifecyclePlugins.d.ts +2 -0
- package/lib/transforms/v5/lifecyclePlugins.js +211 -0
- package/lib/transforms/v5/propertyToAsyncProperty.d.ts +2 -0
- package/lib/transforms/v5/propertyToAsyncProperty.js +135 -0
- package/lib/transforms/v5/randomMethods.d.ts +2 -0
- package/lib/transforms/v5/randomMethods.js +65 -0
- package/lib/transforms/v5/randomTypeFactory.d.ts +2 -0
- package/lib/transforms/v5/randomTypeFactory.js +64 -0
- package/lib/transforms/v5/runnerPlugins.d.ts +2 -0
- package/lib/transforms/v5/runnerPlugins.js +139 -0
- package/lib/transforms/v5/sample.d.ts +2 -0
- package/lib/transforms/v5/sample.js +32 -0
- package/lib/transforms/v5/statistics.d.ts +2 -0
- package/lib/transforms/v5/statistics.js +56 -0
- package/lib/transforms/v5/stringUnit.d.ts +2 -0
- package/lib/transforms/v5/stringUnit.js +64 -0
- package/lib/transforms/v6/asyncPropertyToProperty.d.ts +2 -0
- package/lib/transforms/v6/asyncPropertyToProperty.js +50 -0
- package/package.json +70 -3
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { a as memberName, i as isReference, t as findBinding } from "../../fastCheck-BCuGakPT.js";
|
|
2
|
+
import { t as reportUnsupported } from "../../reportUnsupported-DY7wlHET.js";
|
|
3
|
+
// src/transforms/v5/checkToAssert.ts
|
|
4
|
+
function isPromiseConsumer(j, path) {
|
|
5
|
+
const parent = path.parent.node;
|
|
6
|
+
if (j.AwaitExpression.check(parent)) return true;
|
|
7
|
+
const call = path.parent.parent.node;
|
|
8
|
+
return j.MemberExpression.check(parent) && parent.object === path.node && !parent.computed && j.Identifier.check(parent.property) && [
|
|
9
|
+
"then",
|
|
10
|
+
"catch",
|
|
11
|
+
"finally"
|
|
12
|
+
].includes(parent.property.name) && j.CallExpression.check(call) && call.callee === parent;
|
|
13
|
+
}
|
|
14
|
+
function transform(file, api) {
|
|
15
|
+
const typed = /\.[cm]?tsx?$/.test(file.path);
|
|
16
|
+
const j = api.jscodeshift.withParser(/\.[cm]?ts$/.test(file.path) ? "ts" : "tsx");
|
|
17
|
+
const root = j(file.source);
|
|
18
|
+
let helper = "assertAndGetRunDetails";
|
|
19
|
+
while (root.find(j.Identifier, { name: helper }).size()) helper += "ForCheck";
|
|
20
|
+
let namespace;
|
|
21
|
+
function isPromiseBinding(path) {
|
|
22
|
+
const declarator = path.parent.node;
|
|
23
|
+
const declaration = path.parent.parent;
|
|
24
|
+
if (!j.VariableDeclarator.check(declarator) || declarator.init !== path.node || !j.Identifier.check(declarator.id) || !j.VariableDeclaration.check(declaration.node) || declaration.node.kind !== "const" || j.ExportNamedDeclaration.check(declaration.parent.node)) return false;
|
|
25
|
+
const binding = declarator.id;
|
|
26
|
+
const uses = root.find(j.Identifier, { name: binding.name }).filter((ref) => ref.node !== binding && isReference(j, ref) && findBinding(j, ref, binding.name)?.node === binding).paths();
|
|
27
|
+
return uses.length > 0 && uses.every((ref) => isPromiseConsumer(j, ref));
|
|
28
|
+
}
|
|
29
|
+
root.find(j.CallExpression).forEach((path) => {
|
|
30
|
+
const { callee } = path.node;
|
|
31
|
+
if (memberName(j, path, callee) !== "check" || !j.MemberExpression.check(callee) || !j.Identifier.check(callee.object)) return;
|
|
32
|
+
if (!isPromiseConsumer(j, path) && !isPromiseBinding(path)) {
|
|
33
|
+
reportUnsupported(api, path.node, "Cannot determine that check() is consumed as a promise; synchronous or uncertain usages require manual migration.");
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const discarded = j.AwaitExpression.check(path.parent.node) && j.ExpressionStatement.check(path.parent.parent.node);
|
|
37
|
+
const [property, params] = path.node.arguments;
|
|
38
|
+
if (!j.Expression.check(property) || path.node.arguments.length > 2 || j.SpreadElement.check(params)) {
|
|
39
|
+
reportUnsupported(api, path.node, "Cannot migrate check() with these arguments; migrate the call manually.");
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (!discarded && namespace && namespace !== callee.object.name) {
|
|
43
|
+
reportUnsupported(api, path.node, "Cannot migrate check() using multiple fast-check namespaces in one file.");
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (discarded) {
|
|
47
|
+
const disableDefaults = j.objectProperty(j.identifier("noDefaultPlugins"), j.booleanLiteral(true));
|
|
48
|
+
const options = j.objectExpression(params ? [j.spreadElement(params), disableDefaults] : [disableDefaults]);
|
|
49
|
+
path.node.arguments = [property, options];
|
|
50
|
+
callee.property = j.identifier("assert");
|
|
51
|
+
} else {
|
|
52
|
+
namespace = callee.object.name;
|
|
53
|
+
path.node.callee = j.identifier(helper);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
if (namespace) {
|
|
57
|
+
const type = (annotation) => typed ? annotation : "";
|
|
58
|
+
const definition = `
|
|
59
|
+
async function ${helper}${type("<Ts>")}(property${type(": " + namespace + ".Property<Ts>")}, params${type("?: " + namespace + ".Parameters<Ts>")}${typed ? "" : " = {}"}) {
|
|
60
|
+
let details${type("!: " + namespace + ".RunDetails<Ts>")};
|
|
61
|
+
await ${namespace}.assert(property, {
|
|
62
|
+
...params,
|
|
63
|
+
noDefaultPlugins: true,
|
|
64
|
+
plugins: [...(params?.plugins ?? []), () => ({ onAllRunsComplete(result) { details = result; } })],
|
|
65
|
+
});
|
|
66
|
+
return details;
|
|
67
|
+
}`;
|
|
68
|
+
root.find(j.Program).get().node.body.push(j(definition).find(j.FunctionDeclaration).get().node);
|
|
69
|
+
}
|
|
70
|
+
return root.toSource({ quote: "single" });
|
|
71
|
+
}
|
|
72
|
+
export { transform as default };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { t as reportUnsupported } from "../../reportUnsupported-DY7wlHET.js";
|
|
2
|
+
// src/transforms/v5/defaultImport.ts
|
|
3
|
+
function transform(file, api) {
|
|
4
|
+
const parser = /\.[cm]?ts$/.test(file.path) ? "ts" : "tsx";
|
|
5
|
+
const j = api.jscodeshift.withParser(parser);
|
|
6
|
+
const root = j(file.source);
|
|
7
|
+
let changed = false;
|
|
8
|
+
root.find(j.ImportDeclaration, { source: { value: "fast-check" } }).forEach((path) => {
|
|
9
|
+
const node = path.node;
|
|
10
|
+
const specifiers = node.specifiers ?? [];
|
|
11
|
+
const defaultSpecifier = specifiers.find((specifier) => specifier.type === "ImportDefaultSpecifier");
|
|
12
|
+
if (defaultSpecifier === void 0) return;
|
|
13
|
+
if ("attributes" in node && Array.isArray(node.attributes) && node.attributes.length !== 0) {
|
|
14
|
+
reportUnsupported(api, node, "Cannot migrate a fast-check default import with attributes; migrate it manually.");
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const namespace = j.importNamespaceSpecifier(defaultSpecifier.local);
|
|
18
|
+
namespace.comments = defaultSpecifier.comments;
|
|
19
|
+
const remainingSpecifiers = specifiers.filter((specifier) => specifier !== defaultSpecifier);
|
|
20
|
+
if (remainingSpecifiers.length !== 0) path.insertAfter({
|
|
21
|
+
...node,
|
|
22
|
+
specifiers: remainingSpecifiers,
|
|
23
|
+
comments: null
|
|
24
|
+
});
|
|
25
|
+
node.specifiers = [namespace];
|
|
26
|
+
changed = true;
|
|
27
|
+
});
|
|
28
|
+
return changed ? root.toSource({ quote: "single" }) : file.source;
|
|
29
|
+
}
|
|
30
|
+
export { transform as default };
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { i as isReference, t as findBinding } from "../../fastCheck-BCuGakPT.js";
|
|
2
|
+
import { t as migratePluginCalls } from "../../pluginMigration-TF34gzVd.js";
|
|
3
|
+
// src/transforms/v5/lifecyclePlugins.ts
|
|
4
|
+
const hookNames = [
|
|
5
|
+
"beforeEach",
|
|
6
|
+
"afterEach",
|
|
7
|
+
"asyncBeforeEach",
|
|
8
|
+
"asyncAfterEach"
|
|
9
|
+
];
|
|
10
|
+
function callbackFor(site, expr) {
|
|
11
|
+
const { j } = site;
|
|
12
|
+
if (j.ArrowFunctionExpression.check(expr) || j.FunctionExpression.check(expr)) return expr;
|
|
13
|
+
if (!j.Identifier.check(expr)) return void 0;
|
|
14
|
+
const declaration = findBinding(j, site.path, expr.name)?.parent;
|
|
15
|
+
if (j.FunctionDeclaration.check(declaration?.node)) return declaration.node;
|
|
16
|
+
if (j.VariableDeclarator.check(declaration?.node) && declaration.parent.node.kind === "const") {
|
|
17
|
+
const init = declaration.node.init;
|
|
18
|
+
if (j.ArrowFunctionExpression.check(init) || j.FunctionExpression.check(init)) return init;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function hookCallback(site, expr, asyncProperty) {
|
|
22
|
+
const { j } = site;
|
|
23
|
+
const callback = callbackFor(site, expr);
|
|
24
|
+
const global = site.runner === "configureGlobal";
|
|
25
|
+
if (!callback && (!global || !asyncProperty || !j.Identifier.check(expr))) {
|
|
26
|
+
site.report("Review the hook signature: property plugins do not receive the previous hook.");
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (callback && (callback.params.length || callback.generator || j(callback.body).find(j.Super).size() || j(callback.body).find(j.Identifier, { name: "arguments" }).size() || j(callback.body).find(j.ThisExpression).size())) {
|
|
30
|
+
site.report("Review the hook that uses arguments, a receiver or a generator.");
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
let adapted = expr;
|
|
34
|
+
if (!callback || !j.BlockStatement.check(callback.body) || j(callback.body).find(j.ReturnStatement).some((ret) => ret.node.argument != null)) {
|
|
35
|
+
if (global && !asyncProperty && !callback?.async) {
|
|
36
|
+
site.report("Review this global hook return value: it may need awaiting when used with async properties.");
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const invocation = j.ArrowFunctionExpression.check(expr) && !j.BlockStatement.check(expr.body) ? expr.body : j.callExpression(expr, []);
|
|
40
|
+
const asynchronous = asyncProperty || callback?.async === true;
|
|
41
|
+
const body = j.blockStatement([j.expressionStatement(asynchronous ? j.awaitExpression(invocation) : invocation)]);
|
|
42
|
+
adapted = j.arrowFunctionExpression([], body);
|
|
43
|
+
adapted.async = asynchronous;
|
|
44
|
+
}
|
|
45
|
+
return adapted;
|
|
46
|
+
}
|
|
47
|
+
function globalHooks(site) {
|
|
48
|
+
const remove = hookNames.filter((name) => site.options.has(name));
|
|
49
|
+
if (!remove.length) return void 0;
|
|
50
|
+
if (remove.includes("beforeEach") && remove.includes("asyncBeforeEach") || remove.includes("afterEach") && remove.includes("asyncAfterEach")) {
|
|
51
|
+
site.report("Resolve the conflicting synchronous and asynchronous global hooks before migrating.");
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (site.j(site.path.scope.path).find(site.j.CallExpression).some(({ node }) => {
|
|
55
|
+
const member = node.callee;
|
|
56
|
+
if (!site.j.MemberExpression.check(member) || member.computed || !site.j.Identifier.check(member.property)) return false;
|
|
57
|
+
const hook = member.property.name;
|
|
58
|
+
return ["beforeEach", "afterEach"].includes(hook) && remove.some((name) => name.toLowerCase().endsWith(hook.toLowerCase()));
|
|
59
|
+
})) {
|
|
60
|
+
site.report("Review the global hook together with the local hook that overrides it.");
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const plugins = [];
|
|
64
|
+
for (const name of remove) {
|
|
65
|
+
const callback = hookCallback(site, site.options.get(name), name.startsWith("async"));
|
|
66
|
+
if (!callback) return void 0;
|
|
67
|
+
const plugin = name.toLowerCase().includes("before") ? "beforeEach" : "afterEach";
|
|
68
|
+
plugins.push({ value: site.j.callExpression(site.member(plugin), [callback]) });
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
remove,
|
|
72
|
+
plugins
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function localHookEdit(site, callback, asyncProperty) {
|
|
76
|
+
const { j } = site;
|
|
77
|
+
if (!j.ArrowFunctionExpression.check(callback) && !j.FunctionExpression.check(callback) || !callback.params.length) return {
|
|
78
|
+
previous: "never",
|
|
79
|
+
callback
|
|
80
|
+
};
|
|
81
|
+
const [param] = callback.params;
|
|
82
|
+
if (callback.params.length !== 1 || !j.Identifier.check(param) || callback.generator || callback.async && !asyncProperty) return void 0;
|
|
83
|
+
if (j(callback).find(j.JSXIdentifier, { name: param.name }).size()) return void 0;
|
|
84
|
+
const count = j(callback).find(j.Identifier, { name: param.name }).size();
|
|
85
|
+
if (count === 1) return {
|
|
86
|
+
previous: "never",
|
|
87
|
+
callback: {
|
|
88
|
+
...callback,
|
|
89
|
+
params: []
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
if (count !== 2) return void 0;
|
|
93
|
+
const body = callback.body;
|
|
94
|
+
const statements = j.BlockStatement.check(body) ? body.body : [j.returnStatement(body)];
|
|
95
|
+
const index = statements.findIndex((statement) => {
|
|
96
|
+
const returns = j.ReturnStatement.check(statement);
|
|
97
|
+
if (!returns && !j.ExpressionStatement.check(statement)) return false;
|
|
98
|
+
const expr = returns ? statement.argument : statement.expression;
|
|
99
|
+
const awaited = j.AwaitExpression.check(expr);
|
|
100
|
+
if (asyncProperty && !returns && !awaited) return false;
|
|
101
|
+
const call = awaited ? expr.argument : expr;
|
|
102
|
+
return j.CallExpression.check(call) && !call.arguments.length && j.Identifier.check(call.callee) && call.callee.name === param.name;
|
|
103
|
+
});
|
|
104
|
+
if (index < 0 || index !== 0 && index !== statements.length - 1) return void 0;
|
|
105
|
+
if (statements.length === 1) return { previous: "first" };
|
|
106
|
+
if (!j.BlockStatement.check(body) || index === 0 && j.ReturnStatement.check(statements[0])) return void 0;
|
|
107
|
+
const rest = statements.filter((_, i) => i !== index);
|
|
108
|
+
if (index && rest.some((s) => j.ReturnStatement.check(s) || j(s).find(j.ReturnStatement).size())) return void 0;
|
|
109
|
+
return {
|
|
110
|
+
previous: index === 0 ? "first" : "last",
|
|
111
|
+
callback: {
|
|
112
|
+
...callback,
|
|
113
|
+
params: [],
|
|
114
|
+
body: {
|
|
115
|
+
...body,
|
|
116
|
+
body: rest
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function localHooks(site) {
|
|
122
|
+
const { j, call } = site;
|
|
123
|
+
const argument = call.arguments[0];
|
|
124
|
+
let property = argument;
|
|
125
|
+
let declarator;
|
|
126
|
+
if (j.Identifier.check(argument)) {
|
|
127
|
+
const declaration = findBinding(j, site.path, argument.name)?.parent;
|
|
128
|
+
if (!declaration || !j.VariableDeclarator.check(declaration.node) || declaration.parent.node.kind !== "const" || declaration.parent.node.declarations.length !== 1 || !j.CallExpression.check(declaration.node.init)) return void 0;
|
|
129
|
+
const statement = declaration.parent;
|
|
130
|
+
const next = statement.parentPath.get(Number(statement.name) + 1);
|
|
131
|
+
if (!next.value || next.node !== site.path.node && !j(next).find(j.CallExpression).some((p) => p.node === call) || declaration.scope !== site.path.scope || j(declaration.scope.path).find(j.Identifier, { name: argument.name }).some((ref) => ref.node !== argument && ref.node !== declaration.node.id && isReference(j, ref) && findBinding(j, ref, ref.node.name)?.parent?.node === declaration.node)) return void 0;
|
|
132
|
+
declarator = declaration;
|
|
133
|
+
property = declaration.node.init;
|
|
134
|
+
}
|
|
135
|
+
const hooks = [];
|
|
136
|
+
while (j.CallExpression.check(property) && j.MemberExpression.check(property.callee) && !property.callee.computed && j.Identifier.check(property.callee.property) && ["beforeEach", "afterEach"].includes(property.callee.property.name)) {
|
|
137
|
+
const name = property.callee.property.name;
|
|
138
|
+
const callback = property.arguments[0];
|
|
139
|
+
if (property.arguments.length !== 1 || !callback || callback.type === "SpreadElement" || !j.CallExpression.check(property.callee.object)) {
|
|
140
|
+
site.report("Review the indirect property hook.");
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
hooks.push({
|
|
144
|
+
name,
|
|
145
|
+
callback
|
|
146
|
+
});
|
|
147
|
+
property = property.callee.object;
|
|
148
|
+
}
|
|
149
|
+
if (!hooks.length || !j.CallExpression.check(property)) return void 0;
|
|
150
|
+
const factory = site.importedName(property);
|
|
151
|
+
if (factory !== "property" && factory !== "asyncProperty") return void 0;
|
|
152
|
+
const before = [];
|
|
153
|
+
const after = [];
|
|
154
|
+
const conflicts = [];
|
|
155
|
+
const asyncProperty = factory === "asyncProperty";
|
|
156
|
+
for (const { name, callback } of hooks.reverse()) {
|
|
157
|
+
conflicts.push(name, name === "beforeEach" ? "asyncBeforeEach" : "asyncAfterEach");
|
|
158
|
+
const edit = localHookEdit(site, callback, asyncProperty);
|
|
159
|
+
if (!edit) {
|
|
160
|
+
site.report(`Review how the ${name} callback uses its previous hook.`);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const chain = name === "beforeEach" ? before : after;
|
|
164
|
+
if (edit.previous === "never") chain.length = 0;
|
|
165
|
+
if (edit.callback) {
|
|
166
|
+
const value = hookCallback(site, edit.callback, asyncProperty);
|
|
167
|
+
if (!value) return void 0;
|
|
168
|
+
if (edit.previous === "last") chain.unshift(value);
|
|
169
|
+
else chain.push(value);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const plugins = before.map((fn) => ({ value: j.callExpression(site.member("beforeEach"), [fn]) }));
|
|
173
|
+
if (after.length) {
|
|
174
|
+
let cleanup = after[0];
|
|
175
|
+
if (after.length > 1) {
|
|
176
|
+
const statements = after.flatMap((fn) => {
|
|
177
|
+
if (!asyncProperty && j.ArrowFunctionExpression.check(fn) && !fn.async && j.BlockStatement.check(fn.body) && !fn.body.directives?.length && fn.body.body.every((s) => j.ExpressionStatement.check(s))) return fn.body.body;
|
|
178
|
+
const call = j.callExpression(fn, []);
|
|
179
|
+
return [j.expressionStatement(asyncProperty ? j.awaitExpression(call) : call)];
|
|
180
|
+
});
|
|
181
|
+
const combined = j.arrowFunctionExpression([], j.blockStatement(statements));
|
|
182
|
+
combined.async = asyncProperty;
|
|
183
|
+
cleanup = combined;
|
|
184
|
+
}
|
|
185
|
+
plugins.push({ value: j.callExpression(site.member("afterEach"), [cleanup]) });
|
|
186
|
+
}
|
|
187
|
+
const base = property;
|
|
188
|
+
return {
|
|
189
|
+
remove: [],
|
|
190
|
+
plugins,
|
|
191
|
+
conflicts,
|
|
192
|
+
property: declarator ? void 0 : base,
|
|
193
|
+
apply: declarator ? () => {
|
|
194
|
+
declarator.get("init").replace(base);
|
|
195
|
+
} : void 0,
|
|
196
|
+
insertBefore: (item) => {
|
|
197
|
+
const plugin = j.SpreadElement.check(item) && j.ConditionalExpression.check(item.argument) && j.ArrayExpression.check(item.argument.consequent) ? item.argument.consequent.elements[0] : item;
|
|
198
|
+
return j.CallExpression.check(plugin) && [
|
|
199
|
+
"ignoreEqualValues",
|
|
200
|
+
"skipEqualValues",
|
|
201
|
+
"interruptAfterTimeLimit",
|
|
202
|
+
"unbiased",
|
|
203
|
+
"timeout"
|
|
204
|
+
].includes(site.importedName(plugin) ?? "");
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function transform(file, api) {
|
|
209
|
+
return migratePluginCalls(file, api, (site) => site.runner === "configureGlobal" ? globalHooks(site) : localHooks(site));
|
|
210
|
+
}
|
|
211
|
+
export { transform as default };
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { n as awaitContext } from "../../allowAwait-GB5WG8dx.js";
|
|
2
|
+
import { a as memberName, i as isReference, n as importedName, t as findBinding } from "../../fastCheck-BCuGakPT.js";
|
|
3
|
+
import { t as reportUnsupported } from "../../reportUnsupported-DY7wlHET.js";
|
|
4
|
+
// src/transforms/v5/propertyToAsyncProperty.ts
|
|
5
|
+
function transform(file, api) {
|
|
6
|
+
const j = api.jscodeshift.withParser(/\.[cm]?ts$/.test(file.path) ? "ts" : "tsx");
|
|
7
|
+
const root = j(file.source);
|
|
8
|
+
const references = (binding) => root.find(j.Identifier, { name: binding.node.name }).filter((ref) => ref.node !== binding.node && isReference(j, ref) && findBinding(j, ref, ref.node.name)?.node === binding.node).paths();
|
|
9
|
+
const calls = root.find(j.CallExpression).paths();
|
|
10
|
+
const properties = calls.filter((path) => importedName(j, path, path.node.callee) === "property");
|
|
11
|
+
const migrations = [];
|
|
12
|
+
function planConsumer(use) {
|
|
13
|
+
const path = use.parent;
|
|
14
|
+
const call = path.node;
|
|
15
|
+
const name = j.CallExpression.check(call) ? importedName(j, path, call.callee) : void 0;
|
|
16
|
+
if (!j.CallExpression.check(call) || call.optional || call.arguments[0] !== use.node || !name || ![
|
|
17
|
+
"assert",
|
|
18
|
+
"check",
|
|
19
|
+
"sample",
|
|
20
|
+
"statistics"
|
|
21
|
+
].includes(name)) {
|
|
22
|
+
reportUnsupported(api, use.node, "Cannot migrate a property that is exported or passed to an unknown consumer.");
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (name === "sample" || name === "statistics" || j.AwaitExpression.check(path.parent.node)) return () => {};
|
|
26
|
+
const context = awaitContext(j, path, file.path);
|
|
27
|
+
if (!context) {
|
|
28
|
+
reportUnsupported(api, call, `Cannot make ${name}() asynchronous in this context; migrate its caller manually.`);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const parent = path.parent.node;
|
|
32
|
+
if (j.ReturnStatement.check(parent) && parent.argument === call || j.ArrowFunctionExpression.check(parent) && parent.body === call) return () => {};
|
|
33
|
+
return () => {
|
|
34
|
+
if (context.functionToMakeAsync) context.functionToMakeAsync.async = true;
|
|
35
|
+
path.replace(j.awaitExpression(call));
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
for (const property of properties) {
|
|
39
|
+
const parent = property.parent;
|
|
40
|
+
let uses;
|
|
41
|
+
if (j.VariableDeclarator.check(parent.node) && parent.node.init === property.node) {
|
|
42
|
+
const declaration = parent.parent;
|
|
43
|
+
if (!j.Identifier.check(parent.node.id) || parent.node.id.typeAnnotation || !j.VariableDeclaration.check(declaration.node) || declaration.node.kind !== "const" || j.ExportNamedDeclaration.check(declaration.parent.node)) {
|
|
44
|
+
reportUnsupported(api, property.node, "Cannot migrate an exported, mutable or explicitly typed property binding.");
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
uses = references(parent.get("id"));
|
|
48
|
+
} else uses = [property];
|
|
49
|
+
const actions = uses.map(planConsumer);
|
|
50
|
+
if (actions.every((action) => action !== void 0)) migrations.push({
|
|
51
|
+
property,
|
|
52
|
+
applyConsumers: actions
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
for (const call of calls) {
|
|
56
|
+
const name = importedName(j, call, call.node.callee);
|
|
57
|
+
if (name !== "assert" && name !== "check") continue;
|
|
58
|
+
let input = call.get("arguments", 0);
|
|
59
|
+
if (j.Identifier.check(input.node)) {
|
|
60
|
+
const binding = findBinding(j, input, input.node.name);
|
|
61
|
+
if (j.VariableDeclarator.check(binding?.parent.node)) input = binding.parent.get("init");
|
|
62
|
+
}
|
|
63
|
+
const origin = j.CallExpression.check(input.node) ? importedName(j, input, input.node.callee) : void 0;
|
|
64
|
+
if (origin !== "property" && origin !== "asyncProperty") reportUnsupported(api, call.node, "Cannot determine whether this assertion uses a synchronous fast-check property.");
|
|
65
|
+
}
|
|
66
|
+
const named = /* @__PURE__ */ new Map();
|
|
67
|
+
for (const { property } of migrations) {
|
|
68
|
+
if (!j.Identifier.check(property.node.callee)) continue;
|
|
69
|
+
const binding = findBinding(j, property, property.node.callee.name);
|
|
70
|
+
if (!binding || !j.ImportSpecifier.check(binding.parent.node)) continue;
|
|
71
|
+
let group = named.get(binding.parent.node);
|
|
72
|
+
if (!group) {
|
|
73
|
+
group = {
|
|
74
|
+
binding,
|
|
75
|
+
calls: []
|
|
76
|
+
};
|
|
77
|
+
named.set(binding.parent.node, group);
|
|
78
|
+
}
|
|
79
|
+
group.calls.push(property);
|
|
80
|
+
}
|
|
81
|
+
const importEdits = [];
|
|
82
|
+
const reserved = new Set(root.find(j.Identifier).nodes().map((node) => node.name));
|
|
83
|
+
for (const [specifier, group] of named) {
|
|
84
|
+
const binding = group.binding;
|
|
85
|
+
const declaration = binding.parent.parent.node;
|
|
86
|
+
const allMigrated = references(binding).every((ref) => group.calls.some((call) => call.node.callee === ref.node));
|
|
87
|
+
const existing = root.find(j.ImportSpecifier, { imported: { name: "asyncProperty" } }).paths().find((path) => {
|
|
88
|
+
const local = path.node.local;
|
|
89
|
+
return j.Identifier.check(local) && group.calls.every((call) => importedName(j, call, local) === "asyncProperty" && findBinding(j, call, local.name)?.parent.node === path.node);
|
|
90
|
+
});
|
|
91
|
+
let local;
|
|
92
|
+
if (existing && j.Identifier.check(existing.node.local)) {
|
|
93
|
+
local = existing.node.local.name;
|
|
94
|
+
if (allMigrated) importEdits.push(() => {
|
|
95
|
+
declaration.specifiers = declaration.specifiers.filter((entry) => entry !== specifier);
|
|
96
|
+
});
|
|
97
|
+
} else if (allMigrated) {
|
|
98
|
+
local = binding.node.name === "property" && !reserved.has("asyncProperty") ? "asyncProperty" : binding.node.name;
|
|
99
|
+
const replacement = j.importSpecifier(j.identifier("asyncProperty"), j.identifier(local));
|
|
100
|
+
replacement.comments = specifier.comments;
|
|
101
|
+
importEdits.push(() => {
|
|
102
|
+
declaration.specifiers = declaration.specifiers.map((entry) => entry === specifier ? replacement : entry);
|
|
103
|
+
});
|
|
104
|
+
} else {
|
|
105
|
+
local = "asyncProperty";
|
|
106
|
+
while (reserved.has(local)) local += "ForProperty";
|
|
107
|
+
const addition = j.importSpecifier(j.identifier("asyncProperty"), j.identifier(local));
|
|
108
|
+
importEdits.push(() => declaration.specifiers.push(addition));
|
|
109
|
+
}
|
|
110
|
+
reserved.add(local);
|
|
111
|
+
importEdits.push(() => {
|
|
112
|
+
for (const call of group.calls) call.node.callee = j.identifier(local);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
root.find(j.MemberExpression).forEach((path) => {
|
|
116
|
+
if (memberName(j, path, path.node) !== "property") return;
|
|
117
|
+
if (j.CallExpression.check(path.parent.node) && path.parent.node.callee === path.node) return;
|
|
118
|
+
reportUnsupported(api, path.node, "Cannot migrate a property factory passed around without a direct call.");
|
|
119
|
+
});
|
|
120
|
+
root.find(j.ImportSpecifier, { imported: { name: "property" } }).forEach((path) => {
|
|
121
|
+
if (!j.Identifier.check(path.node.local) || importedName(j, path, path.node.local) !== "property") return;
|
|
122
|
+
for (const ref of references(path.get("local"))) {
|
|
123
|
+
if (j.CallExpression.check(ref.parent.node) && ref.parent.node.callee === ref.node) continue;
|
|
124
|
+
reportUnsupported(api, ref.node, "Cannot migrate a property factory passed around without a direct call.");
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
if (!migrations.length) return file.source;
|
|
128
|
+
for (const edit of importEdits) edit();
|
|
129
|
+
for (const { property, applyConsumers } of migrations) {
|
|
130
|
+
if (j.MemberExpression.check(property.node.callee)) property.node.callee.property = j.identifier("asyncProperty");
|
|
131
|
+
for (const apply of applyConsumers) apply();
|
|
132
|
+
}
|
|
133
|
+
return root.toSource({ quote: "single" });
|
|
134
|
+
}
|
|
135
|
+
export { transform as default };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { t as reportUnsupported } from "../../reportUnsupported-DY7wlHET.js";
|
|
2
|
+
// src/transforms/v5/randomMethods.ts
|
|
3
|
+
function transform(file, api) {
|
|
4
|
+
const j = api.jscodeshift.withParser(/\.[cm]?ts$/.test(file.path) ? "ts" : "tsx");
|
|
5
|
+
const root = j(file.source);
|
|
6
|
+
const expression = j.template.expression;
|
|
7
|
+
function isFastCheckRandom(path, name) {
|
|
8
|
+
const bindings = path.scope.lookup(name)?.getBindings()[name];
|
|
9
|
+
if (bindings?.length !== 1) return false;
|
|
10
|
+
const binding = bindings[0];
|
|
11
|
+
const annotation = binding.node.typeAnnotation?.typeAnnotation;
|
|
12
|
+
if (!j.TSTypeReference.check(annotation)) return false;
|
|
13
|
+
const type = annotation.typeName;
|
|
14
|
+
const qualified = j.TSQualifiedName.check(type);
|
|
15
|
+
if (qualified && (!j.Identifier.check(type.right) || type.right.name !== "Random")) return false;
|
|
16
|
+
const imported = qualified ? type.left : type;
|
|
17
|
+
if (!j.Identifier.check(imported) || binding.scope.lookupType(imported.name)) return false;
|
|
18
|
+
const specifierPath = binding.scope.lookup(imported.name)?.getBindings()[imported.name]?.[0]?.parent;
|
|
19
|
+
const declaration = specifierPath?.parent.node;
|
|
20
|
+
if (!j.ImportDeclaration.check(declaration) || declaration.source.value !== "fast-check") return false;
|
|
21
|
+
const specifier = specifierPath.node;
|
|
22
|
+
return qualified ? j.ImportNamespaceSpecifier.check(specifier) || j.ImportDefaultSpecifier.check(specifier) : j.ImportSpecifier.check(specifier) && specifier.imported.name === "Random";
|
|
23
|
+
}
|
|
24
|
+
let changed = false;
|
|
25
|
+
root.find(j.CallExpression).paths().reverse().forEach((path) => {
|
|
26
|
+
const { callee, arguments: args } = path.node;
|
|
27
|
+
if (!j.MemberExpression.check(callee) || callee.computed || !j.Identifier.check(callee.property)) return;
|
|
28
|
+
const rng = callee.object;
|
|
29
|
+
if (!j.Identifier.check(rng) || !isFastCheckRandom(path, rng.name)) return;
|
|
30
|
+
const method = callee.property.name;
|
|
31
|
+
if (![
|
|
32
|
+
"nextBoolean",
|
|
33
|
+
"nextInt",
|
|
34
|
+
"next",
|
|
35
|
+
"nextDouble"
|
|
36
|
+
].includes(method)) return;
|
|
37
|
+
if (method === "nextInt" && args.length === 2 && args.every((arg) => j.Expression.check(arg))) return;
|
|
38
|
+
if (path.node.type !== "CallExpression" || callee.type !== "MemberExpression") {
|
|
39
|
+
reportUnsupported(api, path.node, `Cannot migrate optional Random.${method}() calls; preserve short-circuiting manually.`);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (method === "next" ? args.length !== 1 || !j.Expression.check(args[0]) : args.length !== 0) {
|
|
43
|
+
reportUnsupported(api, path.node, `Cannot migrate Random.${method}() with these arguments.`);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
switch (method) {
|
|
47
|
+
case "nextBoolean":
|
|
48
|
+
path.replace(expression`${rng}.nextInt(0, 1) === 1`);
|
|
49
|
+
break;
|
|
50
|
+
case "nextInt":
|
|
51
|
+
path.replace(expression`${rng}.nextInt(-2147483648, 2147483647)`);
|
|
52
|
+
break;
|
|
53
|
+
case "next":
|
|
54
|
+
path.replace(expression`${rng}.nextInt(0, (1 << ${args[0]}) - 1)`);
|
|
55
|
+
break;
|
|
56
|
+
case "nextDouble":
|
|
57
|
+
path.replace(expression`(${rng}.nextInt(0, 2 ** 26 - 1) * 2 ** 27 + ${rng}.nextInt(0, 2 ** 27 - 1)) / 2 ** 53`);
|
|
58
|
+
break;
|
|
59
|
+
default: return;
|
|
60
|
+
}
|
|
61
|
+
changed = true;
|
|
62
|
+
});
|
|
63
|
+
return changed ? root.toSource({ quote: "single" }) : file.source;
|
|
64
|
+
}
|
|
65
|
+
export { transform as default };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { a as memberName } from "../../fastCheck-BCuGakPT.js";
|
|
2
|
+
import { t as reportUnsupported } from "../../reportUnsupported-DY7wlHET.js";
|
|
3
|
+
// src/transforms/v5/randomTypeFactory.ts
|
|
4
|
+
const optionsIndex = /* @__PURE__ */ new Map([
|
|
5
|
+
["configureGlobal", 0],
|
|
6
|
+
["assert", 1],
|
|
7
|
+
["check", 1],
|
|
8
|
+
["sample", 1],
|
|
9
|
+
["statistics", 2]
|
|
10
|
+
]);
|
|
11
|
+
const factories = [
|
|
12
|
+
"congruential32",
|
|
13
|
+
"mersenne",
|
|
14
|
+
"xorshift128plus",
|
|
15
|
+
"xoroshiro128plus"
|
|
16
|
+
];
|
|
17
|
+
function transform(file, api) {
|
|
18
|
+
const j = api.jscodeshift.withParser(/\.[cm]?ts$/.test(file.path) ? "ts" : "tsx");
|
|
19
|
+
const root = j(file.source);
|
|
20
|
+
const imports = root.find(j.ImportDeclaration);
|
|
21
|
+
const newFactories = /* @__PURE__ */ new Map();
|
|
22
|
+
const names = new Set(root.find(j.Identifier).nodes().map((node) => node.name));
|
|
23
|
+
root.find(j.CallExpression).forEach((path) => {
|
|
24
|
+
const index = optionsIndex.get(memberName(j, path, path.node.callee) ?? "");
|
|
25
|
+
if (index === void 0) return;
|
|
26
|
+
const options = path.node.arguments[index];
|
|
27
|
+
if (!j.ObjectExpression.check(options)) return;
|
|
28
|
+
for (const prop of options.properties) {
|
|
29
|
+
if (!j.ObjectProperty.check(prop) || prop.computed || !j.Identifier.check(prop.key) || prop.key.name !== "randomType") continue;
|
|
30
|
+
if (!j.StringLiteral.check(prop.value)) {
|
|
31
|
+
if (j.FunctionExpression.check(prop.value) || j.ArrowFunctionExpression.check(prop.value)) continue;
|
|
32
|
+
if (j.Identifier.check(prop.value)) {
|
|
33
|
+
const binding = path.scope.lookup(prop.value.name)?.getBindings()[prop.value.name]?.[0];
|
|
34
|
+
const specifier = binding?.parent.node;
|
|
35
|
+
const declaration = binding?.parent.parent.node;
|
|
36
|
+
if (j.ImportSpecifier.check(specifier) && j.Identifier.check(specifier.imported) && j.ImportDeclaration.check(declaration) && declaration.importKind !== "type" && (!("importKind" in specifier) || specifier.importKind !== "type") && factories.includes(specifier.imported.name) && declaration.source.value === "pure-rand/generator/" + specifier.imported.name) continue;
|
|
37
|
+
}
|
|
38
|
+
reportUnsupported(api, prop, "Cannot migrate dynamic randomType; ensure its value is a generator factory.");
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const factory = prop.value.value === "congruential" ? "congruential32" : prop.value.value;
|
|
42
|
+
if (!factories.includes(factory)) {
|
|
43
|
+
reportUnsupported(api, prop, `Cannot migrate unknown randomType '${prop.value.value}'; choose a generator factory manually.`);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const source = "pure-rand/generator/" + factory;
|
|
47
|
+
const existing = imports.nodes().filter((node) => node.source.value === source && node.importKind !== "type").flatMap((node) => node.specifiers ?? []).find((spec) => j.ImportSpecifier.check(spec) && (!("importKind" in spec) || spec.importKind !== "type") && spec.imported.name === factory && j.Identifier.check(spec.local) && path.scope.lookup(spec.local.name)?.getBindings()[spec.local.name]?.[0]?.parent.node === spec);
|
|
48
|
+
let local = j.Identifier.check(existing?.local) ? existing.local.name : newFactories.get(factory);
|
|
49
|
+
if (!local) {
|
|
50
|
+
local = factory;
|
|
51
|
+
while (names.has(local)) local += "Factory";
|
|
52
|
+
names.add(local);
|
|
53
|
+
newFactories.set(factory, local);
|
|
54
|
+
}
|
|
55
|
+
prop.value = Object.assign(j.identifier(local), { comments: prop.value.comments });
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
for (const [factory, local] of newFactories) {
|
|
59
|
+
const spec = j.importSpecifier(j.identifier(factory), local === factory ? null : j.identifier(local));
|
|
60
|
+
imports.at(0).insertBefore(j.importDeclaration([spec], j.stringLiteral("pure-rand/generator/" + factory)));
|
|
61
|
+
}
|
|
62
|
+
return root.toSource({ quote: "single" });
|
|
63
|
+
}
|
|
64
|
+
export { transform as default };
|