Skip to content

Commit

Permalink
Merge pull request #512 from NathanReb/fix-main-module-name
Browse files Browse the repository at this point in the history
Fix extension removal bug in `Code_path.main_module_name`
  • Loading branch information
NathanReb authored Jul 17, 2024
2 parents fe4c0e9 + 9f65414 commit 44a723e
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.
(#512, @NathanReb)

- Add `-unused-type-warnings` flag to the driver to allow users to disable
only the generation of warning 34 silencing structure items when using
`[@@deriving ...]` on type declarations. (#511, @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 (* split_on_char never returns the empty list *)
| 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 44a723e

Please sign in to comment.