@fast-check/codemod 0.0.0 → 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.
@@ -0,0 +1,139 @@
1
+ import { t as migratePluginCalls } from "../../pluginMigration-TF34gzVd.js";
2
+ import { t as findBinding } from "../../fastCheck-BCuGakPT.js";
3
+ // src/transforms/runnerReporting.ts
4
+ function reporting(site) {
5
+ const { j, options, runner, report } = site;
6
+ const custom = ["reporter", "asyncReporter"].filter((key) => options.has(key));
7
+ if (!custom.length) return {
8
+ remove: [],
9
+ plugins: []
10
+ };
11
+ const format = options.get("includeErrorInReport");
12
+ const remove = format && j.BooleanLiteral.check(format) ? [...custom, "includeErrorInReport"] : custom;
13
+ if (runner === "check") {
14
+ if (custom.length > 1) return void 0;
15
+ const callback = options.get(custom[0]);
16
+ if (custom.length && !j.ArrowFunctionExpression.check(callback) && !j.FunctionExpression.check(callback)) return void 0;
17
+ return {
18
+ remove,
19
+ plugins: []
20
+ };
21
+ }
22
+ if (custom.length > 1 || options.has("noDefaultPlugins") || runner === "configureGlobal" && site.hasCheck) {
23
+ report("Review the reporting policy and its consumers before installing a completion hook.");
24
+ return;
25
+ }
26
+ const callback = options.get(custom[0]);
27
+ if (!j.Identifier.check(callback) && !j.ArrowFunctionExpression.check(callback) && !j.FunctionExpression.check(callback)) {
28
+ report("Review the expression that supplies the custom reporter.");
29
+ return;
30
+ }
31
+ const declaration = j.Identifier.check(callback) ? findBinding(j, site.path, callback.name)?.parent : void 0;
32
+ const init = j.VariableDeclarator.check(declaration?.node) ? declaration.node.init : void 0;
33
+ const known = j.FunctionDeclaration.check(declaration?.node) ? declaration.node : declaration?.parent.node.kind === "const" && (j.FunctionExpression.check(init) || j.ArrowFunctionExpression.check(init)) ? init : callback;
34
+ if (j.Identifier.check(known)) {
35
+ report("Review the reporter reference: it may change or resolve to undefined.");
36
+ return;
37
+ }
38
+ if (j(known).find(j.ThisExpression).size() || j(known).find(j.Super).size()) {
39
+ report("Review the custom reporter: v5 changes its receiver.");
40
+ return;
41
+ }
42
+ let handler = callback;
43
+ const mark = options.get("markInterruptAsFailure");
44
+ if (mark && !(j.BooleanLiteral.check(mark) && !mark.value)) {
45
+ if (!j.BooleanLiteral.check(mark) || !j.FunctionExpression.check(callback) && !j.ArrowFunctionExpression.check(callback)) return void 0;
46
+ const [details] = callback.params;
47
+ if (!j.Identifier.check(details) || callback.params.length !== 1) return void 0;
48
+ const id = j.identifier(details.name);
49
+ const field = (name) => j.memberExpression(id, j.identifier(name));
50
+ const body = j.BlockStatement.check(callback.body) ? callback.body.body : [j.returnStatement(callback.body)];
51
+ const asFailure = j.objectExpression([
52
+ j.spreadElement(id),
53
+ j.objectProperty(j.identifier("failed"), j.booleanLiteral(true)),
54
+ j.objectProperty(j.identifier("interrupted"), j.booleanLiteral(true))
55
+ ]);
56
+ const guard = j.ifStatement(j.logicalExpression("&&", field("interrupted"), j.unaryExpression("!", field("failed"))), j.blockStatement([j.expressionStatement(j.assignmentExpression("=", id, asFailure))]));
57
+ handler = {
58
+ ...callback,
59
+ body: j.blockStatement([guard, ...body]),
60
+ expression: false
61
+ };
62
+ }
63
+ const factory = j.arrowFunctionExpression([], j.objectExpression([j.objectProperty(j.identifier("onAllRunsComplete"), handler)]));
64
+ if (runner === "configureGlobal" && site.typescript) {
65
+ const plugin = j.identifier("PluginInstance");
66
+ const args = j.tsTypeParameterInstantiation([j.tsUnknownKeyword()]);
67
+ factory.returnType = j.tsTypeAnnotation(site.typeNamespace ? j.tsTypeReference(j.tsQualifiedName(j.identifier(site.typeNamespace), plugin), args) : j.tsImportType(j.stringLiteral("fast-check"), plugin, args));
68
+ }
69
+ return {
70
+ remove,
71
+ conflicts: [
72
+ "reporter",
73
+ "asyncReporter",
74
+ "markInterruptAsFailure"
75
+ ],
76
+ plugins: [{ value: factory }],
77
+ parameters: { noDefaultPlugins: j.booleanLiteral(true) }
78
+ };
79
+ }
80
+ // src/transforms/v5/runnerPlugins.ts
81
+ function runnerPlugins(site) {
82
+ const { j, member, options, report, runner, call } = site;
83
+ const reportingEdit = reporting(site);
84
+ if (!reportingEdit) return void 0;
85
+ const remove = [
86
+ "ignoreEqualValues",
87
+ "skipEqualValues",
88
+ "interruptAfterTimeLimit",
89
+ "unbiased",
90
+ "timeout"
91
+ ].filter((name) => options.has(name));
92
+ if (options.has("skipAllAfterTimeLimit")) {
93
+ report("skipAllAfterTimeLimit has no equivalent skipping plugin.");
94
+ return;
95
+ }
96
+ const markFailure = options.get("markInterruptAsFailure");
97
+ const customReporter = options.has("reporter") || options.has("asyncReporter");
98
+ if (markFailure && !options.has("interruptAfterTimeLimit")) {
99
+ report("Review the interruption policy without an explicit time limit.");
100
+ return;
101
+ }
102
+ const emptyLocalOptions = runner !== "configureGlobal" && call.arguments.length === 2 && options.size === 0 && !call.arguments[1].comments?.length;
103
+ if (!remove.length && !reportingEdit.remove.length && !emptyLocalOptions) return void 0;
104
+ const plugins = [];
105
+ for (const name of remove) {
106
+ const value = options.get(name);
107
+ if (name === "timeout" || name === "interruptAfterTimeLimit") {
108
+ let when;
109
+ if (j.Identifier.check(value)) when = j.binaryExpression("!==", value, j.unaryExpression("void", j.numericLiteral(0)));
110
+ else if (!j.NumericLiteral.check(value)) {
111
+ report("Review the time limit expression before moving it into a plugin.");
112
+ return;
113
+ }
114
+ const args = [value];
115
+ if (name === "interruptAfterTimeLimit" && runner === "assert" && !customReporter && markFailure && !(j.BooleanLiteral.check(markFailure) && !markFailure.value)) args.push(j.objectExpression([j.objectProperty(j.identifier("failOnInterrupt"), markFailure)]));
116
+ plugins.push({
117
+ value: j.callExpression(member(name), args),
118
+ when
119
+ });
120
+ } else if (!j.BooleanLiteral.check(value) || value.value) plugins.push({
121
+ value: j.callExpression(member(name), []),
122
+ when: j.BooleanLiteral.check(value) ? void 0 : j.binaryExpression("===", value, j.booleanLiteral(true))
123
+ });
124
+ }
125
+ const result = j.AwaitExpression.check(site.path.parent.node) ? site.path.parent : site.path;
126
+ const ignoresCheckResult = runner === "check" && j.ExpressionStatement.check(result.parent.node);
127
+ if (markFailure && (runner === "assert" || ignoresCheckResult || runner === "configureGlobal" && customReporter || j.BooleanLiteral.check(markFailure) && !markFailure.value)) remove.push("markInterruptAsFailure");
128
+ return {
129
+ remove: [...remove, ...reportingEdit.remove],
130
+ plugins: [...plugins, ...reportingEdit.plugins],
131
+ parameters: reportingEdit.parameters,
132
+ shareLocalOptions: !markFailure && !reportingEdit.remove.length,
133
+ conflicts: [...reportingEdit.conflicts ?? [], ...remove.includes("interruptAfterTimeLimit") ? ["markInterruptAsFailure"] : []]
134
+ };
135
+ }
136
+ function transform(file, api) {
137
+ return migratePluginCalls(file, api, runnerPlugins);
138
+ }
139
+ export { transform as default };
@@ -0,0 +1,2 @@
1
+ import { API, FileInfo } from "jscodeshift";
2
+ export default function transform(file: FileInfo, api: API): string;
@@ -0,0 +1,32 @@
1
+ import { t as allowAwait } from "../../allowAwait-GB5WG8dx.js";
2
+ import { t as reportUnsupported } from "../../reportUnsupported-DY7wlHET.js";
3
+ import { n as importedName } from "../../fastCheck-BCuGakPT.js";
4
+ import { n as samplingOptions, t as isPropertyInput } from "../../sampling-eUHO0ySk.js";
5
+ // src/transforms/v5/sample.ts
6
+ function transform(file, api) {
7
+ const j = api.jscodeshift.withParser(/\.[cm]?ts$/.test(file.path) ? "ts" : "tsx");
8
+ const root = j(file.source);
9
+ for (const path of root.find(j.CallExpression).paths().reverse()) {
10
+ const { callee, arguments: args } = path.node;
11
+ if (importedName(j, path, callee) !== "sample") continue;
12
+ const awaited = j.AwaitExpression.check(path.parent.node);
13
+ if (!awaited && !allowAwait(j, path, file.path)) {
14
+ reportUnsupported(api, path.node, "Cannot await sample() in this synchronous context.");
15
+ continue;
16
+ }
17
+ const property = isPropertyInput(j, path);
18
+ const params = args[1];
19
+ if (property === void 0) reportUnsupported(api, path.node, "If the sample input is a property, set noPredicateExecution to true.");
20
+ if (!awaited) path.replace(j.awaitExpression(path.node));
21
+ if (args.length > 2) continue;
22
+ const options = samplingOptions(j, params);
23
+ if (!options) continue;
24
+ const { opts, keys } = options;
25
+ if (keys.includes("logger")) continue;
26
+ if (property) opts.properties.push(j.objectProperty(j.identifier("noPredicateExecution"), j.booleanLiteral(true)));
27
+ if (opts.properties.length) args[1] = opts;
28
+ else if (params) args.splice(1, 1);
29
+ }
30
+ return root.toSource({ quote: "single" });
31
+ }
32
+ export { transform as default };
@@ -0,0 +1,2 @@
1
+ import { API, FileInfo } from "jscodeshift";
2
+ export default function transform(file: FileInfo, api: API): string;
@@ -0,0 +1,56 @@
1
+ import { t as reportUnsupported } from "../../reportUnsupported-DY7wlHET.js";
2
+ import { t as allowAwait } from "../../allowAwait-GB5WG8dx.js";
3
+ import { a as memberName, t as findBinding } from "../../fastCheck-BCuGakPT.js";
4
+ import { n as samplingOptions, t as isPropertyInput } from "../../sampling-eUHO0ySk.js";
5
+ // src/transforms/v5/statistics.ts
6
+ function canMoveLogger(j, path, log) {
7
+ if (j.ArrowFunctionExpression.check(log) || j.FunctionExpression.check(log)) return true;
8
+ if (!j.Identifier.check(log)) return false;
9
+ const binding = findBinding(j, path, log.name)?.parent;
10
+ return j.FunctionDeclaration.check(binding?.node) || j.VariableDeclarator.check(binding?.node) && binding.parent.node.kind === "const";
11
+ }
12
+ function migrateClassifier(j, classifier) {
13
+ if (!j.ArrowFunctionExpression.check(classifier) || classifier.params.length !== 1) return false;
14
+ const arg = classifier.params[0];
15
+ if ("typeAnnotation" in arg && arg.typeAnnotation) return false;
16
+ if (j.ArrayPattern.check(arg) && arg.elements.every((e) => j.Identifier.check(e))) classifier.params = arg.elements.filter((e) => j.Identifier.check(e));
17
+ else if (j.Identifier.check(arg)) classifier.params = [j.restElement(arg)];
18
+ else return false;
19
+ return true;
20
+ }
21
+ function transform(file, api) {
22
+ const j = api.jscodeshift.withParser(/\.[cm]?ts$/.test(file.path) ? "ts" : "tsx");
23
+ const root = j(file.source);
24
+ for (const path of root.find(j.CallExpression).paths().reverse()) {
25
+ const { callee, arguments: args } = path.node;
26
+ if (memberName(j, path, callee) !== "statistics" || !j.MemberExpression.check(callee)) continue;
27
+ if (!j.ExpressionStatement.check(path.parent.node) || args.length < 2) continue;
28
+ if (args.length > 3) continue;
29
+ if (!allowAwait(j, path, file.path)) {
30
+ reportUnsupported(api, path.node, "Cannot await statistics() in this synchronous context.");
31
+ continue;
32
+ }
33
+ const property = isPropertyInput(j, path);
34
+ if (property === void 0) reportUnsupported(api, path.node, "If the input is a property, set noPredicateExecution to true and adapt the classifier to receive separate arguments.");
35
+ if (property && !migrateClassifier(j, args[1])) reportUnsupported(api, path.node, "Adapt the property classifier to receive separate arguments.");
36
+ const params = args[2];
37
+ const options = samplingOptions(j, params);
38
+ const logger = options?.opts.properties[options.keys.indexOf("logger")];
39
+ const log = j.ObjectProperty.check(logger) ? logger.value : void 0;
40
+ const result = j.awaitExpression(path.node);
41
+ if (!options || log && !canMoveLogger(j, path, log)) {
42
+ reportUnsupported(api, path.node, "Review statistics options and pass the result and original logger to logStatistics.");
43
+ if (property) reportUnsupported(api, path.node, "Set noPredicateExecution to true in the statistics options.");
44
+ path.replace(result);
45
+ continue;
46
+ }
47
+ const { opts, keys } = options;
48
+ if (logger) opts.properties.splice(keys.indexOf("logger"), 1);
49
+ if (property) opts.properties.push(j.objectProperty(j.identifier("noPredicateExecution"), j.booleanLiteral(true)));
50
+ if (opts.properties.length) args[2] = opts;
51
+ else if (params) args.splice(2, 1);
52
+ path.replace(j.callExpression(j.memberExpression(callee.object, j.identifier("logStatistics")), log ? [result, log] : [result]));
53
+ }
54
+ return root.toSource({ quote: "single" });
55
+ }
56
+ export { transform as default };
@@ -0,0 +1,2 @@
1
+ import { API, FileInfo } from "jscodeshift";
2
+ export default function transform(file: FileInfo, api: API): string;
@@ -0,0 +1,64 @@
1
+ import { t as reportUnsupported } from "../../reportUnsupported-DY7wlHET.js";
2
+ // src/transforms/v5/stringUnit.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
+ function arbitraryName(path) {
7
+ const { callee } = path.node;
8
+ const member = j.MemberExpression.check(callee) && !callee.computed ? callee : void 0;
9
+ const receiver = member ? member.object : callee;
10
+ if (!j.Identifier.check(receiver)) return;
11
+ const binding = path.scope.lookup(receiver.name)?.getBindings()[receiver.name]?.[0];
12
+ const specifier = binding?.parent.node;
13
+ const declaration = binding?.parent.parent.node;
14
+ if (!j.ImportDeclaration.check(declaration) || declaration.source.value !== "fast-check") return;
15
+ if (!member && j.ImportSpecifier.check(specifier) && j.Identifier.check(specifier.imported)) return specifier.imported.name;
16
+ if (member && j.Identifier.check(member.property) && (j.ImportNamespaceSpecifier.check(specifier) || j.ImportDefaultSpecifier.check(specifier))) return member.property.name;
17
+ }
18
+ let changed = false;
19
+ root.find(j.CallExpression).forEach((path) => {
20
+ const args = path.node.arguments;
21
+ if (args.length !== 1 || !j.ObjectExpression.check(args[0])) return;
22
+ const name = arbitraryName(path);
23
+ const isJson = name === "json" || name === "jsonValue";
24
+ if (!isJson && name !== "object" && name !== "anything") return;
25
+ const flag = isJson ? "noUnicodeString" : "withUnicodeString";
26
+ let property;
27
+ let hasStringUnit = false;
28
+ let hasUnsupportedProperty = false;
29
+ for (const entry of args[0].properties) {
30
+ if (!j.ObjectProperty.check(entry) || entry.computed || !j.Identifier.check(entry.key)) {
31
+ hasUnsupportedProperty = true;
32
+ continue;
33
+ }
34
+ if (entry.key.name === "stringUnit") hasStringUnit = true;
35
+ if (entry.key.name === flag) {
36
+ if (property) {
37
+ reportUnsupported(api, entry, `Cannot migrate duplicate ${flag} flags; resolve the options manually.`);
38
+ return;
39
+ }
40
+ property = entry;
41
+ }
42
+ }
43
+ if (!property) return;
44
+ if (hasUnsupportedProperty) {
45
+ reportUnsupported(api, property, `Cannot migrate ${flag} in an options object containing spreads or unsupported property keys.`);
46
+ return;
47
+ }
48
+ if (!j.BooleanLiteral.check(property.value)) {
49
+ reportUnsupported(api, property, `Cannot migrate non-literal ${flag}; replace it with stringUnit manually.`);
50
+ return;
51
+ }
52
+ if (hasStringUnit) args[0].properties = args[0].properties.filter((entry) => entry !== property);
53
+ else {
54
+ const unicode = isJson ? !property.value.value : property.value.value;
55
+ const value = j.stringLiteral(unicode ? "binary" : "grapheme-ascii");
56
+ property.key = Object.assign(j.identifier("stringUnit"), { comments: property.key.comments });
57
+ value.comments = property.value.comments;
58
+ property.value = value;
59
+ }
60
+ changed = true;
61
+ });
62
+ return changed ? root.toSource({ quote: "single" }) : file.source;
63
+ }
64
+ export { transform as default };
@@ -0,0 +1,2 @@
1
+ import { API, FileInfo } from "jscodeshift";
2
+ export default function transform(file: FileInfo, api: API): string;
@@ -0,0 +1,50 @@
1
+ import { a as memberName, t as findBinding } from "../../fastCheck-BCuGakPT.js";
2
+ // src/transforms/v6/asyncPropertyToProperty.ts
3
+ function isVariable(j, path) {
4
+ const parent = path.parent.node;
5
+ if (j.ImportSpecifier.check(parent) || j.ImportDefaultSpecifier.check(parent) || j.ImportNamespaceSpecifier.check(parent)) return path.name === "local";
6
+ if (j.ExportSpecifier.check(parent)) return path.name === "local" && !path.parent.parent.node.source;
7
+ if ((path.name === "key" || path.name === "property") && !parent.computed) return false;
8
+ if (j.TSQualifiedName.check(parent) && path.name === "right") return false;
9
+ if (path.name === "label") return false;
10
+ return true;
11
+ }
12
+ function transform(file, api) {
13
+ const j = api.jscodeshift.withParser(/\.[cm]?ts$/.test(file.path) ? "ts" : "tsx");
14
+ const root = j(file.source);
15
+ let changed = false;
16
+ const propertyInUse = root.find(j.Identifier, { name: "property" }).some((path) => isVariable(j, path));
17
+ root.find(j.MemberExpression).forEach((path) => {
18
+ const key = path.node.property;
19
+ if (j.Identifier.check(key) && memberName(j, path, path.node) === "asyncProperty") {
20
+ key.name = "property";
21
+ changed = true;
22
+ }
23
+ });
24
+ root.find(j.ImportDeclaration, { source: { value: "fast-check" } }).forEach(({ node }) => {
25
+ node.specifiers = node.specifiers?.map((specifier) => {
26
+ if (!j.ImportSpecifier.check(specifier) || specifier.imported.name !== "asyncProperty") return specifier;
27
+ let local = specifier.local;
28
+ if (local?.name === "asyncProperty" && !propertyInUse) {
29
+ root.find(j.Identifier, { name: "asyncProperty" }).filter((path) => isVariable(j, path) && findBinding(j, path, "asyncProperty")?.parent.node === specifier).forEach((path) => {
30
+ const parent = path.parent.node;
31
+ if ((j.Property.check(parent) || j.ObjectProperty.check(parent)) && parent.shorthand) parent.shorthand = false;
32
+ const renamed = Object.assign(j.identifier("property"), { comments: path.node.comments });
33
+ if (j.ExportSpecifier.check(parent)) path.parent.replace(j.exportSpecifier.from({
34
+ ...parent,
35
+ local: renamed
36
+ }));
37
+ else path.replace(renamed);
38
+ });
39
+ local = j.identifier("property");
40
+ }
41
+ changed = true;
42
+ const renamed = j.importSpecifier(j.identifier("property"), local);
43
+ renamed.comments = specifier.comments;
44
+ if ("importKind" in specifier) Object.assign(renamed, { importKind: specifier.importKind });
45
+ return renamed;
46
+ });
47
+ });
48
+ return changed ? root.toSource({ quote: "single" }) : file.source;
49
+ }
50
+ export { transform as default };
package/package.json CHANGED
@@ -1,12 +1,22 @@
1
1
  {
2
2
  "name": "@fast-check/codemod",
3
3
  "description": "Codemods to help migrate fast-check projects",
4
- "version": "0.0.0",
4
+ "version": "0.0.1",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
8
8
  "./cli": "./lib/cli.js",
9
- "./transforms/v5/defaultImport": "./lib/transforms/v5/defaultImport.js"
9
+ "./transforms/v5/checkToAssert": "./lib/transforms/v5/checkToAssert.js",
10
+ "./transforms/v5/defaultImport": "./lib/transforms/v5/defaultImport.js",
11
+ "./transforms/v5/lifecyclePlugins": "./lib/transforms/v5/lifecyclePlugins.js",
12
+ "./transforms/v5/propertyToAsyncProperty": "./lib/transforms/v5/propertyToAsyncProperty.js",
13
+ "./transforms/v5/randomMethods": "./lib/transforms/v5/randomMethods.js",
14
+ "./transforms/v5/randomTypeFactory": "./lib/transforms/v5/randomTypeFactory.js",
15
+ "./transforms/v5/runnerPlugins": "./lib/transforms/v5/runnerPlugins.js",
16
+ "./transforms/v5/sample": "./lib/transforms/v5/sample.js",
17
+ "./transforms/v5/statistics": "./lib/transforms/v5/statistics.js",
18
+ "./transforms/v5/stringUnit": "./lib/transforms/v5/stringUnit.js",
19
+ "./transforms/v6/asyncPropertyToProperty": "./lib/transforms/v6/asyncPropertyToProperty.js"
10
20
  },
11
21
  "bin": {
12
22
  "codemod": "./lib/cli.js"
@@ -33,8 +43,9 @@
33
43
  "jscodeshift": "^17.4.0"
34
44
  },
35
45
  "devDependencies": {
36
- "@types/node": "^26.6.6",
37
- "rolldown": "^1.3.2",
46
+ "@types/node": "^26.6.7",
47
+ "rolldown": "^1.3.3",
48
+ "tsx": "^4.23.15",
38
49
  "typescript": "~7.0.2",
39
50
  "vitest": "^5.1.1"
40
51
  },