Skip to content

Commit

Permalink
Fix extension removal bug in Code_path.main_module_name
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Rebours <[email protected]>
  • Loading branch information
NathanReb committed Jul 16, 2024
1 parent bd94da6 commit 92e1b87
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ details.

### Other changes

- Fix a bug where `Code_path.main_module_name` would not properly remove
extensions from the filename and therefore return an invalid module name.
(#<PR_NUMBER>, @NathanReb)

- Make `-unused-code-warnings` flag to the driver also controls the generation
of warning 34 silencing structure items when using `[@@deriving ...]` on type
declarations. (#510, @mbarbin, @NathanReb)
Expand Down
7 changes: 6 additions & 1 deletion src/code_path.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ type t = {
in_expr : bool;
}

let remove_all_extensions basename =
match String.split_on_char ~sep:'.' basename with
| [] -> assert false
| name :: _ -> name

let top_level ~file_path =
let main_module_name =
file_path |> Stdlib.Filename.basename |> Stdlib.Filename.remove_extension
file_path |> Stdlib.Filename.basename |> remove_all_extensions
|> String.capitalize_ascii
in
{
Expand Down
11 changes: 11 additions & 0 deletions test/code_path/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,14 @@ let _ =
- : string =
"(code_path(main_module_name Test)(submodule_path())(enclosing_module Test)(enclosing_value())(value())(fully_qualified_path Test))"
|}]


let _ =
(* The main module name should properly remove all extensions *)
let code_path =
Code_path.top_level ~file_path:"some_dir/module_name.cppo.ml"
in
Code_path.main_module_name code_path
[%%expect{|
- : string = "Module_name"
|}]

0 comments on commit 92e1b87

Please sign in to comment.