From d85cac0f627158dd61459f0d564d00b2c20150db Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Wed, 2 Jul 2025 21:34:33 -0300 Subject: [PATCH 1/4] Add `reanalyze` and `doc` subcommands to rescript cli --- analysis/reanalyze/src/Reanalyze.ml | 15 +++++++-------- analysis/reanalyze/src/Version.ml | 4 ---- cli/rescript.js | 15 ++++++++++++++- .../deadcode/expected/deadcode.txt | 1 - .../deadcode/expected/exception.txt | 1 - tests/build_tests/cli_help/input.js | 2 ++ tools/bin/main.ml | 12 ++---------- tools/bin/version.ml | 1 - 8 files changed, 25 insertions(+), 26 deletions(-) delete mode 100644 analysis/reanalyze/src/Version.ml delete mode 100644 tools/bin/version.ml diff --git a/analysis/reanalyze/src/Reanalyze.ml b/analysis/reanalyze/src/Reanalyze.ml index 7378264908..e6d56ba932 100644 --- a/analysis/reanalyze/src/Reanalyze.ml +++ b/analysis/reanalyze/src/Reanalyze.ml @@ -106,11 +106,12 @@ let runAnalysisAndReport ~cmtRoot = let cli () = let analysisKindSet = ref false in let cmtRootRef = ref None in - let usage = "reanalyze version " ^ Version.version in - let versionAndExit () = - print_endline usage; - exit 0 - [@@raises exit] + let usage = + {|Usage: rescript reanalyze [options] + +`rescript reanalyze` is powerful dead code analysis tools to maintain a clean, efficient, and distraction-free codebase + +Options:|} in let rec setAll cmtRoot = RunConfig.all (); @@ -203,14 +204,12 @@ let cli () = "comma-separated-path-prefixes Report on files whose path has a prefix \ in the list, overriding -suppress (no-op if -suppress is not \ specified)" ); - ("-version", Unit versionAndExit, "Show version information and exit"); - ("--version", Unit versionAndExit, "Show version information and exit"); ( "-write", Set Common.Cli.write, "Write @dead annotations directly in the source files" ); ] in - Arg.parse speclist print_endline usage; + Arg.parse speclist (fun _ -> ()) usage; if !analysisKindSet = false then setConfig (); let cmtRoot = !cmtRootRef in runAnalysisAndReport ~cmtRoot diff --git a/analysis/reanalyze/src/Version.ml b/analysis/reanalyze/src/Version.ml deleted file mode 100644 index 4361834b43..0000000000 --- a/analysis/reanalyze/src/Version.ml +++ /dev/null @@ -1,4 +0,0 @@ -(* CREATED BY reanalyze/scripts/bump_version_module.js *) -(* DO NOT MODIFY BY HAND, WILL BE AUTOMATICALLY UPDATED BY npm version *) - -let version = "2.22.0" diff --git a/cli/rescript.js b/cli/rescript.js index 98d8900ab2..2e8d3f10c3 100755 --- a/cli/rescript.js +++ b/cli/rescript.js @@ -8,8 +8,9 @@ import * as tty from "node:tty"; import * as fs from "node:fs"; +import * as child_process from "node:child_process"; -import { bsc_exe, rescript_exe } from "./common/bins.js"; +import { bsc_exe, rescript_exe, rescript_tools_exe } from "./common/bins.js"; import * as bsb from "./common/bsb.js"; const cwd = process.cwd(); @@ -44,6 +45,8 @@ Subcommands: build clean format + doc + reanalyze dump help @@ -125,6 +128,16 @@ if ( mod.main(subcmdArgs, rescript_exe, bsc_exe); break; } + case "doc": { + const args = process.argv.slice(2); + child_process.spawnSync(rescript_tools_exe, args, { stdio: "inherit" }); + break; + } + case "reanalyze": { + const args = process.argv.slice(2); + child_process.spawnSync(rescript_tools_exe, args, { stdio: "inherit" }); + break; + } default: { console.error(`Error: Unknown command "${subcmd}".\n${helpMessage}`); process.exit(2); diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/expected/deadcode.txt b/tests/analysis_tests/tests-reanalyze/deadcode/expected/deadcode.txt index 6104d9e62b..84ad2170a2 100644 --- a/tests/analysis_tests/tests-reanalyze/deadcode/expected/deadcode.txt +++ b/tests/analysis_tests/tests-reanalyze/deadcode/expected/deadcode.txt @@ -1,4 +1,3 @@ - Scanning AutoAnnotate.cmt Source:AutoAnnotate.res addVariantCaseDeclaration R AutoAnnotate.res:1:15 path:+AutoAnnotate.variant addRecordLabelDeclaration variant AutoAnnotate.res:4:15 path:+AutoAnnotate.record diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt b/tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt index 9bd59a72cc..10802fa86f 100644 --- a/tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt +++ b/tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt @@ -1,5 +1,4 @@ - Exception Analysis Exn.res:1:5-10 raises might raise Not_found (Exn.res:1:19) and is not annotated with @raises(Not_found) diff --git a/tests/build_tests/cli_help/input.js b/tests/build_tests/cli_help/input.js index facfbecf2b..07dde314ab 100755 --- a/tests/build_tests/cli_help/input.js +++ b/tests/build_tests/cli_help/input.js @@ -19,6 +19,8 @@ const cliHelp = " build\n" + " clean\n" + " format\n" + + " doc\n" + + " reanalyze\n" + " dump\n" + " help\n" + "\n" + diff --git a/tools/bin/main.ml b/tools/bin/main.ml index 2d97dea930..811d974fc6 100644 --- a/tools/bin/main.ml +++ b/tools/bin/main.ml @@ -1,11 +1,7 @@ let docHelp = - {|ReScript Tools - -Output documentation to standard output + {|Usage: rescript doc [file] -Usage: rescript-tools doc - -Example: rescript-tools doc ./path/to/EntryPointLib.res|} +`rescript doc` generate documentation and print to standard output|} let help = {|ReScript Tools @@ -16,7 +12,6 @@ Commands: doc Generate documentation reanalyze Reanalyze --v, --version Print version -h, --help Print help|} let logAndExit = function @@ -27,8 +22,6 @@ let logAndExit = function Printf.eprintf "%s\n" log; exit 1 -let version = Version.version - let main () = match Sys.argv |> Array.to_list |> List.tl with | "doc" :: rest -> ( @@ -77,7 +70,6 @@ let main () = close_out oc; exit 0 | ["-h"] | ["--help"] -> logAndExit (Ok help) - | ["-v"] | ["--version"] -> logAndExit (Ok version) | _ -> logAndExit (Error help) let () = main () diff --git a/tools/bin/version.ml b/tools/bin/version.ml deleted file mode 100644 index 8a00911bbc..0000000000 --- a/tools/bin/version.ml +++ /dev/null @@ -1 +0,0 @@ -let version = "0.6.4" From ce8c8f2335eaaa137e665201494765bedfcdf18e Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Wed, 2 Jul 2025 21:46:04 -0300 Subject: [PATCH 2/4] update tests --- cli/rescript.js | 6 +++--- .../tests-reanalyze/termination/expected/termination.txt | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/cli/rescript.js b/cli/rescript.js index 2e8d3f10c3..beed6bfd43 100755 --- a/cli/rescript.js +++ b/cli/rescript.js @@ -6,9 +6,9 @@ // It matters since we need read .sourcedirs(location) // and its content are file/directories with regard to project root -import * as tty from "node:tty"; -import * as fs from "node:fs"; import * as child_process from "node:child_process"; +import * as fs from "node:fs"; +import * as tty from "node:tty"; import { bsc_exe, rescript_exe, rescript_tools_exe } from "./common/bins.js"; import * as bsb from "./common/bsb.js"; @@ -97,7 +97,7 @@ if ( console.log(helpMessage); } else if (argPatterns.version.includes(args[0])) { const packageSpec = JSON.parse( - fs.readFileSync(new URL("../package.json", import.meta.url), "utf-8") + fs.readFileSync(new URL("../package.json", import.meta.url), "utf-8"), ); console.log(packageSpec.version); diff --git a/tests/analysis_tests/tests-reanalyze/termination/expected/termination.txt b/tests/analysis_tests/tests-reanalyze/termination/expected/termination.txt index 63e15f8134..b713e225d5 100644 --- a/tests/analysis_tests/tests-reanalyze/termination/expected/termination.txt +++ b/tests/analysis_tests/tests-reanalyze/termination/expected/termination.txt @@ -1,4 +1,3 @@ - Scanning TestCyberTruck.cmt Source:TestCyberTruck.res Function Table From 559dad76a6700f6919bfb8355891e5f08279912b Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Wed, 2 Jul 2025 21:53:17 -0300 Subject: [PATCH 3/4] restore print_endline --- analysis/reanalyze/src/Reanalyze.ml | 2 +- .../tests-reanalyze/deadcode/expected/deadcode.txt | 1 + .../tests-reanalyze/deadcode/expected/exception.txt | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/analysis/reanalyze/src/Reanalyze.ml b/analysis/reanalyze/src/Reanalyze.ml index e6d56ba932..0f150a4a40 100644 --- a/analysis/reanalyze/src/Reanalyze.ml +++ b/analysis/reanalyze/src/Reanalyze.ml @@ -209,7 +209,7 @@ Options:|} "Write @dead annotations directly in the source files" ); ] in - Arg.parse speclist (fun _ -> ()) usage; + Arg.parse speclist print_endline usage; if !analysisKindSet = false then setConfig (); let cmtRoot = !cmtRootRef in runAnalysisAndReport ~cmtRoot diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/expected/deadcode.txt b/tests/analysis_tests/tests-reanalyze/deadcode/expected/deadcode.txt index 84ad2170a2..6104d9e62b 100644 --- a/tests/analysis_tests/tests-reanalyze/deadcode/expected/deadcode.txt +++ b/tests/analysis_tests/tests-reanalyze/deadcode/expected/deadcode.txt @@ -1,3 +1,4 @@ + Scanning AutoAnnotate.cmt Source:AutoAnnotate.res addVariantCaseDeclaration R AutoAnnotate.res:1:15 path:+AutoAnnotate.variant addRecordLabelDeclaration variant AutoAnnotate.res:4:15 path:+AutoAnnotate.record diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt b/tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt index 10802fa86f..9bd59a72cc 100644 --- a/tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt +++ b/tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt @@ -1,4 +1,5 @@ + Exception Analysis Exn.res:1:5-10 raises might raise Not_found (Exn.res:1:19) and is not annotated with @raises(Not_found) From 06e08f92c54ca914edb6ecf1d2f703ecaa951dca Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Wed, 2 Jul 2025 22:09:49 -0300 Subject: [PATCH 4/4] update tests --- .../tests-reanalyze/termination/expected/termination.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/analysis_tests/tests-reanalyze/termination/expected/termination.txt b/tests/analysis_tests/tests-reanalyze/termination/expected/termination.txt index b713e225d5..63e15f8134 100644 --- a/tests/analysis_tests/tests-reanalyze/termination/expected/termination.txt +++ b/tests/analysis_tests/tests-reanalyze/termination/expected/termination.txt @@ -1,3 +1,4 @@ + Scanning TestCyberTruck.cmt Source:TestCyberTruck.res Function Table