nalloc 0.2.2 → 0.5.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.
Files changed (67) hide show
  1. package/README.md +326 -178
  2. package/build/codemod-cli.cjs +153 -0
  3. package/build/codemod-cli.cjs.map +1 -0
  4. package/build/codemod-cli.d.ts +2 -0
  5. package/build/codemod-cli.js +103 -0
  6. package/build/codemod-cli.js.map +1 -0
  7. package/build/codemod.cjs +652 -0
  8. package/build/codemod.cjs.map +1 -0
  9. package/build/codemod.d.ts +29 -0
  10. package/build/codemod.js +634 -0
  11. package/build/codemod.js.map +1 -0
  12. package/build/eslint.cjs +221 -0
  13. package/build/eslint.cjs.map +1 -0
  14. package/build/eslint.d.ts +36 -0
  15. package/build/eslint.js +198 -0
  16. package/build/eslint.js.map +1 -0
  17. package/build/http.cjs +31 -0
  18. package/build/http.cjs.map +1 -0
  19. package/build/http.d.ts +31 -0
  20. package/build/http.js +13 -0
  21. package/build/http.js.map +1 -0
  22. package/build/nonempty.cjs +35 -0
  23. package/build/nonempty.cjs.map +1 -0
  24. package/build/nonempty.d.ts +34 -0
  25. package/build/nonempty.js +14 -0
  26. package/build/nonempty.js.map +1 -0
  27. package/build/option.cjs +1 -1
  28. package/build/option.cjs.map +1 -1
  29. package/build/option.d.ts +2 -2
  30. package/build/option.js +1 -1
  31. package/build/option.js.map +1 -1
  32. package/build/result.cjs +10 -18
  33. package/build/result.cjs.map +1 -1
  34. package/build/result.d.ts +3 -34
  35. package/build/result.js +10 -15
  36. package/build/result.js.map +1 -1
  37. package/build/safe.cjs +8 -0
  38. package/build/safe.cjs.map +1 -1
  39. package/build/safe.d.ts +3 -0
  40. package/build/safe.js +2 -0
  41. package/build/safe.js.map +1 -1
  42. package/build/schema.cjs +32 -0
  43. package/build/schema.cjs.map +1 -0
  44. package/build/schema.d.ts +44 -0
  45. package/build/schema.js +14 -0
  46. package/build/schema.js.map +1 -0
  47. package/package.json +63 -10
  48. package/src/__tests__/codemod.ts +211 -0
  49. package/src/__tests__/eslint.ts +99 -0
  50. package/src/__tests__/fixtures/tsconfig.json +10 -0
  51. package/src/__tests__/http.ts +64 -0
  52. package/src/__tests__/iter.ts +18 -0
  53. package/src/__tests__/nonempty.ts +46 -0
  54. package/src/__tests__/nonempty.types.ts +38 -0
  55. package/src/__tests__/option.ts +4 -0
  56. package/src/__tests__/result.ts +104 -129
  57. package/src/__tests__/result.types.ts +2 -2
  58. package/src/__tests__/schema.ts +58 -0
  59. package/src/codemod-cli.ts +108 -0
  60. package/src/codemod.ts +623 -0
  61. package/src/eslint.ts +145 -0
  62. package/src/http.ts +42 -0
  63. package/src/nonempty.ts +48 -0
  64. package/src/option.ts +3 -4
  65. package/src/result.ts +18 -49
  66. package/src/safe.ts +3 -0
  67. package/src/schema.ts +52 -0
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env node
2
+ import { readFileSync, writeFileSync, readdirSync, statSync } from 'node:fs';
3
+ import { join } from 'node:path';
4
+ import process from 'node:process';
5
+
6
+ const HELP = `nalloc-codemod - migrate a project from neverthrow to nalloc
7
+
8
+ Usage:
9
+ nalloc-codemod <paths...> [--report <file>] [--dry]
10
+
11
+ Arguments:
12
+ paths Files or directories to migrate (.ts/.tsx/.mts/.cts, .d.ts and node_modules skipped)
13
+
14
+ Options:
15
+ --report <file> Write a markdown report of sites needing manual review
16
+ --dry Analyze and report without writing any files
17
+ --help Show this help
18
+
19
+ Migrate the whole project in one run: converted call sites assume their Result
20
+ values are nalloc values, which only holds once constructors are converted too.
21
+ `;
22
+
23
+ const SOURCE_EXT = /\.(ts|tsx|mts|cts)$/;
24
+
25
+ function collectFiles(path: string, files: string[]): void {
26
+ const stats = statSync(path);
27
+ if (stats.isDirectory()) {
28
+ for (const entry of readdirSync(path)) {
29
+ if (entry === 'node_modules' || entry.startsWith('.')) continue;
30
+ collectFiles(join(path, entry), files);
31
+ }
32
+ } else if (SOURCE_EXT.test(path) && !path.endsWith('.d.ts')) {
33
+ files.push(path);
34
+ }
35
+ }
36
+
37
+ const paths: string[] = [];
38
+ let reportPath: string | undefined;
39
+ let dry = false;
40
+ const args = process.argv.slice(2);
41
+ for (let i = 0; i < args.length; i++) {
42
+ const arg = args[i];
43
+ if (arg === '--help') {
44
+ console.log(HELP);
45
+ process.exit(0);
46
+ } else if (arg === '--dry') {
47
+ dry = true;
48
+ } else if (arg === '--report') {
49
+ reportPath = args[++i];
50
+ if (reportPath === undefined) {
51
+ console.error('--report requires a file path');
52
+ process.exit(1);
53
+ }
54
+ } else {
55
+ paths.push(arg);
56
+ }
57
+ }
58
+
59
+ if (paths.length === 0) {
60
+ console.log(HELP);
61
+ process.exit(1);
62
+ }
63
+
64
+ const codemod = await (async () => {
65
+ try {
66
+ return await import('./codemod.js');
67
+ } catch (error) {
68
+ if ((error as { code?: string }).code === 'ERR_MODULE_NOT_FOUND' && String(error).includes('oxc-parser')) {
69
+ console.error('nalloc-codemod requires the optional peer oxc-parser. Install it first:\n\n npm install -D oxc-parser\n');
70
+ process.exit(1);
71
+ }
72
+ throw error;
73
+ }
74
+ })();
75
+
76
+ const files: string[] = [];
77
+ for (const path of paths) {
78
+ collectFiles(path, files);
79
+ }
80
+
81
+ let filesChanged = 0;
82
+ let converted = 0;
83
+ const skipped = [];
84
+ for (const file of files) {
85
+ const source = readFileSync(file, 'utf8');
86
+ const result = codemod.migrateSource(source, file);
87
+ converted += result.converted;
88
+ skipped.push(...result.skipped);
89
+ if (result.changed) {
90
+ filesChanged++;
91
+ if (!dry) {
92
+ writeFileSync(file, result.output);
93
+ }
94
+ }
95
+ }
96
+
97
+ const report = { filesChanged, converted, skipped };
98
+ if (reportPath !== undefined) {
99
+ writeFileSync(reportPath, codemod.renderReport(report));
100
+ }
101
+ console.log(
102
+ `${dry ? '[dry run] ' : ''}${files.length} files scanned, ${filesChanged} changed, ${converted} sites converted, ${skipped.length} need manual review${reportPath !== undefined ? ` (see ${reportPath})` : ''}`,
103
+ );
104
+ if (skipped.length > 0 && reportPath === undefined) {
105
+ for (const site of skipped) {
106
+ console.log(` ${site.file}:${site.line} [${site.reason}] ${site.text}`);
107
+ }
108
+ }