@fast-check/codemod 0.0.0-stage → 0.0.0
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 +34 -2
- package/lib/cli.d.ts +1 -0
- package/lib/cli.js +31 -0
- package/lib/transforms/v5/defaultImport.d.ts +4 -0
- package/lib/transforms/v5/defaultImport.js +26 -0
- package/package.json +59 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nicolas DUBIEN
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@fast-check/codemod`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
Codemods to help upgrade projects using `fast-check`.
|
|
6
|
+
|
|
7
|
+
## Getting Started
|
|
8
|
+
|
|
9
|
+
Requires Node.js 20.19.0 or later. Run a transform on a file or directory:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npx @fast-check/codemod@latest <transform> <path>
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Add `--dry --print` to preview changes without writing files. Commit your work before running a codemod, then review the diff.
|
|
16
|
+
|
|
17
|
+
## Transforms
|
|
18
|
+
|
|
19
|
+
### v5
|
|
20
|
+
|
|
21
|
+
#### `v5/defaultImport`
|
|
22
|
+
|
|
23
|
+
**Requirements:** fast-check ≥1.0.0.
|
|
24
|
+
|
|
25
|
+
Replaces default imports with namespace imports. Also handles `import type`.
|
|
26
|
+
|
|
27
|
+
<details>
|
|
28
|
+
<summary>Migration example</summary>
|
|
29
|
+
|
|
30
|
+
```diff
|
|
31
|
+
-import fc from 'fast-check';
|
|
32
|
+
+import * as fc from 'fast-check';
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
</details>
|
package/lib/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
package/lib/cli.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
// src/cli.ts
|
|
6
|
+
const allTransforms = ["v5/defaultImport"];
|
|
7
|
+
const [transform, ...args] = process.argv.slice(2);
|
|
8
|
+
const usage = "Usage: fast-check-codemod <transform> <path> [jscodeshift options]\nwith transform being one of: " + allTransforms.join(", ");
|
|
9
|
+
if (transform === "--help" || transform === "-h") console.log(usage);
|
|
10
|
+
else if (!allTransforms.includes(transform) || args.length === 0 || args[0].startsWith("-")) {
|
|
11
|
+
console.error(usage);
|
|
12
|
+
process.exitCode = 1;
|
|
13
|
+
} else if (!existsSync(args[0])) {
|
|
14
|
+
console.error(`Path does not exist: ${args[0]}`);
|
|
15
|
+
process.exitCode = 1;
|
|
16
|
+
} else {
|
|
17
|
+
const extension = import.meta.url.endsWith(".ts") ? "ts" : "js";
|
|
18
|
+
const result = spawnSync(process.execPath, [
|
|
19
|
+
fileURLToPath(import.meta.resolve("jscodeshift/bin/jscodeshift.js")),
|
|
20
|
+
"--transform",
|
|
21
|
+
fileURLToPath(import.meta.resolve(`./transforms/${transform}.${extension}`)),
|
|
22
|
+
"--no-babel",
|
|
23
|
+
"--extensions=js,jsx,ts,tsx,mjs,cjs,mts,cts",
|
|
24
|
+
"--ignore-pattern=**/node_modules/**",
|
|
25
|
+
"--ignore-pattern=**/.git/**",
|
|
26
|
+
"--fail-on-error",
|
|
27
|
+
...args
|
|
28
|
+
], { stdio: "inherit" });
|
|
29
|
+
if (result.error) console.error(result.error.message);
|
|
30
|
+
process.exitCode = result.status ?? 1;
|
|
31
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// src/transforms/v5/defaultImport.ts
|
|
2
|
+
function transform(file, api) {
|
|
3
|
+
const parser = /\.[cm]?ts$/.test(file.path) ? "ts" : "tsx";
|
|
4
|
+
const j = api.jscodeshift.withParser(parser);
|
|
5
|
+
const root = j(file.source);
|
|
6
|
+
let changed = false;
|
|
7
|
+
root.find(j.ImportDeclaration, { source: { value: "fast-check" } }).forEach((path) => {
|
|
8
|
+
const node = path.node;
|
|
9
|
+
if ("attributes" in node && Array.isArray(node.attributes) && node.attributes.length !== 0) return;
|
|
10
|
+
const specifiers = node.specifiers ?? [];
|
|
11
|
+
const defaultSpecifier = specifiers.find((specifier) => specifier.type === "ImportDefaultSpecifier");
|
|
12
|
+
if (defaultSpecifier === void 0) return;
|
|
13
|
+
const namespace = j.importNamespaceSpecifier(defaultSpecifier.local);
|
|
14
|
+
namespace.comments = defaultSpecifier.comments;
|
|
15
|
+
const remainingSpecifiers = specifiers.filter((specifier) => specifier !== defaultSpecifier);
|
|
16
|
+
if (remainingSpecifiers.length !== 0) path.insertAfter({
|
|
17
|
+
...node,
|
|
18
|
+
specifiers: remainingSpecifiers,
|
|
19
|
+
comments: null
|
|
20
|
+
});
|
|
21
|
+
node.specifiers = [namespace];
|
|
22
|
+
changed = true;
|
|
23
|
+
});
|
|
24
|
+
return changed ? root.toSource({ quote: "single" }) : file.source;
|
|
25
|
+
}
|
|
26
|
+
export { transform as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fast-check/codemod",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "Codemods to help migrate fast-check projects",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./package.json": "./package.json",
|
|
8
|
+
"./cli": "./lib/cli.js",
|
|
9
|
+
"./transforms/v5/defaultImport": "./lib/transforms/v5/defaultImport.js"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"codemod": "./lib/cli.js"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"lib"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/dubzzz/fast-check.git",
|
|
20
|
+
"directory": "packages/codemod"
|
|
21
|
+
},
|
|
22
|
+
"author": "Nicolas DUBIEN <github@dubien.org>",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/dubzzz/fast-check/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/dubzzz/fast-check/tree/main/packages/codemod#readme",
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22.12.0"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@types/jscodeshift": "^17.3.0",
|
|
33
|
+
"jscodeshift": "^17.4.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^26.6.6",
|
|
37
|
+
"rolldown": "^1.3.2",
|
|
38
|
+
"typescript": "~7.0.2",
|
|
39
|
+
"vitest": "^5.1.1"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"codemod",
|
|
43
|
+
"fast-check",
|
|
44
|
+
"migration"
|
|
45
|
+
],
|
|
46
|
+
"funding": [
|
|
47
|
+
{
|
|
48
|
+
"type": "individual",
|
|
49
|
+
"url": "https://github.com/sponsors/dubzzz"
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
"type": "opencollective",
|
|
53
|
+
"url": "https://opencollective.com/fast-check"
|
|
54
|
+
}
|
|
55
|
+
],
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "rolldown -c",
|
|
58
|
+
"build-ci": "rolldown -c",
|
|
59
|
+
"test-bundle": "node -e \"const { bin } = require('./package.json'); const { execFileSync } = require('node:child_process'); console.log('>> print help...'); execFileSync(process.execPath, [bin, '-h'], { stdio: 'inherit' }); console.log('>> launch one transform...'); execFileSync(process.execPath, [bin, 'v5/defaultImport', './test-bundle'], { stdio: 'inherit' });\"",
|
|
60
|
+
"typecheck": "tsc --noEmit"
|
|
61
|
+
}
|
|
6
62
|
}
|